|
| 1 | +import { describe, expect, it, vi, beforeEach } from 'vitest'; |
| 2 | +import { POST } from '@/app/api/upload-url/route'; |
| 3 | +import { NextRequest } from 'next/server'; |
| 4 | +import * as s3Utils from '@/utils/s3'; |
| 5 | + |
| 6 | +// Mock environment variables |
| 7 | +vi.stubEnv('AWS_REGION', 'us-east-1'); |
| 8 | +vi.stubEnv('NEXT_PUBLIC_AWS_S3_BUCKET_NAME', 'test-bucket'); |
| 9 | + |
| 10 | +vi.mock('@/utils/s3', () => ({ |
| 11 | + checkFileExists: vi.fn(), |
| 12 | + generateSignedUrl: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +describe('POST /api/upload-url', () => { |
| 16 | + beforeEach(() => { |
| 17 | + vi.clearAllMocks(); |
| 18 | + }); |
| 19 | + |
| 20 | + const createRequest = (body: object) => |
| 21 | + new NextRequest('http://localhost/api/upload-url', { |
| 22 | + method: 'POST', |
| 23 | + body: JSON.stringify(body), |
| 24 | + headers: { 'Content-Type': 'application/json' }, |
| 25 | + }); |
| 26 | + |
| 27 | + it('returns fileUrl, signedUrl, and fileExist=false if new filename is uploaded', async () => { |
| 28 | + vi.spyOn(s3Utils, 'checkFileExists').mockResolvedValue(false); |
| 29 | + vi.spyOn(s3Utils, 'generateSignedUrl').mockResolvedValue( |
| 30 | + 'https://signed-url.com' |
| 31 | + ); |
| 32 | + |
| 33 | + const req = createRequest({ |
| 34 | + filename: 'newfile.png', |
| 35 | + filetype: 'image/png', |
| 36 | + }); |
| 37 | + const res = await POST(req); |
| 38 | + const json = await res.json(); |
| 39 | + |
| 40 | + expect(res.status).toBe(200); |
| 41 | + expect(json).toEqual({ |
| 42 | + uploadUrl: 'https://signed-url.com', |
| 43 | + fileUrl: 'https://test-bucket.s3.us-east-1.amazonaws.com/newfile.png', |
| 44 | + fileExists: false, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns fileUrl, signedUrl, and fileExist=true if existing filename is uploaded', async () => { |
| 49 | + vi.spyOn(s3Utils, 'checkFileExists').mockResolvedValue(true); |
| 50 | + vi.spyOn(s3Utils, 'generateSignedUrl').mockResolvedValue( |
| 51 | + 'https://signed-url.com' |
| 52 | + ); |
| 53 | + |
| 54 | + const req = createRequest({ |
| 55 | + filename: 'existingfile.png', |
| 56 | + filetype: 'image/png', |
| 57 | + }); |
| 58 | + const res = await POST(req); |
| 59 | + const json = await res.json(); |
| 60 | + |
| 61 | + expect(res.status).toBe(200); |
| 62 | + expect(json).toEqual({ |
| 63 | + uploadUrl: 'https://signed-url.com', |
| 64 | + fileUrl: |
| 65 | + 'https://test-bucket.s3.us-east-1.amazonaws.com/existingfile.png', |
| 66 | + fileExists: true, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns a 400 error if filename is missing from POST body', async () => { |
| 71 | + const req = createRequest({ filetype: 'image/png' }); |
| 72 | + const res = await POST(req); |
| 73 | + expect(res.status).toBe(400); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns a 400 error if filetype is missing from POST body', async () => { |
| 77 | + const req = createRequest({ filename: 'test.png' }); |
| 78 | + const res = await POST(req); |
| 79 | + expect(res.status).toBe(400); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns a 400 error if filetype is not jpg or png', async () => { |
| 83 | + const req = createRequest({ filename: 'test.txt', filetype: 'text/plain' }); |
| 84 | + const res = await POST(req); |
| 85 | + expect(res.status).toBe(400); |
| 86 | + }); |
| 87 | + |
| 88 | + it('returns a 500 error if unable to check file existence in S3', async () => { |
| 89 | + vi.spyOn(s3Utils, 'checkFileExists').mockRejectedValue( |
| 90 | + new Error('S3 Error') |
| 91 | + ); |
| 92 | + |
| 93 | + const req = createRequest({ filename: 'test.png', filetype: 'image/png' }); |
| 94 | + const res = await POST(req); |
| 95 | + expect(res.status).toBe(500); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns a 500 error if unable to generate signed URL', async () => { |
| 99 | + vi.spyOn(s3Utils, 'checkFileExists').mockResolvedValue(false); |
| 100 | + vi.spyOn(s3Utils, 'generateSignedUrl').mockRejectedValue( |
| 101 | + new Error('S3 Error') |
| 102 | + ); |
| 103 | + |
| 104 | + const req = createRequest({ filename: 'test.png', filetype: 'image/png' }); |
| 105 | + const res = await POST(req); |
| 106 | + expect(res.status).toBe(500); |
| 107 | + }); |
| 108 | +}); |
0 commit comments