Skip to content

Commit 7a74d09

Browse files
committed
add UT for acess denied cases
Signed-off-by: Xun Zhang <xunzh@amazon.com>
1 parent 1f6c183 commit 7a74d09

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

memory/src/test/java/org/opensearch/ml/memory/index/ConversationMetaIndexTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -701,4 +701,19 @@ public void testUpdateConversation_ClientFails() {
701701
verify(getListener, times(1)).onFailure(argCaptor.capture());
702702
assert (argCaptor.getValue().getMessage().equals("Client Failure"));
703703
}
704+
705+
public void testUpdateConversation_NoAccess_ThenFail() {
706+
doReturn(true).when(metadata).hasIndex(anyString());
707+
doAnswer(invocation -> {
708+
ActionListener<Boolean> al = invocation.getArgument(1);
709+
al.onResponse(false);
710+
return null;
711+
}).when(conversationMetaIndex).checkAccess(anyString(), any());
712+
713+
ActionListener<UpdateResponse> updateListener = mock(ActionListener.class);
714+
conversationMetaIndex.updateConversation("conversationId", new UpdateRequest(), updateListener);
715+
ArgumentCaptor<Exception> argCaptor = ArgumentCaptor.forClass(Exception.class);
716+
verify(updateListener, times(1)).onFailure(argCaptor.capture());
717+
assert (argCaptor.getValue().getMessage().equals("User [BAD_USER] does not have access to conversation conversationId"));
718+
}
704719
}

memory/src/test/java/org/opensearch/ml/memory/index/InteractionsIndexTests.java

+75
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.time.Instant;
3333
import java.util.Collections;
3434
import java.util.List;
35+
import java.util.Map;
3536

3637
import org.junit.Before;
3738
import org.mockito.ArgumentCaptor;
@@ -47,6 +48,8 @@
4748
import org.opensearch.action.search.SearchResponse;
4849
import org.opensearch.action.search.SearchResponseSections;
4950
import org.opensearch.action.search.ShardSearchFailure;
51+
import org.opensearch.action.update.UpdateRequest;
52+
import org.opensearch.action.update.UpdateResponse;
5053
import org.opensearch.client.AdminClient;
5154
import org.opensearch.client.Client;
5255
import org.opensearch.client.IndicesAdminClient;
@@ -800,4 +803,76 @@ public void testGetSg_ClientFails_ThenFail() {
800803
verify(getListener, times(1)).onFailure(argCaptor.capture());
801804
assert (argCaptor.getValue().getMessage().equals("Client Failure in Sg Get"));
802805
}
806+
807+
public void testGetSg_NoAccess_ThenFail() {
808+
doReturn(true).when(metadata).hasIndex(anyString());
809+
setupDenyAccess("Henry");
810+
setupRefreshSuccess();
811+
GetResponse response = setUpInteractionResponse("iid");
812+
doAnswer(invocation -> {
813+
ActionListener<GetResponse> listener = invocation.getArgument(1);
814+
listener.onResponse(response);
815+
return null;
816+
}).when(client).get(any(), any());
817+
ActionListener<Interaction> getListener = mock(ActionListener.class);
818+
interactionsIndex.getInteraction("iid", getListener);
819+
ArgumentCaptor<Exception> argCaptor = ArgumentCaptor.forClass(Exception.class);
820+
verify(getListener, times(1)).onFailure(argCaptor.capture());
821+
assert (argCaptor.getValue().getMessage().equals("User [Henry] does not have access to interaction iid"));
822+
}
823+
824+
public void testGetTraces_NoAccess_ThenFail() {
825+
doReturn(true).when(metadata).hasIndex(anyString());
826+
setupRefreshSuccess();
827+
setupDenyAccess("Xun");
828+
GetResponse response = setUpInteractionResponse("iid");
829+
doAnswer(invocation -> {
830+
ActionListener<GetResponse> listener = invocation.getArgument(1);
831+
listener.onResponse(response);
832+
return null;
833+
}).when(client).get(any(), any());
834+
835+
ActionListener<List<Interaction>> getListener = mock(ActionListener.class);
836+
interactionsIndex.getTraces("iid", 0, 10, getListener);
837+
ArgumentCaptor<Exception> argCaptor = ArgumentCaptor.forClass(Exception.class);
838+
verify(getListener, times(1)).onFailure(argCaptor.capture());
839+
assert (argCaptor.getValue().getMessage().equals("User [Xun] does not have access to interaction iid"));
840+
}
841+
842+
public void testUpdateInteraction_NoAccess_ThenFail() {
843+
doReturn(true).when(metadata).hasIndex(anyString());
844+
setupRefreshSuccess();
845+
setupDenyAccess("Xun");
846+
GetResponse response = setUpInteractionResponse("iid");
847+
doAnswer(invocation -> {
848+
ActionListener<GetResponse> listener = invocation.getArgument(1);
849+
listener.onResponse(response);
850+
return null;
851+
}).when(client).get(any(), any());
852+
853+
ActionListener<UpdateResponse> updateListener = mock(ActionListener.class);
854+
interactionsIndex.updateInteraction("iid", new UpdateRequest(), updateListener);
855+
ArgumentCaptor<Exception> argCaptor = ArgumentCaptor.forClass(Exception.class);
856+
verify(updateListener, times(1)).onFailure(argCaptor.capture());
857+
assert (argCaptor.getValue().getMessage().equals("User [Xun] does not have access to interaction iid"));
858+
}
859+
860+
private GetResponse setUpInteractionResponse(String interactionId) {
861+
@SuppressWarnings("unchecked")
862+
GetResponse response = mock(GetResponse.class);
863+
doReturn(true).when(response).isExists();
864+
doReturn(interactionId).when(response).getId();
865+
doReturn(
866+
Map
867+
.of(
868+
ConversationalIndexConstants.INTERACTIONS_CREATE_TIME_FIELD,
869+
Instant.now().toString(),
870+
ConversationalIndexConstants.INTERACTIONS_CONVERSATION_ID_FIELD,
871+
"conversation test 1",
872+
ConversationalIndexConstants.INTERACTIONS_RESPONSE_FIELD,
873+
"answer1"
874+
)
875+
).when(response).getSourceAsMap();
876+
return response;
877+
}
803878
}

0 commit comments

Comments
 (0)