1- import { beforeAll , describe , it } from "vitest" ;
1+ import { beforeEach , describe , expect , it } from "vitest" ;
22import supertest from "supertest" ;
33import { initializeTranslations } from "../services/i18n.js" ;
44import type { Application , Request , Response , NextFunction } from "express" ;
5+ import dayjs from "dayjs" ;
56
67let app : Application ;
78
89describe ( "Login Route test" , ( ) => {
910
10- beforeAll ( async ( ) => {
11+ beforeEach ( async ( ) => {
1112 initializeTranslations ( ) ;
1213 app = ( await import ( "../app.js" ) ) . default ;
1314 } ) ;
1415
15- it ( "return a 401 status, when login fails with wrong password" , async ( ) => {
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 = / a s s e t s \/ v [ 0 - 9 . a - z ] + \/ a p p \/ l o g i n \. c s s / ;
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 ( ) => {
1631
1732 await supertest ( app )
1833 . post ( "/login" )
@@ -21,16 +36,51 @@ describe("Login Route test", () => {
2136
2237 } ) ;
2338
24- // TriliumNextTODO: how to handle different configs here? e.g. TOTP, or different cookieMaxAge from config.ini
25-
26- /*
2739
2840 it ( "sets correct Expires, when 'Remember Me' is ticked" , async ( ) => {
29- await supertest(app)
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 = / E x p i r e s = (?< 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+
68+
69+ it ( "does not set Expires, when 'Remember Me' is not ticked" , async ( ) => {
70+
71+ const res = await supertest ( app )
3072 . post ( "/login" )
73+ . send ( { password : "demo1234" } )
3174 . expect ( 302 )
32- .expect("Set-Cookie", "trilium.sid=trilium.sid; Path=/; Expires=TODO");
75+
76+ const setCookieHeader = res . headers [ "set-cookie" ] [ 0 ] ;
77+
78+ // match for e.g. "Expires=Wed, 07 May 2025 07:02:59 GMT;"
79+ const expiresCookieRegExp = / E x p i r e s = (?< date > [ \w \s , : ] + ) / ;
80+ const expiresCookieMatch = setCookieHeader . match ( expiresCookieRegExp ) ;
81+ expect ( expiresCookieMatch ) . toBeNull ( ) ;
82+
3383 } ) ;
3484
35- */
85+
3686} ) ;
0 commit comments