From e0b0fa993ed5f7744720b4c1959db624cc3bd6c0 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 11 Jul 2023 15:00:19 +0100 Subject: [PATCH] full login automated testing still not working - will come back to this and probably run in docker --- cypress/e2e/login/login.cy.js | 37 ++++++++++++++++++++++++++++-- cypress/fixtures/session.json | 8 +++++++ cypress/support/commands.ts | 43 ++++++++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 cypress/fixtures/session.json diff --git a/cypress/e2e/login/login.cy.js b/cypress/e2e/login/login.cy.js index 97eb99ca..35d72892 100644 --- a/cypress/e2e/login/login.cy.js +++ b/cypress/e2e/login/login.cy.js @@ -1,5 +1,22 @@ +const USER = { + name: "John", +}; + 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.visit("/"); }); @@ -13,7 +30,7 @@ describe("Login page", () => { password, loginUrl, headless: true, - logs: false, + 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 @@ -22,11 +39,20 @@ describe("Login page", () => { return cy .task("GoogleSocialLogin", socialLoginOptions) .then(({ cookies }) => { - cy.clearCookies(); + cy.log("Logging in with Google"); + cy.login(USER); + }) + .then(({ cookies }) => { + cy.log("Logging in with cookies"); + cy.log(cookies); + cy.log(cookieName); + // cy.clearCookies(); const cookie = cookies .filter((cookie) => cookie.name === cookieName) .pop(); + + cy.log(cookies); if (cookie) { cy.setCookie(cookie.name, cookie.value, { domain: cookie.domain, @@ -40,10 +66,17 @@ describe("Login page", () => { 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 { + cy.log("Cookie not found!"); } }); }); diff --git a/cypress/fixtures/session.json b/cypress/fixtures/session.json new file mode 100644 index 00000000..b1a4a827 --- /dev/null +++ b/cypress/fixtures/session.json @@ -0,0 +1,8 @@ +{ + "user": { + "name": "Test User", + "email": "assessmentmodeltester123@gmail.com", + "image": "https://example.com/profile.jpg" + }, + "expires": "2055-08-12T15:00:00.000Z" +} diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index 698b01a4..9bf3f54d 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -34,4 +34,45 @@ // visit(originalFn: CommandOriginalFn, url: string, options: Partial): Chainable // } // } -// } \ No newline at end of file +// } + +import hkdf from "@panva/hkdf"; +import { EncryptJWT, JWTPayload } from "jose"; + +// Function logic derived from https://github.com/nextauthjs/next-auth/blob/5c1826a8d1f8d8c2d26959d12375704b0a693bfc/packages/next-auth/src/jwt/index.ts#L113-L121 +async function getDerivedEncryptionKey(secret: string) { + return await hkdf( + "sha256", + secret, + "", + "NextAuth.js Generated Encryption Key", + 32 + ); +} + +// Function logic derived from https://github.com/nextauthjs/next-auth/blob/5c1826a8d1f8d8c2d26959d12375704b0a693bfc/packages/next-auth/src/jwt/index.ts#L16-L25 +export async function encode( + token: JWTPayload, + secret: string +): Promise { + const maxAge = 30 * 24 * 60 * 60; // 30 days + const encryptionSecret = await getDerivedEncryptionKey(secret); + return await new EncryptJWT(token) + .setProtectedHeader({ alg: "dir", enc: "A256GCM" }) + .setIssuedAt() + .setExpirationTime(Math.round(Date.now() / 1000 + maxAge)) + .setJti("test") + .encrypt(encryptionSecret); +} + +// @ts-ignore +Cypress.Commands.add("login", (userObj: JWTPayload) => { + // Generate and set a valid cookie from the fixture that next-auth can decrypt + cy.wrap(null) + .then(() => { + return encode(userObj, Cypress.env("NEXTAUTH_JWT_SECRET")); + }) + .then((encryptedToken) => { + cy.setCookie("next-auth.session-token", encryptedToken); + }); +});