-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDirectUploadClient.test.ts
More file actions
250 lines (195 loc) · 9.45 KB
/
DirectUploadClient.test.ts
File metadata and controls
250 lines (195 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import {
createMultipartFileBlob,
createSinglepartFileBlob
} from '../../testHelpers/files/filesHelper'
import { IFilesRepository } from '../../../src/files/domain/repositories/IFilesRepository'
import { ApiConfig, ReadError } from '../../../src'
import { DirectUploadClient } from '../../../src/files/infra/clients/DirectUploadClient'
import { UrlGenerationError } from '../../../src/files/infra/clients/errors/UrlGenerationError'
import {
createMultipartFileUploadDestinationModel,
createSingleFileUploadDestinationModel
} from '../../testHelpers/files/fileUploadDestinationHelper'
import axios from 'axios'
import { FileUploadError } from '../../../src/files/infra/clients/errors/FileUploadError'
import { MultipartCompletionError } from '../../../src/files/infra/clients/errors/MultipartCompletionError'
import { FilePartUploadError } from '../../../src/files/infra/clients/errors/FilePartUploadError'
import { MultipartAbortError } from '../../../src/files/infra/clients/errors/MultipartAbortError'
import { TestConstants } from '../../testHelpers/TestConstants'
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
import { FileUploadDestination } from '../../../src/files/domain/models/FileUploadDestination'
describe('uploadFile', () => {
beforeEach(() => {
ApiConfig.init(
TestConstants.TEST_API_URL,
DataverseApiAuthMechanism.API_KEY,
TestConstants.TEST_DUMMY_API_KEY
)
jest.clearAllMocks()
})
describe('Single part file', () => {
let testFile: File
beforeAll(async () => {
testFile = await createSinglepartFileBlob()
})
test('should return UrlGenerationError when repository raises error on URL generation', async () => {
const filesRepositoryStub: IFilesRepository = {} as IFilesRepository
filesRepositoryStub.getFileUploadDestination = jest
.fn()
.mockRejectedValue(new ReadError('test'))
const sut = new DirectUploadClient(filesRepositoryStub)
const progressMock = jest.fn()
const abortController = new AbortController()
await expect(sut.uploadFile(1, testFile, progressMock, abortController)).rejects.toThrow(
UrlGenerationError
)
})
test('should return FileUploadError when there is an error on single file upload', async () => {
const filesRepositoryStub: IFilesRepository = {} as IFilesRepository
filesRepositoryStub.getFileUploadDestination = jest
.fn()
.mockResolvedValue(createSingleFileUploadDestinationModel())
jest.spyOn(axios, 'put').mockRejectedValue('error')
const sut = new DirectUploadClient(filesRepositoryStub)
const progressMock = jest.fn()
const abortController = new AbortController()
await expect(sut.uploadFile(1, testFile, progressMock, abortController)).rejects.toThrow(
FileUploadError
)
})
test('should storage identifier on operation success', async () => {
const filesRepositoryStub: IFilesRepository = {} as IFilesRepository
const testDestination: FileUploadDestination = createSingleFileUploadDestinationModel()
filesRepositoryStub.getFileUploadDestination = jest.fn().mockResolvedValue(testDestination)
filesRepositoryStub.addUploadedFilesToDataset = jest.fn().mockResolvedValue(undefined)
jest.spyOn(axios, 'put').mockResolvedValue(undefined)
const sut = new DirectUploadClient(filesRepositoryStub)
const progressMock = jest.fn()
const abortController = new AbortController()
const actual = await sut.uploadFile(1, testFile, progressMock, abortController)
expect(actual).toEqual(testDestination.storageId)
})
test('should include S3 tagging header by default', async () => {
const filesRepositoryStub: IFilesRepository = {} as IFilesRepository
const testDestination: FileUploadDestination = createSingleFileUploadDestinationModel()
filesRepositoryStub.getFileUploadDestination = jest.fn().mockResolvedValue(testDestination)
const axiosPutSpy = jest.spyOn(axios, 'put').mockResolvedValue(undefined)
const sut = new DirectUploadClient(filesRepositoryStub)
const progressMock = jest.fn()
const abortController = new AbortController()
await sut.uploadFile(1, testFile, progressMock, abortController)
expect(axiosPutSpy).toHaveBeenCalledWith(
testDestination.urls[0],
expect.anything(),
expect.objectContaining({
headers: expect.objectContaining({
'x-amz-tagging': 'dv-state=temp'
})
})
)
})
test('should not include S3 tagging header when useS3Tagging is false', async () => {
const filesRepositoryStub: IFilesRepository = {} as IFilesRepository
const testDestination: FileUploadDestination = createSingleFileUploadDestinationModel()
filesRepositoryStub.getFileUploadDestination = jest.fn().mockResolvedValue(testDestination)
const axiosPutSpy = jest.spyOn(axios, 'put').mockResolvedValue(undefined)
const sut = new DirectUploadClient(filesRepositoryStub, { useS3Tagging: false })
const progressMock = jest.fn()
const abortController = new AbortController()
await sut.uploadFile(1, testFile, progressMock, abortController)
expect(axiosPutSpy).toHaveBeenCalledWith(
testDestination.urls[0],
expect.anything(),
expect.objectContaining({
headers: expect.not.objectContaining({
'x-amz-tagging': expect.anything()
})
})
)
})
})
describe('Multiple parts file', () => {
let testFile: File
const successfulPartResponse = {
data: {},
status: 200,
statusText: 'OK',
headers: { etag: 'test' },
config: {}
}
beforeAll(async () => {
testFile = await createMultipartFileBlob()
})
test('should return FilePartUploadError when there is an error on multipart file upload and abort endpoint call works', async () => {
const filesRepositoryStub: IFilesRepository = {} as IFilesRepository
filesRepositoryStub.getFileUploadDestination = jest
.fn()
.mockResolvedValue(createMultipartFileUploadDestinationModel())
jest.spyOn(axios, 'delete').mockResolvedValue(undefined)
jest.spyOn(axios, 'put').mockRejectedValue('error')
const sut = new DirectUploadClient(filesRepositoryStub, { maxMultipartRetries: 1 })
const progressMock = jest.fn()
const abortController = new AbortController()
await expect(sut.uploadFile(1, testFile, progressMock, abortController)).rejects.toThrow(
FilePartUploadError
)
expect(axios.delete).toHaveBeenCalledWith(
`${ApiConfig.dataverseApiUrl}/datasets/mpupload/testAbort`,
{
headers: { 'Content-Type': 'application/json', 'X-Dataverse-Key': 'dummyApiKey' },
params: {}
}
)
})
test('should return MultipartAbortError when there is an error on multipart file upload and abort endpoint call fails', async () => {
const filesRepositoryStub: IFilesRepository = {} as IFilesRepository
filesRepositoryStub.getFileUploadDestination = jest
.fn()
.mockResolvedValue(createMultipartFileUploadDestinationModel())
jest.spyOn(axios, 'delete').mockRejectedValue('error')
jest.spyOn(axios, 'put').mockRejectedValue('error')
const progressMock = jest.fn()
const abortController = new AbortController()
const sut = new DirectUploadClient(filesRepositoryStub, { maxMultipartRetries: 1 })
await expect(sut.uploadFile(1, testFile, progressMock, abortController)).rejects.toThrow(
MultipartAbortError
)
})
test('should return MultipartCompletionError when there is an error on multipart file completion', async () => {
const filesRepositoryStub: IFilesRepository = {} as IFilesRepository
filesRepositoryStub.getFileUploadDestination = jest
.fn()
.mockResolvedValue(createMultipartFileUploadDestinationModel())
jest
.spyOn(axios, 'put')
.mockResolvedValueOnce(successfulPartResponse)
.mockResolvedValueOnce(successfulPartResponse)
.mockRejectedValueOnce(new Error('Third call failed'))
const progressMock = jest.fn()
const abortController = new AbortController()
const sut = new DirectUploadClient(filesRepositoryStub, { maxMultipartRetries: 1 })
await expect(sut.uploadFile(1, testFile, progressMock, abortController)).rejects.toThrow(
MultipartCompletionError
)
expect(axios.put).toHaveBeenCalledTimes(3)
})
test('should return storage identifier on operation success', async () => {
const testMultipartDestination = createMultipartFileUploadDestinationModel()
const filesRepositoryStub: IFilesRepository = {} as IFilesRepository
filesRepositoryStub.getFileUploadDestination = jest
.fn()
.mockResolvedValue(testMultipartDestination)
filesRepositoryStub.addUploadedFilesToDataset = jest.fn().mockResolvedValue(undefined)
jest
.spyOn(axios, 'put')
.mockResolvedValueOnce(successfulPartResponse)
.mockResolvedValueOnce(successfulPartResponse)
.mockResolvedValueOnce(undefined)
const sut = new DirectUploadClient(filesRepositoryStub, { maxMultipartRetries: 1 })
const progressMock = jest.fn()
const abortController = new AbortController()
const actual = await sut.uploadFile(1, testFile, progressMock, abortController)
expect(actual).toEqual(testMultipartDestination.storageId)
})
})
})