Skip to content

Commit

Permalink
fix: clean up shared record on discard (#285)(#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim-Gadalov authored Mar 20, 2024
1 parent a9f9222 commit b6d6448
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/epam/aidial/core/service/ShareService.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ public void discardSharedAccess(String bucket, String location, ResourceLinkColl
// if userLocations is NULL - this means that provided resource wasn't shared
if (userLocations != null) {
userLocations.remove(location);

// clean up shared resource
if (userLocations.isEmpty()) {
sharedByMeDto.getResourceToUsers().remove(resourceUrl);
}
}
}

Expand Down
155 changes: 146 additions & 9 deletions src/test/java/com/epam/aidial/core/ShareApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public void testDiscardSharedAccess() {
}
""");

// verify user1 has shared_by_me resource
// verify user1 has no shared_by_me resource
response = operationRequest("/v1/ops/resource/share/list", """
{
"resourceTypes": ["CONVERSATION"],
Expand All @@ -438,14 +438,151 @@ public void testDiscardSharedAccess() {
""");
verifyJson(response, 200, """
{
"resources" : [ {
"name" : "conversation@",
"parentPath" : "folder",
"bucket" : "3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST",
"url" : "conversations/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/folder/conversation@",
"nodeType" : "ITEM",
"resourceType" : "CONVERSATION"
} ]
"resources" : []
}
""");

// verify user2 has no shared_by_me resources
response = operationRequest("/v1/ops/resource/share/list", """
{
"resourceTypes": ["CONVERSATION"],
"with": "others"
}
""", "Api-key", "proxyKey2");
verifyJson(response, 200, """
{
"resources": []
}
""");
}

@Test
public void testDiscardSharedFolderAccess() {
// check no conversations shared with me
Response response = operationRequest("/v1/ops/resource/share/list", """
{
"resourceTypes": ["CONVERSATION"],
"with": "me"
}
""");
verifyJson(response, 200, """
{
"resources": []
}
""");

// check no conversations shared by me
response = operationRequest("/v1/ops/resource/share/list", """
{
"resourceTypes": ["CONVERSATION"],
"with": "others"
}
""");
verifyJson(response, 200, """
{
"resources": []
}
""");

// create conversation1
response = resourceRequest(HttpMethod.PUT, "/folder/conversation1", CONVERSATION_BODY_1);
verifyNotExact(response, 200, "\"url\":\"conversations/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/folder/conversation1\"");

// create conversation2
response = resourceRequest(HttpMethod.PUT, "/folder/conversation2", CONVERSATION_BODY_2);
verifyNotExact(response, 200, "\"url\":\"conversations/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/folder/conversation2\"");

// initialize share request
response = operationRequest("/v1/ops/resource/share/create", """
{
"invitationType": "link",
"resources": [
{
"url": "conversations/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/folder/"
}
]
}
""");
verify(response, 200);
InvitationLink invitationLink = ProxyUtil.convertToObject(response.body(), InvitationLink.class);
assertNotNull(invitationLink);

// verify user2 do not have access to the conversation1
response = resourceRequest(HttpMethod.GET, "/folder/conversation1", null, "Api-key", "proxyKey2");
verify(response, 403);

// verify user2 do not have access to the conversation2
response = resourceRequest(HttpMethod.GET, "/folder/conversation2", null, "Api-key", "proxyKey2");
verify(response, 403);

// accept invitation
response = send(HttpMethod.GET, invitationLink.invitationLink(), "accept=true", null, "Api-key", "proxyKey2");
verify(response, 200);

// verify user2 has access to the conversation1
response = resourceRequest(HttpMethod.GET, "/folder/conversation1", null, "Api-key", "proxyKey2");
verify(response, 200, CONVERSATION_BODY_1);

// verify user2 has access to the conversation2
response = resourceRequest(HttpMethod.GET, "/folder/conversation2", null, "Api-key", "proxyKey2");
verify(response, 200, CONVERSATION_BODY_2);

// discard share access
response = operationRequest("/v1/ops/resource/share/discard", """
{
"resources": [
{
"url": "conversations/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/folder/"
}
]
}
""", "Api-key", "proxyKey2");
verify(response, 200);

// verify user2 do not have access to the conversation1
response = resourceRequest(HttpMethod.GET, "/folder/conversation1", null, "Api-key", "proxyKey2");
verify(response, 403);

// verify user2 do not have access to the conversation2
response = resourceRequest(HttpMethod.GET, "/folder/conversation2", null, "Api-key", "proxyKey2");
verify(response, 403);

// verify user1 has no shared_with_me resources
response = operationRequest("/v1/ops/resource/share/list", """
{
"resourceTypes": ["CONVERSATION"],
"with": "me"
}
""");
verifyJson(response, 200, """
{
"resources": []
}
""");

// verify user2 has no shared_with_me resource
response = operationRequest("/v1/ops/resource/share/list", """
{
"resourceTypes": ["CONVERSATION"],
"with": "me"
}
""", "Api-key", "proxyKey2");
verifyJson(response, 200, """
{
"resources" : []
}
""");

// verify user1 has no shared_by_me resource
response = operationRequest("/v1/ops/resource/share/list", """
{
"resourceTypes": ["CONVERSATION"],
"with": "others"
}
""");
verifyJson(response, 200, """
{
"resources" : []
}
""");

Expand Down

0 comments on commit b6d6448

Please sign in to comment.