2
2
// https://github.com/aws/aws-sdk-js-v3/blob/main/codegen/smithy-aws-typescript-codegen/src/main/resources/software/amazon/smithy/aws/typescript/codegen/sts-client-defaultRoleAssumers.spec.ts
3
3
import { HttpResponse } from "@aws-sdk/protocol-http" ;
4
4
import { Readable } from "stream" ;
5
- const assumeRoleResponse = `<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
5
+
6
+ const mockHandle = jest . fn ( ) . mockResolvedValue ( {
7
+ response : new HttpResponse ( {
8
+ statusCode : 200 ,
9
+ body : Readable . from ( [ "" ] ) ,
10
+ } ) ,
11
+ } ) ;
12
+ jest . mock ( "@aws-sdk/node-http-handler" , ( ) => ( {
13
+ NodeHttpHandler : jest . fn ( ) . mockImplementation ( ( ) => ( {
14
+ destroy : ( ) => { } ,
15
+ handle : mockHandle ,
16
+ } ) ) ,
17
+ streamCollector : jest . fn ( ) ,
18
+ } ) ) ;
19
+
20
+ import { getDefaultRoleAssumer , getDefaultRoleAssumerWithWebIdentity } from "./defaultRoleAssumers" ;
21
+ import type { AssumeRoleCommandInput } from "./commands/AssumeRoleCommand" ;
22
+ import { NodeHttpHandler , streamCollector } from "@aws-sdk/node-http-handler" ;
23
+ import { AssumeRoleWithWebIdentityCommandInput } from "./commands/AssumeRoleWithWebIdentityCommand" ;
24
+ const mockConstructorInput = jest . fn ( ) ;
25
+ jest . mock ( "./STSClient" , ( ) => ( {
26
+ STSClient : function ( params : any ) {
27
+ mockConstructorInput ( params ) ;
28
+ //@ts -ignore
29
+ return new ( jest . requireActual ( "./STSClient" ) . STSClient ) ( params ) ;
30
+ } ,
31
+ } ) ) ;
32
+
33
+ describe ( "getDefaultRoleAssumer" , ( ) => {
34
+ const assumeRoleResponse = `<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
6
35
<AssumeRoleResult>
7
36
<AssumedRoleUser>
8
37
<AssumedRoleId>AROAZOX2IL27GNRBJHWC2:session</AssumedRoleId>
@@ -19,27 +48,15 @@ const assumeRoleResponse = `<AssumeRoleResponse xmlns="https://sts.amazonaws.com
19
48
<RequestId>12345678id</RequestId>
20
49
</ResponseMetadata>
21
50
</AssumeRoleResponse>` ;
22
- const mockHandle = jest . fn ( ) . mockResolvedValue ( {
23
- response : new HttpResponse ( {
24
- statusCode : 200 ,
25
- body : Readable . from ( [ "" ] ) ,
26
- } ) ,
27
- } ) ;
28
- jest . mock ( "@aws-sdk/node-http-handler" , ( ) => ( {
29
- NodeHttpHandler : jest . fn ( ) . mockImplementation ( ( ) => ( {
30
- destroy : ( ) => { } ,
31
- handle : mockHandle ,
32
- } ) ) ,
33
- streamCollector : async ( ) => Buffer . from ( assumeRoleResponse ) ,
34
- } ) ) ;
35
51
36
- import { getDefaultRoleAssumer } from "./defaultRoleAssumers" ;
37
- import type { AssumeRoleCommandInput } from "./commands/AssumeRoleCommand" ;
52
+ beforeAll ( ( ) => {
53
+ ( streamCollector as jest . Mock ) . mockImplementation ( async ( ) => Buffer . from ( assumeRoleResponse ) ) ;
54
+ } ) ;
38
55
39
- describe ( "getDefaultRoleAssumer" , ( ) => {
40
56
beforeEach ( ( ) => {
41
57
jest . clearAllMocks ( ) ;
42
58
} ) ;
59
+
43
60
it ( "should use supplied source credentials" , async ( ) => {
44
61
const roleAssumer = getDefaultRoleAssumer ( ) ;
45
62
const params : AssumeRoleCommandInput = {
@@ -61,4 +78,71 @@ describe("getDefaultRoleAssumer", () => {
61
78
expect . stringContaining ( "AWS4-HMAC-SHA256 Credential=key2/" )
62
79
) ;
63
80
} ) ;
81
+
82
+ it ( "should use the STS client config" , async ( ) => {
83
+ const logger = console ;
84
+ const region = "some-region" ;
85
+ const handler = new NodeHttpHandler ( ) ;
86
+ const roleAssumer = getDefaultRoleAssumer ( {
87
+ region,
88
+ logger,
89
+ requestHandler : handler ,
90
+ } ) ;
91
+ const params : AssumeRoleCommandInput = {
92
+ RoleArn : "arn:aws:foo" ,
93
+ RoleSessionName : "session" ,
94
+ } ;
95
+ const sourceCred = { accessKeyId : "key" , secretAccessKey : "secrete" } ;
96
+ await roleAssumer ( sourceCred , params ) ;
97
+ expect ( mockConstructorInput ) . toHaveBeenCalledTimes ( 1 ) ;
98
+ expect ( mockConstructorInput . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
99
+ logger,
100
+ requestHandler : handler ,
101
+ region,
102
+ } ) ;
103
+ } ) ;
104
+ } ) ;
105
+
106
+ describe ( "getDefaultRoleAssumerWithWebIdentity" , ( ) => {
107
+ const assumeRoleResponse = `<Response xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
108
+ <AssumeRoleWithWebIdentityResult>
109
+ <Credentials>
110
+ <AccessKeyId>key</AccessKeyId>
111
+ <SecretAccessKey>secrete</SecretAccessKey>
112
+ <SessionToken>session-token</SessionToken>
113
+ <Expiration>2021-05-05T23:22:08Z</Expiration>
114
+ </Credentials>
115
+ </AssumeRoleWithWebIdentityResult>
116
+ </Response>` ;
117
+
118
+ beforeAll ( ( ) => {
119
+ ( streamCollector as jest . Mock ) . mockImplementation ( async ( ) => Buffer . from ( assumeRoleResponse ) ) ;
120
+ } ) ;
121
+
122
+ beforeEach ( ( ) => {
123
+ jest . clearAllMocks ( ) ;
124
+ } ) ;
125
+
126
+ it ( "should use the STS client config" , async ( ) => {
127
+ const logger = console ;
128
+ const region = "some-region" ;
129
+ const handler = new NodeHttpHandler ( ) ;
130
+ const roleAssumerWithWebIdentity = getDefaultRoleAssumerWithWebIdentity ( {
131
+ region,
132
+ logger,
133
+ requestHandler : handler ,
134
+ } ) ;
135
+ const params : AssumeRoleWithWebIdentityCommandInput = {
136
+ RoleArn : "arn:aws:foo" ,
137
+ RoleSessionName : "session" ,
138
+ WebIdentityToken : "token" ,
139
+ } ;
140
+ await roleAssumerWithWebIdentity ( params ) ;
141
+ expect ( mockConstructorInput ) . toHaveBeenCalledTimes ( 1 ) ;
142
+ expect ( mockConstructorInput . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( {
143
+ logger,
144
+ requestHandler : handler ,
145
+ region,
146
+ } ) ;
147
+ } ) ;
64
148
} ) ;
0 commit comments