From bcbbd5c3e6e05374fcd359f2fdfeff6e0b39e9c0 Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Wed, 13 Nov 2024 16:37:05 +0100 Subject: [PATCH] refactor: sessions for user without error (#8742) --- src/lib/db/session-store.ts | 4 +--- src/lib/services/session-service.ts | 7 ++----- src/test/e2e/services/session-service.e2e.test.ts | 5 ++--- src/test/e2e/services/user-service.e2e.test.ts | 5 ++--- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/lib/db/session-store.ts b/src/lib/db/session-store.ts index b12ef5b772c7..31cdd2e7485f 100644 --- a/src/lib/db/session-store.ts +++ b/src/lib/db/session-store.ts @@ -43,9 +43,7 @@ export default class SessionStore implements ISessionStore { if (rows && rows.length > 0) { return rows.map(this.rowToSession); } - throw new NotFoundError( - `Could not find sessions for user with id ${userId}`, - ); + return []; } async get(sid: string): Promise { diff --git a/src/lib/services/session-service.ts b/src/lib/services/session-service.ts index f4edddae277f..cd6f518a2c51 100644 --- a/src/lib/services/session-service.ts +++ b/src/lib/services/session-service.ts @@ -37,11 +37,8 @@ export default class SessionService { userId: number, maxSessions: number, ): Promise { - let userSessions: ISession[] = []; - try { - // this method may throw errors when no session - userSessions = await this.sessionStore.getSessionsForUser(userId); - } catch (e) {} + const userSessions: ISession[] = + await this.sessionStore.getSessionsForUser(userId); const newestFirst = userSessions.sort((a, b) => compareDesc(a.createdAt, b.createdAt), ); diff --git a/src/test/e2e/services/session-service.e2e.test.ts b/src/test/e2e/services/session-service.e2e.test.ts index c03fd770a48e..50722d7dc020 100644 --- a/src/test/e2e/services/session-service.e2e.test.ts +++ b/src/test/e2e/services/session-service.e2e.test.ts @@ -95,9 +95,8 @@ test('Can delete sessions by user', async () => { const sessions = await sessionService.getActiveSessions(); expect(sessions.length).toBe(2); await sessionService.deleteSessionsForUser(2); - await expect(async () => { - await sessionService.getSessionsForUser(2); - }).rejects.toThrow(NotFoundError); + const noSessions = await sessionService.getSessionsForUser(2); + expect(noSessions.length).toBe(0); }); test('Can delete session by sid', async () => { diff --git a/src/test/e2e/services/user-service.e2e.test.ts b/src/test/e2e/services/user-service.e2e.test.ts index 31d878c1bfe9..56bac4b504cc 100644 --- a/src/test/e2e/services/user-service.e2e.test.ts +++ b/src/test/e2e/services/user-service.e2e.test.ts @@ -357,9 +357,8 @@ test("deleting a user should delete the user's sessions", async () => { const userSessions = await sessionService.getSessionsForUser(user.id); expect(userSessions.length).toBe(1); await userService.deleteUser(user.id, TEST_AUDIT_USER); - await expect(async () => - sessionService.getSessionsForUser(user.id), - ).rejects.toThrow(NotFoundError); + const noSessions = await sessionService.getSessionsForUser(user.id); + expect(noSessions.length).toBe(0); }); test('updating a user without an email should not strip the email', async () => {