From 5806a1b81e4f255dbf7508cd0b3441047dc900c5 Mon Sep 17 00:00:00 2001 From: Minh Nguyen Cong Date: Wed, 9 Aug 2023 16:51:20 +0200 Subject: [PATCH] feat: Support access only collaboration --- .../java/com/box/sdk/BoxCollaborationIT.java | 30 +++++++++++- .../java/com/box/sdk/BoxCollaboration.java | 47 ++++++++++++++++++- .../CreateCollaboration201.json | 3 +- .../CreateFileCollaboration201.json | 3 +- .../GetCollaborationInfo200.json | 3 +- .../GetCollaborationOnFolder200.json | 6 ++- .../GetPendingCollaborationInfo200.json | 3 +- .../UpdateCollaboration200.json | 3 +- .../com/box/sdk/BoxCollaborationTest.java | 14 ++++-- 9 files changed, 97 insertions(+), 15 deletions(-) diff --git a/src/intTest/java/com/box/sdk/BoxCollaborationIT.java b/src/intTest/java/com/box/sdk/BoxCollaborationIT.java index 76d5ce36d..b70c53ca8 100644 --- a/src/intTest/java/com/box/sdk/BoxCollaborationIT.java +++ b/src/intTest/java/com/box/sdk/BoxCollaborationIT.java @@ -11,11 +11,13 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; + +import com.eclipsesource.json.JsonObject; import org.hamcrest.Matchers; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -47,6 +49,7 @@ public void updateInfoSucceeds() { BoxCollaboration.Info collabInfo = folder.collaborate(collaboratorLogin, originalRole); assertThat(collabInfo.getRole(), is(equalTo(originalRole))); + assertNotNull(collabInfo.getIsAccessOnly()); BoxCollaboration collab = collabInfo.getResource(); collabInfo.setRole(newRole); @@ -167,4 +170,29 @@ public void acceptPendingCollaboration() { } } + @Test + public void singleFileCollabWithAccessOnlySucceeds() { + HashMap collabsMap = new HashMap<>(); + BoxAPIConnection api = jwtApiForServiceAccount(); + String fileName = "[singleFileCollabSucceeds] Test File.txt"; + BoxFile uploadedFile = null; + try { + uploadedFile = uploadFileToUniqueFolderWithSomeContent(api, fileName); + JsonObject user = new JsonObject() + .add("login", TestConfig.getCollaborator()) + .add("type", "user"); + JsonObject file = new JsonObject() + .add("id", uploadedFile.getID()) + .add("type", "file"); + + BoxCollaboration.Role originalRole = BoxCollaboration.Role.VIEWER; + BoxCollaboration.Info collabInfo = BoxCollaboration.create(api, user, file, originalRole, + false, false, null, true); + + assertThat(collabInfo.getRole(), is(equalTo(originalRole))); + assertTrue(collabInfo.getIsAccessOnly()); + } finally { + deleteFile(uploadedFile); + } + } } diff --git a/src/main/java/com/box/sdk/BoxCollaboration.java b/src/main/java/com/box/sdk/BoxCollaboration.java index 926e85b23..0270efeeb 100644 --- a/src/main/java/com/box/sdk/BoxCollaboration.java +++ b/src/main/java/com/box/sdk/BoxCollaboration.java @@ -25,7 +25,7 @@ public class BoxCollaboration extends BoxResource { */ public static final String[] ALL_FIELDS = {"type", "id", "item", "accessible_by", "role", "expires_at", "can_view_path", "status", "acknowledged_at", "created_by", - "created_at", "modified_at"}; + "created_at", "modified_at", "is_access_only"}; /** * Collaborations URL Template. @@ -67,7 +67,7 @@ public BoxCollaboration(BoxAPIConnection api, String id) { */ protected static BoxCollaboration.Info create(BoxAPIConnection api, JsonObject accessibleBy, JsonObject item, BoxCollaboration.Role role, Boolean notify, Boolean canViewPath) { - return create(api, accessibleBy, item, role, notify, canViewPath, null); + return create(api, accessibleBy, item, role, notify, canViewPath, null, null); } /** @@ -91,6 +91,32 @@ protected static BoxCollaboration.Info create( Boolean canViewPath, Date expiresAt ) { + return create(api, accessibleBy, item, role, notify, canViewPath, expiresAt, null); + } + + /** + * Create a new collaboration object. + * + * @param api the API connection used to make the request. + * @param accessibleBy the JSON object describing who should be collaborated. + * @param item the JSON object describing which item to collaborate. + * @param role the role to give the collaborators. + * @param notify the user/group should receive email notification of the collaboration or not. + * @param canViewPath the view path collaboration feature is enabled or not. + * @param expiresAt the date the collaboration expires + * @param isAccessOnly the collaboration is an access only collaboration or not. + * @return info about the new collaboration. + */ + protected static BoxCollaboration.Info create( + BoxAPIConnection api, + JsonObject accessibleBy, + JsonObject item, + BoxCollaboration.Role role, + Boolean notify, + Boolean canViewPath, + Date expiresAt, + Boolean isAccessOnly + ) { String queryString = ""; if (notify != null) { @@ -113,6 +139,9 @@ protected static BoxCollaboration.Info create( if (expiresAt != null) { requestJSON.add("expires_at", BoxDateFormat.format(expiresAt)); } + if (isAccessOnly != null) { + requestJSON.add("is_access_only", isAccessOnly); + } BoxJSONRequest request = new BoxJSONRequest(api, url, "POST"); @@ -364,6 +393,7 @@ public class Info extends BoxResource.Info { private BoxItem.Info item; private String inviteEmail; private boolean canViewPath; + private boolean isAccessOnly; /** * Constructs an empty Info object. @@ -453,6 +483,16 @@ public void setCanViewPath(boolean canViewState) { this.addPendingChange("can_view_path", canViewState); } + /** + * Gets a boolean indicator weather "is access only" feature is enabled or not. This field is read only. + * It is used to indicate whether a collaboration is an Access Only Collaboration (AOC). + * When set to true, it separates access from interest by hiding collaborated items from the All Files page + * and the ALF stream. + * This means that users who have been granted access through AOCs will not see these items in their + * regular file view. + */ + public boolean getIsAccessOnly() { return this.isAccessOnly; } + /** * The email address used to invite an un-registered collaborator, if they are not a registered user. * @@ -583,6 +623,9 @@ protected void parseJSONMember(JsonObject.Member member) { case "can_view_path": this.canViewPath = value.asBoolean(); break; + case "is_access_only": + this.isAccessOnly = value.asBoolean(); + break; case "invite_email": this.inviteEmail = value.asString(); break; diff --git a/src/test/Fixtures/BoxCollaboration/CreateCollaboration201.json b/src/test/Fixtures/BoxCollaboration/CreateCollaboration201.json index 68cd5c0fc..e2bb56771 100644 --- a/src/test/Fixtures/BoxCollaboration/CreateCollaboration201.json +++ b/src/test/Fixtures/BoxCollaboration/CreateCollaboration201.json @@ -25,5 +25,6 @@ "sequence_id": "2", "etag": "2", "name": "Ball Valve Diagram" - } + }, + "is_access_only": false } diff --git a/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json b/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json index 22948ee83..5e149347b 100644 --- a/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json +++ b/src/test/Fixtures/BoxCollaboration/CreateFileCollaboration201.json @@ -31,5 +31,6 @@ "etag": "5", "sha1": "aa810e72823f3c3dad2d1d4488966f6f1e0a8a9d", "name": "1_1-4_bsp_ball_valve.pdf" - } + }, + "is_access_only": false } diff --git a/src/test/Fixtures/BoxCollaboration/GetCollaborationInfo200.json b/src/test/Fixtures/BoxCollaboration/GetCollaborationInfo200.json index f1840da64..19e7ad9a8 100644 --- a/src/test/Fixtures/BoxCollaboration/GetCollaborationInfo200.json +++ b/src/test/Fixtures/BoxCollaboration/GetCollaborationInfo200.json @@ -25,5 +25,6 @@ "sequence_id": "2", "etag": "2", "name": "Ball Valve Diagram" - } + }, + "is_access_only": false } diff --git a/src/test/Fixtures/BoxCollaboration/GetCollaborationOnFolder200.json b/src/test/Fixtures/BoxCollaboration/GetCollaborationOnFolder200.json index cd6ab3bbe..e0345e737 100644 --- a/src/test/Fixtures/BoxCollaboration/GetCollaborationOnFolder200.json +++ b/src/test/Fixtures/BoxCollaboration/GetCollaborationOnFolder200.json @@ -28,7 +28,8 @@ "sequence_id": "2", "etag": "2", "name": "Ball Valve Diagram" - } + }, + "is_access_only": false }, { "type": "collaboration", @@ -57,7 +58,8 @@ "sequence_id": "2", "etag": "2", "name": "Ball Valve Diagram" - } + }, + "is_access_only": false } ] } diff --git a/src/test/Fixtures/BoxCollaboration/GetPendingCollaborationInfo200.json b/src/test/Fixtures/BoxCollaboration/GetPendingCollaborationInfo200.json index 07fa2b7c0..8651be3a2 100644 --- a/src/test/Fixtures/BoxCollaboration/GetPendingCollaborationInfo200.json +++ b/src/test/Fixtures/BoxCollaboration/GetPendingCollaborationInfo200.json @@ -22,7 +22,8 @@ }, "role": "editor", "acknowledged_at": null, - "item": null + "item": null, + "is_access_only": false } ], "limit": 100, diff --git a/src/test/Fixtures/BoxCollaboration/UpdateCollaboration200.json b/src/test/Fixtures/BoxCollaboration/UpdateCollaboration200.json index 579c48286..2cb48ee8c 100644 --- a/src/test/Fixtures/BoxCollaboration/UpdateCollaboration200.json +++ b/src/test/Fixtures/BoxCollaboration/UpdateCollaboration200.json @@ -25,5 +25,6 @@ "sequence_id": "2", "etag": "2", "name": "Ball Valve Diagram" - } + }, + "is_access_only": false } diff --git a/src/test/java/com/box/sdk/BoxCollaborationTest.java b/src/test/java/com/box/sdk/BoxCollaborationTest.java index 214c2ab83..e6231b4e1 100644 --- a/src/test/java/com/box/sdk/BoxCollaborationTest.java +++ b/src/test/java/com/box/sdk/BoxCollaborationTest.java @@ -4,8 +4,7 @@ import static com.box.sdk.http.ContentType.APPLICATION_JSON; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static java.lang.String.format; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.*; import com.eclipsesource.json.JsonObject; import com.github.tomakehurst.wiremock.client.WireMock; @@ -187,11 +186,13 @@ public void testCreateAndEditCollaborationSucceeds() { .add("item", folder) .add("role", BoxCollaboration.Role.EDITOR.toJSONString()) .add("can_view_path", false) - .add("expires_at", BoxDateFormat.format(expiresAt)); + .add("expires_at", BoxDateFormat.format(expiresAt)) + .add("is_access_only", false); JsonObject updateBody = new JsonObject() .add("role", BoxCollaboration.Role.VIEWER.toJSONString()) - .add("expires_at", BoxDateFormat.format(expiresAt)); + .add("expires_at", BoxDateFormat.format(expiresAt)) + .add("is_access_only", true); wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(createCollaborationURL)) .withRequestBody(WireMock.equalToJson(createBody.toString())) @@ -211,7 +212,7 @@ public void testCreateAndEditCollaborationSucceeds() { .withBody(editResult))); BoxCollaboration.Info collabInfo = BoxCollaboration.create(this.api, user, folder, BoxCollaboration.Role.EDITOR, - false, false, expiresAt); + false, false, expiresAt, false); assertEquals(BoxCollaboration.Status.ACCEPTED, collabInfo.getStatus()); assertEquals(BoxCollaboration.Role.EDITOR, collabInfo.getRole()); @@ -219,6 +220,8 @@ public void testCreateAndEditCollaborationSucceeds() { assertEquals(collabID, collabInfo.getID()); assertEquals(itemName, collabInfo.getItem().getName()); assertEquals(expiresAt, collabInfo.getExpiresAt()); + assertFalse(collabInfo.getIsAccessOnly()); + BoxCollaboration collaboration = new BoxCollaboration(this.api, collabID); collabInfo.setRole(BoxCollaboration.Role.VIEWER); @@ -254,6 +257,7 @@ public void testGetCollaborationInfoSucceeds() { assertEquals(createdByEmail, collabInfo.getCreatedBy().getLogin()); assertEquals(collabItemID, collabInfo.getItem().getID()); assertEquals(collabItemName, collabInfo.getItem().getName()); + assertFalse(collabInfo.getIsAccessOnly()); } @Test