Skip to content

Commit

Permalink
logoutByApi
Browse files Browse the repository at this point in the history
  • Loading branch information
unlikelyzero committed Nov 3, 2024
1 parent 0bd0091 commit 2c0fc53
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cypress/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ declare namespace Cypress {
*/
loginByApi(username: string, password?: string): Chainable<Response>;

/**
* Logs-out user by using API request
*/
logoutByApi(): Chainable<Response>;

/**
* Logs-in user by using Google API request
*/
Expand Down
7 changes: 7 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
14 changes: 14 additions & 0 deletions cypress/tests/api/api-users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
});
});
});

0 comments on commit 2c0fc53

Please sign in to comment.