Skip to content

Commit

Permalink
Make add patient e2e test rerunnable
Browse files Browse the repository at this point in the history
  • Loading branch information
emyl3 committed Oct 20, 2023
1 parent f6683ef commit b91b8cc
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public List<ApiOrganization> organizations(@Argument Boolean identityVerified) {
.collect(Collectors.toList());
}

/**
* Retrieves a list of all organizations, filtered by name
*
* @return a list of organizations
*/
@QueryMapping
@AuthorizationConfiguration.RequireGlobalAdminUser
public List<ApiOrganization> organizationsByName(@Argument String name) {
List<Organization> orgs = _organizationService.getOrganizationsByName(name);
if (orgs.isEmpty()) {
return null;
} else {
return orgs.stream()
.map(o -> new ApiOrganization(o, _organizationService.getFacilities(o)))
.collect(Collectors.toList());
}
}

/**
* Retrieves a list of all pending organizations AND organization queue items
*
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/graphql/admin.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# which is enforced in the API not in the schema validator.
extend type Query {
organizations(identityVerified: Boolean): [Organization!]!
organizationsByName(name: String!): [Organization]
pendingOrganizations: [PendingOrganization!]!
organization(id: ID!): Organization
facilityStats(facilityId: ID!): FacilityStats
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gov.cdc.usds.simplereport.api.organization;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import gov.cdc.usds.simplereport.api.model.ApiOrganization;
import gov.cdc.usds.simplereport.api.model.ApiPendingOrganization;
import gov.cdc.usds.simplereport.api.model.accountrequest.OrganizationAccountRequest;
import gov.cdc.usds.simplereport.api.model.errors.IllegalGraphqlArgumentException;
Expand Down Expand Up @@ -75,4 +77,29 @@ void organization_null() {
verify(organizationService).getOrganizationById(id);
verify(organizationService, times(0)).getFacilities(org);
}

@Test
void organizationsByName_success() {
String orgName = "org name";
Organization org = new Organization(orgName, "type", "123", true);
when(organizationService.getOrganizationsByName(orgName)).thenReturn(List.of(org));

organizationMutationResolver.organizationsByName(orgName);

verify(organizationService).getOrganizationsByName(orgName);
verify(organizationService).getFacilities(org);
}

@Test
void organizationsByName_null() {
String orgName = "org name";
Organization org = new Organization(orgName, "type", "123", true);
when(organizationService.getOrganizationsByName(orgName)).thenReturn(List.of());

List<ApiOrganization> actual = organizationMutationResolver.organizationsByName(orgName);

assertThat(actual).isNull();
verify(organizationService).getOrganizationsByName(orgName);
verify(organizationService, never()).getFacilities(org);
}
}
7 changes: 7 additions & 0 deletions cypress/cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ module.exports = {
getMultiplexDeviceName() {
return global.multiplexDeviceName;
},
setSpecName(name) {
global.specName = name;
return name;
},
getSpecName() {
return global.specName || null;
},
});
on("before:browser:launch", (browser = {}, launchOptions = {}) => {
launchOptions.args = launchOptions.args.filter(
Expand Down
13 changes: 12 additions & 1 deletion cypress/e2e/02-add_patient.cy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { generatePatient, loginHooks } from "../support/e2e";
import { generatePatient, loginHooks, testNumber } from "../support/e2e";
import { cleanUpPreviousOrg, setupOrgAndFacility } from "../utils/setup-utils";

const patient = generatePatient();

describe("Adding a single patient", () => {
loginHooks();
before("store patient info", () => {
// TODO: clean up after no tests rely on this patient
cy.task("setPatientName", patient.fullName);
cy.task("setPatientDOB", patient.dobForPatientLink);
cy.task("setPatientPhone", patient.phone);
cy.task("getSpecName")
.then((prevSpecName) => {
if (prevSpecName) {
cleanUpPreviousOrg(prevSpecName);
}
let currentSpecName = `${testNumber()}-cypress-spec-2`
cy.task("setSpecName", currentSpecName)
setupOrgAndFacility(currentSpecName);
})
});
it("navigates to the add patient form", () => {
cy.visit("/");
Expand Down
27 changes: 12 additions & 15 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import "cypress-localstorage-commands";
import { authenticator } from "otplib";
import { graphqlURL } from "../utils/request-utils";
import { addOrgToQueueURL, graphqlURL } from "../utils/request-utils";

// read environment variables

Expand Down Expand Up @@ -95,20 +95,6 @@ Cypress.Commands.add("login", () => {
});
});

Cypress.Commands.add("selectFacility", () => {
cy.get("body").then(($body) => {
if (
$body
.text()
.includes(
"Please select the testing facility where you are working today.",
)
) {
cy.get(".usa-card__body").last().click();
}
});
});

Cypress.Commands.add("addDevice", (device) => {
cy.get('input[name="name"]').type(device.name);
cy.get('input[name="model"]').type(device.model);
Expand Down Expand Up @@ -178,6 +164,17 @@ Cypress.Commands.add("makePOSTRequest", (requestBody) => {
);
});

Cypress.Commands.add("makeAccountRequest", (requestBody) => {
cy.request({
method: "POST",
url: addOrgToQueueURL,
headers: {
"Content-Type": "application/json",
},
body: requestBody,
});
});

Cypress.Commands.add("injectSRAxe", () => {
return isLocalRun
? cy.injectAxe({
Expand Down
6 changes: 3 additions & 3 deletions cypress/utils/request-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const graphqlURL = `${
Cypress.env("BACKEND_URL") || "http://localhost:8080"
}/graphql`;
const backendURL = Cypress.env("BACKEND_URL") || "http://localhost:8080";
export const graphqlURL = `${backendURL}/graphql`;
export const addOrgToQueueURL = `${backendURL}/account-request/organization-add-to-queue`;
58 changes: 58 additions & 0 deletions cypress/utils/setup-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
accessOrganization,
addMockFacility,
createOrganization,
getOrganizationsByName, getPatientsByFacilityId,
markOrganizationAsDeleted, markPatientAsDeleted,
verifyPendingOrganization
} from "./testing-data-utils";
import { generateUser } from "../support/e2e";

const createOrgName = (specName) => {
return `${specName}-org`;
}

const createFacilityName = (specName) => {
return `${specName}-facility`;
}

const createAndVerifyOrganization = (orgName) => {
const adminUser = generateUser();
return createOrganization(orgName, adminUser.email)
.then((res) => verifyPendingOrganization(res.body.orgExternalId))
}
const archivePatientsForFacility = (facilityId) => {
return getPatientsByFacilityId(facilityId)
.then((res) => {
let patients = res.body.data.patients;
if (patients.length > 0) {
patients.map(
(patient) => markPatientAsDeleted(patient.internalId, true))
}
})
}

export const cleanUpPreviousOrg = (specName) => {
let orgName = createOrgName(specName);
getOrganizationsByName(orgName)
.then((res) => {
let orgs = res.body.data.organizationsByName;
let org = orgs.length > 0 ? orgs[0] : null;
if (org) {
let facilities = org.facilities
if (facilities.length > 0) {
facilities.map((facility) => archivePatientsForFacility(facility.id))
}
markOrganizationAsDeleted(org.id, true);
}
})
}

export const setupOrgAndFacility = (specName) => {
let orgName = createOrgName(specName);
let facilityName = createFacilityName(specName);
createAndVerifyOrganization(orgName)
.then(() => getOrganizationsByName(orgName))
.then((res) => accessOrganization(res.body.data.organizationsByName[0].externalId))
.then(() => addMockFacility(facilityName))
};
Loading

0 comments on commit b91b8cc

Please sign in to comment.