Skip to content

Commit

Permalink
OCLOMRS-791: Automated Test: New organization creation form (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnasambu committed Jul 22, 2021
1 parent c6cbeac commit be1d242
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 3 deletions.
1 change: 1 addition & 0 deletions cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"baseUrl": "http://localhost:8080",
"defaultCommandTimeout": 8000,
"requestTimeout": 6000,
"responseTimeout": 90000,
"retries": {
"runMode": 1,
"openMode": 0
Expand Down
24 changes: 24 additions & 0 deletions cypress/integration/organisation/create.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Feature: Creating an organisation
Background:
Given the user is logged in

Scenario: The user should be able to click the button to create a new organisation
Given the user is on the organisations page
When the user clicks on the create new organisation button
Then the user should be on the create new organisation page

@organisation
Scenario: The user should be able to create a new organisation
Given the user is on the create new organisation page
When the user enters the organisation information
And the user submits the form
Then the new organisation should be created

@organisation
Scenario: The user should be able to create a public organisation
Given the user is on the create new organisation page
When the user enters the organisation information
And the user selects "View" view
And the user submits the form
Then the new organisation should be created
And the organisation should be publicly visible
73 changes: 73 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,76 @@ Cypress.Commands.add(
});
}
);

Cypress.Commands.add(
"createOrganisation",
(
organisation: string = `Org-${nanoid()}`,
public_access: boolean = false
) => {
getAuthToken().then(authToken =>
cy
.request({
method: "GET",
headers: {
Authorization: authToken
},
url: `${apiUrl}/orgs/${organisation}/`,
failOnStatusCode: false
})
.then(response => {
if (response.status !== 200) {
cy.request({
method: "POST",
headers: {
Authorization: authToken
},
url: `${apiUrl}/orgs/`,
body: {
id: organisation,
custom_validation_schema: "OpenMRS",
name: "Test Organisation",
company: "",
website: "",
location: "",
public_access: public_access ? "View" : "None",
orgs_type: "organisation"
}
});
}
})
);

return cy.wrap(organisation);
}
);

Cypress.Commands.add(
"deleteOrganisation",
(organisation: string, isCleanup: boolean = false) => {
getAuthToken().then(authToken =>
cy.request({
method: "DELETE",
headers: {
Authorization: authToken
},
url: `${apiUrl}/orgs/${organisation}/`,
failOnStatusCode: !!!isCleanup
})
);
}
);

Cypress.Commands.add("getOrganisation", (organisation: string) => {
return getAuthToken().then(authToken => {
return cy
.request({
method: "GET",
headers: {
Authorization: authToken
},
url: `${apiUrl}/orgs/${organisation}/`
})
.its("body");
});
});
12 changes: 12 additions & 0 deletions cypress/support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,17 @@ declare namespace Cypress {
isCleanup?: boolean
): Chainable<Subject>;
getSource(source: string, username?: string): Chainable<Subject>;
createOrganisation(
organisation?: string,
public_access?: boolean
): Chainable<Subject>;
deleteOrganisation(
organisation: string,
isCleanup?: boolean
): Chainable<Subject>;
getOrganisation(
organisation: string,
username?: string
): Chainable<Subject>;
}
}
23 changes: 23 additions & 0 deletions cypress/support/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
// ***********************************************************
// This example support/index.ts is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.ts using ES2015 syntax:
import "./commands";

Cypress.on("uncaught:exception", err => {
console.debug("Uncaught exception", err);
// returning false here prevents Cypress from
// failing the test
return false;
});
81 changes: 81 additions & 0 deletions cypress/support/step_definitions/organisation/create/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/// <reference types="cypress" />
/// <reference types="../../../" />
import {
After,
Before,
Given,
Then,
When
} from "cypress-cucumber-preprocessor/steps";
import { customAlphabet } from "nanoid";
import { getOrganisationId, isLoggedIn } from "../../../utils";

let organisationID = "";
const nanoid = customAlphabet("abcdefghijklmnopqrstuvwxyz", 4);

Given("the user is on the organisations page", () => cy.visit("/orgs/"));

Given("the user is on the create new organisation page", () =>
cy.visit("/orgs/new/")
);

When("the user clicks on the create new organisation button", () =>
cy.findByTitle(/Create new organisation/i).click()
);

When("the user enters the organisation information", () => {
cy.findByLabelText(/Organisation ID/i).type(organisationID);
cy.findByLabelText(/Organisation Name/i).type(organisationID);
cy.get("#public_access").type("{enter}");
});

When(/the user selects "(.+)" view/, public_access => {
switch (public_access) {
case "View":
cy.get("#public_access").type("{downarrow}{uparrow}{enter}");
break;
case "None":
cy.get("#public_access").type("{downarrow}{downarrow}{enter}");
break;
default:
}
});

When("the user submits the form", () => {
cy.get("form").submit();
cy.url().should("contain", `/orgs/${organisationID}/`);
});

Then("the user should be on the create new organisation page", () =>
cy.url().should("contain", "/orgs/new/")
);

Then("the new organisation should be created", () =>
cy.getOrganisation(organisationID).should("exist")
);

Then("the organisation should be publicly visible", () =>
cy
.getOrganisation(organisationID)
.its("public_access")
.should("eq", "View")
);

Then("the organisation should be viewed publicly accessed", () =>
cy
.getOrganisation(organisationID)
.its("public_access")
.should("eq", "None")
);

Before({ tags: "@organisation" }, () => {
organisationID = `Org-${nanoid()}`;
});

After({ tags: "@organisation" }, () => {
isLoggedIn().then(loggedIn => {
if (loggedIn) {
cy.deleteOrganisation(organisationID, true);
}
});
});
10 changes: 7 additions & 3 deletions cypress/support/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ export const getAuthToken = () =>
.its("auth")
.its("token")
.then(token => `Token ${token}`);
export const getUser = () => Cypress.env("USERNAME") || "ocladmin";
export const getPassword = () => Cypress.env("PASSWORD") || "Root123";
export const getDictionaryId = () => Cypress.env("dictionaryId");
export const setDictionaryId = (dictionaryId: string) => Cypress.env("dictionaryId", dictionaryId);
export const setDictionaryId = (dictionaryId: string) =>
Cypress.env("dictionaryId", dictionaryId);
export const getConceptId = () => Cypress.env("conceptId");
export const setConceptId = (conceptId: string) => Cypress.env("conceptId", conceptId);
export const getUser = () => Cypress.env("USERNAME") || "ocladmin";
export const getPassword = () => Cypress.env("PASSWORD") || "Root123";
export const getOrganisationId = () => Cypress.env("organisationId");
export const setOrganisationId = (organisationId: string) =>
Cypress.env("organisationId", organisationId);

0 comments on commit be1d242

Please sign in to comment.