| 
 | 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