Skip to content

Commit e17cb4c

Browse files
committed
test(client-s3): convert feature test to vitest
1 parent c63c1e3 commit e17cb4c

File tree

2 files changed

+568
-0
lines changed

2 files changed

+568
-0
lines changed
Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
import { S3, waitUntilBucketExists, waitUntilBucketNotExists } from "@aws-sdk/client-s3";
2+
import { type GetCallerIdentityCommandOutput, STS } from "@aws-sdk/client-sts";
3+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
4+
5+
describe("@aws-sdk/client-s3 - Working with Buckets", () => {
6+
const config = {
7+
region: "us-west-2",
8+
};
9+
const s3 = new S3(config);
10+
const s3East = new S3({
11+
region: "us-east-1",
12+
});
13+
const stsClient = new STS(config);
14+
15+
function getBucketName(id: string) {
16+
const alphabet = "abcdefghijklmnopqrstuvwxyz";
17+
const randId = Array.from({ length: 6 }, () => alphabet[(Math.random() * alphabet.length) | 0]).join("");
18+
return `${callerID.Account}-${randId}-js-sdk-e2e-${config.region}-${(Date.now() / 1000) | 0}`;
19+
}
20+
21+
let Bucket: string;
22+
let callerID: GetCallerIdentityCommandOutput;
23+
24+
beforeAll(async () => {
25+
callerID = await stsClient.getCallerIdentity({});
26+
Bucket = getBucketName(`js-sdk-e2e`);
27+
});
28+
29+
describe("CRUD buckets using classic endpoint", () => {
30+
let bucketEast: string | undefined;
31+
32+
beforeAll(async () => {
33+
bucketEast = Bucket.replace("us-west-2", "us-east-1");
34+
});
35+
36+
afterAll(async () => {
37+
await s3East.deleteBucket({
38+
Bucket: bucketEast,
39+
});
40+
await waitUntilBucketNotExists(
41+
{
42+
client: s3East,
43+
maxWaitTime: 60,
44+
},
45+
{
46+
Bucket: bucketEast,
47+
}
48+
);
49+
});
50+
51+
it("should create and verify bucket in us-east-1", async () => {
52+
await s3East.createBucket({
53+
Bucket: bucketEast,
54+
});
55+
await waitUntilBucketExists(
56+
{
57+
client: s3East,
58+
maxWaitTime: 60,
59+
},
60+
{
61+
Bucket: bucketEast,
62+
}
63+
);
64+
await s3East.headBucket({ Bucket: bucketEast });
65+
});
66+
});
67+
68+
describe("CRUD buckets using regional endpoint", () => {
69+
afterAll(async () => {
70+
await s3.deleteBucket({
71+
Bucket,
72+
});
73+
await waitUntilBucketNotExists(
74+
{
75+
client: s3,
76+
maxWaitTime: 60,
77+
},
78+
{
79+
Bucket,
80+
}
81+
);
82+
});
83+
it("should create and verify bucket in us-west-2", async () => {
84+
await s3.createBucket({
85+
Bucket,
86+
});
87+
await waitUntilBucketExists(
88+
{
89+
client: s3,
90+
maxWaitTime: 60,
91+
},
92+
{
93+
Bucket,
94+
}
95+
);
96+
await s3.headBucket({ Bucket });
97+
});
98+
});
99+
100+
describe("Bucket CORS", () => {
101+
let corsBucket: string;
102+
103+
beforeAll(async () => {
104+
corsBucket = getBucketName("cors");
105+
});
106+
107+
afterAll(async () => {
108+
await s3.deleteBucket({
109+
Bucket: corsBucket,
110+
});
111+
});
112+
113+
it("should configure and verify CORS settings", async () => {
114+
await s3.createBucket({
115+
Bucket: corsBucket,
116+
});
117+
await waitUntilBucketExists(
118+
{
119+
client: s3,
120+
maxWaitTime: 60,
121+
},
122+
{
123+
Bucket: corsBucket,
124+
}
125+
);
126+
127+
await s3.putBucketCors({
128+
Bucket: corsBucket,
129+
CORSConfiguration: {
130+
CORSRules: [
131+
{
132+
AllowedMethods: ["DELETE", "POST", "PUT"],
133+
AllowedOrigins: ["http://example.com"],
134+
AllowedHeaders: ["*"],
135+
ExposeHeaders: ["x-amz-server-side-encryption"],
136+
MaxAgeSeconds: 5000,
137+
},
138+
],
139+
},
140+
});
141+
const getBucketCors = await s3.getBucketCors({
142+
Bucket: corsBucket,
143+
});
144+
const corsConfig = getBucketCors.CORSRules?.[0];
145+
146+
expect(corsConfig?.AllowedMethods).toContain("DELETE");
147+
expect(corsConfig?.AllowedMethods).toContain("POST");
148+
expect(corsConfig?.AllowedMethods).toContain("PUT");
149+
expect(corsConfig?.AllowedOrigins?.[0]).toBe("http://example.com");
150+
expect(corsConfig?.AllowedHeaders?.[0]).toBe("*");
151+
expect(corsConfig?.ExposeHeaders?.[0]).toBe("x-amz-server-side-encryption");
152+
expect(corsConfig?.MaxAgeSeconds).toBe(5000);
153+
});
154+
});
155+
156+
describe("Bucket lifecycles", () => {
157+
let lifecycleBucket: string;
158+
159+
beforeAll(async () => {
160+
lifecycleBucket = getBucketName("lifecyc");
161+
});
162+
163+
afterAll(async () => {
164+
await s3.deleteBucket({
165+
Bucket: lifecycleBucket,
166+
});
167+
});
168+
169+
it("should configure and verify lifecycle rules", async () => {
170+
await s3.createBucket({
171+
Bucket: lifecycleBucket,
172+
});
173+
await waitUntilBucketExists(
174+
{
175+
client: s3,
176+
maxWaitTime: 60,
177+
},
178+
{
179+
Bucket: lifecycleBucket,
180+
}
181+
);
182+
await s3.putBucketLifecycleConfiguration({
183+
Bucket: lifecycleBucket,
184+
LifecycleConfiguration: {
185+
Rules: [
186+
{
187+
Filter: {
188+
Prefix: "/",
189+
},
190+
Status: "Enabled",
191+
Transitions: [
192+
{
193+
Days: 0,
194+
StorageClass: "GLACIER",
195+
},
196+
],
197+
},
198+
],
199+
},
200+
});
201+
const lcConfig = await s3.getBucketLifecycleConfiguration({
202+
Bucket: lifecycleBucket,
203+
});
204+
205+
expect(lcConfig?.Rules?.[0]?.Transitions?.[0]?.Days).toBe(0);
206+
expect(lcConfig?.Rules?.[0]?.Transitions?.[0]?.StorageClass).toBe("GLACIER");
207+
});
208+
});
209+
210+
// describe("Bucket Tagging", () => {
211+
// let s3Client: S3Client;
212+
//
213+
// it("should set and verify bucket tags", async () => {
214+
// const bucket = await createBucket(s3Client);
215+
// await putBucketTag(s3Client, bucket, "KEY", "XXXXX");
216+
// const tags = await getBucketTags(s3Client, bucket);
217+
//
218+
// expect(tags[0]).toEqual({
219+
// Key: "KEY",
220+
// Value: "VALUE",
221+
// });
222+
// });
223+
// });
224+
225+
// describe("Access bucket following 307 redirects", () => {
226+
// let s3Client: S3Client;
227+
//
228+
// it("should handle bucket creation with location constraint", async () => {
229+
// s3Client = new S3Client({
230+
// region: "us-east-1",
231+
// signatureVersion: "s3",
232+
// });
233+
//
234+
// const bucket = await createBucketWithLocation(s3Client, "XXXXXXXXX");
235+
// expect(await bucketExistsInRegion(s3Client, bucket, "XXXXXXXXX")).toBe(true);
236+
// expect(await getBucketLocation(s3Client, bucket)).toBe("XXXXXXXXX");
237+
// await deleteBucketInRegion(s3Client, bucket, "XXXXXXXXX");
238+
// });
239+
// });
240+
//
241+
// describe("Working with bucket names containing dots", () => {
242+
// let s3Client: S3Client;
243+
//
244+
// it("should create bucket with DNS compatible dotted name", async () => {
245+
// const bucket = await createBucketWithDottedName(s3Client);
246+
// expect(await bucketExists(s3Client, bucket)).toBe(true);
247+
// // await deleteBucket(s3Client, bucket);
248+
// // expect(await bucketExists(s3Client, bucket)).toBe(false);
249+
// });
250+
// });
251+
//
252+
// describe("Operating on bucket using path style", () => {
253+
// let s3Client: S3Client;
254+
//
255+
// it("should use path style addressing", async () => {
256+
// s3Client = new S3Client({ forcePathStyle: true });
257+
// const bucket = await createBucket(s3Client);
258+
//
259+
// await putObject(s3Client, bucket, "hello", "abc");
260+
// const obj = await getObject(s3Client, bucket, "hello");
261+
//
262+
// expect(obj.request.path).toContain(bucket);
263+
// expect(obj.request.hostname).not.toContain(bucket);
264+
//
265+
// await deleteObject(s3Client, bucket, "hello");
266+
// });
267+
// });
268+
269+
// Known bug: https://github.com/aws/aws-sdk-js-v3/issues/1802
270+
/*
271+
describe('Follow 307 redirect on new buckets', () => {
272+
let s3Client: S3Client;
273+
274+
it('should handle redirects with large objects', async () => {
275+
s3Client = new S3Client({
276+
region: 'us-east-1',
277+
signatureVersion: 's3'
278+
});
279+
280+
const bucket = await createBucketWithLocation(s3Client, 'XXXXXXXXX');
281+
await putLargeObject(s3Client, bucket, 'largeobject');
282+
expect(await objectExists(s3Client, bucket, 'largeobject')).toBe(true);
283+
await deleteObject(s3Client, bucket, 'largeobject');
284+
await deleteBucket(s3Client, bucket);
285+
});
286+
});
287+
*/
288+
}, 60_000);

0 commit comments

Comments
 (0)