AbstractOnServerIT.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Nextcloud - Android Client
  3. *
  4. * SPDX-FileCopyrightText: 2020 Tobias Kaminsky <tobias@kaminsky.me>
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. package com.owncloud.android;
  8. import android.accounts.Account;
  9. import android.accounts.AccountManager;
  10. import android.accounts.AuthenticatorException;
  11. import android.accounts.OperationCanceledException;
  12. import android.content.ActivityNotFoundException;
  13. import android.net.Uri;
  14. import android.os.Bundle;
  15. import com.nextcloud.client.account.User;
  16. import com.nextcloud.client.account.UserAccountManager;
  17. import com.nextcloud.client.account.UserAccountManagerImpl;
  18. import com.nextcloud.client.device.BatteryStatus;
  19. import com.nextcloud.client.device.PowerManagementService;
  20. import com.nextcloud.client.jobs.upload.FileUploadWorker;
  21. import com.nextcloud.client.network.Connectivity;
  22. import com.nextcloud.client.network.ConnectivityService;
  23. import com.nextcloud.java.util.Optional;
  24. import com.owncloud.android.datamodel.OCFile;
  25. import com.owncloud.android.datamodel.UploadsStorageManager;
  26. import com.owncloud.android.db.OCUpload;
  27. import com.owncloud.android.files.services.NameCollisionPolicy;
  28. import com.owncloud.android.lib.common.OwnCloudClient;
  29. import com.owncloud.android.lib.common.OwnCloudClientFactory;
  30. import com.owncloud.android.lib.common.accounts.AccountUtils;
  31. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  32. import com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation;
  33. import com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation;
  34. import com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation;
  35. import com.owncloud.android.lib.resources.files.model.RemoteFile;
  36. import com.owncloud.android.operations.RefreshFolderOperation;
  37. import com.owncloud.android.operations.UploadFileOperation;
  38. import org.apache.commons.httpclient.HttpStatus;
  39. import org.apache.commons.httpclient.methods.GetMethod;
  40. import org.junit.After;
  41. import org.junit.Assert;
  42. import org.junit.BeforeClass;
  43. import java.io.File;
  44. import java.io.IOException;
  45. import androidx.annotation.NonNull;
  46. import androidx.test.platform.app.InstrumentationRegistry;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertTrue;
  49. /**
  50. * Common base for all integration tests.
  51. */
  52. public abstract class AbstractOnServerIT extends AbstractIT {
  53. @BeforeClass
  54. public static void beforeAll() {
  55. try {
  56. // clean up
  57. targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
  58. AccountManager platformAccountManager = AccountManager.get(targetContext);
  59. for (Account account : platformAccountManager.getAccounts()) {
  60. if (account.type.equalsIgnoreCase("nextcloud")) {
  61. platformAccountManager.removeAccountExplicitly(account);
  62. }
  63. }
  64. Bundle arguments = androidx.test.platform.app.InstrumentationRegistry.getArguments();
  65. Uri baseUrl = Uri.parse(arguments.getString("TEST_SERVER_URL"));
  66. String loginName = arguments.getString("TEST_SERVER_USERNAME");
  67. String password = arguments.getString("TEST_SERVER_PASSWORD");
  68. Account temp = new Account(loginName + "@" + baseUrl, MainApp.getAccountType(targetContext));
  69. UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
  70. if (!accountManager.exists(temp)) {
  71. platformAccountManager.addAccountExplicitly(temp, password, null);
  72. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION,
  73. Integer.toString(UserAccountManager.ACCOUNT_VERSION));
  74. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_VERSION, "14.0.0.0");
  75. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_BASE_URL, baseUrl.toString());
  76. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_USER_ID, loginName); // same as userId
  77. }
  78. final UserAccountManager userAccountManager = UserAccountManagerImpl.fromContext(targetContext);
  79. account = userAccountManager.getAccountByName(loginName + "@" + baseUrl);
  80. if (account == null) {
  81. throw new ActivityNotFoundException();
  82. }
  83. Optional<User> optionalUser = userAccountManager.getUser(account.name);
  84. user = optionalUser.orElseThrow(IllegalAccessError::new);
  85. client = OwnCloudClientFactory.createOwnCloudClient(account, targetContext);
  86. nextcloudClient = OwnCloudClientFactory.createNextcloudClient(user, targetContext);
  87. createDummyFiles();
  88. waitForServer(client, baseUrl);
  89. // deleteAllFilesOnServer(); // makes sure that no file/folder is in root
  90. } catch (OperationCanceledException |
  91. IOException |
  92. AccountUtils.AccountNotFoundException |
  93. AuthenticatorException e) {
  94. throw new RuntimeException("Error setting up clients", e);
  95. }
  96. }
  97. @After
  98. public void after() {
  99. deleteAllFilesOnServer();
  100. super.after();
  101. }
  102. public static void deleteAllFilesOnServer() {
  103. RemoteOperationResult result = new ReadFolderRemoteOperation("/").execute(client);
  104. assertTrue(result.getLogMessage(), result.isSuccess());
  105. for (Object object : result.getData()) {
  106. RemoteFile remoteFile = (RemoteFile) object;
  107. if (!remoteFile.getRemotePath().equals("/")) {
  108. if (remoteFile.isEncrypted()) {
  109. ToggleEncryptionRemoteOperation operation = new ToggleEncryptionRemoteOperation(remoteFile.getLocalId(),
  110. remoteFile.getRemotePath(),
  111. false);
  112. boolean operationResult = operation
  113. .execute(client)
  114. .isSuccess();
  115. assertTrue(operationResult);
  116. }
  117. boolean removeResult = false;
  118. for (int i = 0; i < 5; i++) {
  119. removeResult = new RemoveFileRemoteOperation(remoteFile.getRemotePath())
  120. .execute(client)
  121. .isSuccess();
  122. if (removeResult) {
  123. break;
  124. }
  125. shortSleep();
  126. }
  127. assertTrue(removeResult);
  128. }
  129. }
  130. }
  131. private static void waitForServer(OwnCloudClient client, Uri baseUrl) {
  132. GetMethod get = new GetMethod(baseUrl + "/status.php");
  133. try {
  134. int i = 0;
  135. while (client.executeMethod(get) != HttpStatus.SC_OK && i < 3) {
  136. System.out.println("wait…");
  137. Thread.sleep(60 * 1000);
  138. i++;
  139. }
  140. if (i == 3) {
  141. Assert.fail("Server not ready!");
  142. }
  143. } catch (IOException e) {
  144. e.printStackTrace();
  145. } catch (InterruptedException e) {
  146. e.printStackTrace();
  147. }
  148. }
  149. public void uploadOCUpload(OCUpload ocUpload) {
  150. uploadOCUpload(ocUpload, FileUploadWorker.LOCAL_BEHAVIOUR_COPY);
  151. }
  152. public void uploadOCUpload(OCUpload ocUpload, int localBehaviour) {
  153. ConnectivityService connectivityServiceMock = new ConnectivityService() {
  154. @Override
  155. public boolean isConnected() {
  156. return false;
  157. }
  158. @Override
  159. public boolean isInternetWalled() {
  160. return false;
  161. }
  162. @Override
  163. public Connectivity getConnectivity() {
  164. return Connectivity.CONNECTED_WIFI;
  165. }
  166. };
  167. PowerManagementService powerManagementServiceMock = new PowerManagementService() {
  168. @NonNull
  169. @Override
  170. public BatteryStatus getBattery() {
  171. return new BatteryStatus();
  172. }
  173. @Override
  174. public boolean isPowerSavingEnabled() {
  175. return false;
  176. }
  177. @Override
  178. public boolean isPowerSavingExclusionAvailable() {
  179. return false;
  180. }
  181. };
  182. UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
  183. UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(accountManager,
  184. targetContext.getContentResolver());
  185. UploadFileOperation newUpload = new UploadFileOperation(
  186. uploadsStorageManager,
  187. connectivityServiceMock,
  188. powerManagementServiceMock,
  189. user,
  190. null,
  191. ocUpload,
  192. NameCollisionPolicy.DEFAULT,
  193. localBehaviour,
  194. targetContext,
  195. false,
  196. false,
  197. getStorageManager()
  198. );
  199. newUpload.addRenameUploadListener(() -> {
  200. // dummy
  201. });
  202. newUpload.setRemoteFolderToBeCreated();
  203. RemoteOperationResult result = newUpload.execute(client);
  204. assertTrue(result.getLogMessage(), result.isSuccess());
  205. OCFile parentFolder = getStorageManager()
  206. .getFileByEncryptedRemotePath(new File(ocUpload.getRemotePath()).getParent() + "/");
  207. String uploadedFileName = new File(ocUpload.getRemotePath()).getName();
  208. OCFile uploadedFile = getStorageManager().
  209. getFileByDecryptedRemotePath(parentFolder.getDecryptedRemotePath() + uploadedFileName);
  210. assertNotNull(uploadedFile.getRemoteId());
  211. assertNotNull(uploadedFile.getPermissions());
  212. if (localBehaviour == FileUploadWorker.LOCAL_BEHAVIOUR_COPY ||
  213. localBehaviour == FileUploadWorker.LOCAL_BEHAVIOUR_MOVE) {
  214. assertTrue(new File(uploadedFile.getStoragePath()).exists());
  215. }
  216. }
  217. protected void refreshFolder(String path) {
  218. assertTrue(new RefreshFolderOperation(getStorageManager().getFileByEncryptedRemotePath(path),
  219. System.currentTimeMillis(),
  220. false,
  221. false,
  222. getStorageManager(),
  223. user,
  224. targetContext
  225. ).execute(client).isSuccess());
  226. }
  227. }