Skip to content

Commit 99332b8

Browse files
committed
test(aws-client-api-test): integration test for credentials forceRefresh
1 parent 052971b commit 99332b8

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, test as it, expect, vi } from "vitest";
2+
import { S3 } from "@aws-sdk/client-s3";
3+
import { S3Control } from "@aws-sdk/client-s3-control";
4+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
5+
import { EC2 } from "@aws-sdk/client-ec2";
6+
import { SQS } from "@aws-sdk/client-sqs";
7+
import { Glacier } from "@aws-sdk/client-glacier";
8+
import { STS } from "@aws-sdk/client-sts";
9+
import { TranscribeStreaming } from "@aws-sdk/client-transcribe-streaming";
10+
import { Route53 } from "@aws-sdk/client-route-53";
11+
import { RDS } from "@aws-sdk/client-rds";
12+
13+
describe("client credentials forceRefresh", () => {
14+
describe("should force refresh the credentials if requested in code", () => {
15+
const credentials = vi.fn().mockReturnValue({
16+
accessKeyId: "accessKeyId",
17+
secretAccessKey: "secretAccessKey",
18+
sessionToken: "sessionToken",
19+
expiration: new Date(Date.now() + 3600 * 1000), // 1 hour from now
20+
});
21+
const config = { credentials };
22+
23+
afterEach(() => {
24+
vi.clearAllMocks();
25+
});
26+
27+
it.each([S3, S3Control, DynamoDB, EC2, Glacier, RDS, Route53, SQS, STS, TranscribeStreaming])(
28+
"%o",
29+
async (ClientCtr) => {
30+
const client = new ClientCtr(config);
31+
await client.config.credentials();
32+
expect(credentials).toHaveBeenCalledTimes(1);
33+
34+
// Returns the cached credentials for the second time.
35+
await client.config.credentials();
36+
expect(credentials).toHaveBeenCalledTimes(1);
37+
38+
// Calls the function, if forceRefresh is set to true.
39+
await client.config.credentials({ forceRefresh: true });
40+
expect(credentials).toHaveBeenCalledTimes(2);
41+
42+
// Doesn't call the function, if forceRefresh is set to false.
43+
await client.config.credentials({ forceRefresh: false });
44+
expect(credentials).toHaveBeenCalledTimes(2);
45+
46+
// Doesn't call the function, if forceRefresh is not set.
47+
await client.config.credentials();
48+
expect(credentials).toHaveBeenCalledTimes(2);
49+
}
50+
);
51+
});
52+
});

0 commit comments

Comments
 (0)