Passed
Pull Request — master (#56)
by
unknown
03:10 queued 01:29
created

api/src/Infrastructure/Adapter/FileUploadS3Adapter.spec.ts   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 50
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 45
mnd 1
bc 1
fnc 0
dl 0
loc 50
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
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