Skip to content

Commit

Permalink
Refactor unit tests (#71)
Browse files Browse the repository at this point in the history
* Update logout routes

* Refactor DELETE tests
  • Loading branch information
Advayp authored Jan 24, 2025
1 parent c4ad9b3 commit 5f0445f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 52 deletions.
16 changes: 0 additions & 16 deletions backend/app/api/user/logout/auth.test.ts

This file was deleted.

15 changes: 0 additions & 15 deletions backend/app/api/user/logout/noAuth.test.ts

This file was deleted.

31 changes: 31 additions & 0 deletions backend/app/api/user/logout/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { verifySession } from '@/lib/session';
import { GET } from './route';

jest.mock('@/lib/session', () => ({
verifySession: jest.fn(),
deleteSession: jest.fn(),
}));

const mockVerifySession = verifySession as jest.Mock;

test('Logout succeeds for valid session', async () => {
mockVerifySession.mockResolvedValue({
isAuth: true,
uid: 2,
});

const res = await GET();
const data = await res.json();

expect(data.success).toBeTruthy();
});

test('Logout fails for invalid session', async () => {
mockVerifySession.mockResolvedValue({
isAuth: false,
});
const res = await GET();
const data = await res.json();

expect(data.error).toEqual('User not authenticated');
});
16 changes: 0 additions & 16 deletions backend/app/api/user/noAuth.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { verifySession } from '@/lib/session';
import { prismaMock } from '@/__test__/singleton';
import { User } from '@prisma/client';
import { DELETE } from './route';

jest.mock('@/lib/session', () => ({
verifySession: jest.fn(() => ({
isAuth: true,
uid: 2,
})),
deleteSession: jest.fn(() => {}),
verifySession: jest.fn(),
deleteSession: jest.fn(),
}));

const mockVerifySession = verifySession as jest.Mock;

test('Delete succeeds if user is authenticated', async () => {
mockVerifySession.mockResolvedValue({
isAuth: true,
uid: 1,
});

const temporaryUser: User = {
id: 1,
username: 'test',
Expand All @@ -24,3 +29,15 @@ test('Delete succeeds if user is authenticated', async () => {

expect(data.success).toBe(true);
});

test('Delete fails for an invalid session', async () => {
mockVerifySession.mockResolvedValue({
isAuth: false,
});

const res = await DELETE();
const data = await res.json();

expect(res.status).toEqual(400);
expect(data.error).toBe('Invalid session');
});

0 comments on commit 5f0445f

Please sign in to comment.