|
1
|
/** |
|
2
|
* Tests for the `acl` configuration option on S3FileService. |
|
3
|
*/ |
|
4
|
|
|
5
|
/* eslint-disable @typescript-eslint/no-var-requires */ |
|
6
|
var mockS3Send: jest.Mock |
|
7
|
|
|
8
|
jest.mock("@aws-sdk/client-s3", () => { |
|
9
|
const actual = jest.requireActual("@aws-sdk/client-s3") as typeof import("@aws-sdk/client-s3") |
|
10
|
mockS3Send = jest.fn().mockResolvedValue({}) |
|
11
|
return { |
|
12
|
...actual, |
|
13
|
S3Client: jest.fn().mockImplementation(() => ({ |
|
14
|
send: (...args: unknown[]) => mockS3Send(...args), |
|
15
|
})), |
|
16
|
} |
|
17
|
}) |
|
18
|
|
|
19
|
jest.mock("@aws-sdk/s3-request-presigner", () => ({ |
|
20
|
getSignedUrl: jest.fn().mockResolvedValue("https://bucket.s3.amazonaws.com/signed"), |
|
21
|
})) |
|
22
|
|
|
23
|
jest.mock("@aws-sdk/lib-storage", () => ({ |
|
24
|
Upload: jest.fn().mockImplementation(() => ({ |
|
25
|
done: jest.fn().mockResolvedValue(undefined), |
|
26
|
})), |
|
27
|
})) |
|
28
|
|
|
29
|
import { PutObjectCommand } from "@aws-sdk/client-s3" |
|
30
|
import { S3FileService } from "../s3-file" |
|
31
|
|
|
32
|
const baseOptions = { |
|
33
|
file_url: "https://media.example.com", |
|
34
|
access_key_id: "test", |
|
35
|
secret_access_key: "test", |
|
36
|
region: "us-east-1", |
|
37
|
bucket: "test-bucket", |
|
38
|
} |
|
39
|
|
|
40
|
const logger = { error: jest.fn(), warn: jest.fn(), info: jest.fn() } as any |
|
41
|
|
|
42
|
describe("S3FileService ACL option", () => { |
|
43
|
beforeEach(() => { |
|
44
|
mockS3Send.mockClear() |
|
45
|
}) |
|
46
|
|
|
47
|
it("defaults to public-read for public files when acl is not configured", async () => { |
|
48
|
const service = new S3FileService({ logger }, baseOptions) |
|
49
|
|
|
50
|
await service.upload({ |
|
51
|
filename: "test.png", |
|
52
|
mimeType: "image/png", |
|
53
|
content: Buffer.from("test").toString("base64"), |
|
54
|
access: "public", |
|
55
|
}) |
|
56
|
|
|
57
|
const command = mockS3Send.mock.calls[0][0] as PutObjectCommand |
|
58
|
expect(command.input.ACL).toBe("public-read") |
|
59
|
}) |
|
60
|
|
|
61
|
it("defaults to private when acl is not configured and access is private", async () => { |
|
62
|
const service = new S3FileService({ logger }, baseOptions) |
|
63
|
|
|
64
|
await service.upload({ |
|
65
|
filename: "test.png", |
|
66
|
mimeType: "image/png", |
|
67
|
content: Buffer.from("test").toString("base64"), |
|
68
|
access: "private", |
|
69
|
}) |
|
70
|
|
|
71
|
const command = mockS3Send.mock.calls[0][0] as PutObjectCommand |
|
72
|
expect(command.input.ACL).toBe("private") |
|
73
|
}) |
|
74
|
|
|
75
|
it("defaults to private when acl is not configured and access is undefined", async () => { |
|
76
|
const service = new S3FileService({ logger }, baseOptions) |
|
77
|
|
|
78
|
await service.upload({ |
|
79
|
filename: "test.png", |
|
80
|
mimeType: "image/png", |
|
81
|
content: Buffer.from("test").toString("base64"), |
|
82
|
} as any) |
|
83
|
|
|
84
|
const command = mockS3Send.mock.calls[0][0] as PutObjectCommand |
|
85
|
expect(command.input.ACL).toBe("private") |
|
86
|
}) |
|
87
|
|
|
88
|
it("omits ACL header when acl is set to false", async () => { |
|
89
|
const service = new S3FileService({ logger }, { |
|
90
|
...baseOptions, |
|
91
|
acl: false, |
|
92
|
} as any) |
|
93
|
|
|
94
|
await service.upload({ |
|
95
|
filename: "test.png", |
|
96
|
mimeType: "image/png", |
|
97
|
content: Buffer.from("test").toString("base64"), |
|
98
|
access: "public", |
|
99
|
}) |
|
100
|
|
|
101
|
const command = mockS3Send.mock.calls[0][0] as PutObjectCommand |
|
102
|
expect(command.input.ACL).toBeUndefined() |
|
103
|
}) |
|
104
|
|
|
105
|
it("uses the configured canned ACL for all uploads", async () => { |
|
106
|
const service = new S3FileService({ logger }, { |
|
107
|
...baseOptions, |
|
108
|
acl: "bucket-owner-full-control", |
|
109
|
} as any) |
|
110
|
|
|
111
|
await service.upload({ |
|
112
|
filename: "test.png", |
|
113
|
mimeType: "image/png", |
|
114
|
content: Buffer.from("test").toString("base64"), |
|
115
|
access: "public", |
|
116
|
}) |
|
117
|
|
|
118
|
const command = mockS3Send.mock.calls[0][0] as PutObjectCommand |
|
119
|
expect(command.input.ACL).toBe("bucket-owner-full-control") |
|
120
|
}) |
|
121
|
}) |