Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate CheckRemoteWipeRemoteOperation to NextcloudClient #1472

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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/";

Expand All @@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this variable was made static.


client = OwnCloudClientFactory.createOwnCloudClient(url, context, true);
client.setCredentials(new OwnCloudBasicCredentials(loginName, password));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH
* SPDX-FileCopyrightText: 2024 Tobias Kaminsky <[email protected]>
* 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,78 @@
*/
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<Void> {

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<Void> run(NextcloudClient client) {
PostMethod postMethod = null;
RemoteOperationResult<Void> result;

try {
postMethod = new Utf8PostMethod(client.getBaseUri() + REMOTE_WIPE_URL + JSON_FORMAT);
HashMap<String, String> 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();

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);
Comment on lines 69 to +72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken, this if statement can be simplified to the following expression:

result = new RemoteOperationResult<>(json.getBoolean(WIPE), 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();
Expand Down
Loading