Skip to content

Commit

Permalink
feat: Support access only collaboration
Browse files Browse the repository at this point in the history
  • Loading branch information
congminh1254 committed Aug 9, 2023
1 parent be875e8 commit 5806a1b
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 15 deletions.
30 changes: 29 additions & 1 deletion src/intTest/java/com/box/sdk/BoxCollaborationIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -167,4 +170,29 @@ public void acceptPendingCollaboration() {
}
}

@Test
public void singleFileCollabWithAccessOnlySucceeds() {
HashMap<String, BoxCollaboration.Info> 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);
}
}
}
47 changes: 45 additions & 2 deletions src/main/java/com/box/sdk/BoxCollaboration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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) {
Expand All @@ -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");

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"sequence_id": "2",
"etag": "2",
"name": "Ball Valve Diagram"
}
},
"is_access_only": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
"etag": "5",
"sha1": "aa810e72823f3c3dad2d1d4488966f6f1e0a8a9d",
"name": "1_1-4_bsp_ball_valve.pdf"
}
},
"is_access_only": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"sequence_id": "2",
"etag": "2",
"name": "Ball Valve Diagram"
}
},
"is_access_only": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"sequence_id": "2",
"etag": "2",
"name": "Ball Valve Diagram"
}
},
"is_access_only": false
},
{
"type": "collaboration",
Expand Down Expand Up @@ -57,7 +58,8 @@
"sequence_id": "2",
"etag": "2",
"name": "Ball Valve Diagram"
}
},
"is_access_only": false
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"role": "editor",
"acknowledged_at": null,
"item": null
"item": null,
"is_access_only": false
}
],
"limit": 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"sequence_id": "2",
"etag": "2",
"name": "Ball Valve Diagram"
}
},
"is_access_only": false
}
14 changes: 9 additions & 5 deletions src/test/java/com/box/sdk/BoxCollaborationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()))
Expand All @@ -211,14 +212,16 @@ 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());
assertFalse(collabInfo.getCanViewPath());
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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5806a1b

Please sign in to comment.