mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-31 04:02:26 +08:00
refactor(test): group login tests
This commit is contained in:
parent
8516df8f9b
commit
3cf35f9e0c
@ -1,9 +1,8 @@
|
|||||||
import { beforeAll, describe, expect, it } from "vitest";
|
import { beforeAll, describe, expect, it } from "vitest";
|
||||||
import supertest from "supertest";
|
import supertest, { type Response } from "supertest";
|
||||||
import type { Application } from "express";
|
import type { Application } from "express";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import type { SQLiteSessionStore } from "./session_parser.js";
|
import type { SQLiteSessionStore } from "./session_parser.js";
|
||||||
import { promisify } from "util";
|
|
||||||
import { SessionData } from "express-session";
|
import { SessionData } from "express-session";
|
||||||
|
|
||||||
let app: Application;
|
let app: Application;
|
||||||
@ -37,23 +36,27 @@ describe("Login Route test", () => {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("Login when 'Remember Me' is ticked", async () => {
|
||||||
it("sets correct Expires, when 'Remember Me' is ticked", async () => {
|
|
||||||
|
|
||||||
// TriliumNextTODO: make setting cookieMaxAge via env variable work
|
// TriliumNextTODO: make setting cookieMaxAge via env variable work
|
||||||
// => process.env.TRILIUM_SESSION_COOKIEMAXAGE
|
// => process.env.TRILIUM_SESSION_COOKIEMAXAGE
|
||||||
// the custom cookieMaxAge is currently hardocded in the test data dir's config.ini
|
// the custom cookieMaxAge is currently hardocded in the test data dir's config.ini
|
||||||
|
|
||||||
const CUSTOM_MAX_AGE_SECONDS = 86400;
|
let res: Response;
|
||||||
const expectedExpiresDate = dayjs().utc().add(CUSTOM_MAX_AGE_SECONDS, "seconds").toDate().toUTCString();
|
let setCookieHeader: string;
|
||||||
|
let expectedExpiresDate: string;
|
||||||
|
|
||||||
const res = await supertest(app)
|
beforeAll(async () => {
|
||||||
|
const CUSTOM_MAX_AGE_SECONDS = 86400;
|
||||||
|
|
||||||
|
expectedExpiresDate = dayjs().utc().add(CUSTOM_MAX_AGE_SECONDS, "seconds").toDate().toUTCString();
|
||||||
|
res = await supertest(app)
|
||||||
.post("/login")
|
.post("/login")
|
||||||
.send({ password: "demo1234", rememberMe: 1 })
|
.send({ password: "demo1234", rememberMe: 1 })
|
||||||
.expect(302)
|
.expect(302);
|
||||||
|
setCookieHeader = res.headers["set-cookie"][0];
|
||||||
const setCookieHeader = res.headers["set-cookie"][0];
|
});
|
||||||
|
|
||||||
|
it("sets correct Expires for the cookie", async () => {
|
||||||
// match for e.g. "Expires=Wed, 07 May 2025 07:02:59 GMT;"
|
// match for e.g. "Expires=Wed, 07 May 2025 07:02:59 GMT;"
|
||||||
const expiresCookieRegExp = /Expires=(?<date>[\w\s,:]+)/;
|
const expiresCookieRegExp = /Expires=(?<date>[\w\s,:]+)/;
|
||||||
const expiresCookieMatch = setCookieHeader.match(expiresCookieRegExp);
|
const expiresCookieMatch = setCookieHeader.match(expiresCookieRegExp);
|
||||||
@ -64,7 +67,9 @@ describe("Login Route test", () => {
|
|||||||
// ignore the seconds in the comparison, just to avoid flakiness in tests,
|
// ignore the seconds in the comparison, just to avoid flakiness in tests,
|
||||||
// if for some reason execution is slow between calculation of expected and actual
|
// if for some reason execution is slow between calculation of expected and actual
|
||||||
expect(actualExpiresDate.slice(0,23)).toBe(expectedExpiresDate.slice(0,23))
|
expect(actualExpiresDate.slice(0,23)).toBe(expectedExpiresDate.slice(0,23))
|
||||||
|
});
|
||||||
|
|
||||||
|
it("sets the correct sesssion data", async () => {
|
||||||
// Check the session is stored in the database.
|
// Check the session is stored in the database.
|
||||||
const { session, expiry } = await getSessionFromCookie(setCookieHeader);
|
const { session, expiry } = await getSessionFromCookie(setCookieHeader);
|
||||||
expect(session!).toBeTruthy();
|
expect(session!).toBeTruthy();
|
||||||
@ -73,24 +78,28 @@ describe("Login Route test", () => {
|
|||||||
.toBe(expectedExpiresDate.substring(0, 23));
|
.toBe(expectedExpiresDate.substring(0, 23));
|
||||||
expect(session!.loggedIn).toBe(true);
|
expect(session!.loggedIn).toBe(true);
|
||||||
expect(expiry).toStrictEqual(new Date(session!.cookie.expires!));
|
expect(expiry).toStrictEqual(new Date(session!.cookie.expires!));
|
||||||
}, 10_000);
|
});
|
||||||
// use 10 sec (10_000 ms) timeout for now, instead of default 5 sec to work around
|
});
|
||||||
// failing CI, because for some reason it currently takes approx. 6 secs to run
|
|
||||||
// TODO: actually identify what is causing this and fix the flakiness
|
|
||||||
|
|
||||||
|
describe("Login when 'Remember Me' is not ticked", async () => {
|
||||||
|
let res: Response;
|
||||||
|
let setCookieHeader: string;
|
||||||
|
|
||||||
it("does not set Expires, when 'Remember Me' is not ticked", async () => {
|
beforeAll(async () => {
|
||||||
const res = await supertest(app)
|
res = await supertest(app)
|
||||||
.post("/login")
|
.post("/login")
|
||||||
.send({ password: "demo1234" })
|
.send({ password: "demo1234" })
|
||||||
.expect(302)
|
.expect(302)
|
||||||
|
|
||||||
const setCookieHeader = res.headers["set-cookie"][0];
|
setCookieHeader = res.headers["set-cookie"][0];
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not set Expires", async () => {
|
||||||
// match for e.g. "Expires=Wed, 07 May 2025 07:02:59 GMT;"
|
// match for e.g. "Expires=Wed, 07 May 2025 07:02:59 GMT;"
|
||||||
expect(setCookieHeader).not.toMatch(/Expires=(?<date>[\w\s,:]+)/)
|
expect(setCookieHeader).not.toMatch(/Expires=(?<date>[\w\s,:]+)/)
|
||||||
|
});
|
||||||
|
|
||||||
// Check the session is stored in the database.
|
it("stores the session in the database", async () => {
|
||||||
const { session, expiry } = await getSessionFromCookie(setCookieHeader);
|
const { session, expiry } = await getSessionFromCookie(setCookieHeader);
|
||||||
expect(session!).toBeTruthy();
|
expect(session!).toBeTruthy();
|
||||||
expect(session!.cookie.expires).toBeUndefined();
|
expect(session!.cookie.expires).toBeUndefined();
|
||||||
@ -99,11 +108,8 @@ describe("Login Route test", () => {
|
|||||||
const expectedExpirationDate = dayjs().utc().add(1, "hour").toDate();
|
const expectedExpirationDate = dayjs().utc().add(1, "hour").toDate();
|
||||||
expect(expiry?.getTime()).toBeGreaterThan(new Date().getTime());
|
expect(expiry?.getTime()).toBeGreaterThan(new Date().getTime());
|
||||||
expect(expiry?.getTime()).toBeLessThan(expectedExpirationDate.getTime());
|
expect(expiry?.getTime()).toBeLessThan(expectedExpirationDate.getTime());
|
||||||
}, 10_000);
|
});
|
||||||
// use 10 sec (10_000 ms) timeout for now, instead of default 5 sec to work around
|
});
|
||||||
// failing CI, because for some reason it currently takes approx. 6 secs to run
|
|
||||||
// TODO: actually identify what is causing this and fix the flakiness
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
async function getSessionFromCookie(setCookieHeader: string) {
|
async function getSessionFromCookie(setCookieHeader: string) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user