|
1
|
import axios from "axios" |
|
2
|
import fs from "fs/promises" |
|
3
|
import { S3FileService } from "../../src/services/s3-file" |
|
4
|
jest.setTimeout(100000) |
|
5
|
|
|
6
|
// Note: This test hits the S3 service, and it is mainly meant to be run manually after setting all the envvars below. |
|
7
|
// We can also set up some test buckets in our pipeline to run this test, but it is not really that important to do so for now. |
|
8
|
describe.skip("S3 File Plugin", () => { |
|
9
|
let s3Service: S3FileService |
|
10
|
let fixtureImagePath: string |
|
11
|
beforeAll(() => { |
|
12
|
fixtureImagePath = |
|
13
|
process.cwd() + "/integration-tests/__fixtures__/catphoto.jpg" |
|
14
|
|
|
15
|
s3Service = new S3FileService( |
|
16
|
{ |
|
17
|
logger: console as any, |
|
18
|
}, |
|
19
|
{ |
|
20
|
endpoint: process.env.S3_TEST_ENDPOINT ?? "", |
|
21
|
file_url: process.env.S3_TEST_FILE_URL ?? "", |
|
22
|
access_key_id: process.env.S3_TEST_ACCESS_KEY_ID ?? "", |
|
23
|
secret_access_key: process.env.S3_TEST_SECRET_ACCESS_KEY ?? "", |
|
24
|
region: process.env.S3_TEST_REGION ?? "", |
|
25
|
bucket: process.env.S3_TEST_BUCKET ?? "", |
|
26
|
prefix: "tests/", |
|
27
|
additional_client_config: process.env.S3_TEST_ENDPOINT?.includes( |
|
28
|
"localhost" |
|
29
|
) |
|
30
|
? { |
|
31
|
sslEnabled: false, |
|
32
|
s3ForcePathStyle: true, |
|
33
|
} |
|
34
|
: {}, |
|
35
|
} |
|
36
|
) |
|
37
|
}) |
|
38
|
;(["public", "private"] as const).forEach((access) => { |
|
39
|
it("uploads, reads, and then deletes a file successfully", async () => { |
|
40
|
const fileContent = await fs.readFile(fixtureImagePath) |
|
41
|
const fixtureAsBinary = fileContent.toString("binary") |
|
42
|
|
|
43
|
const resp = await s3Service.upload({ |
|
44
|
filename: "catphoto.jpg", |
|
45
|
mimeType: "image/jpeg", |
|
46
|
content: fixtureAsBinary, |
|
47
|
access, |
|
48
|
}) |
|
49
|
|
|
50
|
expect(resp).toEqual({ |
|
51
|
key: expect.stringMatching(/tests\/catphoto.*\.jpg/), |
|
52
|
url: expect.stringMatching(/https?:\/\/.*\.jpg/), |
|
53
|
}) |
|
54
|
|
|
55
|
const urlResp = await axios.get(resp.url).catch((e) => e.response) |
|
56
|
expect(urlResp.status).toEqual(access === "public" ? 200 : 403) |
|
57
|
|
|
58
|
const signedUrl = await s3Service.getPresignedDownloadUrl({ |
|
59
|
fileKey: resp.key, |
|
60
|
}) |
|
61
|
|
|
62
|
const signedUrlFile = Buffer.from( |
|
63
|
await axios |
|
64
|
.get(signedUrl, { responseType: "arraybuffer" }) |
|
65
|
.then((r) => r.data) |
|
66
|
) |
|
67
|
|
|
68
|
expect(signedUrlFile.toString("binary")).toEqual(fixtureAsBinary) |
|
69
|
|
|
70
|
await s3Service.delete({ fileKey: resp.key }) |
|
71
|
|
|
72
|
// TODO: Currently the presignedURL will be returned even if the file doesn't exist. Should we check for existence first? |
|
73
|
const deletedFileUrl = await s3Service.getPresignedDownloadUrl({ |
|
74
|
fileKey: resp.key, |
|
75
|
}) |
|
76
|
|
|
77
|
const { response } = await axios |
|
78
|
.get(deletedFileUrl, { responseType: "arraybuffer" }) |
|
79
|
.catch((e) => e) |
|
80
|
|
|
81
|
expect(response.status).toEqual(404) |
|
82
|
}) |
|
83
|
}) |
|
84
|
|
|
85
|
it("uploads a file with non-ascii characters in the name", async () => { |
|
86
|
const fileContent = await fs.readFile(fixtureImagePath) |
|
87
|
const fixtureAsBinary = fileContent.toString("base64") |
|
88
|
|
|
89
|
const resp = await s3Service.upload({ |
|
90
|
filename: "catphoto-か.jpg", |
|
91
|
mimeType: "image/jpeg", |
|
92
|
content: fixtureAsBinary, |
|
93
|
access: "private", |
|
94
|
}) |
|
95
|
|
|
96
|
expect(resp).toEqual({ |
|
97
|
key: expect.stringMatching(/tests\/catphoto-か.*\.jpg/), |
|
98
|
url: expect.stringMatching(/https?:\/\/.*\/catphoto-%E3%81%8B.*\.jpg/), |
|
99
|
}) |
|
100
|
}) |
|
101
|
|
|
102
|
it("uploads a file with special URL characters in the name", async () => { |
|
103
|
const fileContent = await fs.readFile(fixtureImagePath) |
|
104
|
const fixtureAsBinary = fileContent.toString("base64") |
|
105
|
|
|
106
|
const resp = await s3Service.upload({ |
|
107
|
filename: "cat?photo.jpg", |
|
108
|
mimeType: "image/jpeg", |
|
109
|
content: fixtureAsBinary, |
|
110
|
access: "private", |
|
111
|
}) |
|
112
|
|
|
113
|
expect(resp).toEqual({ |
|
114
|
key: expect.stringMatching(/tests\/catphoto.*\.jpg/), |
|
115
|
url: expect.stringMatching(/https?:\/\/.*\/cat%3Fphoto.*\.jpg/), |
|
116
|
}) |
|
117
|
}) |
|
118
|
|
|
119
|
it("gets a presigned upload URL and uploads a file successfully", async () => { |
|
120
|
const fileContent = await fs.readFile(fixtureImagePath) |
|
121
|
const fixtureAsBinary = fileContent.toString("binary") |
|
122
|
|
|
123
|
const resp = await s3Service.getPresignedUploadUrl({ |
|
124
|
filename: "catphoto.jpg", |
|
125
|
mimeType: "image/jpeg", |
|
126
|
access: "private", |
|
127
|
}) |
|
128
|
|
|
129
|
expect(resp).toEqual({ |
|
130
|
key: expect.stringMatching(/tests\/catphoto.*\.jpg/), |
|
131
|
url: expect.stringMatching(/https?:\/\/.*catphoto\.jpg/), |
|
132
|
}) |
|
133
|
|
|
134
|
const uploadResp = await axios.put(resp.url, fileContent, { |
|
135
|
headers: { |
|
136
|
// On Digitalocean, among others, despite the ACL set on the upload URL, the caller can set the acl to anything they want. |
|
137
|
// On AWS passing the ACL in the upload will fail since it's set on the signed URL. |
|
138
|
// "x-amz-acl": "private", |
|
139
|
"Content-Type": "image/jpeg", |
|
140
|
}, |
|
141
|
}) |
|
142
|
|
|
143
|
expect(uploadResp.status).toEqual(200) |
|
144
|
|
|
145
|
const signedUrl = await s3Service.getPresignedDownloadUrl({ |
|
146
|
fileKey: resp.key, |
|
147
|
}) |
|
148
|
|
|
149
|
const signedUrlFile = Buffer.from( |
|
150
|
await axios |
|
151
|
.get(signedUrl, { responseType: "arraybuffer" }) |
|
152
|
.then((r) => r.data) |
|
153
|
) |
|
154
|
|
|
155
|
expect(signedUrlFile.toString("binary")).toEqual(fixtureAsBinary) |
|
156
|
|
|
157
|
await s3Service.delete({ fileKey: resp.key }) |
|
158
|
}) |
|
159
|
|
|
160
|
it("gets a presigned upload URL for a nested filename structure and uploads a file successfully", async () => { |
|
161
|
const fileContent = await fs.readFile(fixtureImagePath) |
|
162
|
const fixtureAsBinary = fileContent.toString("binary") |
|
163
|
|
|
164
|
const resp = await s3Service.getPresignedUploadUrl({ |
|
165
|
filename: "testfolder/catphoto.jpg", |
|
166
|
mimeType: "image/jpeg", |
|
167
|
access: "private", |
|
168
|
}) |
|
169
|
|
|
170
|
expect(resp).toEqual({ |
|
171
|
key: expect.stringMatching(/tests\/testfolder\/catphoto.*\.jpg/), |
|
172
|
url: expect.stringMatching(/https?:\/\/.*testfolder\/catphoto\.jpg/), |
|
173
|
}) |
|
174
|
|
|
175
|
const uploadResp = await axios.put(resp.url, fileContent, { |
|
176
|
headers: { |
|
177
|
"Content-Type": "image/jpeg", |
|
178
|
}, |
|
179
|
}) |
|
180
|
|
|
181
|
expect(uploadResp.status).toEqual(200) |
|
182
|
|
|
183
|
const signedUrl = await s3Service.getPresignedDownloadUrl({ |
|
184
|
fileKey: resp.key, |
|
185
|
}) |
|
186
|
|
|
187
|
const signedUrlFile = Buffer.from( |
|
188
|
await axios |
|
189
|
.get(signedUrl, { responseType: "arraybuffer" }) |
|
190
|
.then((r) => r.data) |
|
191
|
) |
|
192
|
|
|
193
|
expect(signedUrlFile.toString("binary")).toEqual(fixtureAsBinary) |
|
194
|
|
|
195
|
await s3Service.delete({ fileKey: resp.key }) |
|
196
|
}) |
|
197
|
|
|
198
|
it("deletes multiple files in bulk", async () => { |
|
199
|
const fileContent = await fs.readFile(fixtureImagePath) |
|
200
|
const fixtureAsBinary = fileContent.toString("binary") |
|
201
|
|
|
202
|
const cat = await s3Service.upload({ |
|
203
|
filename: "catphoto.jpg", |
|
204
|
mimeType: "image/jpeg", |
|
205
|
content: fixtureAsBinary, |
|
206
|
}) |
|
207
|
const cat1 = await s3Service.upload({ |
|
208
|
filename: "catphoto-1.jpg", |
|
209
|
mimeType: "image/jpeg", |
|
210
|
content: fixtureAsBinary, |
|
211
|
}) |
|
212
|
const cat2 = await s3Service.upload({ |
|
213
|
filename: "catphoto-2.jpg", |
|
214
|
mimeType: "image/jpeg", |
|
215
|
content: fixtureAsBinary, |
|
216
|
}) |
|
217
|
|
|
218
|
await s3Service.delete([ |
|
219
|
{ fileKey: cat.key }, |
|
220
|
{ fileKey: cat1.key }, |
|
221
|
{ fileKey: cat2.key }, |
|
222
|
]) |
|
223
|
}) |
|
224
|
|
|
225
|
it("uploads using stream", async () => { |
|
226
|
const fileContent = await fs.readFile(fixtureImagePath) |
|
227
|
const fixtureAsBinary = fileContent.toString("binary") |
|
228
|
|
|
229
|
const { writeStream, promise } = await s3Service.getUploadStream({ |
|
230
|
filename: "catphoto-stream.jpg", |
|
231
|
mimeType: "image/jpeg", |
|
232
|
access: "public", |
|
233
|
}) |
|
234
|
|
|
235
|
writeStream.write(fileContent) |
|
236
|
writeStream.end() |
|
237
|
|
|
238
|
const resp = await promise |
|
239
|
|
|
240
|
expect(resp).toEqual({ |
|
241
|
key: expect.stringMatching(/tests\/catphoto-stream.*\.jpg/), |
|
242
|
url: expect.stringMatching(/https?:\/\/.*\.jpg/), |
|
243
|
}) |
|
244
|
|
|
245
|
const urlResp = await axios.get(resp.url).catch((e) => e.response) |
|
246
|
expect(urlResp.status).toEqual(200) |
|
247
|
|
|
248
|
const signedUrl = await s3Service.getPresignedDownloadUrl({ |
|
249
|
fileKey: resp.key, |
|
250
|
}) |
|
251
|
|
|
252
|
const signedUrlFile = Buffer.from( |
|
253
|
await axios |
|
254
|
.get(signedUrl, { responseType: "arraybuffer" }) |
|
255
|
.then((r) => r.data) |
|
256
|
) |
|
257
|
|
|
258
|
expect(signedUrlFile.toString("binary")).toEqual(fixtureAsBinary) |
|
259
|
|
|
260
|
await s3Service.delete({ fileKey: resp.key }) |
|
261
|
}) |
|
262
|
}) |