OwnCloudClientManagerTest.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Nextcloud Android Library is available under MIT license
  2. *
  3. * @author Tobias Kaminsky
  4. * Copyright (C) 2019 Tobias Kaminsky
  5. * Copyright (C) 2019 Nextcloud GmbH
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  21. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. package com.nextcloud.client.account;
  28. import android.accounts.Account;
  29. import android.accounts.AccountManager;
  30. import android.accounts.AuthenticatorException;
  31. import android.accounts.OperationCanceledException;
  32. import android.net.Uri;
  33. import android.os.Bundle;
  34. import com.owncloud.android.AbstractOnServerIT;
  35. import com.owncloud.android.lib.common.OwnCloudAccount;
  36. import com.owncloud.android.lib.common.OwnCloudClient;
  37. import com.owncloud.android.lib.common.OwnCloudClientManager;
  38. import com.owncloud.android.lib.common.accounts.AccountUtils;
  39. import org.junit.Test;
  40. import java.io.IOException;
  41. import androidx.test.platform.app.InstrumentationRegistry;
  42. import static org.junit.Assert.assertEquals;
  43. public class OwnCloudClientManagerTest extends AbstractOnServerIT {
  44. /**
  45. * Like on files app we create & store an account in Android's account manager.
  46. */
  47. @Test
  48. public void testUserId() throws OperationCanceledException, AuthenticatorException, IOException,
  49. AccountUtils.AccountNotFoundException {
  50. Bundle arguments = InstrumentationRegistry.getArguments();
  51. Uri url = Uri.parse(arguments.getString("TEST_SERVER_URL"));
  52. String loginName = arguments.getString("TEST_SERVER_USERNAME");
  53. String password = arguments.getString("TEST_SERVER_PASSWORD");
  54. AccountManager accountManager = AccountManager.get(targetContext);
  55. String accountName = AccountUtils.buildAccountName(url, loginName);
  56. Account newAccount = new Account(accountName, "nextcloud");
  57. accountManager.addAccountExplicitly(newAccount, password, null);
  58. accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_OC_BASE_URL, url.toString());
  59. accountManager.setUserData(newAccount, AccountUtils.Constants.KEY_USER_ID, loginName);
  60. OwnCloudClientManager manager = new OwnCloudClientManager();
  61. OwnCloudAccount account = new OwnCloudAccount(newAccount, targetContext);
  62. OwnCloudClient client = manager.getClientFor(account, targetContext);
  63. assertEquals(loginName, client.getUserId());
  64. accountManager.removeAccountExplicitly(newAccount);
  65. assertEquals(1, accountManager.getAccounts().length);
  66. }
  67. }