Skip to content

Commit

Permalink
refactor: sessions for user without error (#8742)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Nov 13, 2024
1 parent fecf57c commit bcbbd5c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
4 changes: 1 addition & 3 deletions src/lib/db/session-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISession> {
Expand Down
7 changes: 2 additions & 5 deletions src/lib/services/session-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ export default class SessionService {
userId: number,
maxSessions: number,
): Promise<void> {
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),
);
Expand Down
5 changes: 2 additions & 3 deletions src/test/e2e/services/session-service.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
5 changes: 2 additions & 3 deletions src/test/e2e/services/user-service.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit bcbbd5c

Please sign in to comment.