From 911260e1c5a4c83122ee3bbea9f6f872c6f8bfc3 Mon Sep 17 00:00:00 2001 From: iliapolo Date: Sat, 31 Aug 2024 17:18:47 +0300 Subject: [PATCH] mid work --- lib/aws.ts | 2 +- test/aws.test.ts | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/aws.ts b/lib/aws.ts index 4e5a523..a761c21 100644 --- a/lib/aws.ts +++ b/lib/aws.ts @@ -163,7 +163,7 @@ export class DefaultAwsClient implements IAws { RoleArn: roleArn, ExternalId: externalId, RoleSessionName: `cdk-assets-${safeUsername()}`, - // ...(additionalOptions ?? {}), + ...(additionalOptions ?? {}), }, stsConfig: { region, diff --git a/test/aws.test.ts b/test/aws.test.ts index 8d6e498..3282cbe 100644 --- a/test/aws.test.ts +++ b/test/aws.test.ts @@ -1,10 +1,6 @@ import * as os from 'os'; import { DefaultAwsClient } from '../lib'; -afterEach(() => { - jest.requireActual('aws-sdk'); -}); - beforeEach(() => { jest.requireActual('aws-sdk'); }); @@ -60,7 +56,7 @@ test('assumeRole passes the right parameters to STS', async () => { test('assumeRole defaults session tags to all', async () => { // eslint-disable-next-line @typescript-eslint/no-require-imports - require('aws-sdk'); + const AWS = require('aws-sdk'); jest.mock('aws-sdk', () => { return { @@ -75,13 +71,36 @@ test('assumeRole defaults session tags to all', async () => { ChainableTemporaryCredentials: jest.fn(), }; }); - const aws = new DefaultAwsClient(); - - const account = await aws.discoverTargetAccount({}); - expect(account).toEqual({ - accountId: '123456789012', - partition: 'aws', + await withMocked(os, 'userInfo', async (userInfo) => { + userInfo.mockReturnValue({ + username: 'foo', + uid: 1, + gid: 1, + homedir: '/here', + shell: '/bin/sh', + }); + await aws.discoverTargetAccount({ + region: 'us-east-1', + assumeRoleArn: 'arn:aws:iam::123456789012:role/my-role', + assumeRoleExternalId: 'external-id', + assumeRoleAdditionalOptions: { + Tags: [{ Key: 'Departement', Value: 'Engineering' }], + }, + }); + expect(AWS.ChainableTemporaryCredentials).toHaveBeenCalledWith({ + params: { + ExternalId: 'external-id', + RoleArn: 'arn:aws:iam::123456789012:role/my-role', + Tags: [{ Key: 'Departement', Value: 'Engineering' }], + TransitiveTagKeys: ['Departement'], + RoleSessionName: `cdk-assets-foo`, + }, + stsConfig: { + customUserAgent: 'cdk-assets', + region: 'us-east-1', + }, + }); }); });