-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update userIsInSameOrg to use DB when feature flag enabled
- Loading branch information
Showing
2 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...st/java/gov/cdc/usds/simplereport/config/authorization/UserAuthorizationVerifierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package gov.cdc.usds.simplereport.config.authorization; | ||
|
||
import static gov.cdc.usds.simplereport.test_util.TestUserIdentities.ALL_FACILITIES_USER; | ||
import static gov.cdc.usds.simplereport.test_util.TestUserIdentities.OTHER_ORG_ADMIN; | ||
import static gov.cdc.usds.simplereport.test_util.TestUserIdentities.OTHER_ORG_USER; | ||
import static gov.cdc.usds.simplereport.test_util.TestUserIdentities.STANDARD_USER; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import gov.cdc.usds.simplereport.config.FeatureFlagsConfig; | ||
import gov.cdc.usds.simplereport.db.model.ApiUser; | ||
import gov.cdc.usds.simplereport.db.repository.ApiUserRepository; | ||
import gov.cdc.usds.simplereport.service.BaseServiceTest; | ||
import gov.cdc.usds.simplereport.test_util.SliceTestConfiguration; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
@TestPropertySource(properties = {"spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true"}) | ||
class UserAuthorizationVerifierTest extends BaseServiceTest<UserAuthorizationVerifier> { | ||
@Autowired @SpyBean ApiUserRepository _apiUserRepo; | ||
@MockBean FeatureFlagsConfig _featureFlagsConfig; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
initSampleData(); | ||
} | ||
|
||
@Test | ||
@SliceTestConfiguration.WithSimpleReportOrgAdminUser | ||
void userIsInSameOrg_whenOktaMigrationDisabled_forUsersInSameOrg_returnsTrue() { | ||
// GIVEN | ||
when(_featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(false); | ||
ApiUser user = _apiUserRepo.findByLoginEmail(ALL_FACILITIES_USER).get(); | ||
ApiUser userSpy = spy(user); | ||
when(_apiUserRepo.findByIdIncludeArchived(user.getInternalId())) | ||
.thenReturn(Optional.of(userSpy)); | ||
|
||
// WHEN | ||
boolean isSameOrg = _service.userIsInSameOrg(user.getInternalId()); | ||
|
||
// THEN | ||
verify(userSpy, times(0)).getOrganizations(); | ||
assertTrue(isSameOrg); | ||
} | ||
|
||
@Test | ||
@SliceTestConfiguration.WithSimpleReportOrgAdminUser | ||
void userIsInSameOrg_whenOktaMigrationDisabled_forUsersInDifferentOrgs_returnsFalse() { | ||
// GIVEN | ||
when(_featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(false); | ||
ApiUser user = _apiUserRepo.findByLoginEmail(OTHER_ORG_USER).get(); | ||
ApiUser userSpy = spy(user); | ||
when(_apiUserRepo.findByIdIncludeArchived(user.getInternalId())) | ||
.thenReturn(Optional.of(userSpy)); | ||
|
||
// WHEN | ||
boolean isSameOrg = _service.userIsInSameOrg(user.getInternalId()); | ||
|
||
// THEN | ||
verify(userSpy, times(0)).getOrganizations(); | ||
assertFalse(isSameOrg); | ||
} | ||
|
||
@Test | ||
@SliceTestConfiguration.WithSimpleReportOrgAdminUser | ||
void userIsInSameOrg_whenOktaMigrationEnabled_forUsersInSameOrg_returnsTrue() { | ||
// GIVEN | ||
when(_featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(true); | ||
ApiUser user = _apiUserRepo.findByLoginEmail(STANDARD_USER).get(); | ||
ApiUser userSpy = spy(user); | ||
when(_apiUserRepo.findByIdIncludeArchived(user.getInternalId())) | ||
.thenReturn(Optional.of(userSpy)); | ||
|
||
// WHEN | ||
boolean isSameOrg = _service.userIsInSameOrg(user.getInternalId()); | ||
|
||
// THEN | ||
verify(userSpy, times(1)).getOrganizations(); | ||
assertTrue(isSameOrg); | ||
} | ||
|
||
@Test | ||
@SliceTestConfiguration.WithSimpleReportEntryOnlyUser | ||
void userIsInSameOrg_whenOktaMigrationEnabled_forUsersInDifferentOrgs_returnsFalse() { | ||
// GIVEN | ||
when(_featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(true); | ||
ApiUser user = _apiUserRepo.findByLoginEmail(OTHER_ORG_ADMIN).get(); | ||
ApiUser userSpy = spy(user); | ||
when(_apiUserRepo.findByIdIncludeArchived(user.getInternalId())) | ||
.thenReturn(Optional.of(userSpy)); | ||
|
||
// WHEN | ||
boolean isSameOrg = _service.userIsInSameOrg(user.getInternalId()); | ||
|
||
// THEN | ||
verify(userSpy, times(1)).getOrganizations(); | ||
assertFalse(isSameOrg); | ||
} | ||
} |