Skip to content

Commit

Permalink
Mock api tokenrepository behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Ho <[email protected]>
  • Loading branch information
derek-ho committed Jan 21, 2025
1 parent 685dcfc commit 6ffc060
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.security.action.apitokens.ApiTokenRepository;
import org.opensearch.security.action.apitokens.Permissions;
import org.opensearch.security.auditlog.NullAuditLog;
import org.opensearch.security.securityconf.ConfigModel;
import org.opensearch.security.securityconf.DynamicConfigModel;
Expand All @@ -40,6 +41,7 @@
import org.opensearch.security.securityconf.impl.v7.RoleV7;
import org.opensearch.security.user.User;

import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.quality.Strictness;
Expand Down Expand Up @@ -149,6 +151,8 @@ public void testEvaluate_Unsuccessful() throws Exception {
}

PrivilegesEvaluator createPrivilegesEvaluator(SecurityDynamicConfiguration<RoleV7> roles) {
ApiTokenRepository mockApiTokenRepository = mock(ApiTokenRepository.class);
when(mockApiTokenRepository.getApiTokenPermissionsForUser(ArgumentMatchers.any())).thenReturn(new Permissions());
PrivilegesEvaluator privilegesEvaluator = new PrivilegesEvaluator(
clusterService,
() -> clusterService.state(),
Expand All @@ -162,7 +166,7 @@ PrivilegesEvaluator createPrivilegesEvaluator(SecurityDynamicConfiguration<RoleV
null,
null,
null,
mock(ApiTokenRepository.class)
mockApiTokenRepository
);
privilegesEvaluator.onConfigModelChanged(configModel); // Defaults to the mocked config model
privilegesEvaluator.onDynamicConfigModelChanged(dynamicConfigModel);
Expand Down

0 comments on commit 6ffc060

Please sign in to comment.