|
| 1 | +import { CredentialsProviderError } from "@aws-sdk/property-provider"; |
| 2 | + |
| 3 | +import { ENV_EXPIRATION, ENV_KEY, ENV_SECRET, ENV_SESSION, fromEnv } from "./fromEnv"; |
| 4 | + |
| 5 | +describe(fromEnv.name, () => { |
| 6 | + const ORIGINAL_ENV = process.env; |
| 7 | + const mockAccessKeyId = "mockAccessKeyId"; |
| 8 | + const mockSecretAccessKey = "mockSecretAccessKey"; |
| 9 | + const mockSessionToken = "mockSessionToken"; |
| 10 | + const mockExpiration = new Date().toISOString(); |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + process.env = { |
| 14 | + ...ORIGINAL_ENV, |
| 15 | + [ENV_KEY]: mockAccessKeyId, |
| 16 | + [ENV_SECRET]: mockSecretAccessKey, |
| 17 | + [ENV_SESSION]: mockSessionToken, |
| 18 | + [ENV_EXPIRATION]: mockExpiration, |
| 19 | + }; |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + process.env = ORIGINAL_ENV; |
| 24 | + }); |
| 25 | + |
| 26 | + it("should read credentials from known environment variables", async () => { |
| 27 | + const receivedCreds = await fromEnv()(); |
| 28 | + expect(receivedCreds).toStrictEqual({ |
| 29 | + accessKeyId: mockAccessKeyId, |
| 30 | + secretAccessKey: mockSecretAccessKey, |
| 31 | + sessionToken: mockSessionToken, |
| 32 | + expiration: new Date(mockExpiration), |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("can create credentials without a session token or expiration", async () => { |
| 37 | + delete process.env[ENV_SESSION]; |
| 38 | + delete process.env[ENV_EXPIRATION]; |
| 39 | + const receivedCreds = await fromEnv()(); |
| 40 | + expect(receivedCreds).toStrictEqual({ |
| 41 | + accessKeyId: mockAccessKeyId, |
| 42 | + secretAccessKey: mockSecretAccessKey, |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it.each([ENV_KEY, ENV_SECRET])("throws if env['%s'] is not found", async (key) => { |
| 47 | + delete process.env[key]; |
| 48 | + const expectedError = new CredentialsProviderError("Unable to find environment variable credentials."); |
| 49 | + try { |
| 50 | + await fromEnv()(); |
| 51 | + fail(`expected ${expectedError}`); |
| 52 | + } catch (error) { |
| 53 | + expect(error).toStrictEqual(expectedError); |
| 54 | + } |
| 55 | + }); |
| 56 | +}); |
0 commit comments