Skip to content

Commit a796cf5

Browse files
committed
test(core): new integration tests
1 parent 9c6a29e commit a796cf5

File tree

7 files changed

+444
-8
lines changed

7 files changed

+444
-8
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { requireRequestsFrom } from "@aws-sdk/aws-util-test/src";
2+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
3+
import { AwsCredentialIdentity } from "@smithy/types";
4+
import { afterEach, beforeEach, describe, expect, test as it } from "vitest";
5+
6+
describe("account id endpoint", () => {
7+
const credentialsWithAccountId: AwsCredentialIdentity = {
8+
accessKeyId: "INTEG_TEST",
9+
secretAccessKey: "INTEG_TEST",
10+
accountId: "123456789012",
11+
};
12+
const credentials = {
13+
accessKeyId: "INTEG_TEST",
14+
secretAccessKey: "INTEG_TEST",
15+
};
16+
17+
beforeEach(async () => {
18+
delete process.env.AWS_ACCOUNT_ID_ENDPOINT_MODE;
19+
});
20+
afterEach(async () => {
21+
delete process.env.AWS_ACCOUNT_ID_ENDPOINT_MODE;
22+
});
23+
24+
describe("when credentials have account id", () => {
25+
it("should default to resolving endpoint with account id when it is available in the client credentials", async () => {
26+
const ddb = new DynamoDB({
27+
region: "us-west-2",
28+
credentials: credentialsWithAccountId,
29+
});
30+
requireRequestsFrom(ddb).toMatch({
31+
hostname: /123456789012.ddb.us-west-2.amazonaws.com/,
32+
});
33+
await ddb.listTables();
34+
});
35+
36+
describe("config values", () => {
37+
it("disabled", async () => {
38+
const ddb = new DynamoDB({
39+
region: "us-west-2",
40+
credentials: credentialsWithAccountId,
41+
accountIdEndpointMode: "disabled",
42+
});
43+
requireRequestsFrom(ddb).toMatch({
44+
hostname: /dynamodb.us-west-2.amazonaws.com/,
45+
});
46+
await ddb.listTables();
47+
});
48+
it("preferred", async () => {
49+
const ddb = new DynamoDB({
50+
region: "us-west-2",
51+
credentials: credentialsWithAccountId,
52+
accountIdEndpointMode: "preferred",
53+
});
54+
requireRequestsFrom(ddb).toMatch({
55+
hostname: /123456789012.ddb.us-west-2.amazonaws.com/,
56+
});
57+
await ddb.listTables();
58+
});
59+
it("required", async () => {
60+
const ddb = new DynamoDB({
61+
region: "us-west-2",
62+
credentials: credentialsWithAccountId,
63+
accountIdEndpointMode: "required",
64+
});
65+
requireRequestsFrom(ddb).toMatch({
66+
hostname: /123456789012.ddb.us-west-2.amazonaws.com/,
67+
});
68+
await ddb.listTables();
69+
});
70+
});
71+
72+
describe("ENV values", () => {
73+
it("disabled", async () => {
74+
process.env.AWS_ACCOUNT_ID_ENDPOINT_MODE = "disabled";
75+
const ddb = new DynamoDB({
76+
region: "us-west-2",
77+
credentials: credentialsWithAccountId,
78+
});
79+
requireRequestsFrom(ddb).toMatch({
80+
hostname: /dynamodb.us-west-2.amazonaws.com/,
81+
});
82+
await ddb.listTables();
83+
});
84+
it("preferred", async () => {
85+
process.env.AWS_ACCOUNT_ID_ENDPOINT_MODE = "preferred";
86+
const ddb = new DynamoDB({
87+
region: "us-west-2",
88+
credentials: credentialsWithAccountId,
89+
});
90+
requireRequestsFrom(ddb).toMatch({
91+
hostname: /123456789012.ddb.us-west-2.amazonaws.com/,
92+
});
93+
await ddb.listTables();
94+
});
95+
it("required", async () => {
96+
process.env.AWS_ACCOUNT_ID_ENDPOINT_MODE = "required";
97+
const ddb = new DynamoDB({
98+
region: "us-west-2",
99+
credentials: credentialsWithAccountId,
100+
});
101+
requireRequestsFrom(ddb).toMatch({
102+
hostname: /123456789012.ddb.us-west-2.amazonaws.com/,
103+
});
104+
await ddb.listTables();
105+
});
106+
});
107+
});
108+
109+
describe("when credentials do not have account id", () => {
110+
it("it follows that the hostname will not have it either", async () => {
111+
const ddb = new DynamoDB({
112+
region: "us-west-2",
113+
credentials: credentials,
114+
});
115+
requireRequestsFrom(ddb).toMatch({
116+
hostname: /dynamodb.us-west-2.amazonaws.com/,
117+
});
118+
await ddb.listTables();
119+
});
120+
121+
it("will fail if required by ENV", async () => {
122+
process.env.AWS_ACCOUNT_ID_ENDPOINT_MODE = "required";
123+
const ddb = new DynamoDB({
124+
region: "us-west-2",
125+
credentials: credentials,
126+
});
127+
const error = await ddb.listTables().catch((e) => e);
128+
expect(error.name).toEqual("EndpointError");
129+
});
130+
131+
it("will fail if required by config", async () => {
132+
const ddb = new DynamoDB({
133+
region: "us-west-2",
134+
credentials: credentials,
135+
accountIdEndpointMode: "required",
136+
});
137+
const error = await ddb.listTables().catch((e) => e);
138+
expect(error.name).toEqual("EndpointError");
139+
});
140+
});
141+
});
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { requireRequestsFrom } from "@aws-sdk/aws-util-test/src";
2+
import { DynamoDB, paginateScan, ScanCommandInput } from "@aws-sdk/client-dynamodb";
3+
import { HttpResponse } from "@smithy/protocol-http";
4+
import { describe, expect, test as it } from "vitest";
5+
6+
describe("pagination", () => {
7+
it("makes sequential requests using a pagination token", async () => {
8+
const ddb = new DynamoDB({
9+
credentials: {
10+
accessKeyId: "INTEG_TEST",
11+
secretAccessKey: "INTEG_TEST",
12+
},
13+
region: "us-west-2",
14+
});
15+
16+
requireRequestsFrom(ddb)
17+
.toMatch(
18+
{
19+
hostname: /dynamodb/,
20+
body(b) {
21+
expect(b).toContain("TableName");
22+
expect(b).not.toContain("ExclusiveStartKey");
23+
},
24+
},
25+
{
26+
hostname: /dynamodb/,
27+
body: /ExclusiveStartKey/,
28+
}
29+
)
30+
.respondWith(
31+
new HttpResponse({
32+
statusCode: 200,
33+
headers: {},
34+
body: Buffer.from(
35+
JSON.stringify({
36+
Items: [
37+
{
38+
id: { S: "1" },
39+
name: { S: "Item 1" },
40+
},
41+
{
42+
id: { S: "2" },
43+
name: { S: "Item 2" },
44+
},
45+
],
46+
Count: 2,
47+
ScannedCount: 2,
48+
LastEvaluatedKey: {
49+
id: { S: "2" },
50+
},
51+
})
52+
),
53+
}),
54+
new HttpResponse({
55+
statusCode: 200,
56+
headers: {},
57+
body: Buffer.from(
58+
JSON.stringify({
59+
Items: [
60+
{
61+
id: { S: "3" },
62+
name: { S: "Item 3" },
63+
},
64+
{
65+
id: { S: "4" },
66+
name: { S: "Item 4" },
67+
},
68+
],
69+
Count: 2,
70+
ScannedCount: 2,
71+
})
72+
),
73+
})
74+
);
75+
76+
const requestParams: ScanCommandInput = {
77+
TableName: "test",
78+
};
79+
80+
let pages = 0;
81+
for await (const page of paginateScan({ client: ddb }, requestParams)) {
82+
void page;
83+
pages += 1;
84+
}
85+
86+
expect(pages).toEqual(2);
87+
expect(requestParams.ExclusiveStartKey).toEqual({
88+
id: { S: "2" },
89+
});
90+
expect.assertions(7);
91+
});
92+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { requireRequestsFrom } from "@aws-sdk/aws-util-test/src";
2+
import { Schemas } from "@aws-sdk/client-schemas";
3+
import { LazyJsonString } from "@smithy/core/serde";
4+
import { describe, expect, test as it } from "vitest";
5+
6+
describe(LazyJsonString.name, () => {
7+
it("should auto-serialize fields to JSON", async () => {
8+
const client = new Schemas({
9+
region: "us-west-2",
10+
});
11+
12+
let request = 0;
13+
14+
requireRequestsFrom(client).toMatch({
15+
body(b) {
16+
if (request === 0) {
17+
expect(b).toEqual(`{"Policy":"this is a plain string"}`);
18+
request += 1;
19+
} else if (request === 1) {
20+
expect(b).toEqual(`{"Policy":"{\\"this\\":\\"is a json string\\"}"}`);
21+
request += 1;
22+
} else if (request === 2) {
23+
expect(b).toEqual(`{"Policy":"{\\"message\\":\\"this is a js object\\"}"}`);
24+
request += 1;
25+
}
26+
},
27+
});
28+
29+
await client.putResourcePolicy({
30+
Policy: "this is a plain string",
31+
});
32+
await client.putResourcePolicy({
33+
Policy: `{"this":"is a json string"}`,
34+
});
35+
await client.putResourcePolicy({
36+
Policy: {
37+
message: "this is a js object",
38+
},
39+
});
40+
expect.assertions(3);
41+
});
42+
});

0 commit comments

Comments
 (0)