From 78167d8bae60634ba6f094fd8e1bf306b3921837 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Wed, 12 Jun 2024 16:47:50 +0200 Subject: [PATCH] migrate CheckRemoteWipeRemoteOperation to NextcloudClient Signed-off-by: tobiasKaminsky --- .../java/com/owncloud/android/AbstractIT.java | 3 +- .../users/CheckRemoteWipeRemoteOperationIT.kt | 32 ++++++++++++ .../users/CheckRemoteWipeRemoteOperation.java | 51 ++++++++++++------- 3 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 library/src/androidTest/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperationIT.kt diff --git a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java index 629747cce..2d2bd6607 100644 --- a/library/src/androidTest/java/com/owncloud/android/AbstractIT.java +++ b/library/src/androidTest/java/com/owncloud/android/AbstractIT.java @@ -72,6 +72,7 @@ public abstract class AbstractIT { protected static NextcloudClient nextcloudClient; protected static Context context; protected static Uri url; + protected static String password; protected String baseFolderPath = "/test_for_build/"; @@ -89,7 +90,7 @@ public static void beforeAll() throws InterruptedException, url = Uri.parse(arguments.getString("TEST_SERVER_URL")); String loginName = arguments.getString("TEST_SERVER_USERNAME"); - String password = arguments.getString("TEST_SERVER_PASSWORD"); + password = arguments.getString("TEST_SERVER_PASSWORD"); client = OwnCloudClientFactory.createOwnCloudClient(url, context, true); client.setCredentials(new OwnCloudBasicCredentials(loginName, password)); diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperationIT.kt new file mode 100644 index 000000000..a2db18c52 --- /dev/null +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperationIT.kt @@ -0,0 +1,32 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH + * SPDX-FileCopyrightText: 2024 Tobias Kaminsky + * SPDX-License-Identifier: MIT + */ +package com.owncloud.android.lib.resources.users + +import com.owncloud.android.AbstractIT +import junit.framework.TestCase.assertEquals +import junit.framework.TestCase.assertFalse +import junit.framework.TestCase.assertTrue +import org.junit.Test +import java.net.HttpURLConnection + +class CheckRemoteWipeRemoteOperationIT : AbstractIT() { + @Test + fun checkRemoteWipe() { + val sut = ConvertAppTokenRemoteOperation() + val resultAppToken = sut.execute(nextcloudClient) + assertTrue(resultAppToken.isSuccess) + + val newPassword = resultAppToken.resultData + + val result = CheckRemoteWipeRemoteOperation(newPassword).execute(nextcloudClient) + + // if no remote wipe, then 404 / false + assertEquals(HttpURLConnection.HTTP_NOT_FOUND, result.httpCode) + assertFalse(result.isSuccess) + } +} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperation.java b/library/src/main/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperation.java index 3a1e4ae55..26c279736 100644 --- a/library/src/main/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperation.java +++ b/library/src/main/java/com/owncloud/android/lib/resources/users/CheckRemoteWipeRemoteOperation.java @@ -7,44 +7,59 @@ */ package com.owncloud.android.lib.resources.users; -import com.owncloud.android.lib.common.OwnCloudClient; +import com.google.gson.Gson; +import com.nextcloud.common.NextcloudClient; +import com.nextcloud.operations.PostMethod; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.utils.Log_OC; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.Utf8PostMethod; import org.json.JSONObject; +import java.util.HashMap; + +import okhttp3.MediaType; +import okhttp3.RequestBody; + /** * Remote operation performing check if app token is scheduled for remote wipe */ -public class CheckRemoteWipeRemoteOperation extends RemoteOperation { +public class CheckRemoteWipeRemoteOperation extends RemoteOperation { private static final String TAG = CheckRemoteWipeRemoteOperation.class.getSimpleName(); - private static final int SYNC_READ_TIMEOUT = 40000; - private static final int SYNC_CONNECTION_TIMEOUT = 5000; private static final String REMOTE_WIPE_URL = "/index.php/core/wipe/check"; // JSON node names private static final String WIPE = "wipe"; + private String authToken; + + public CheckRemoteWipeRemoteOperation(String authToken) { + this.authToken = authToken; + } + /** * @param client Client object */ @Override - protected RemoteOperationResult run(OwnCloudClient client) { - Utf8PostMethod postMethod = null; - RemoteOperationResult result; + public RemoteOperationResult run(NextcloudClient client) { + PostMethod postMethod = null; + RemoteOperationResult result; try { - postMethod = new Utf8PostMethod(client.getBaseUri() + REMOTE_WIPE_URL + JSON_FORMAT); + HashMap map = new HashMap<>(); + map.put(REMOTE_WIPE_TOKEN, authToken); + + String jsonString = new Gson().toJson(map); + + RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonString); + postMethod = new PostMethod(client.getBaseUri() + REMOTE_WIPE_URL + JSON_FORMAT, true, requestBody); postMethod.addRequestHeader(CONTENT_TYPE, FORM_URLENCODED); - postMethod.setParameter(REMOTE_WIPE_TOKEN, client.getCredentials().getAuthToken()); - int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT); + int status = client.execute(postMethod); if (HttpStatus.SC_OK == status) { String response = postMethod.getResponseBodyAsString(); @@ -52,20 +67,18 @@ protected RemoteOperationResult run(OwnCloudClient client) { JSONObject json = new JSONObject(response); if (json.getBoolean(WIPE)) { - result = new RemoteOperationResult(true, postMethod); + result = new RemoteOperationResult<>(true, postMethod); } else { - result = new RemoteOperationResult(false, postMethod); + result = new RemoteOperationResult<>(false, postMethod); } } else { - result = new RemoteOperationResult(false, postMethod); + result = new RemoteOperationResult<>(false, postMethod); } - - client.exhaustResponse(postMethod.getResponseBodyAsStream()); } catch (Exception e) { - result = new RemoteOperationResult(e); + result = new RemoteOperationResult<>(e); Log_OC.e(TAG, - "Getting remote wipe status failed: " + result.getLogMessage(), - result.getException()); + "Getting remote wipe status failed: " + result.getLogMessage(), + result.getException()); } finally { if (postMethod != null) { postMethod.releaseConnection();