Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
test(server): ensure session expiry date is well set
Elian Doran8516df8f9bf9
diff --git a/apps/server/src/routes/login.spec.ts b/apps/server/src/routes/login.spec.tsindex 5e27cca43f..30bd2479e9 100644--- a/apps/server/src/routes/login.spec.ts+++ b/apps/server/src/routes/login.spec.ts@@ -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) {diff --git a/apps/server/src/routes/session_parser.ts b/apps/server/src/routes/session_parser.tsindex 8ad8cd5f56..e8100bc7ed 100644--- a/apps/server/src/routes/session_parser.ts+++ b/apps/server/src/routes/session_parser.ts@@ -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();