1
|
|
|
import { mock, instance, when, objectContaining, anyFunction } from 'ts-mockito'; |
2
|
|
|
import { ConfigService } from '@nestjs/config'; |
3
|
|
|
import { S3 } from 'aws-sdk'; |
4
|
|
|
import { FileUploadS3Adapter } from './FileUploadS3Adapter'; |
5
|
|
|
import { S3Factory } from '../Ingestion/S3Factory'; |
6
|
|
|
|
7
|
|
|
describe('FileUploadS3Adapter', () => { |
8
|
|
|
it('testGetEnpoint', async () => { |
9
|
|
|
const configService: ConfigService = mock(ConfigService); |
10
|
|
|
when(configService.get<string>('STORAGE_BUCKET')).thenReturn('my-test-bucket'); |
11
|
|
|
|
12
|
|
|
const s3: S3 = mock(S3); |
13
|
|
|
when(s3.getSignedUrl( |
14
|
|
|
'putObject', |
15
|
|
|
objectContaining({Bucket: 'my-test-bucket', Key: 'path/to/file'}), |
16
|
|
|
anyFunction() |
17
|
|
|
)).thenCall((operation: string, params: any, callback) => callback(null, 'https://bucket.endpoint.url/path/to/file')); |
18
|
|
|
|
19
|
|
|
const s3Factory: S3Factory = mock(S3Factory); |
20
|
|
|
when(s3Factory.create()).thenReturn(instance(s3)); |
21
|
|
|
|
22
|
|
|
const fileUploadAdapter = new FileUploadS3Adapter(instance(configService), instance(s3Factory)); |
23
|
|
|
expect( |
24
|
|
|
await fileUploadAdapter.getEndPoint('path/to/file') |
25
|
|
|
).toBe('https://bucket.endpoint.url/path/to/file'); |
26
|
|
|
}); |
27
|
|
|
|
28
|
|
|
it('testGetEnpointFailed', async () => { |
29
|
|
|
const configService: ConfigService = mock(ConfigService); |
30
|
|
|
when(configService.get<string>('STORAGE_BUCKET')).thenReturn('my-test-bucket'); |
31
|
|
|
|
32
|
|
|
const s3: S3 = mock(S3); |
33
|
|
|
when(s3.getSignedUrl( |
34
|
|
|
'putObject', |
35
|
|
|
objectContaining({Bucket: 'my-test-bucket', Key: 'path/to/file'}), |
36
|
|
|
anyFunction() |
37
|
|
|
)).thenCall((operation: string, params: any, callback) => callback('bad params', null)); |
38
|
|
|
|
39
|
|
|
const s3Factory: S3Factory = mock(S3Factory); |
40
|
|
|
when(s3Factory.create()).thenReturn(instance(s3)); |
41
|
|
|
|
42
|
|
|
const fileUploadAdapter = new FileUploadS3Adapter(instance(configService), instance(s3Factory)); |
43
|
|
|
try { |
44
|
|
|
await fileUploadAdapter.getEndPoint('path/to/file') |
45
|
|
|
} catch (error) { |
46
|
|
|
expect(error).toBe('bad params'); |
47
|
|
|
} |
48
|
|
|
}); |
49
|
|
|
}); |
50
|
|
|
|