Skip to content

Commit

Permalink
Handle user disabled exception as its own result code
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
  • Loading branch information
tobiasKaminsky authored and AndyScherzinger committed Feb 3, 2024
1 parent 944f99e commit 9c4d46d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ package com.owncloud.android.lib.resources.files

import android.os.Build
import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.common.OwnCloudBasicCredentials
import com.owncloud.android.lib.common.OwnCloudClientFactory
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.resources.files.model.RemoteFile
import junit.framework.TestCase.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test
Expand All @@ -36,6 +40,7 @@ import java.nio.file.attribute.BasicFileAttributes
import java.util.concurrent.TimeUnit

class UploadFileRemoteOperationIT : AbstractIT() {
@Throws(IOException::class)
@Test
fun creationTime() {
val imageFile = getFile("imageFile.png")
Expand All @@ -44,6 +49,7 @@ class UploadFileRemoteOperationIT : AbstractIT() {
assertTrue(creationDate!! > (System.currentTimeMillis() / MILLI_TO_SECOND) - TIME_OFFSET)
}

@Throws(IOException::class)
@Test
fun upload() {
// create file
Expand Down Expand Up @@ -94,6 +100,34 @@ class UploadFileRemoteOperationIT : AbstractIT() {
)
}

@Throws(IOException::class)
@Test
fun uploadWithDisabledUser() {
// use disabled user
val client3 = OwnCloudClientFactory.createOwnCloudClient(url, context, true)
client3.credentials = OwnCloudBasicCredentials("disabled", "disabled")

// create file
val filePath = createFile("text")
val remotePath = "/test.md"

val creationTimestamp = getCreationTimestamp(File(filePath))
val sut =
UploadFileRemoteOperation(
filePath,
remotePath,
"text/markdown",
"",
RANDOM_MTIME,
creationTimestamp,
true
)

val uploadResult = sut.execute(client3)
assertFalse(uploadResult.isSuccess)
assertEquals(RemoteOperationResult.ResultCode.USER_DISABLED, uploadResult.code)
}

private fun getCreationTimestamp(file: File): Long? {
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
package com.owncloud.android.lib.resources.files.webdav

import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.common.OwnCloudBasicCredentials
import com.owncloud.android.lib.common.OwnCloudClientFactory
import com.owncloud.android.lib.common.network.WebdavEntry
import com.owncloud.android.lib.common.network.WebdavUtils
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode
import com.owncloud.android.lib.resources.files.ChunkedFileUploadRemoteOperation
import junit.framework.TestCase
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertFalse
import junit.framework.TestCase.assertNotNull
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod
import org.junit.Test
Expand Down Expand Up @@ -73,6 +77,19 @@ class ChunkedFileUploadRemoteOperationIT : AbstractIT() {
TestCase.assertSame(ResultCode.CANCELLED, uploadResult?.code)
}

@Test
fun uploadWithDisabledUser() {
// use disabled user
val client3 = OwnCloudClientFactory.createOwnCloudClient(url, context, true)
client3.credentials = OwnCloudBasicCredentials("disabled", "disabled")

val sut = genLargeUpload(true)
val uploadResult = sut.execute(client3)

assertFalse(uploadResult.isSuccess)
assertEquals(ResultCode.USER_DISABLED, uploadResult.code)
}

@Test
fun resume() {
val filePath = createFile("chunkedFile.txt", BIG_FILE_ITERATION * 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class ExceptionParser {
private static final String INVALID_PATH_EXCEPTION_STRING = "OC\\Connector\\Sabre\\Exception\\InvalidPath";
private static final String INVALID_PATH_EXCEPTION_UPLOAD_STRING = "OCP\\Files\\InvalidPathException";
private static final String VIRUS_EXCEPTION_STRING = "OCA\\DAV\\Connector\\Sabre\\Exception\\UnsupportedMediaType";
private static final String USER_DISABLED_MESSAGE = "OC\\User\\LoginException: User disabled";
private static final String SERVICE_UNAIVALABLE_STRING = "Sabre\\DAV\\Exception\\ServiceUnavailable";

// No namespaces
private static final String ns = null;
Expand Down Expand Up @@ -92,6 +94,10 @@ public boolean isVirusException() {
return VIRUS_EXCEPTION_STRING.equalsIgnoreCase(exception) && message.startsWith("Virus");
}

public boolean isUserDisabledException() {
return SERVICE_UNAIVALABLE_STRING.equals(exception) && message.equalsIgnoreCase(USER_DISABLED_MESSAGE);
}

/**
* Parse OCS node
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ public enum ResultCode {
ETAG_UNCHANGED,
VIRUS_DETECTED,
FOLDER_ALREADY_EXISTS,
CANNOT_CREATE_FILE
CANNOT_CREATE_FILE,
USER_DISABLED
}

private boolean mSuccess = false;
Expand Down Expand Up @@ -329,7 +330,9 @@ public RemoteOperationResult(boolean success, OkHttpMethodBase httpMethod) {
public RemoteOperationResult(boolean success, HttpMethod httpMethod) {
this(success, httpMethod.getStatusCode(), httpMethod.getStatusText(), httpMethod.getResponseHeaders());

if (mHttpCode == HttpStatus.SC_BAD_REQUEST || mHttpCode == HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE) {
if (mHttpCode == HttpStatus.SC_BAD_REQUEST ||
mHttpCode == HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE ||
mHttpCode == HttpStatus.SC_SERVICE_UNAVAILABLE) {
try {
String bodyResponse = httpMethod.getResponseBodyAsString();

Expand All @@ -344,6 +347,10 @@ public RemoteOperationResult(boolean success, HttpMethod httpMethod) {
mCode = ResultCode.VIRUS_DETECTED;
}

if (xmlParser.isUserDisabledException()) {
mCode = ResultCode.USER_DISABLED;
}

mHttpPhrase = xmlParser.getMessage();
}
} catch (Exception e) {
Expand Down

0 comments on commit 9c4d46d

Please sign in to comment.