Search in sources :

Example 1 with SynchronizeUserAccessRequest

use of org.pmiops.workbench.model.SynchronizeUserAccessRequest in project workbench by all-of-us.

the class CloudTaskUserControllerTest method testSynchronizeAccess.

@Test
public void testSynchronizeAccess() {
    when(mockAccessModuleService.getAccessModuleStatus(userA, DbAccessModuleName.TWO_FACTOR_AUTH)).thenReturn(Optional.of(new AccessModuleStatus().completionEpochMillis(123L)));
    when(mockAccessModuleService.getAccessModuleStatus(userB, DbAccessModuleName.TWO_FACTOR_AUTH)).thenReturn(Optional.of(new AccessModuleStatus()));
    // kluge to prevent test NPEs on the return value of syncDuccVersionStatus()
    when(mockUserService.syncDuccVersionStatus(userA, Agent.asSystem())).thenReturn(userA);
    when(mockUserService.syncDuccVersionStatus(userB, Agent.asSystem())).thenReturn(userB);
    controller.synchronizeUserAccess(new SynchronizeUserAccessRequest().addUserIdsItem(userA.getUserId()).addUserIdsItem(userB.getUserId()));
    // Ideally we would use a real implementation of UserService and mock its external deps, but
    // unfortunately UserService is too sprawling to replicate in a unit test.
    // we sync DUCC for all users
    verify(mockUserService).syncDuccVersionStatus(userA, Agent.asSystem());
    verify(mockUserService).syncDuccVersionStatus(userB, Agent.asSystem());
    // we only sync 2FA users with completed 2FA
    verify(mockUserService).syncTwoFactorAuthStatus(userA, Agent.asSystem());
    // normally we would expect the userService sync methods to add to this count, but userService
    // is mocked so we only see the direct calls from synchronizeUserAccess(), one per user
    verify(mockUserService, times(2)).updateUserAccessTiers(any(), any());
    verifyNoMoreInteractions(mockUserService);
}
Also used : SynchronizeUserAccessRequest(org.pmiops.workbench.model.SynchronizeUserAccessRequest) AccessModuleStatus(org.pmiops.workbench.model.AccessModuleStatus) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) Test(org.junit.jupiter.api.Test)

Example 2 with SynchronizeUserAccessRequest

use of org.pmiops.workbench.model.SynchronizeUserAccessRequest in project workbench by all-of-us.

the class OfflineUserControllerTest method testSynchronizeUserAccess.

@Test
public void testSynchronizeUserAccess() {
    offlineUserController.synchronizeUserAccess();
    // We set a batch size of 3, so we expect two cloud tasks.
    List<SynchronizeUserAccessRequest> expectedRequests = ImmutableList.of(new SynchronizeUserAccessRequest().userIds(ImmutableList.of(1L, 2L, 3L)), new SynchronizeUserAccessRequest().userIds(ImmutableList.of(4L)));
    for (SynchronizeUserAccessRequest expected : expectedRequests) {
        verify(mockCloudTasksClient).createTask(matches(Pattern.compile(".*/synchronizeAccessQueue$")), argThat(taskRequest -> expected.equals(cloudTaskToSynchronizeRequest(taskRequest))));
    }
    verifyNoMoreInteractions(mockCloudTasksClient);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TaskQueueService(org.pmiops.workbench.cloudtasks.TaskQueueService) DirtiesContext(org.springframework.test.annotation.DirtiesContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) SynchronizeUserAccessRequest(org.pmiops.workbench.model.SynchronizeUserAccessRequest) Autowired(org.springframework.beans.factory.annotation.Autowired) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) Task(com.google.cloud.tasks.v2.Task) Scope(org.springframework.context.annotation.Scope) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) TestConfiguration(org.springframework.boot.test.context.TestConfiguration) ImmutableList(com.google.common.collect.ImmutableList) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Gson(com.google.gson.Gson) MockBean(org.springframework.boot.test.mock.mockito.MockBean) UserService(org.pmiops.workbench.db.dao.UserService) Timestamp(java.sql.Timestamp) AuditProjectAccessRequest(org.pmiops.workbench.model.AuditProjectAccessRequest) Import(org.springframework.context.annotation.Import) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) WorkbenchConfig(org.pmiops.workbench.config.WorkbenchConfig) ArgumentMatchers.matches(org.mockito.ArgumentMatchers.matches) WorkbenchLocationConfigService(org.pmiops.workbench.config.WorkbenchLocationConfigService) FakeClockConfiguration(org.pmiops.workbench.FakeClockConfiguration) Pattern(java.util.regex.Pattern) DbUser(org.pmiops.workbench.db.model.DbUser) Bean(org.springframework.context.annotation.Bean) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SynchronizeUserAccessRequest(org.pmiops.workbench.model.SynchronizeUserAccessRequest) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)2 SynchronizeUserAccessRequest (org.pmiops.workbench.model.SynchronizeUserAccessRequest)2 DataJpaTest (org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)2 CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)1 Task (com.google.cloud.tasks.v2.Task)1 ImmutableList (com.google.common.collect.ImmutableList)1 Gson (com.google.gson.Gson)1 Timestamp (java.sql.Timestamp)1 Instant (java.time.Instant)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)1 ArgumentMatchers.matches (org.mockito.ArgumentMatchers.matches)1 Mockito.verify (org.mockito.Mockito.verify)1 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)1