diff --git a/cypress/e2e/login/login.cy.js b/cypress/e2e/login/login.cy.js index 35d7289..a009bbf 100644 --- a/cypress/e2e/login/login.cy.js +++ b/cypress/e2e/login/login.cy.js @@ -4,22 +4,10 @@ const USER = { describe("Login page", () => { before(() => { - // Intercept the api/auth/session endpoint and provide a mock response - cy.intercept("/api/auth/session", { - statusCode: 200, - body: { - user: { - name: "Test User", - email: Cypress.env("GOOGLE_USER"), - image: "https://example.com/profile.jpg", - }, - expires: "2055-08-12T15:00:00.000Z", - }, - }); - - cy.log(`Visiting https://company.tld`); + cy.log(`Visiting base url at localhost`); cy.visit("/"); }); + it("Login with Google", () => { const username = Cypress.env("GOOGLE_USER"); const password = Cypress.env("GOOGLE_PW"); @@ -32,52 +20,39 @@ describe("Login page", () => { headless: true, logs: true, isPopup: true, - loginSelector: `[data-testid="google-signin-btn"]`, // Look for the data-testid attribute of the login button - postLoginSelector: `[data-testid="app-navbar"]`, // Look for the navbar to indicate a successful login + loginSelector: `[data-testid="google-signin-btn"]`, + postLoginSelector: `[data-testid="app-navbar"]`, }; - return cy - .task("GoogleSocialLogin", socialLoginOptions) - .then(({ cookies }) => { - cy.log("Logging in with Google"); - cy.login(USER); - }) - .then(({ cookies }) => { - cy.log("Logging in with cookies"); + // Call cy.login() before cy.task() + cy.login(USER); + + return cy.task("GoogleSocialLogin", socialLoginOptions).then(() => { + cy.log("Logging in with cookies"); + + // Check cookies with cy.getCookies() command after login and task are completed + cy.getCookies().then((cookies) => { cy.log(cookies); - cy.log(cookieName); - // cy.clearCookies(); - const cookie = cookies - .filter((cookie) => cookie.name === cookieName) - .pop(); + const cookie = cookies.find((cookie) => cookie.name === cookieName); + cy.log("Cookie found!"); - cy.log(cookies); - if (cookie) { - cy.setCookie(cookie.name, cookie.value, { - domain: cookie.domain, - expiry: cookie.expires, - httpOnly: cookie.httpOnly, - path: cookie.path, - secure: cookie.secure, - }); - - Cypress.Cookies.defaults({ - preserve: cookieName, - }); - - // After logging in, verify that we have navigated to the /home page. - cy.log("Checking if we are on the /home page"); - cy.url().should("include", "/home"); - cy.log("We are on the /home page!"); - - // remove the two lines below if you need to stay logged in - // for your remaining tests - cy.visit("/api/auth/signout"); - cy.get("form").submit(); - } else { + if (cookie.length === 0) { cy.log("Cookie not found!"); + throw new Error(`Cookie '${cookieName}' not found!`); } + + cy.visit("/home"); + // After logging in, verify that we have navigated to the /home page. + cy.log("Checking if we are on the /home page"); + cy.url().should("include", "/home"); + cy.log("We are on the /home page!"); + + // remove the two lines below if you need to stay logged in + // for your remaining tests + cy.visit("/api/auth/signout"); + cy.get("form").submit(); }); + }); }); }); diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index 9bf3f54..ed856a0 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -68,6 +68,7 @@ export async function encode( // @ts-ignore Cypress.Commands.add("login", (userObj: JWTPayload) => { // Generate and set a valid cookie from the fixture that next-auth can decrypt + cy.intercept("/api/auth/session", { fixture: "session.json" }).as("session"); cy.wrap(null) .then(() => { return encode(userObj, Cypress.env("NEXTAUTH_JWT_SECRET"));