Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 2e8ab7e

Browse files
authored
Merge pull request #1711 from TriliumNext/test_add-login-route-tests
test(routes/login): add initial tests via supertest
2 parents cb80d62 + a149b56 commit 2e8ab7e

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

integration-tests/db/config.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ keyPath=
2727
# once set, expressjs will use the X-Forwarded-For header set by the rev. proxy to determinate the real IPs of clients.
2828
# expressjs shortcuts are supported: loopback(127.0.0.1/8, ::1/128), linklocal(169.254.0.0/16, fe80::/10), uniquelocal(10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fc00::/7)
2929
trustedReverseProxy=false
30+
31+
[Session]
32+
# Default value is 1814400 Seconds, which is 21 Days.
33+
# For integration test we set the value to just 86400 seconds, which is 1 Day
34+
cookieMaxAge=86400

src/routes/login.spec.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { beforeAll, describe, expect, it } from "vitest";
2+
import supertest from "supertest";
3+
import { initializeTranslations } from "../services/i18n.js";
4+
import type { Application, Request, Response, NextFunction } from "express";
5+
import dayjs from "dayjs";
6+
7+
let app: Application;
8+
9+
describe("Login Route test", () => {
10+
11+
beforeAll(async () => {
12+
initializeTranslations();
13+
app = (await import("../app.js")).default;
14+
});
15+
16+
it("should return the login page, when using a GET request", async () => {
17+
18+
// RegExp for login page specific string in HTML: e.g. "assets/v0.92.7/app/login.css"
19+
const loginCssRegexp = /assets\/v[0-9.a-z]+\/app\/login\.css/;
20+
21+
const res = await supertest(app)
22+
.get("/login")
23+
.expect(200)
24+
25+
26+
expect(loginCssRegexp.test(res.text)).toBe(true);
27+
28+
});
29+
30+
it("returns a 401 status, when login fails with wrong password", async () => {
31+
32+
await supertest(app)
33+
.post("/login")
34+
.send({ password: "fakePassword" })
35+
.expect(401)
36+
37+
});
38+
39+
40+
it("sets correct Expires, when 'Remember Me' is ticked", async () => {
41+
42+
// TriliumNextTODO: make setting cookieMaxAge via env variable work
43+
// => process.env.TRILIUM_SESSION_COOKIEMAXAGE
44+
// the custom cookieMaxAge is currently hardocded in the test data dir's config.ini
45+
46+
const CUSTOM_MAX_AGE_SECONDS = 86400;
47+
const expectedExpiresDate = dayjs().utc().add(CUSTOM_MAX_AGE_SECONDS, "seconds").toDate().toUTCString();
48+
49+
const res = await supertest(app)
50+
.post("/login")
51+
.send({ password: "demo1234", rememberMe: 1 })
52+
.expect(302)
53+
54+
const setCookieHeader = res.headers["set-cookie"][0];
55+
56+
// match for e.g. "Expires=Wed, 07 May 2025 07:02:59 GMT;"
57+
const expiresCookieRegExp = /Expires=(?<date>[\w\s,:]+)/;
58+
const expiresCookieMatch = setCookieHeader.match(expiresCookieRegExp);
59+
const actualExpiresDate = new Date(expiresCookieMatch?.groups?.date || "").toUTCString()
60+
61+
expect(actualExpiresDate).to.not.eql("Invalid Date");
62+
63+
// ignore the seconds in the comparison, just to avoid flakiness in tests,
64+
// if for some reason execution is slow between calculation of expected and actual
65+
expect(actualExpiresDate.slice(0,23)).toBe(expectedExpiresDate.slice(0,23))
66+
67+
}, 10_000);
68+
// use 10 sec (10_000 ms) timeout for now, instead of default 5 sec to work around
69+
// failing CI, because for some reason it currently takes approx. 6 secs to run
70+
// TODO: actually identify what is causing this and fix the flakiness
71+
72+
73+
it("does not set Expires, when 'Remember Me' is not ticked", async () => {
74+
75+
const res = await supertest(app)
76+
.post("/login")
77+
.send({ password: "demo1234" })
78+
.expect(302)
79+
80+
const setCookieHeader = res.headers["set-cookie"][0];
81+
82+
// match for e.g. "Expires=Wed, 07 May 2025 07:02:59 GMT;"
83+
const expiresCookieRegExp = /Expires=(?<date>[\w\s,:]+)/;
84+
const expiresCookieMatch = setCookieHeader.match(expiresCookieRegExp);
85+
expect(expiresCookieMatch).toBeNull();
86+
87+
}, 10_000);
88+
// use 10 sec (10_000 ms) timeout for now, instead of default 5 sec to work around
89+
// failing CI, because for some reason it currently takes approx. 6 secs to run
90+
// TODO: actually identify what is causing this and fix the flakiness
91+
92+
93+
});

0 commit comments

Comments
 (0)