From 2c0fc53063293f4fc7c81d356a0da23d3d9bb2f4 Mon Sep 17 00:00:00 2001 From: John Hill Date: Sun, 3 Nov 2024 08:56:00 -0800 Subject: [PATCH] logoutByApi --- cypress/global.d.ts | 5 +++++ cypress/support/commands.ts | 7 +++++++ cypress/tests/api/api-users.spec.ts | 14 ++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/cypress/global.d.ts b/cypress/global.d.ts index 56c4721eb..56dd252e2 100644 --- a/cypress/global.d.ts +++ b/cypress/global.d.ts @@ -105,6 +105,11 @@ declare namespace Cypress { */ loginByApi(username: string, password?: string): Chainable; + /** + * Logs-out user by using API request + */ + logoutByApi(): Chainable; + /** * Logs-in user by using Google API request */ diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index c206e87f8..f35c89203 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -92,6 +92,13 @@ Cypress.Commands.add("loginByApi", (username, password = Cypress.env("defaultPas }); }); +Cypress.Commands.add("logoutByApi", () => { + return cy.request("POST", `${Cypress.env("apiUrl")}/logout`).then((response) => { + expect(response.status).to.eq(200); + expect(response.body).to.have.property("message", "User logged out successfully"); + }); +}); + Cypress.Commands.add("reactComponent", { prevSubject: "element" }, ($el) => { if ($el.length !== 1) { throw new Error(`cy.component() requires element of length 1 but got ${$el.length}`); diff --git a/cypress/tests/api/api-users.spec.ts b/cypress/tests/api/api-users.spec.ts index caa26df07..62e9a2ed0 100644 --- a/cypress/tests/api/api-users.spec.ts +++ b/cypress/tests/api/api-users.spec.ts @@ -202,4 +202,18 @@ describe("Users API", function () { }); }); }); + + context("POST /logout", function () { + it("logs out the authenticated user", function () { + // First, log in the user + cy.loginByApi(ctx.authenticatedUser!.username).then(() => { + // Then, log out the user using the custom command + cy.logoutByApi().then((response) => { + // Keep the assertions for status code and message + expect(response.status).to.eq(200); + expect(response.body).to.have.property("message", "User logged out successfully"); + }); + }); + }); + }); });