-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add route to retrieve user's current genre preferences
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { verifySession } from '@/lib/session'; | ||
import { GET } from './route'; | ||
import { prismaMock } from '@/__test__/singleton'; | ||
|
||
jest.mock('@/lib/session', () => ({ | ||
verifySession: jest.fn(), | ||
})); | ||
|
||
const mockVerifySession = verifySession as jest.Mock; | ||
|
||
test("Preference retrieval fails if session isn't authenticated", async () => { | ||
mockVerifySession.mockResolvedValue({ isAuth: false }); | ||
|
||
const res = await GET(); | ||
const data = await res.json(); | ||
|
||
expect(data.error).toEqual('Invalid session'); | ||
}); | ||
|
||
test('Preference retrieval succeeds for valid session and returns appropriately', async () => { | ||
mockVerifySession.mockResolvedValue({ isAuth: true, uid: 2 }); | ||
|
||
prismaMock.user.findFirst.mockResolvedValue({ | ||
id: 2, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
preferences: ['genre 1'], | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} as any); | ||
|
||
const expectedGenres = ['genre 1']; | ||
|
||
const res = await GET(); | ||
const data = await res.json(); | ||
|
||
expect(data.genres).toEqual(expectedGenres); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import prisma from '@/lib/prisma'; | ||
import { verifySession } from '@/lib/session'; | ||
import { ErrorResponse } from '@/lib/types'; | ||
import { Genre } from '@prisma/client'; | ||
import { NextResponse } from 'next/server'; | ||
|
||
type GetResponse = { | ||
genres: Genre[]; | ||
}; | ||
|
||
export const GET = async (): Promise< | ||
NextResponse<GetResponse | ErrorResponse> | ||
> => { | ||
const session = await verifySession(); | ||
|
||
if (!session.isAuth) { | ||
return NextResponse.json({ error: 'Invalid session' }); | ||
} | ||
|
||
const user = await prisma.user.findFirst({ | ||
where: { | ||
id: session.uid, | ||
}, | ||
include: { | ||
preferences: true, | ||
}, | ||
}); | ||
|
||
return NextResponse.json({ genres: user!.preferences }); | ||
}; |