|
| 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 | + |
| 32 | + // Credentials function is only called on the first call (done by API call). |
| 33 | + expect(credentials).not.toHaveBeenCalled(); |
| 34 | + await client.config.credentials(); |
| 35 | + expect(credentials).toHaveBeenCalledTimes(1); |
| 36 | + |
| 37 | + // Returns the cached credentials for the second time. |
| 38 | + await client.config.credentials(); |
| 39 | + expect(credentials).toHaveBeenCalledTimes(1); |
| 40 | + |
| 41 | + // Calls the function, if forceRefresh is set to true. |
| 42 | + await client.config.credentials({ forceRefresh: true }); |
| 43 | + expect(credentials).toHaveBeenCalledTimes(2); |
| 44 | + |
| 45 | + // Doesn't call the function, if forceRefresh is set to false. |
| 46 | + await client.config.credentials({ forceRefresh: false }); |
| 47 | + expect(credentials).toHaveBeenCalledTimes(2); |
| 48 | + |
| 49 | + // Doesn't call the function, if forceRefresh is not set. |
| 50 | + await client.config.credentials(); |
| 51 | + expect(credentials).toHaveBeenCalledTimes(2); |
| 52 | + } |
| 53 | + ); |
| 54 | + }); |
| 55 | +}); |
0 commit comments