From 46f930f6cf63859871c252a3e7a27556a42fea08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Gonz=C3=A1lez=20Cabrero?= Date: Mon, 14 Mar 2016 12:04:57 +0100 Subject: [PATCH 1/4] Add new PUT argument for the public upload permission and include in the operation --- .../shares/UpdateRemoteShareOperation.java | 94 ++++++++++++------- 1 file changed, 60 insertions(+), 34 deletions(-) diff --git a/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java index 5b798032..137d62a8 100644 --- a/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java @@ -25,28 +25,28 @@ package com.owncloud.android.lib.resources.shares; - import android.net.Uri; - import android.util.Pair; +import android.net.Uri; +import android.util.Pair; - import com.owncloud.android.lib.common.OwnCloudClient; - 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 com.owncloud.android.lib.common.OwnCloudClient; +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.PutMethod; - import org.apache.commons.httpclient.methods.StringRequestEntity; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.httpclient.methods.StringRequestEntity; - import java.text.DateFormat; - import java.text.SimpleDateFormat; - import java.util.ArrayList; - import java.util.Calendar; - import java.util.List; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; /** * Updates parameters of an existing Share resource, known its remote ID. - * + *

* Allow updating several parameters, triggering a request to the server per parameter. */ @@ -57,42 +57,57 @@ public class UpdateRemoteShareOperation extends RemoteOperation { private static final String PARAM_PASSWORD = "password"; private static final String PARAM_EXPIRATION_DATE = "expireDate"; private static final String PARAM_PERMISSIONS = "permissions"; + private static final String PARAM_PUBLIC_UPLOAD = "publicUpload"; private static final String FORMAT_EXPIRATION_DATE = "yyyy-MM-dd"; - private static final String ENTITY_CONTENT_TYPE = "application/x-www-form-urlencoded"; + private static final String ENTITY_CONTENT_TYPE = "application/x-www-form-urlencoded"; private static final String ENTITY_CHARSET = "UTF-8"; - /** Identifier of the share to update */ + /** + * Identifier of the share to update + */ private long mRemoteId; - /** Password to set for the public link */ + /** + * Password to set for the public link + */ private String mPassword; - /** Expiration date to set for the public link */ + /** + * Expiration date to set for the public link + */ private long mExpirationDateInMillis; - /** Access permissions for the file bound to the share */ + /** + * Access permissions for the file bound to the share + */ private int mPermissions; + /** + * Upload permissions for the public link (only folders) + */ + private boolean mPublicUpload; + /** * Constructor. No update is initialized by default, need to be applied with setters below. * - * @param remoteId Identifier of the share to update. + * @param remoteId Identifier of the share to update. */ public UpdateRemoteShareOperation(long remoteId) { mRemoteId = remoteId; mPassword = null; // no update mExpirationDateInMillis = 0; // no update + mPublicUpload = false; } /** * Set password to update in Share resource. * - * @param password Password to set to the target share. - * Empty string clears the current password. - * Null results in no update applied to the password. + * @param password Password to set to the target share. + * Empty string clears the current password. + * Null results in no update applied to the password. */ public void setPassword(String password) { mPassword = password; @@ -102,10 +117,10 @@ public class UpdateRemoteShareOperation extends RemoteOperation { /** * Set expiration date to update in Share resource. * - * @param expirationDateInMillis Expiration date to set to the target share. - * A negative value clears the current expiration date. - * Zero value (start-of-epoch) results in no update done on - * the expiration date. + * @param expirationDateInMillis Expiration date to set to the target share. + * A negative value clears the current expiration date. + * Zero value (start-of-epoch) results in no update done on + * the expiration date. */ public void setExpirationDate(long expirationDateInMillis) { mExpirationDateInMillis = expirationDateInMillis; @@ -115,13 +130,22 @@ public class UpdateRemoteShareOperation extends RemoteOperation { /** * Set permissions to update in Share resource. * - * @param permissions Permissions date to set to the target share. - * Values <= 0 result in no update applied to the permissions. + * @param permissions Permissions to set to the target share. + * Values <= 0 result in no update applied to the permissions. */ public void setPermissions(int permissions) { mPermissions = permissions; } + /** + * Enable upload permissions to update in Share resource. + * + * @param publicUpload Upload Permission to set to the target share. + */ + public void setPublicUpload(boolean publicUpload) { + mPublicUpload = publicUpload; + } + @Override protected RemoteOperationResult run(OwnCloudClient client) { RemoteOperationResult result = null; @@ -150,6 +174,8 @@ public class UpdateRemoteShareOperation extends RemoteOperation { parametersToUpdate.add(new Pair(PARAM_PERMISSIONS, Integer.toString(mPermissions))); } + parametersToUpdate.add(new Pair(PARAM_PUBLIC_UPLOAD, Boolean.toString(mPublicUpload))); + /* TODO complete rest of parameters if (mPublicUpload != null) { parametersToUpdate.add(new Pair("publicUpload", mPublicUpload.toString()); @@ -160,7 +186,7 @@ public class UpdateRemoteShareOperation extends RemoteOperation { PutMethod put = null; String uriString = null; - try{ + try { Uri requestUri = client.getBaseUri(); Uri.Builder uriBuilder = requestUri.buildUpon(); uriBuilder.appendEncodedPath(ShareUtils.SHARING_API_PATH.substring(1)); @@ -174,9 +200,9 @@ public class UpdateRemoteShareOperation extends RemoteOperation { put = new PutMethod(uriString); put.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE); put.setRequestEntity(new StringRequestEntity( - parameter.first + "=" + parameter.second, - ENTITY_CONTENT_TYPE, - ENTITY_CHARSET + parameter.first + "=" + parameter.second, + ENTITY_CONTENT_TYPE, + ENTITY_CHARSET )); status = client.executeMethod(put); From 5c68c3f6052e56786749f359fb064c81727c04ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Gonz=C3=A1lez=20Cabrero?= Date: Mon, 14 Mar 2016 13:23:26 +0100 Subject: [PATCH 2/4] Added automated test for creation of the share operation with edit permission enabled --- .../lib/test_project/test/UpdatePublicShareTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java index 6bc9837f..8da09eb0 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java @@ -164,7 +164,13 @@ public class UpdatePublicShareTest extends RemoteTest { updateShare.setExpirationDate(expirationDateInMillis); result = updateShare.execute(mClient); assertTrue(result.isSuccess()); - + + // Update Share with edit permission + updateShare = new UpdateRemoteShareOperation(mShare.getRemoteId()); + updateShare.setPublicUpload(true); + result = updateShare.execute(mClient); + assertTrue(result.isSuccess()); + // unsuccessful test // Update Share with expiration date in the past updateShare = new UpdateRemoteShareOperation(mShare.getRemoteId()); @@ -173,7 +179,7 @@ public class UpdatePublicShareTest extends RemoteTest { updateShare.setExpirationDate(expirationDateInMillis); result = updateShare.execute(mClient); assertFalse(result.isSuccess()); - + // Unshare the file before the unsuccessful tests RemoveRemoteShareOperation unshare = new RemoveRemoteShareOperation((int) mShare.getRemoteId()); result = unshare.execute(mClient); From 57aa370be0fc99ff716127ecd9a5557617eda217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Gonz=C3=A1lez=20Cabrero?= Date: Fri, 18 Mar 2016 13:34:12 +0100 Subject: [PATCH 3/4] Update public share tests and refactored publicUpload permission handling --- .../shares/UpdateRemoteShareOperation.java | 10 ++-- .../test/UpdatePublicShareTest.java | 51 ++++++++++++++++--- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java b/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java index 137d62a8..aed53074 100644 --- a/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java +++ b/src/com/owncloud/android/lib/resources/shares/UpdateRemoteShareOperation.java @@ -86,7 +86,7 @@ public class UpdateRemoteShareOperation extends RemoteOperation { /** * Upload permissions for the public link (only folders) */ - private boolean mPublicUpload; + private Boolean mPublicUpload; /** @@ -98,7 +98,7 @@ public class UpdateRemoteShareOperation extends RemoteOperation { mRemoteId = remoteId; mPassword = null; // no update mExpirationDateInMillis = 0; // no update - mPublicUpload = false; + mPublicUpload = null; } @@ -174,13 +174,9 @@ public class UpdateRemoteShareOperation extends RemoteOperation { parametersToUpdate.add(new Pair(PARAM_PERMISSIONS, Integer.toString(mPermissions))); } - parametersToUpdate.add(new Pair(PARAM_PUBLIC_UPLOAD, Boolean.toString(mPublicUpload))); - - /* TODO complete rest of parameters if (mPublicUpload != null) { - parametersToUpdate.add(new Pair("publicUpload", mPublicUpload.toString()); + parametersToUpdate.add(new Pair(PARAM_PUBLIC_UPLOAD, Boolean.toString(mPublicUpload))); } - */ /// perform required PUT requests PutMethod put = null; diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java index 8da09eb0..fe01e348 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java @@ -64,13 +64,18 @@ public class UpdatePublicShareTest extends RemoteTest { /* File to share and update.*/ private static final String FILE_TO_SHARE = "/fileToShare.txt"; + /* Folder to share and update */ + private static final String FOLDER_TO_SHARE = "/folderToShare"; + // Data for tests private static final String PASSWORD = "password"; private static final String PASS_SPECIAL_CHARS = "p@ssw�rd"; private String mFullPath2FileToShare; + private String mFullPath2FolderToShare; private OCShare mShare; + private OCShare mFolderShare; String mServerUri, mUser, mPass; OwnCloudClient mClient = null; @@ -107,7 +112,7 @@ public class UpdatePublicShareTest extends RemoteTest { Log.v(LOG_TAG, "Setting up the remote fixture..."); - // Upload the files + // Upload the file mFullPath2FileToShare = mBaseFolderPath + FILE_TO_SHARE; File textFile = getActivity().extractAsset(TestActivity.ASSETS__TEXT_FILE_NAME); @@ -126,7 +131,7 @@ public class UpdatePublicShareTest extends RemoteTest { "", false, "", - 1); + OCShare.READ_PERMISSION_FLAG); if (result.isSuccess()){ mShare = (OCShare) result.getData().get(0); @@ -134,7 +139,31 @@ public class UpdatePublicShareTest extends RemoteTest { mShare = null; } - Log.v(LOG_TAG, "Remote fixture created."); + // Create the folder + mFullPath2FolderToShare = mBaseFolderPath + FOLDER_TO_SHARE; + result = getActivity().createFolder( + mFullPath2FolderToShare, + true); + if (!result.isSuccess()) { + Utils.logAndThrow(LOG_TAG, result); + } + + // Share the folder privately with a group + result = getActivity().createShare( + mFullPath2FolderToShare, + ShareType.PUBLIC_LINK, + "", + false, + "", + OCShare.READ_PERMISSION_FLAG); + + if (result.isSuccess()){ + mFolderShare = (OCShare) result.getData().get(0); + } else{ + mFolderShare = null; + } + + Log.v(LOG_TAG, "Remote fixtures created."); } @@ -165,11 +194,11 @@ public class UpdatePublicShareTest extends RemoteTest { result = updateShare.execute(mClient); assertTrue(result.isSuccess()); - // Update Share with edit permission - updateShare = new UpdateRemoteShareOperation(mShare.getRemoteId()); - updateShare.setPublicUpload(true); - result = updateShare.execute(mClient); - assertTrue(result.isSuccess()); + // Update the Folder Share with edit permission + updateShare = new UpdateRemoteShareOperation(mFolderShare.getRemoteId()); + updateShare.setPublicUpload(true); + result = updateShare.execute(mClient); + assertTrue(result.isSuccess()); // unsuccessful test // Update Share with expiration date in the past @@ -179,6 +208,12 @@ public class UpdatePublicShareTest extends RemoteTest { updateShare.setExpirationDate(expirationDateInMillis); result = updateShare.execute(mClient); assertFalse(result.isSuccess()); + + // Try to update the file Share with edit permission + updateShare = new UpdateRemoteShareOperation(mShare.getRemoteId()); + updateShare.setPublicUpload(true); + result = updateShare.execute(mClient); + assertFalse(result.isSuccess()); // Unshare the file before the unsuccessful tests RemoveRemoteShareOperation unshare = new RemoveRemoteShareOperation((int) mShare.getRemoteId()); From 573afa15382b67cc84f67fc9a7b2329a72ecb352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Gonz=C3=A1lez=20Cabrero?= Date: Mon, 21 Mar 2016 09:03:51 +0100 Subject: [PATCH 4/4] Update comment, error handling and indentation in the automatic test --- .../test/UpdatePublicShareTest.java | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java b/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java index fe01e348..21f474ae 100644 --- a/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java +++ b/test_client/tests/src/com/owncloud/android/lib/test_project/test/UpdatePublicShareTest.java @@ -136,32 +136,32 @@ public class UpdatePublicShareTest extends RemoteTest { if (result.isSuccess()){ mShare = (OCShare) result.getData().get(0); } else{ - mShare = null; + Utils.logAndThrow(LOG_TAG, result); } - // Create the folder - mFullPath2FolderToShare = mBaseFolderPath + FOLDER_TO_SHARE; - result = getActivity().createFolder( - mFullPath2FolderToShare, - true); - if (!result.isSuccess()) { - Utils.logAndThrow(LOG_TAG, result); - } + // Create the folder + mFullPath2FolderToShare = mBaseFolderPath + FOLDER_TO_SHARE; + result = getActivity().createFolder( + mFullPath2FolderToShare, + true); + if (!result.isSuccess()) { + Utils.logAndThrow(LOG_TAG, result); + } - // Share the folder privately with a group - result = getActivity().createShare( - mFullPath2FolderToShare, - ShareType.PUBLIC_LINK, - "", - false, - "", - OCShare.READ_PERMISSION_FLAG); + // Share the folder publicly via link + result = getActivity().createShare( + mFullPath2FolderToShare, + ShareType.PUBLIC_LINK, + "", + false, + "", + OCShare.READ_PERMISSION_FLAG); - if (result.isSuccess()){ - mFolderShare = (OCShare) result.getData().get(0); - } else{ - mFolderShare = null; - } + if (result.isSuccess()){ + mFolderShare = (OCShare) result.getData().get(0); + } else{ + Utils.logAndThrow(LOG_TAG, result); + } Log.v(LOG_TAG, "Remote fixtures created.");