diff --git a/cypress/e2e/login.cy.ts b/cypress/e2e/login.cy.ts index c8d34d12..b7fd7ae0 100644 --- a/cypress/e2e/login.cy.ts +++ b/cypress/e2e/login.cy.ts @@ -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"); + }); }); diff --git a/cypress/e2e/section/mentor-student-interaction.cy.ts b/cypress/e2e/section/mentor-student-interaction.cy.ts index e089e411..f807a821 100644 --- a/cypress/e2e/section/mentor-student-interaction.cy.ts +++ b/cypress/e2e/section/mentor-student-interaction.cy.ts @@ -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" }); @@ -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" }); diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index 4b092e50..6db3288c 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -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; diff --git a/cypress/support/index.d.ts b/cypress/support/index.d.ts index 814d9a27..df71577d 100644 --- a/cypress/support/index.d.ts +++ b/cypress/support/index.d.ts @@ -11,6 +11,8 @@ interface SetupDBOptions { declare namespace Cypress { interface Chainable { login(loginInfo?: LoginInfo): Chainable; + logout(): Chainable; + logout_redirect(): Chainable; setupDB(script_name: string, func_name: string, options?: SetupDBOptions): Chainable; initDB(): Chainable; _exec(command: string): Chainable;