|
32 | 32 | import java.time.Instant;
|
33 | 33 | import java.util.Collections;
|
34 | 34 | import java.util.List;
|
| 35 | +import java.util.Map; |
35 | 36 |
|
36 | 37 | import org.junit.Before;
|
37 | 38 | import org.mockito.ArgumentCaptor;
|
|
47 | 48 | import org.opensearch.action.search.SearchResponse;
|
48 | 49 | import org.opensearch.action.search.SearchResponseSections;
|
49 | 50 | import org.opensearch.action.search.ShardSearchFailure;
|
| 51 | +import org.opensearch.action.update.UpdateRequest; |
| 52 | +import org.opensearch.action.update.UpdateResponse; |
50 | 53 | import org.opensearch.client.AdminClient;
|
51 | 54 | import org.opensearch.client.Client;
|
52 | 55 | import org.opensearch.client.IndicesAdminClient;
|
@@ -800,4 +803,74 @@ public void testGetSg_ClientFails_ThenFail() {
|
800 | 803 | verify(getListener, times(1)).onFailure(argCaptor.capture());
|
801 | 804 | assert (argCaptor.getValue().getMessage().equals("Client Failure in Sg Get"));
|
802 | 805 | }
|
| 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( Map |
| 866 | + .of( |
| 867 | + ConversationalIndexConstants.INTERACTIONS_CREATE_TIME_FIELD, |
| 868 | + Instant.now().toString(), |
| 869 | + ConversationalIndexConstants.INTERACTIONS_CONVERSATION_ID_FIELD, |
| 870 | + "conversation test 1", |
| 871 | + ConversationalIndexConstants.INTERACTIONS_RESPONSE_FIELD, |
| 872 | + "answer1" |
| 873 | + )).when(response).getSourceAsMap(); |
| 874 | + return response; |
| 875 | + } |
803 | 876 | }
|
0 commit comments