|
1
|
|
|
import {mock, instance, when, verify, anything} from 'ts-mockito'; |
|
2
|
|
|
import {FileRepository} from 'src/Infrastructure/File/Repository/FileRepository'; |
|
3
|
|
|
import {LocalFileStorageAdapter} from 'src/Infrastructure/Adapter/LocalFileStorageAdapter'; |
|
4
|
|
|
import {File} from 'src/Domain/File/File.entity'; |
|
5
|
|
|
import {DownloadFileQueryHandler} from './DownloadFileQueryHandler'; |
|
6
|
|
|
import {DownloadFileQuery} from './DownloadFileQuery'; |
|
7
|
|
|
import {DownloadedFileView} from '../View/DownloadedFileView'; |
|
8
|
|
|
import {FileNotFoundException} from 'src/Domain/File/Exception/FileNotFoundException'; |
|
9
|
|
|
|
|
10
|
|
|
describe('DownloadFileCommandHandler', () => { |
|
11
|
|
|
let handler: DownloadFileQueryHandler; |
|
12
|
|
|
let localFileStorageAdapter: LocalFileStorageAdapter; |
|
13
|
|
|
let fileRepository: FileRepository; |
|
14
|
|
|
|
|
15
|
|
|
beforeEach(() => { |
|
16
|
|
|
localFileStorageAdapter = mock(LocalFileStorageAdapter); |
|
17
|
|
|
fileRepository = mock(FileRepository); |
|
18
|
|
|
|
|
19
|
|
|
handler = new DownloadFileQueryHandler( |
|
20
|
|
|
instance(fileRepository), |
|
21
|
|
|
instance(localFileStorageAdapter) |
|
22
|
|
|
); |
|
23
|
|
|
}); |
|
24
|
|
|
|
|
25
|
|
|
it('testDownloadFile', async () => { |
|
26
|
|
|
const buffer = mock(Buffer); |
|
27
|
|
|
const file = mock(File); |
|
28
|
|
|
when(file.getOriginalName()).thenReturn('file_mathieu_marchois.pdf'); |
|
29
|
|
|
when(file.getMimeType()).thenReturn('application/pdf'); |
|
30
|
|
|
|
|
31
|
|
|
when( |
|
32
|
|
|
fileRepository.findOneById('cfdd06eb-cd71-44b9-82c6-46110b30ce05') |
|
33
|
|
|
).thenResolve(instance(file)); |
|
34
|
|
|
when(localFileStorageAdapter.download(instance(file))).thenResolve( |
|
35
|
|
|
instance(buffer) |
|
36
|
|
|
); |
|
37
|
|
|
expect( |
|
38
|
|
|
await handler.execute( |
|
39
|
|
|
new DownloadFileQuery('cfdd06eb-cd71-44b9-82c6-46110b30ce05') |
|
40
|
|
|
) |
|
41
|
|
|
).toMatchObject( |
|
42
|
|
|
new DownloadedFileView( |
|
43
|
|
|
'file_mathieu_marchois.pdf', |
|
44
|
|
|
'application/pdf', |
|
45
|
|
|
instance(buffer) |
|
46
|
|
|
) |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
verify( |
|
50
|
|
|
fileRepository.findOneById('cfdd06eb-cd71-44b9-82c6-46110b30ce05') |
|
51
|
|
|
).once(); |
|
52
|
|
|
verify(localFileStorageAdapter.download(instance(file))).once(); |
|
53
|
|
|
verify(file.getOriginalName()).once(); |
|
54
|
|
|
verify(file.getMimeType()).once(); |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
it('testBufferNullable', async () => { |
|
58
|
|
|
const file = mock(File); |
|
59
|
|
|
when(file.getOriginalName()).thenReturn('file_mathieu_marchois.pdf'); |
|
60
|
|
|
when(file.getMimeType()).thenReturn('application/pdf'); |
|
61
|
|
|
|
|
62
|
|
|
when( |
|
63
|
|
|
fileRepository.findOneById('cfdd06eb-cd71-44b9-82c6-46110b30ce05') |
|
64
|
|
|
).thenResolve(instance(file)); |
|
65
|
|
|
when(localFileStorageAdapter.download(instance(file))).thenResolve(null); |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
|
|
await handler.execute( |
|
69
|
|
|
new DownloadFileQuery('cfdd06eb-cd71-44b9-82c6-46110b30ce05') |
|
70
|
|
|
); |
|
71
|
|
|
} catch (e) { |
|
72
|
|
|
expect(e).toBeInstanceOf(FileNotFoundException); |
|
73
|
|
|
expect(e.message).toBe('file.errors.not_found'); |
|
74
|
|
|
verify( |
|
75
|
|
|
fileRepository.findOneById('cfdd06eb-cd71-44b9-82c6-46110b30ce05') |
|
76
|
|
|
).once(); |
|
77
|
|
|
verify(file.getOriginalName()).never(); |
|
78
|
|
|
verify(file.getMimeType()).never(); |
|
79
|
|
|
verify(localFileStorageAdapter.download(instance(file))).once(); |
|
80
|
|
|
} |
|
81
|
|
|
}); |
|
82
|
|
|
|
|
83
|
|
|
it('testFileNotFound', async () => { |
|
84
|
|
|
when( |
|
85
|
|
|
fileRepository.findOneById('cfdd06eb-cd71-44b9-82c6-46110b30ce05') |
|
86
|
|
|
).thenResolve(null); |
|
87
|
|
|
|
|
88
|
|
|
try { |
|
89
|
|
|
await handler.execute( |
|
90
|
|
|
new DownloadFileQuery('cfdd06eb-cd71-44b9-82c6-46110b30ce05') |
|
91
|
|
|
); |
|
92
|
|
|
} catch (e) { |
|
93
|
|
|
expect(e).toBeInstanceOf(FileNotFoundException); |
|
94
|
|
|
expect(e.message).toBe('file.errors.not_found'); |
|
95
|
|
|
verify( |
|
96
|
|
|
fileRepository.findOneById('cfdd06eb-cd71-44b9-82c6-46110b30ce05') |
|
97
|
|
|
).once(); |
|
98
|
|
|
verify(localFileStorageAdapter.download(anything())).never(); |
|
99
|
|
|
} |
|
100
|
|
|
}); |
|
101
|
|
|
}); |
|
102
|
|
|
|