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 = / 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 ( ) => {
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 = / 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+ } , 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 = / E x p i r e s = (?< 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