|
| 1 | +import { dbMigration } from '../../utils/dbMigrate.js' |
| 2 | +import { |
| 3 | + CompleteMultipartUploadCommand, |
| 4 | + CreateMultipartUploadCommand, |
| 5 | + GetObjectCommand, |
| 6 | + HeadObjectCommand, |
| 7 | + PutObjectCommand, |
| 8 | + S3Client, |
| 9 | + UploadPartCommand, |
| 10 | +} from '@aws-sdk/client-s3' |
| 11 | +import { |
| 12 | + createMockUser, |
| 13 | + mockRabbitPublish, |
| 14 | + unmockMethods, |
| 15 | +} from '../../utils/mocks.js' |
| 16 | +import { jest } from '@jest/globals' |
| 17 | +import { AuthManager } from '../../../src/infrastructure/services/auth/index.js' |
| 18 | +import { config } from '../../../src/config.js' |
| 19 | +import { SubscriptionsUseCases } from '../../../dist/core/users/subscriptions.js' |
| 20 | + |
| 21 | +describe('AWS S3 - SDK', () => { |
| 22 | + let s3Client: S3Client |
| 23 | + const user = createMockUser() |
| 24 | + const BASE_PATH = `http://localhost:${config.express.port}` |
| 25 | + const Bucket = `${BASE_PATH}/s3` |
| 26 | + |
| 27 | + beforeAll(async () => { |
| 28 | + await dbMigration.up() |
| 29 | + |
| 30 | + // Mock warnings to clean test logs |
| 31 | + const consoleWarn = global.console.warn |
| 32 | + global.console.warn = () => {} |
| 33 | + // Start the frontend server |
| 34 | + await import('../../../src/app/apis/download.js') |
| 35 | + global.console.warn = consoleWarn |
| 36 | + // Wait for the server to start |
| 37 | + await new Promise((resolve) => setTimeout(resolve, 500)) |
| 38 | + |
| 39 | + mockRabbitPublish() |
| 40 | + // onboard the user |
| 41 | + await SubscriptionsUseCases.getOrCreateSubscription(user) |
| 42 | + |
| 43 | + // mock auth manager returning the mock user |
| 44 | + jest.spyOn(AuthManager, 'getUserFromAccessToken').mockResolvedValue(user) |
| 45 | + jest.spyOn(AuthManager, 'getUserFromPublicId').mockResolvedValue(user) |
| 46 | + |
| 47 | + s3Client = new S3Client({ |
| 48 | + region: 'us-east-1', |
| 49 | + credentials: { |
| 50 | + accessKeyId: 'e046e71c8dc3459c8da189e62418203a', |
| 51 | + secretAccessKey: '', |
| 52 | + }, |
| 53 | + bucketEndpoint: true, |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + afterAll(async () => { |
| 58 | + unmockMethods() |
| 59 | + await dbMigration.down() |
| 60 | + }) |
| 61 | + |
| 62 | + it('should be healthy the server', async () => { |
| 63 | + const response = await fetch( |
| 64 | + `http://localhost:${config.express.port}/health`, |
| 65 | + ) |
| 66 | + expect(response.status).toBe(204) |
| 67 | + }) |
| 68 | + |
| 69 | + const Key = 'test.txt' |
| 70 | + const Body = Buffer.from('Hello, world!') |
| 71 | + |
| 72 | + it('should upload an object', async () => { |
| 73 | + const command = new PutObjectCommand({ |
| 74 | + Bucket, |
| 75 | + Key: 'test.txt', |
| 76 | + Body, |
| 77 | + }) |
| 78 | + const result = await s3Client.send(command) |
| 79 | + expect(result).toMatchObject({ |
| 80 | + ETag: expect.any(String), |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('should download the object', async () => { |
| 85 | + const command = new GetObjectCommand({ |
| 86 | + Bucket, |
| 87 | + Key, |
| 88 | + }) |
| 89 | + |
| 90 | + const result = await s3Client.send(command) |
| 91 | + |
| 92 | + expect(result.Body).toBeDefined() |
| 93 | + expect(Buffer.from(await result.Body!.transformToByteArray())).toEqual(Body) |
| 94 | + }) |
| 95 | + |
| 96 | + it('should be able download first 10 bytes of the object', async () => { |
| 97 | + const command = new GetObjectCommand({ |
| 98 | + Bucket, |
| 99 | + Key, |
| 100 | + Range: 'bytes 0-9', |
| 101 | + }) |
| 102 | + |
| 103 | + const result = await s3Client.send(command) |
| 104 | + |
| 105 | + expect(result.Body).toBeDefined() |
| 106 | + expect(result.ContentRange).toBe('bytes 0-9/13') |
| 107 | + const body = await result.Body!.transformToByteArray() |
| 108 | + expect(body.length).toBe(10) |
| 109 | + expect(body).toMatchObject(Body.subarray(0, 10).buffer) |
| 110 | + }) |
| 111 | + |
| 112 | + const SecondKey = 'test2.txt' |
| 113 | + const SecondBody = Buffer.from('Hello, world!') |
| 114 | + |
| 115 | + it('should be able to upload an object with multipart upload', async () => { |
| 116 | + const createCommand = new CreateMultipartUploadCommand({ |
| 117 | + Bucket, |
| 118 | + Key: SecondKey, |
| 119 | + }) |
| 120 | + |
| 121 | + const result = await s3Client.send(createCommand) |
| 122 | + expect(result).toMatchObject({ |
| 123 | + UploadId: expect.any(String), |
| 124 | + }) |
| 125 | + |
| 126 | + const uploadId = result.UploadId! |
| 127 | + |
| 128 | + const uploadPartCommand = new UploadPartCommand({ |
| 129 | + Bucket, |
| 130 | + Key: SecondKey, |
| 131 | + UploadId: uploadId, |
| 132 | + PartNumber: 1, |
| 133 | + Body: SecondBody, |
| 134 | + }) |
| 135 | + |
| 136 | + const partUploadResult = await s3Client.send(uploadPartCommand) |
| 137 | + expect(partUploadResult).toMatchObject({ |
| 138 | + ETag: expect.any(String), |
| 139 | + }) |
| 140 | + |
| 141 | + const completeCommand = new CompleteMultipartUploadCommand({ |
| 142 | + Bucket, |
| 143 | + Key: SecondKey, |
| 144 | + UploadId: uploadId, |
| 145 | + MultipartUpload: { |
| 146 | + Parts: [ |
| 147 | + { |
| 148 | + ETag: partUploadResult.ETag!, |
| 149 | + PartNumber: 1, |
| 150 | + }, |
| 151 | + ], |
| 152 | + }, |
| 153 | + }) |
| 154 | + |
| 155 | + const completeResult = await s3Client.send(completeCommand) |
| 156 | + expect(completeResult).toMatchObject({ |
| 157 | + ETag: expect.any(String), |
| 158 | + }) |
| 159 | + }) |
| 160 | + |
| 161 | + it('should be able to download the object', async () => { |
| 162 | + const command = new GetObjectCommand({ |
| 163 | + Bucket, |
| 164 | + Key: SecondKey, |
| 165 | + }) |
| 166 | + |
| 167 | + const result = await s3Client.send(command) |
| 168 | + |
| 169 | + expect(result.Body).toBeDefined() |
| 170 | + expect(Buffer.from(await result.Body!.transformToByteArray())).toEqual( |
| 171 | + SecondBody, |
| 172 | + ) |
| 173 | + }) |
| 174 | + |
| 175 | + describe('Metadata handled correctly', () => { |
| 176 | + const ThirdKey = 'test3.txt' |
| 177 | + |
| 178 | + it('should be able to upload an object with compression and encryption', async () => { |
| 179 | + const command = new PutObjectCommand({ |
| 180 | + Bucket, |
| 181 | + Key: ThirdKey, |
| 182 | + Body, |
| 183 | + Metadata: { |
| 184 | + compression: 'ZLIB', |
| 185 | + encryption: 'AES_256_GCM', |
| 186 | + }, |
| 187 | + }) |
| 188 | + |
| 189 | + const result = await s3Client.send(command) |
| 190 | + expect(result).toMatchObject({ |
| 191 | + ETag: expect.any(String), |
| 192 | + }) |
| 193 | + }) |
| 194 | + |
| 195 | + it('should be able to download the object with compression and encryption', async () => { |
| 196 | + const command = new HeadObjectCommand({ |
| 197 | + Bucket, |
| 198 | + Key: ThirdKey, |
| 199 | + }) |
| 200 | + |
| 201 | + const result = await s3Client.send(command) |
| 202 | + |
| 203 | + expect(result.Metadata?.compression).toBe('ZLIB') |
| 204 | + expect(result.Metadata?.encryption).toBe('AES_256_GCM') |
| 205 | + }) |
| 206 | + }) |
| 207 | +}) |
0 commit comments