Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests to verify error handling and access restrictions during Facility Creation #9119

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cypress/e2e/facility_spec/FacilityCreation.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,39 @@ describe("Facility Creation", () => {
);
});

it("Should display error when district admin tries to create facility in a different district", () => {
facilityPage.visitCreateFacilityPage();
facilityPage.fillFacilityName(facilityName);
facilityPage.selectFacilityType(facilityType);
facilityPage.fillPincode("682001");
facilityPage.selectStateOnPincode("Kerala");
facilityPage.selectDistrictOnPincode("Kottayam");
facilityPage.selectLocalBody("Arpookara");
facilityPage.selectWard("5");
facilityPage.fillAddress(facilityAddress);
facilityPage.fillPhoneNumber(facilityNumber);
facilityPage.submitForm();
facilityPage.verifyErrorNotification(
"You do not have permission to perform this action.",
);
});
Comment on lines +320 to +335
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance test reliability with API verification and data management

The test case needs improvements to meet the PR objectives and enhance reliability:

  1. Add API request verification
  2. Add test data cleanup
  3. Move test data to fixtures

Apply these improvements:

 it("Should display error when district admin tries to create facility in a different district", () => {
+  cy.intercept('POST', '**/api/v1/facility/').as('createFacility');
+
+  // Load test data from fixture
+  cy.fixture('facility_data').then((data) => {
+    const { facilityName, facilityType, differentDistrict } = data;
+
     facilityPage.visitCreateFacilityPage();
     facilityPage.fillFacilityName(facilityName);
     facilityPage.selectFacilityType(facilityType);
     facilityPage.fillPincode("682001");
     facilityPage.selectStateOnPincode("Kerala");
-    facilityPage.selectDistrictOnPincode("Kottayam");
-    facilityPage.selectLocalBody("Arpookara");
+    facilityPage.selectDistrictOnPincode(differentDistrict.name);
+    facilityPage.selectLocalBody(differentDistrict.localBody);
     facilityPage.selectWard("5");
     facilityPage.fillAddress(facilityAddress);
     facilityPage.fillPhoneNumber(facilityNumber);
     facilityPage.submitForm();
+
+    // Verify API response
+    cy.wait('@createFacility').then((interception) => {
+      expect(interception.response.statusCode).to.equal(403);
+      expect(interception.response.body).to.have.property('detail', 
+        'You do not have permission to perform this action.');
+    });
+
     facilityPage.verifyErrorNotification(
       "You do not have permission to perform this action.",
     );
+
+    // Cleanup
+    cy.clearCookies();
+    cy.clearLocalStorage();
   });
 });

Committable suggestion skipped: line range outside the PR's diff.


it("Access Restriction for Non-Admin Users to facility creation page", () => {
const nonAdminLoginMethods = [
loginPage.loginAsDevDoctor.bind(loginPage),
loginPage.loginAsStaff.bind(loginPage),
];

nonAdminLoginMethods.forEach((loginMethod) => {
loginMethod();
cy.visit("/facility/create");
facilityPage.verifyErrorNotification(
"You don't have permission to perform this action. Contact the admin",
);
cy.clearCookies();
});
});
Comment on lines +337 to +351
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve test structure and API verification

While the test case handles session cleanup well, it needs improvements in several areas:

  1. Add API request verification
  2. Decouple login methods
  3. Verify HTTP status code
  4. Align error message with PR objectives

Apply these improvements:

 it("Access Restriction for Non-Admin Users to facility creation page", () => {
+  cy.intercept('GET', '**/api/v1/facility/create/').as('accessFacility');
+
-  const nonAdminLoginMethods = [
-    loginPage.loginAsDevDoctor.bind(loginPage),
-    loginPage.loginAsStaff.bind(loginPage),
+  // Load test users from fixture
+  cy.fixture('users').then((data) => {
+    const { nonAdminUsers } = data;
+
+    nonAdminUsers.forEach(({ username, password }) => {
+      loginPage.login(username, password);
+      cy.visit("/facility/create");
+
+      // Verify API response
+      cy.wait('@accessFacility').then((interception) => {
+        expect(interception.response.statusCode).to.equal(403);
+      });
+
+      facilityPage.verifyErrorNotification(
+        "You don't have permission to perform this action. Contact the admin"
+      );
+
+      cy.clearCookies();
+    });
   });
-
-  nonAdminLoginMethods.forEach((loginMethod) => {
-    loginMethod();
-    cy.visit("/facility/create");
-    facilityPage.verifyErrorNotification(
-      "You don't have permission to perform this action. Contact the admin",
-    );
-    cy.clearCookies();
-  });
 });

Create a fixture file cypress/fixtures/users.json:

{
  "nonAdminUsers": [
    {
      "username": "dummynurse1",
      "password": "Coronasafe@123",
      "role": "Nurse"
    },
    {
      "username": "devdoctor",
      "password": "Coronasafe@123",
      "role": "Doctor"
    }
  ]
}


afterEach(() => {
cy.saveLocalStorage();
});
Expand Down
4 changes: 4 additions & 0 deletions cypress/pageobject/Facility/FacilityCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ class FacilityPage {
cy.verifyNotification(message);
}

verifyErrorNotification(message: string) {
cy.verifyNotification(message);
}

visitAlreadyCreatedFacility() {
cy.intercept("GET", "**/api/v1/facility/**").as("getFacilities");
cy.get("[id='facility-details']").first().click();
Expand Down
Loading