full login automated testing still not working - will come back to this and probably run in docker

This commit is contained in:
Khalim Conn-Kowlessar 2023-07-11 15:00:19 +01:00
parent 0f8d63f588
commit e0b0fa993e
3 changed files with 85 additions and 3 deletions

View file

@ -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!");
}
});
});

View file

@ -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"
}

View file

@ -34,4 +34,45 @@
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
// }
// }
// }
// }
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<string> {
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);
});
});