Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpbrown committed Dec 26, 2024
1 parent eb53a20 commit 1598985
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public void reset() {
}

@Override
public Integer getUsersInSingleFacility(Facility facility) {
public Integer getUsersCountInSingleFacility(Facility facility) {
Integer accessCount = 0;

for (OrganizationRoleClaims existingClaims : usernameOrgRolesMap.values()) {
Expand All @@ -434,7 +434,7 @@ public Integer getUsersInSingleFacility(Facility facility) {
}

@Override
public Integer getUsersInOrganization(Organization org) {
public Integer getUsersCountInOrganization(Organization org) {
return orgUsernamesMap.get(org.getExternalId()).size();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ private Integer getUsersCountInOktaGroup(String groupName) {
}
}

public Integer getUsersInSingleFacility(Facility facility) {
public Integer getUsersCountInSingleFacility(Facility facility) {

Check notice

Code scanning / CodeQL

Missing Override annotation Note

This method overrides
OktaRepository.getUsersCountInSingleFacility
; it is advisable to add an Override annotation.
String facilityAccessGroupName =
generateFacilityGroupName(
facility.getOrganization().getExternalId(), facility.getInternalId());
Expand All @@ -697,7 +697,7 @@ public Integer getUsersInSingleFacility(Facility facility) {
}

@Override
public Integer getUsersInOrganization(Organization org) {
public Integer getUsersCountInOrganization(Organization org) {
String orgDefaultGroupName =
generateRoleGroupName(org.getExternalId(), OrganizationRole.getDefault());
return getUsersCountInOktaGroup(orgDefaultGroupName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ Map<String, UserStatus> getPagedUsersWithStatusForOrganization(

Optional<OrganizationRoleClaims> getOrganizationRoleClaimsForUser(String username);

Integer getUsersInSingleFacility(Facility facility);
Integer getUsersCountInSingleFacility(Facility facility);

Integer getUsersInOrganization(Organization org);
Integer getUsersCountInOrganization(Organization org);

PartialOktaUser findUser(String username);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ public Page<ApiUserWithStatus> getPagedUsersAndStatusInCurrentOrg(int pageNumber
.map(u -> new ApiUserWithStatus(u, emailsToStatus.get(u.getLoginEmail())))
.toList();

Integer userCountInOrg = _oktaRepo.getUsersInOrganization(org);
Integer userCountInOrg = _oktaRepo.getUsersCountInOrganization(org);
PageRequest pageRequest = PageRequest.of(pageNumber, pageSize);

return new PageImpl<>(userWithStatusList, pageRequest, userCountInOrg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public FacilityStats getFacilityStats(@Argument UUID facilityId) {
usersWithSingleFacilityAccess =
dbAuthorizationService.getUsersWithSingleFacilityAccessCount(facility);
} else {
usersWithSingleFacilityAccess = this.oktaRepository.getUsersInSingleFacility(facility);
usersWithSingleFacilityAccess = this.oktaRepository.getUsersCountInSingleFacility(facility);
}
return FacilityStats.builder()
.usersSingleAccessCount(usersWithSingleFacilityAccess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.okta.sdk.resource.model.UserStatus;
import com.okta.sdk.resource.user.UserBuilder;
import gov.cdc.usds.simplereport.api.CurrentTenantDataAccessContextHolder;
import gov.cdc.usds.simplereport.api.model.errors.BadRequestException;
import gov.cdc.usds.simplereport.api.model.errors.ConflictingUserException;
import gov.cdc.usds.simplereport.api.model.errors.IllegalGraphqlArgumentException;
import gov.cdc.usds.simplereport.config.AuthorizationProperties;
Expand All @@ -47,6 +48,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -692,6 +694,109 @@ void getPagedUsersWithStatusForOrganization() {
assertEquals(Map.of("[email protected]", UserStatus.ACTIVE), actual);
}

@Test
void getUsersCountInOrganization() {
String orgExternalId = "ce8782ca-18ba-4384-95fc-fbb7be0ef577";

var groupProfilePrefix = "SR-UNITTEST-TENANT:" + orgExternalId + ":NO_ACCESS";

var mockOrg = mock(Organization.class);
when(mockOrg.getExternalId()).thenReturn(orgExternalId);

var mockGroup = mock(Group.class);
var mockGroupList = List.of(mockGroup);
when(groupApi.listGroups(
eq(groupProfilePrefix),
isNull(),
isNull(),
eq(1),
eq("stats"),
isNull(),
isNull(),
isNull()))
.thenReturn(mockGroupList);

var mockEmbeddedMap = mock(Map.class);
when(mockGroup.getEmbedded()).thenReturn(mockEmbeddedMap);
var mockStatsLinkedHashMap = mock(LinkedHashMap.class);
when(mockEmbeddedMap.get(eq("stats"))).thenReturn(mockStatsLinkedHashMap);
when(mockStatsLinkedHashMap.get(eq("usersCount"))).thenReturn(10);

var actual = _repo.getUsersCountInOrganization(mockOrg);
assertEquals(10, actual);
}

@Test
void getUsersCountInSingleFacility() {
String facilityInternalId = "05bc0080-ad53-4b7a-a4b5-d1f86059a304";
String orgExternalId = "ce8782ca-18ba-4384-95fc-fbb7be0ef577";

var facilitySuffix = ":FACILITY_ACCESS:" + facilityInternalId;
var groupProfilePrefix = "SR-UNITTEST-TENANT:" + orgExternalId + facilitySuffix;

var mockOrg = mock(Organization.class);
var mockFacility = mock(Facility.class);
when(mockFacility.getOrganization()).thenReturn(mockOrg);
when(mockOrg.getExternalId()).thenReturn(orgExternalId);
when(mockFacility.getInternalId()).thenReturn(UUID.fromString(facilityInternalId));

var mockGroup = mock(Group.class);
var mockGroupList = List.of(mockGroup);
when(groupApi.listGroups(
eq(groupProfilePrefix),
isNull(),
isNull(),
eq(1),
eq("stats"),
isNull(),
isNull(),
isNull()))
.thenReturn(mockGroupList);

var mockEmbeddedMap = mock(Map.class);
when(mockGroup.getEmbedded()).thenReturn(mockEmbeddedMap);
var mockStatsLinkedHashMap = mock(LinkedHashMap.class);
when(mockEmbeddedMap.get(eq("stats"))).thenReturn(mockStatsLinkedHashMap);
when(mockStatsLinkedHashMap.get(eq("usersCount"))).thenReturn(10);

var actual = _repo.getUsersCountInSingleFacility(mockFacility);
assertEquals(10, actual);
}

@Test
void
getUsersCountInSingleFacility_throwsBadRequestException_whenUnableToRetrieveOktaGroupStats() {
String facilityInternalId = "05bc0080-ad53-4b7a-a4b5-d1f86059a304";
String orgExternalId = "ce8782ca-18ba-4384-95fc-fbb7be0ef577";

var facilitySuffix = ":FACILITY_ACCESS:" + facilityInternalId;
var groupProfilePrefix = "SR-UNITTEST-TENANT:" + orgExternalId + facilitySuffix;

var mockOrg = mock(Organization.class);
var mockFacility = mock(Facility.class);
when(mockFacility.getOrganization()).thenReturn(mockOrg);
when(mockOrg.getExternalId()).thenReturn(orgExternalId);
when(mockFacility.getInternalId()).thenReturn(UUID.fromString(facilityInternalId));

var mockGroup = mock(Group.class);
var mockGroupList = List.of(mockGroup);
when(groupApi.listGroups(
eq(groupProfilePrefix),
isNull(),
isNull(),
eq(1),
eq("stats"),
isNull(),
isNull(),
isNull()))
.thenReturn(mockGroupList);

Throwable caught =
assertThrows(
BadRequestException.class, () -> _repo.getUsersCountInSingleFacility(mockFacility));
assertEquals("Unable to retrieve okta group stats", caught.getMessage());
}

@Test
void updateUserPrivileges() {
var username = "[email protected]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ void getFacilityStats_withOktaMigrationDisabled_success() {
UUID facilityId = UUID.randomUUID();
Facility mockFacility = mock(Facility.class);
doReturn(Optional.of(mockFacility)).when(this.facilityRepository).findById(facilityId);
doReturn(2).when(oktaRepository).getUsersInSingleFacility(mockFacility);
doReturn(2).when(oktaRepository).getUsersCountInSingleFacility(mockFacility);
doReturn(1).when(personRepository).countByFacilityAndIsDeleted(mockFacility, false);
FacilityStats stats = _service.getFacilityStats(facilityId);

Expand All @@ -479,7 +479,7 @@ void getFacilityStats_withOktaMigrationEnabled_success() {
doReturn(2).when(personRepository).countByFacilityAndIsDeleted(mockFacility, false);
FacilityStats stats = _service.getFacilityStats(facilityId);

verify(oktaRepository, times(0)).getUsersInSingleFacility(mockFacility);
verify(oktaRepository, times(0)).getUsersCountInSingleFacility(mockFacility);
assertEquals(4, stats.getUsersSingleAccessCount());
assertEquals(2, stats.getPatientsSingleAccessCount());
}
Expand Down

0 comments on commit 1598985

Please sign in to comment.