-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor sendOrgAdminEmail tests (#8497)
Co-authored-by: elisa lee <[email protected]>
- Loading branch information
Showing
1 changed file
with
170 additions
and
145 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import gov.cdc.usds.simplereport.config.FeatureFlagsConfig; | ||
import gov.cdc.usds.simplereport.db.model.DeviceType; | ||
import gov.cdc.usds.simplereport.db.model.Facility; | ||
import gov.cdc.usds.simplereport.db.model.IdentifiedEntity; | ||
import gov.cdc.usds.simplereport.db.model.Organization; | ||
import gov.cdc.usds.simplereport.db.model.PatientSelfRegistrationLink; | ||
import gov.cdc.usds.simplereport.db.model.Person; | ||
|
@@ -38,17 +39,19 @@ | |
import gov.cdc.usds.simplereport.test_util.SliceTestConfiguration.WithSimpleReportOrgAdminUser; | ||
import gov.cdc.usds.simplereport.test_util.SliceTestConfiguration.WithSimpleReportSiteAdminUser; | ||
import gov.cdc.usds.simplereport.test_util.SliceTestConfiguration.WithSimpleReportStandardUser; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
|
@@ -587,203 +590,225 @@ void getOrgAdminUserIds_skipsUser_forNonExistentUserInOrg() { | |
assertThat(adminIds).isEqualTo(expectedIds); | ||
} | ||
|
||
private void sendOrgAdminEmailCSVAsync_mnFacilities_test() | ||
throws ExecutionException, InterruptedException { | ||
when(oktaRepository.getOktaRateLimitSleepMs()).thenReturn(0); | ||
when(oktaRepository.getOktaOrgsLimit()).thenReturn(1); | ||
|
||
String type = "facilities"; | ||
String mnExternalId = "747e341d-0467-45b8-b92f-a638da2bf1ee"; | ||
UUID mnId = organizationRepository.findByExternalId(mnExternalId).get().getInternalId(); | ||
List<String> mnEmails = _service.sendOrgAdminEmailCSVAsync(List.of(mnId), type, "MN").get(); | ||
List<String> expectedMnEmails = | ||
List.of("[email protected]", "[email protected]"); | ||
ArgumentCaptor<List<String>> arg1 = ArgumentCaptor.forClass(List.class); | ||
ArgumentCaptor<String> arg2 = ArgumentCaptor.forClass(String.class); | ||
ArgumentCaptor<String> arg3 = ArgumentCaptor.forClass(String.class); | ||
verify(emailService, times(1)) | ||
.sendWithCSVAttachment(arg1.capture(), arg2.capture(), arg3.capture()); | ||
assertEquals(expectedMnEmails, arg1.getValue()); | ||
assertEquals("MN", arg2.getValue()); | ||
assertEquals(type, arg3.getValue()); | ||
assertThat(mnEmails).isEqualTo(expectedMnEmails); | ||
} | ||
|
||
private void sendOrgAdminEmailCSVAsync_paFacilities_test() | ||
throws ExecutionException, InterruptedException { | ||
when(oktaRepository.getOktaRateLimitSleepMs()).thenReturn(0); | ||
when(oktaRepository.getOktaOrgsLimit()).thenReturn(1); | ||
|
||
String type = "facilities"; | ||
List<String> nonExistentOrgEmails = | ||
_service.sendOrgAdminEmailCSVAsync(List.of(), type, "PA").get(); | ||
ArgumentCaptor<List<String>> arg1 = ArgumentCaptor.forClass(List.class); | ||
ArgumentCaptor<String> arg2 = ArgumentCaptor.forClass(String.class); | ||
ArgumentCaptor<String> arg3 = ArgumentCaptor.forClass(String.class); | ||
verify(emailService, times(1)) | ||
.sendWithCSVAttachment(arg1.capture(), arg2.capture(), arg3.capture()); | ||
assertEquals(nonExistentOrgEmails, arg1.getValue()); | ||
assertEquals("PA", arg2.getValue()); | ||
assertEquals(type, arg3.getValue()); | ||
assertThat(nonExistentOrgEmails).isEmpty(); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSVAsync_withEmails_withOktaMigrationDisabled_success() | ||
throws ExecutionException, InterruptedException { | ||
when(featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(false); | ||
setupDataByFacility(); | ||
sendOrgAdminEmailCSVAsync_mnFacilities_test(); | ||
} | ||
void deleteE2EOktaOrganization_succeeds() { | ||
Organization createdOrg = _dataFactory.saveValidOrganization(); | ||
Organization deletedOrg = _service.deleteE2EOktaOrganization(createdOrg.getExternalId()); | ||
|
||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSVAsync_withNoEmails_withOktaMigrationDisabled_success() | ||
throws ExecutionException, InterruptedException { | ||
when(featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(false); | ||
setupDataByFacility(); | ||
sendOrgAdminEmailCSVAsync_paFacilities_test(); | ||
assertThat(deletedOrg).isEqualTo(createdOrg); | ||
verify(oktaRepository, times(1)).deleteOrganization(createdOrg); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSVAsync_withEmails_withOktaMigrationEnabled_success() | ||
throws ExecutionException, InterruptedException { | ||
when(featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(true); | ||
setupDataByFacility(); | ||
sendOrgAdminEmailCSVAsync_mnFacilities_test(); | ||
} | ||
@Nested | ||
@DisplayName("Sending org admin email CSV") | ||
class SendOrgAdminEmailCSVTest { | ||
@BeforeEach | ||
void setupOrgs() { | ||
when(oktaRepository.getOktaRateLimitSleepMs()).thenReturn(0); | ||
when(oktaRepository.getOktaOrgsLimit()).thenReturn(1); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSVAsync_withNoEmails_withOktaMigrationEnabled_success() | ||
throws ExecutionException, InterruptedException { | ||
when(featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(true); | ||
setupDataByFacility(); | ||
sendOrgAdminEmailCSVAsync_paFacilities_test(); | ||
} | ||
private void sendOrgAdminEmailCSVAsync_mnFacilities_test( | ||
Map<String, List<String>> expectedMnFacilityOrgEmails) | ||
throws ExecutionException, InterruptedException { | ||
String type = "facilities"; | ||
String state = "MN"; | ||
String mnExternalId = expectedMnFacilityOrgEmails.keySet().stream().findFirst().get(); | ||
UUID mnId = organizationRepository.findByExternalId(mnExternalId).get().getInternalId(); | ||
List<String> mnEmails = _service.sendOrgAdminEmailCSVAsync(List.of(mnId), type, state).get(); | ||
List<String> expectedEmails = expectedMnFacilityOrgEmails.get(mnExternalId); | ||
verify(emailService, times(1)).sendWithCSVAttachment(expectedEmails, state, type); | ||
assertThat(mnEmails).isEqualTo(expectedEmails); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportStandardUser | ||
void sendOrgAdminEmailCSV_accessDeniedException() { | ||
assertThrows( | ||
AccessDeniedException.class, | ||
() -> { | ||
_service.sendOrgAdminEmailCSV("facilities", "NM"); | ||
}); | ||
} | ||
private void sendOrgAdminEmailCSVAsync_paFacilities_test() | ||
throws ExecutionException, InterruptedException { | ||
String type = "facilities"; | ||
String state = "PA"; | ||
List<String> nonExistentOrgEmails = | ||
_service.sendOrgAdminEmailCSVAsync(List.of(), type, state).get(); | ||
verify(emailService, times(1)).sendWithCSVAttachment(nonExistentOrgEmails, state, type); | ||
assertThat(nonExistentOrgEmails).isEmpty(); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSV_byFacilities_success() { | ||
setupDataByFacility(); | ||
when(oktaRepository.getOktaRateLimitSleepMs()).thenReturn(0); | ||
when(oktaRepository.getOktaOrgsLimit()).thenReturn(1); | ||
private void sendOrgAdminEmailCSVAsync_njPatients_test(List<String> expectedNJPatientsOrgEmails) | ||
throws ExecutionException, InterruptedException { | ||
String type = "patients"; | ||
String state = "NJ"; | ||
List<UUID> orgsWithNJPatientTestEvents = | ||
organizationRepository.findAllByPatientStateWithTestEvents(state).stream() | ||
.map(IdentifiedEntity::getInternalId) | ||
.toList(); | ||
List<String> njPatientsOrgEmails = | ||
_service.sendOrgAdminEmailCSVAsync(orgsWithNJPatientTestEvents, type, state).get(); | ||
verify(emailService, times(1)).sendWithCSVAttachment(njPatientsOrgEmails, state, type); | ||
assertThat(njPatientsOrgEmails).isEqualTo(expectedNJPatientsOrgEmails); | ||
} | ||
|
||
boolean mnEmailSent = _service.sendOrgAdminEmailCSV("facilities", "MN"); | ||
verify(facilityRepository, times(1)).findByFacilityState("MN"); | ||
assertThat(mnEmailSent).isTrue(); | ||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSVAsync_withEmailsByFacility_withOktaMigrationDisabled_success() | ||
throws ExecutionException, InterruptedException { | ||
when(featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(false); | ||
Map<String, List<String>> orgEmailsByFacility = setupFacilitiesAndReturnMNFacOrgEmails(); | ||
sendOrgAdminEmailCSVAsync_mnFacilities_test(orgEmailsByFacility); | ||
} | ||
|
||
boolean njEmailSent = _service.sendOrgAdminEmailCSV("faCilities", "NJ"); | ||
verify(facilityRepository, times(1)).findByFacilityState("NJ"); | ||
assertThat(njEmailSent).isTrue(); | ||
} | ||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSVAsync_withEmailsByFacility_withOktaMigrationEnabled_success() | ||
throws ExecutionException, InterruptedException { | ||
when(featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(true); | ||
Map<String, List<String>> orgEmailsByFacility = setupFacilitiesAndReturnMNFacOrgEmails(); | ||
sendOrgAdminEmailCSVAsync_mnFacilities_test(orgEmailsByFacility); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSV_byPatients_success() { | ||
setupDataByPatient(); | ||
when(oktaRepository.getOktaRateLimitSleepMs()).thenReturn(0); | ||
when(oktaRepository.getOktaOrgsLimit()).thenReturn(1); | ||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSVAsync_withNoEmailsByFacility_withOktaMigrationDisabled_success() | ||
throws ExecutionException, InterruptedException { | ||
when(featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(false); | ||
sendOrgAdminEmailCSVAsync_paFacilities_test(); | ||
} | ||
|
||
boolean caEmailSent = _service.sendOrgAdminEmailCSV("patients", "CA"); | ||
verify(organizationRepository, times(1)).findAllByPatientStateWithTestEvents("CA"); | ||
assertThat(caEmailSent).isTrue(); | ||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSVAsync_withNoEmailsByFacility_withOktaMigrationEnabled_success() | ||
throws ExecutionException, InterruptedException { | ||
when(featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(true); | ||
sendOrgAdminEmailCSVAsync_paFacilities_test(); | ||
} | ||
|
||
boolean njEmailSent = _service.sendOrgAdminEmailCSV("PATIENTS", "NJ"); | ||
verify(organizationRepository, times(1)).findAllByPatientStateWithTestEvents("NJ"); | ||
assertThat(njEmailSent).isTrue(); | ||
} | ||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSVAsync_withEmailsByPatient_withOktaMigrationDisabled_success() | ||
throws ExecutionException, InterruptedException { | ||
when(featureFlagsConfig.isOktaMigrationEnabled()).thenReturn(false); | ||
List<String> njPatientOrgAdminEmails = setupPatientsAndReturnNJPatientsOrgEmails(); | ||
sendOrgAdminEmailCSVAsync_njPatients_test(njPatientOrgAdminEmails); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSV_byUnsupportedType_success() { | ||
setupDataByPatient(); | ||
when(oktaRepository.getOktaRateLimitSleepMs()).thenReturn(0); | ||
when(oktaRepository.getOktaOrgsLimit()).thenReturn(1); | ||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSVAsync_withEmailsByPatient_withOktaMigrationEnabled_success() | ||
throws ExecutionException, InterruptedException { | ||
List<String> njPatientOrgAdminEmails = setupPatientsAndReturnNJPatientsOrgEmails(); | ||
sendOrgAdminEmailCSVAsync_njPatients_test(njPatientOrgAdminEmails); | ||
} | ||
|
||
boolean unsupportedTypeEmailSent = _service.sendOrgAdminEmailCSV("Unsuported", "CA"); | ||
verify(organizationRepository, times(0)).findAllByPatientStateWithTestEvents("CA"); | ||
verify(facilityRepository, times(0)).findByFacilityState("CA"); | ||
assertThat(unsupportedTypeEmailSent).isTrue(); | ||
} | ||
@Test | ||
@WithSimpleReportStandardUser | ||
void sendOrgAdminEmailCSV_accessDeniedException() { | ||
assertThrows( | ||
AccessDeniedException.class, | ||
() -> { | ||
_service.sendOrgAdminEmailCSV("facilities", "NM"); | ||
}); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void deleteE2EOktaOrganization_succeeds() { | ||
Organization createdOrg = _dataFactory.saveValidOrganization(); | ||
Organization deletedOrg = _service.deleteE2EOktaOrganization(createdOrg.getExternalId()); | ||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSV_byFacilities_success() { | ||
boolean mnEmailSent = _service.sendOrgAdminEmailCSV("facilities", "MN"); | ||
verify(facilityRepository, times(1)).findByFacilityState("MN"); | ||
assertThat(mnEmailSent).isTrue(); | ||
|
||
boolean njEmailSent = _service.sendOrgAdminEmailCSV("faCilities", "NJ"); | ||
verify(facilityRepository, times(1)).findByFacilityState("NJ"); | ||
assertThat(njEmailSent).isTrue(); | ||
} | ||
|
||
assertThat(deletedOrg).isEqualTo(createdOrg); | ||
verify(oktaRepository, times(1)).deleteOrganization(createdOrg); | ||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSV_byPatients_success() { | ||
boolean caEmailSent = _service.sendOrgAdminEmailCSV("patients", "CA"); | ||
verify(organizationRepository, times(1)).findAllByPatientStateWithTestEvents("CA"); | ||
assertThat(caEmailSent).isTrue(); | ||
|
||
boolean njEmailSent = _service.sendOrgAdminEmailCSV("PATIENTS", "NJ"); | ||
verify(organizationRepository, times(1)).findAllByPatientStateWithTestEvents("NJ"); | ||
assertThat(njEmailSent).isTrue(); | ||
} | ||
|
||
@Test | ||
@WithSimpleReportSiteAdminUser | ||
void sendOrgAdminEmailCSV_byUnsupportedType_success() { | ||
boolean unsupportedTypeEmailSent = _service.sendOrgAdminEmailCSV("Unsupported", "CA"); | ||
verify(organizationRepository, times(0)).findAllByPatientStateWithTestEvents("CA"); | ||
verify(facilityRepository, times(0)).findByFacilityState("CA"); | ||
assertThat(unsupportedTypeEmailSent).isTrue(); | ||
} | ||
} | ||
|
||
private void setupDataByFacility() { | ||
private Map<String, List<String>> setupFacilitiesAndReturnMNFacOrgEmails() { | ||
StreetAddress orgAStreetAddress = | ||
new StreetAddress("123 Main Street", null, "Hackensack", "NJ", "07601", "Bergen"); | ||
String njOrgUUID = UUID.randomUUID().toString(); | ||
String njOrgEmail = njOrgUUID + "@example.com"; | ||
Organization orgA = | ||
_dataFactory.saveOrganization( | ||
new Organization("Org A", "k12", "d6b3951b-6698-4ee7-9d63-aaadee85bac0", true)); | ||
_dataFactory.saveOrganization(new Organization(njOrgUUID, "k12", njOrgUUID, true)); | ||
_dataFactory.createValidFacility(orgA, "Org A Facility 1", orgAStreetAddress); | ||
_dataFactory.createValidFacility(orgA, "Org A Facility 2", orgAStreetAddress); | ||
_dataFactory.createValidApiUser("[email protected]", orgA, Role.ADMIN); | ||
_dataFactory.createValidApiUser(njOrgEmail, orgA, Role.ADMIN); | ||
|
||
String mnOrgUUID = UUID.randomUUID().toString(); | ||
String mnOrgEmail1 = mnOrgUUID + "[email protected]"; | ||
String mnOrgEmail2 = mnOrgUUID + "[email protected]"; | ||
|
||
StreetAddress orgBStreetAddress = | ||
new StreetAddress("234 Red Street", null, "Minneapolis", "MN", "55407", "Hennepin"); | ||
Organization orgB = | ||
_dataFactory.saveOrganization( | ||
new Organization("Org B", "airport", "747e341d-0467-45b8-b92f-a638da2bf1ee", true)); | ||
_dataFactory.saveOrganization(new Organization(mnOrgUUID, "airport", mnOrgUUID, true)); | ||
_dataFactory.createValidFacility(orgB, "Org B Facility 1", orgBStreetAddress); | ||
_dataFactory.createValidApiUser("[email protected]", orgB, Role.ADMIN); | ||
_dataFactory.createValidApiUser("[email protected]", orgB, Role.ADMIN); | ||
_dataFactory.createValidApiUser(mnOrgUUID + "[email protected]", orgB, Role.ADMIN); | ||
_dataFactory.createValidApiUser(mnOrgUUID + "[email protected]", orgB, Role.ADMIN); | ||
|
||
Map<String, List<String>> mnEmails = new HashMap<>(); | ||
mnEmails.put(mnOrgUUID, List.of(mnOrgEmail1, mnOrgEmail2)); | ||
|
||
return mnEmails; | ||
} | ||
|
||
private void setupDataByPatient() { | ||
private List<String> setupPatientsAndReturnNJPatientsOrgEmails() { | ||
String orgAUUID = UUID.randomUUID().toString(); | ||
String orgAEmail = orgAUUID + "@example.com"; | ||
StreetAddress njStreetAddress = | ||
new StreetAddress("123 Main Street", null, "Hackensack", "NJ", "07601", "Bergen"); | ||
StreetAddress caStreetAddress = | ||
new StreetAddress("456 Red Street", null, "Sunnyvale", "CA", "94086", "Santa Clara"); | ||
StreetAddress mnStreetAddress = | ||
new StreetAddress("234 Red Street", null, "Minneapolis", "MN", "55407", "Hennepin"); | ||
Organization orgA = | ||
_dataFactory.saveOrganization( | ||
new Organization( | ||
"Org A", "k12", "CA-org-a-5359aa13-93b2-4680-802c-9c90acb5d251", true)); | ||
_dataFactory.createValidApiUser("[email protected]", orgA, Role.ADMIN); | ||
_dataFactory.saveOrganization(new Organization(orgAUUID, "k12", orgAUUID, true)); | ||
_dataFactory.createValidApiUser(orgAEmail, orgA, Role.ADMIN); | ||
Facility orgAFacility = | ||
_dataFactory.createValidFacility(orgA, "Org A Facility 1", caStreetAddress); | ||
_dataFactory.createValidFacility(orgA, "Org A Facility 1", njStreetAddress); | ||
|
||
// create patient in NJ with a test event for Org A | ||
Person orgAPatient1 = | ||
_dataFactory.createFullPersonWithAddress(orgA, njStreetAddress, "Joe", "Moe"); | ||
_dataFactory.createTestEvent(orgAPatient1, orgAFacility, TestResult.POSITIVE); | ||
|
||
String orgBUUID = UUID.randomUUID().toString(); | ||
String orgBEmail1 = orgBUUID + "[email protected]"; | ||
String orgBEmail2 = orgBUUID + "[email protected]"; | ||
Organization orgB = | ||
_dataFactory.saveOrganization( | ||
new Organization( | ||
"Org B", "airport", "MN-org-b-3dddkv89-8981-421b-bd61-f293723284", true)); | ||
_dataFactory.createValidApiUser("[email protected]", orgB, Role.ADMIN); | ||
_dataFactory.createValidApiUser("[email protected]", orgB, Role.USER); | ||
_dataFactory.saveOrganization(new Organization(orgBUUID, "airport", orgBUUID, true)); | ||
_dataFactory.createValidApiUser(orgBEmail1, orgB, Role.ADMIN); | ||
_dataFactory.createValidApiUser( | ||
orgBEmail2, orgB, Role.USER); // should not be returned since not an admin | ||
Facility orgBFacility = | ||
_dataFactory.createValidFacility(orgB, "Org B Facility 1", mnStreetAddress); | ||
// create patient in CA with a test event for Org A | ||
Person orgAPatient2 = | ||
_dataFactory.createFullPersonWithAddress(orgA, caStreetAddress, "Ed", "Eaves"); | ||
_dataFactory.createTestEvent(orgAPatient2, orgBFacility, TestResult.UNDETERMINED); | ||
// create patient in CA with a test event for Org B | ||
// create patient in NJ with a test event for Org B | ||
Person orgBPatient1 = | ||
_dataFactory.createFullPersonWithAddress(orgB, caStreetAddress, "Mary", "Meade"); | ||
_dataFactory.createFullPersonWithAddress(orgB, njStreetAddress, "Mary", "Meade"); | ||
_dataFactory.createTestEvent(orgBPatient1, orgBFacility, TestResult.NEGATIVE); | ||
|
||
return Stream.of(orgAEmail, orgBEmail1).sorted().collect(Collectors.toList()); | ||
} | ||
} |