-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add tests for routes #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b604a48
e52f709
221e627
8c4417e
7a6e5ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,52 +1,53 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { auth } from '@/app/(auth)/auth'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { ArtifactKind } from '@/components/artifact'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import type { ArtifactKind } from '@/components/artifact'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||
| deleteDocumentsByIdAfterTimestamp, | ||||||||||||||||||||||||||||||||||||||||||||||||
| getDocumentsById, | ||||||||||||||||||||||||||||||||||||||||||||||||
| saveDocument, | ||||||||||||||||||||||||||||||||||||||||||||||||
| } from '@/lib/db/queries'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { apiErrors, successResponse } from '@/lib/responses'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| export async function GET(request: Request) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const { searchParams } = new URL(request.url); | ||||||||||||||||||||||||||||||||||||||||||||||||
| const id = searchParams.get('id'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!id) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response('Missing id', { status: 400 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.missingParameter(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const session = await auth(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!session || !session.user) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response('Unauthorized', { status: 401 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (!session?.user?.id) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.unauthorized(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const documents = await getDocumentsById({ id }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const [document] = documents; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!document) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response('Not Found', { status: 404 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.documentNotFound(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (document.userId !== session.user.id) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response('Unauthorized', { status: 401 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.documentForbidden(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return Response.json(documents, { status: 200 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return successResponse(documents); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| export async function POST(request: Request) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const { searchParams } = new URL(request.url); | ||||||||||||||||||||||||||||||||||||||||||||||||
| const id = searchParams.get('id'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!id) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response('Missing id', { status: 400 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.missingParameter(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const session = await auth(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!session) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response('Unauthorized', { status: 401 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.unauthorized(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -56,49 +57,62 @@ export async function POST(request: Request) { | |||||||||||||||||||||||||||||||||||||||||||||||
| }: { content: string; title: string; kind: ArtifactKind } = | ||||||||||||||||||||||||||||||||||||||||||||||||
| await request.json(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (session.user?.id) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const document = await saveDocument({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| content, | ||||||||||||||||||||||||||||||||||||||||||||||||
| title, | ||||||||||||||||||||||||||||||||||||||||||||||||
| kind, | ||||||||||||||||||||||||||||||||||||||||||||||||
| userId: session.user.id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (!session?.user?.id) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.unauthorized(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const documents = await getDocumentsById({ id: id }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (documents.length > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const [document] = documents; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return Response.json(document, { status: 200 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (document.userId !== session.user.id) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.documentForbidden(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response('Unauthorized', { status: 401 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| const [createdDocument] = await saveDocument({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| content, | ||||||||||||||||||||||||||||||||||||||||||||||||
| title, | ||||||||||||||||||||||||||||||||||||||||||||||||
| kind, | ||||||||||||||||||||||||||||||||||||||||||||||||
| userId: session.user.id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return successResponse(createdDocument); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+74
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing Error HandlingThe saveDocument operation lacks error handling, which could result in unhandled promise rejections if the database operation fails, causing the API to crash instead of returning an error response.
Suggested change
Standards
|
||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| export async function PATCH(request: Request) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| export async function DELETE(request: Request) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const { searchParams } = new URL(request.url); | ||||||||||||||||||||||||||||||||||||||||||||||||
| const id = searchParams.get('id'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const { timestamp }: { timestamp: string } = await request.json(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| const timestamp = searchParams.get('timestamp'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!id) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response('Missing id', { status: 400 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.missingParameter(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!timestamp) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.missingParameter(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const session = await auth(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!session || !session.user) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response('Unauthorized', { status: 401 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (!session?.user?.id) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.unauthorized(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const documents = await getDocumentsById({ id }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const [document] = documents; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (document.userId !== session.user.id) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response('Unauthorized', { status: 401 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return apiErrors.documentForbidden(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| await deleteDocumentsByIdAfterTimestamp({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| const deletedDocuments = await deleteDocumentsByIdAfterTimestamp({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| timestamp: new Date(timestamp), | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return new Response('Deleted', { status: 200 }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return successResponse(deletedDocuments); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| export const ERRORS = { | ||
| MISSING_PARAMETER: { | ||
| type: 'missing_parameter', | ||
| message: 'Missing parameter', | ||
| }, | ||
| UNAUTHORIZED: { | ||
| type: 'unauthorized', | ||
| message: 'Unauthorized', | ||
| }, | ||
| DOCUMENT_NOT_FOUND: { | ||
| type: 'document_not_found', | ||
| message: 'Document not found', | ||
| }, | ||
| DOCUMENT_FORBIDDEN: { | ||
| type: 'document_forbidden', | ||
| message: | ||
| 'Access to this document is forbidden. You may not have the required permissions.', | ||
| }, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { ERRORS } from './errors'; | ||
|
|
||
| export function successResponse(data: any) { | ||
| return Response.json({ data, error: null }, { status: 200 }); | ||
| } | ||
|
|
||
| export function errorResponse( | ||
| error: { type: string; message: string }, | ||
| status: number, | ||
| ) { | ||
| return Response.json({ data: null, error }, { status }); | ||
| } | ||
|
|
||
| export const apiErrors = { | ||
| missingParameter: () => errorResponse(ERRORS.MISSING_PARAMETER, 400), | ||
| unauthorized: () => errorResponse(ERRORS.UNAUTHORIZED, 401), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error Handling: The error response system is missing a specific error type for invalid parameters. Consider adding an 'invalidParameter' error type to provide more accurate error messages for validation failures. Recommendation: invalidParameter: (message = 'Invalid parameter') => errorResponse({ type: 'invalid_parameter', message }, 400), |
||
| documentNotFound: () => errorResponse(ERRORS.DOCUMENT_NOT_FOUND, 404), | ||
| documentForbidden: () => errorResponse(ERRORS.DOCUMENT_FORBIDDEN, 403), | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| "db:pull": "drizzle-kit pull", | ||
| "db:check": "drizzle-kit check", | ||
| "db:up": "drizzle-kit up", | ||
| "test": "export PLAYWRIGHT=True && pnpm exec playwright test --workers=4" | ||
| "test": "pnpm tsx tests/run-tests" | ||
| }, | ||
| "dependencies": { | ||
| "@ai-sdk/react": "^1.2.8", | ||
|
|
@@ -92,6 +92,7 @@ | |
| "@types/pdf-parse": "^1.1.4", | ||
| "@types/react": "^18", | ||
| "@types/react-dom": "^18", | ||
| "@types/wait-on": "^5.3.4", | ||
| "drizzle-kit": "^0.25.0", | ||
| "eslint": "^8.57.0", | ||
| "eslint-config-next": "14.2.5", | ||
|
|
@@ -101,7 +102,8 @@ | |
| "postcss": "^8", | ||
| "tailwindcss": "^3.4.1", | ||
| "tsx": "^4.19.1", | ||
| "typescript": "^5.6.3" | ||
| "typescript": "^5.6.3", | ||
| "wait-on": "^8.0.3" | ||
| }, | ||
| "packageManager": "[email protected]" | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Input Validation: Missing timestamp parameter validation in document deletion endpoint. While the code checks for the presence of the timestamp parameter, it doesn't validate the format or range. This could lead to unintended document deletions with malformed timestamps.
Recommendation: