|
| 1 | +import { S3Provider, S3ProviderConfiguration } from '../src' |
| 2 | +import { FileStatus } from 'shared-types' |
| 3 | +import { afterEach } from 'node:test' |
| 4 | +import { resetS3Mock } from './s3-mock/localstack' |
| 5 | +import { addFile, uploadFile } from './utils/file-upload' |
| 6 | +import { expectS3Url } from './helpers/s3-url' |
| 7 | + |
| 8 | +describe('S3Provider', () => { |
| 9 | + const options: S3ProviderConfiguration = { |
| 10 | + bucketPath: 'upload', |
| 11 | + bucketRegion: 'eu-central-1', |
| 12 | + bucketName: 'test-bucket', |
| 13 | + } |
| 14 | + |
| 15 | + afterEach(async () => { |
| 16 | + await resetS3Mock() |
| 17 | + }) |
| 18 | + |
| 19 | + const file = new Blob(['Hello World'], { type: 'text/plain' }) |
| 20 | + |
| 21 | + const fileId = '1234' |
| 22 | + const expiresIn = 2000 |
| 23 | + |
| 24 | + it('should return a correctly signed upload url', async () => { |
| 25 | + const provider = new S3Provider({ |
| 26 | + ...options, |
| 27 | + }) |
| 28 | + |
| 29 | + const { url, id, expiry } = await provider.signedUploadUrl( |
| 30 | + fileId, |
| 31 | + expiresIn |
| 32 | + ) |
| 33 | + |
| 34 | + const date = new Date(expiry) |
| 35 | + const parsedDate = date |
| 36 | + .toISOString() |
| 37 | + .replace(/[:-]/g, '') |
| 38 | + .replace(/\.\d{3}/, '') |
| 39 | + |
| 40 | + const parsedUrl = new URL(url) |
| 41 | + const searchParams = parsedUrl.searchParams |
| 42 | + |
| 43 | + expectS3Url(parsedUrl, 'upload', fileId) |
| 44 | + |
| 45 | + expect(searchParams.get('X-Amz-Date')).toBe(parsedDate) |
| 46 | + expect(searchParams.get('X-Amz-Expires')).toBe(expiresIn.toString()) |
| 47 | + expect(searchParams.get('x-amz-acl')).toBe('bucket-owner-full-control') |
| 48 | + expect(searchParams.get('x-id')).toBe('PutObject') |
| 49 | + |
| 50 | + expect(id).toBe(fileId) |
| 51 | + }) |
| 52 | + |
| 53 | + test('uploading a file to the signed url', async () => { |
| 54 | + const provider = new S3Provider({ |
| 55 | + ...options, |
| 56 | + }) |
| 57 | + |
| 58 | + const { url } = await provider.signedUploadUrl(fileId, expiresIn) |
| 59 | + |
| 60 | + await uploadFile(url, file) |
| 61 | + }) |
| 62 | + |
| 63 | + const cases: Partial<S3ProviderConfiguration>[] = [ |
| 64 | + { |
| 65 | + optimisticFileDataResponse: true, |
| 66 | + }, |
| 67 | + { |
| 68 | + optimisticFileDataResponse: false, |
| 69 | + }, |
| 70 | + ] |
| 71 | + |
| 72 | + it.each(cases)( |
| 73 | + 'should return a correctly signed download url, %p', |
| 74 | + async (p) => { |
| 75 | + const provider = new S3Provider<string>({ |
| 76 | + ...options, |
| 77 | + ...p, |
| 78 | + }) |
| 79 | + |
| 80 | + await addFile(provider, fileId, file) |
| 81 | + |
| 82 | + const { status, variants } = await provider.getData(fileId) |
| 83 | + |
| 84 | + const url = new URL(variants[0]) |
| 85 | + const searchParams = url.searchParams |
| 86 | + |
| 87 | + expect(status).toBe(FileStatus.PROCESSED) |
| 88 | + expect(variants).toHaveLength(1) |
| 89 | + |
| 90 | + expectS3Url(url, 'upload', fileId, false) |
| 91 | + |
| 92 | + expect(searchParams.get('X-Amz-Expires')).toBe('900') |
| 93 | + expect(searchParams.get('x-id')).toBe('GetObject') |
| 94 | + } |
| 95 | + ) |
| 96 | + |
| 97 | + it('should delete a file', async () => { |
| 98 | + const provider = new S3Provider<string>({ |
| 99 | + ...options, |
| 100 | + // TODO: When set to `true` the status of the file is "PROCESSED" |
| 101 | + // since it does not check if the file actually exists |
| 102 | + optimisticFileDataResponse: false, |
| 103 | + }) |
| 104 | + |
| 105 | + await addFile(provider, fileId, file) |
| 106 | + |
| 107 | + await provider.delete(fileId) |
| 108 | + |
| 109 | + const { status } = await provider.getData(fileId) |
| 110 | + |
| 111 | + expect(status).toBe(FileStatus.NOT_FOUND) |
| 112 | + }) |
| 113 | +}) |
0 commit comments