Skip to content

Commit

Permalink
Add cypress logout command
Browse files Browse the repository at this point in the history
  • Loading branch information
smartspot2 committed Jul 13, 2024
1 parent 3925c50 commit 51a226d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
39 changes: 39 additions & 0 deletions cypress/e2e/login.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,43 @@ describe("login", () => {
cy.visit("/");
cy.get("h3.page-title").should("contain", "My courses");
});

it("should be able to log out after login", () => {
cy.setupDB("login", "setup");

cy.login();

// log out the current user
cy.logout();

// when visiting the home page now, it should be redirected to a login form
cy.visit("/");
cy.get("#login-btn").should("be.visible");
});

it("should be able to log out and redirect after login", () => {
cy.setupDB("login", "setup");

cy.login();

// log out the current user
cy.logout_redirect();

// should be redirected to a login form
cy.get("#login-btn").should("be.visible");
});

it("should be able to click the log out button to log out", () => {
cy.setupDB("login", "setup");

cy.login();

cy.visit("/");

// log out by clicking the log out button
cy.get("#logout-btn").should("be.visible").click();

// should redirect to the login screen
cy.get("#login-btn").should("be.visible");
});
});
4 changes: 2 additions & 2 deletions cypress/e2e/section/mentor-student-interaction.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("word of the day", () => {
cy.contains(".primary-btn", /update/i).should("be.disabled"); // disabled because unchanged

// logout
cy.visit("/logout/", { method: "POST" });
cy.logout();

// log in as student next
cy.login({ username: "demo_student", password: "pass" });
Expand Down Expand Up @@ -89,7 +89,7 @@ describe("word of the day", () => {
});

// logout
cy.visit("/logout/", { method: "POST" });
cy.logout();

// log in as mentor again
cy.login({ username: "demo_mentor", password: "pass" });
Expand Down
26 changes: 26 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ Cypress.Commands.add("login", (loginInfo: LoginInfo = { username: "demo_user", p
});
});

/**
* Headless logout
*/
Cypress.Commands.add("logout", () => {
cy.getCookie("csrftoken").then(csrfCookie => {
return cy.request({
method: "POST",
url: "/logout/",
form: true,
body: { csrfmiddlewaretoken: csrfCookie.value }
});
});
});

/**
* Logout with redirect
*/
Cypress.Commands.add("logout_redirect", () => {
cy.getCookie("csrftoken").then(csrfCookie => {
cy.visit("/logout/", {
method: "POST",
body: { csrfmiddlewaretoken: csrfCookie.value }
});
});
});

interface SetupDBOptions {
force?: boolean;
mutate?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions cypress/support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface SetupDBOptions {
declare namespace Cypress {
interface Chainable {
login(loginInfo?: LoginInfo): Chainable<void>;
logout(): Chainable<void>;
logout_redirect(): Chainable<void>;
setupDB(script_name: string, func_name: string, options?: SetupDBOptions): Chainable<void>;
initDB(): Chainable<void>;
_exec(command: string): Chainable<void>;
Expand Down

0 comments on commit 51a226d

Please sign in to comment.