mirror of
https://github.com/zadam/trilium.git
synced 2025-10-20 15:19:01 +02:00
test(server): ensure session expiry date is well set
This commit is contained in:
parent
e003ec3b6f
commit
8516df8f9b
@ -66,12 +66,13 @@ describe("Login Route test", () => {
|
||||
expect(actualExpiresDate.slice(0,23)).toBe(expectedExpiresDate.slice(0,23))
|
||||
|
||||
// Check the session is stored in the database.
|
||||
const session = await getSessionFromCookie(setCookieHeader);
|
||||
const { session, expiry } = await getSessionFromCookie(setCookieHeader);
|
||||
expect(session!).toBeTruthy();
|
||||
expect(session!.cookie.expires).toBeTruthy();
|
||||
expect(new Date(session!.cookie.expires!).toUTCString().substring(0, 23))
|
||||
.toBe(expectedExpiresDate.substring(0, 23));
|
||||
expect(session!.loggedIn).toBe(true);
|
||||
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
|
||||
@ -90,10 +91,14 @@ describe("Login Route test", () => {
|
||||
expect(setCookieHeader).not.toMatch(/Expires=(?<date>[\w\s,:]+)/)
|
||||
|
||||
// Check the session is stored in the database.
|
||||
const session = await getSessionFromCookie(setCookieHeader);
|
||||
const { session, expiry } = await getSessionFromCookie(setCookieHeader);
|
||||
expect(session!).toBeTruthy();
|
||||
expect(session!.cookie.expires).toBeUndefined();
|
||||
expect(session!.loggedIn).toBe(true);
|
||||
|
||||
const expectedExpirationDate = dayjs().utc().add(1, "hour").toDate();
|
||||
expect(expiry?.getTime()).toBeGreaterThan(new Date().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
|
||||
@ -108,7 +113,10 @@ async function getSessionFromCookie(setCookieHeader: string) {
|
||||
|
||||
// Check the session is stored in the database.
|
||||
const sessionId = decodeURIComponent(sessionIdMatch!).slice(2).split(".")[0];
|
||||
return await getSessionFromStore(sessionId);
|
||||
return {
|
||||
session: await getSessionFromStore(sessionId),
|
||||
expiry: sessionStore.getSessionExpiry(sessionId)
|
||||
};
|
||||
}
|
||||
|
||||
function getSessionFromStore(sessionId: string) {
|
||||
|
@ -50,6 +50,22 @@ export class SQLiteSessionStore extends Store {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a session ID, returns the expiry date of the session.
|
||||
*
|
||||
* @param sid the session ID to check.
|
||||
* @returns the expiry date of the session or null if the session does not exist.
|
||||
*/
|
||||
getSessionExpiry(sid: string): Date | null {
|
||||
try {
|
||||
const expires = sql.getValue<number>(/*sql*/`SELECT expires FROM sessions WHERE id = ?`, sid);
|
||||
return expires !== undefined ? new Date(expires) : null;
|
||||
} catch (e) {
|
||||
log.error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const sessionStore = new SQLiteSessionStore();
|
||||
|
Loading…
x
Reference in New Issue
Block a user