-
Notifications
You must be signed in to change notification settings - Fork 0
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
Incorporate error handling in a few places #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { describe, expect, it, beforeEach } from 'vitest' | ||
import { POST } from './route' | ||
import { NextRequest } from 'next/server' | ||
import path from 'path' | ||
import fs from 'fs' | ||
import { UPLOAD_DIR } from '@/app/utils' | ||
import mockFs from 'mock-fs' | ||
import { v4 as uuidv4 } from 'uuid' | ||
|
||
beforeEach(() => { | ||
fs.rmSync(UPLOAD_DIR, { recursive: true, force: true }) | ||
}) | ||
|
||
describe('POST /api/run/[runId]/upload', () => { | ||
it('should upload a file successfully', async () => { | ||
const mockFile = new Blob(['id,name\n1,John'], { type: 'text/csv' }) | ||
const mockRunId = '123' | ||
const mockRunId = uuidv4() | ||
|
||
const formData = new FormData() | ||
formData.append('file', new File([mockFile], mockRunId)) | ||
|
@@ -38,9 +44,43 @@ describe('POST /api/run/[runId]/upload', () => { | |
const req = { | ||
formData: async () => formData, | ||
} as NextRequest | ||
const params = { runId: '123' } | ||
const params = { runId: uuidv4() } | ||
const response = await POST(req, { params }) | ||
expect(response.status).toBe(400) | ||
expect((await response.json()).error).toBe('Form data does not include expected file key') | ||
}) | ||
|
||
it('should return failure if unexpected form data is included', async () => { | ||
const mockFile = new Blob(['id,name\n1,John'], { type: 'text/csv' }) | ||
const mockRunId = uuidv4() | ||
|
||
const formData = new FormData() | ||
formData.append('file', new File([mockFile], mockRunId)) | ||
formData.append('file2', new File([mockFile], mockRunId)) | ||
|
||
const req = { | ||
formData: async () => formData, | ||
} as NextRequest | ||
const params = { runId: uuidv4() } | ||
const response = await POST(req, { params }) | ||
expect(response.status).toBe(400) | ||
expect((await response.json()).error).toBe('Form data includes unexpected data keys') | ||
}) | ||
|
||
it('should return failure if runId is not a UUID', async () => { | ||
const mockRunId = '123' | ||
|
||
const formData = new FormData() | ||
|
||
const req = { | ||
formData: async () => formData, | ||
} as NextRequest | ||
|
||
const params = { runId: mockRunId } | ||
|
||
const response = await POST(req, { params }) | ||
expect(response.status).toBe(400) | ||
expect((await response.json()).error).toBe('runId is not a UUID') | ||
}) | ||
|
||
it('should return an error if no runID is provided', async () => { | ||
|
@@ -55,4 +95,27 @@ describe('POST /api/run/[runId]/upload', () => { | |
const response = await POST(req, { params }) | ||
expect(response.status).toBe(400) | ||
}) | ||
|
||
it('should return an error if the runID has results already', async () => { | ||
const mockRunId = uuidv4() | ||
|
||
mockFs({ | ||
[UPLOAD_DIR]: { | ||
[mockRunId]: '', | ||
}, | ||
}) | ||
|
||
const formData = new FormData() | ||
formData.append('file', '') | ||
|
||
const req = { | ||
formData: async () => formData, | ||
} as NextRequest | ||
|
||
const params = { runId: mockRunId } | ||
|
||
const response = await POST(req, { params }) | ||
expect(response.status).toBe(400) | ||
expect((await response.json()).error).toBe('Data already exists for runId') | ||
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. @philschatz had left a comment on my PR about testing for the error string of the 400 response and maybe having that not be necessary? im not sure if that applies here - i dont feel strongly either way, just pointing out for consistency 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. Yeah -- I added it only to make sure we are getting the 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. oh yeah good point! Im happy keeping it in, doesn't matter much to me |
||
}) | ||
}) |
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.
one "product" question i have around this is
just thinking out loud 🤔 💭
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.
Ah, I see. Yeah, my implicit assumption currently is it's a safeguard in case the researcher code (for some reason that was missed during code review 😅 ) attempts to submit multiple times for the same run.
I guess another case it could protect against is if (for whatever reason) the SA runs things twice, we ignore the erroneous re-submit.
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.
🤔 -- FWIW, I think if we allow "override", we should version artifacts vs write over. That would definitely require some product definition, though, since it would have cascading impacts 😅 .
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.
yeah with your 2nd comment in mind, happy to just reject it and enforce a new runid upload :)