AbstractOnServerIT.java 10 KB

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