1
- import { ProviderError } from "@aws-sdk/property-provider" ;
2
1
import { readFileSync } from "fs" ;
3
-
4
- import { fromTokenFile , FromTokenFileInit } from "./fromTokenFile" ;
5
- import { AssumeRoleWithWebIdentityParams } from "./index" ;
2
+ jest . mock ( "./fromWebToken" , ( ) => ( {
3
+ fromWebToken : jest . fn ( ) . mockReturnValue ( ( ) => Promise . resolve ( MOCK_CREDS ) ) ,
4
+ } ) ) ;
5
+ import { fromTokenFile } from "./fromTokenFile" ;
6
+ import { fromWebToken } from "./fromWebToken" ;
6
7
7
8
const ENV_TOKEN_FILE = "AWS_WEB_IDENTITY_TOKEN_FILE" ;
8
9
const ENV_ROLE_ARN = "AWS_ROLE_ARN" ;
@@ -31,57 +32,6 @@ describe(fromTokenFile.name, () => {
31
32
jest . restoreAllMocks ( ) ;
32
33
} ) ;
33
34
34
- const testRoleAssumerWithWebIdentityNotDefined = async ( init : FromTokenFileInit , roleArn : string ) => {
35
- try {
36
- // @ts -ignore An argument for 'init' was not provided.
37
- await fromTokenFile ( init ) ( ) ;
38
- fail ( `Expected error to be thrown` ) ;
39
- } catch ( error ) {
40
- expect ( error ) . toEqual (
41
- new ProviderError (
42
- `Role Arn '${ roleArn } ' needs to be assumed with web identity, but no role assumption callback was provided.` ,
43
- false
44
- )
45
- ) ;
46
- }
47
- } ;
48
-
49
- const testReadFileSyncError = async ( init : FromTokenFileInit ) => {
50
- const readFileSyncError = new Error ( "readFileSyncError" ) ;
51
- ( readFileSync as jest . Mock ) . mockImplementation ( ( ) => {
52
- throw readFileSyncError ;
53
- } ) ;
54
- try {
55
- await fromTokenFile ( init ) ( ) ;
56
- fail ( `Expected error to be thrown` ) ;
57
- } catch ( error ) {
58
- expect ( error ) . toEqual ( readFileSyncError ) ;
59
- }
60
- expect ( readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
61
- } ;
62
-
63
- const testRoleAssumerWithWebIdentitySuccess = async ( init : FromTokenFileInit ) => {
64
- const creds = await fromTokenFile ( init ) ( ) ;
65
- expect ( creds ) . toEqual ( MOCK_CREDS ) ;
66
- expect ( readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
67
- expect ( readFileSync ) . toHaveBeenCalledWith ( mockTokenFile , { encoding : "ascii" } ) ;
68
- } ;
69
-
70
- const testRandomValueForRoleSessionName = async ( init : FromTokenFileInit ) => {
71
- const mockDateNow = Date . now ( ) ;
72
- const spyDateNow = jest . spyOn ( Date , "now" ) . mockReturnValueOnce ( mockDateNow ) ;
73
-
74
- const creds = await fromTokenFile ( {
75
- ...init ,
76
- roleAssumerWithWebIdentity : async ( params : AssumeRoleWithWebIdentityParams ) => {
77
- expect ( params . RoleSessionName ) . toEqual ( `aws-sdk-js-session-${ mockDateNow } ` ) ;
78
- return MOCK_CREDS ;
79
- } ,
80
- } ) ( ) ;
81
- expect ( creds ) . toEqual ( MOCK_CREDS ) ;
82
- expect ( spyDateNow ) . toHaveBeenCalledTimes ( 1 ) ;
83
- } ;
84
-
85
35
describe ( "reads config from env" , ( ) => {
86
36
const original_ENV_TOKEN_FILE = process . env [ ENV_TOKEN_FILE ] ;
87
37
const original_ENV_ROLE_ARN = process . env [ ENV_ROLE_ARN ] ;
@@ -99,83 +49,70 @@ describe(fromTokenFile.name, () => {
99
49
process . env [ ENV_ROLE_SESSION_NAME ] = original_ENV_ROLE_SESSION_NAME ;
100
50
} ) ;
101
51
102
- it ( "throws if roleAssumerWithWebIdentity is not defined" , async ( ) => {
103
- return testRoleAssumerWithWebIdentityNotDefined ( { } , process . env [ ENV_ROLE_ARN ] ) ;
52
+ it ( `passes values to ${ fromWebToken . name } ` , async ( ) => {
53
+ const roleAssumerWithWebIdentity = jest . fn ( ) ;
54
+ const creds = await fromTokenFile ( {
55
+ roleAssumerWithWebIdentity,
56
+ } ) ( ) ;
57
+ expect ( creds ) . toEqual ( MOCK_CREDS ) ;
58
+ expect ( fromWebToken as jest . Mock ) . toBeCalledTimes ( 1 ) ;
59
+ const webTokenInit = ( fromWebToken as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
60
+ expect ( webTokenInit . webIdentityToken ) . toBe ( mockTokenValue ) ;
61
+ expect ( webTokenInit . roleSessionName ) . toBe ( mockRoleSessionName ) ;
62
+ expect ( webTokenInit . roleArn ) . toBe ( mockRoleArn ) ;
63
+ expect ( webTokenInit . roleAssumerWithWebIdentity ) . toBe ( roleAssumerWithWebIdentity ) ;
104
64
} ) ;
105
65
106
- it ( "throws if ENV_TOKEN_FILE read from disk failed" , async ( ) => {
107
- return testReadFileSyncError ( {
108
- roleAssumerWithWebIdentity : async ( params : AssumeRoleWithWebIdentityParams ) => {
109
- return MOCK_CREDS ;
110
- } ,
111
- } ) ;
66
+ it ( "prefers init parameters over environmental variables" , async ( ) => {
67
+ const roleAssumerWithWebIdentity = jest . fn ( ) ;
68
+ const init = {
69
+ webIdentityTokenFile : "anotherTokenFile" ,
70
+ roleArn : "anotherRoleArn" ,
71
+ roleSessionName : "anotherRoleSessionName" ,
72
+ roleAssumerWithWebIdentity,
73
+ } ;
74
+ const creds = await fromTokenFile ( init ) ( ) ;
75
+ expect ( creds ) . toEqual ( MOCK_CREDS ) ;
76
+ expect ( fromWebToken as jest . Mock ) . toBeCalledTimes ( 1 ) ;
77
+ const webTokenInit = ( fromWebToken as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
78
+ expect ( webTokenInit . roleSessionName ) . toBe ( init . roleSessionName ) ;
79
+ expect ( webTokenInit . roleArn ) . toBe ( init . roleArn ) ;
80
+ expect ( webTokenInit . roleAssumerWithWebIdentity ) . toBe ( roleAssumerWithWebIdentity ) ;
81
+ expect ( readFileSync as jest . Mock ) . toBeCalledTimes ( 1 ) ;
82
+ expect ( ( readFileSync as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ) . toBe ( init . webIdentityTokenFile ) ;
112
83
} ) ;
113
84
114
- it ( "passes values to roleAssumerWithWebIdentity" , async ( ) => {
115
- return testRoleAssumerWithWebIdentitySuccess ( {
116
- roleAssumerWithWebIdentity : async ( params : AssumeRoleWithWebIdentityParams ) => {
117
- expect ( params . WebIdentityToken ) . toEqual ( mockTokenValue ) ;
118
- expect ( params . RoleArn ) . toEqual ( mockRoleArn ) ;
119
- expect ( params . RoleSessionName ) . toEqual ( mockRoleSessionName ) ;
120
- return MOCK_CREDS ;
121
- } ,
85
+ it ( "throws if ENV_TOKEN_FILE read from disk failed" , async ( ) => {
86
+ const readFileSyncError = new Error ( "readFileSyncError" ) ;
87
+ ( readFileSync as jest . Mock ) . mockImplementation ( ( ) => {
88
+ throw readFileSyncError ;
122
89
} ) ;
123
- } ) ;
124
-
125
- it ( "generates a random value for RoleSessionName if not available" , async ( ) => {
126
- delete process . env [ ENV_ROLE_SESSION_NAME ] ;
127
- return testRandomValueForRoleSessionName ( { } ) ;
128
- } ) ;
129
- } ) ;
130
-
131
- describe ( "reads config from configuration keys" , ( ) => {
132
- const original_ENV_TOKEN_FILE = process . env [ ENV_TOKEN_FILE ] ;
133
- const original_ENV_ROLE_ARN = process . env [ ENV_ROLE_ARN ] ;
134
- const original_ENV_ROLE_SESSION_NAME = process . env [ ENV_ROLE_SESSION_NAME ] ;
135
-
136
- beforeAll ( ( ) => {
137
- delete process . env [ ENV_TOKEN_FILE ] ;
138
- delete process . env [ ENV_ROLE_ARN ] ;
139
- delete process . env [ ENV_ROLE_SESSION_NAME ] ;
140
- } ) ;
141
-
142
- afterAll ( ( ) => {
143
- process . env [ ENV_TOKEN_FILE ] = original_ENV_TOKEN_FILE ;
144
- process . env [ ENV_ROLE_ARN ] = original_ENV_ROLE_ARN ;
145
- process . env [ ENV_ROLE_SESSION_NAME ] = original_ENV_ROLE_SESSION_NAME ;
146
- } ) ;
147
-
148
- it ( "throws if roleAssumerWithWebIdentity is not defined" , async ( ) => {
149
- return testRoleAssumerWithWebIdentityNotDefined ( { roleArn : mockRoleArn } , mockRoleArn ) ;
90
+ try {
91
+ await fromTokenFile ( { roleAssumerWithWebIdentity : jest . fn ( ) } ) ( ) ;
92
+ fail ( `Expected error to be thrown` ) ;
93
+ } catch ( error ) {
94
+ expect ( error ) . toEqual ( readFileSyncError ) ;
95
+ }
96
+ expect ( readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
150
97
} ) ;
151
98
152
99
it ( "throws if web_identity_token_file read from disk failed" , async ( ) => {
153
- return testReadFileSyncError ( {
154
- webIdentityTokenFile : mockTokenFile ,
155
- roleArn : mockRoleArn ,
156
- roleSessionName : mockRoleSessionName ,
157
- roleAssumerWithWebIdentity : async ( params : AssumeRoleWithWebIdentityParams ) => {
158
- return MOCK_CREDS ;
159
- } ,
160
- } ) ;
161
- } ) ;
162
-
163
- it ( "passes values to roleAssumerWithWebIdentity" , async ( ) => {
164
- return testRoleAssumerWithWebIdentitySuccess ( {
165
- webIdentityTokenFile : mockTokenFile ,
166
- roleArn : mockRoleArn ,
167
- roleSessionName : mockRoleSessionName ,
168
- roleAssumerWithWebIdentity : async ( params : AssumeRoleWithWebIdentityParams ) => {
169
- expect ( params . WebIdentityToken ) . toEqual ( mockTokenValue ) ;
170
- expect ( params . RoleArn ) . toEqual ( mockRoleArn ) ;
171
- expect ( params . RoleSessionName ) . toEqual ( mockRoleSessionName ) ;
172
- return MOCK_CREDS ;
173
- } ,
100
+ const readFileSyncError = new Error ( "readFileSyncError" ) ;
101
+ ( readFileSync as jest . Mock ) . mockImplementation ( ( ) => {
102
+ throw readFileSyncError ;
174
103
} ) ;
175
- } ) ;
176
-
177
- it ( "generates a random value for RoleSessionName if not available" , async ( ) => {
178
- return testRandomValueForRoleSessionName ( { webIdentityTokenFile : mockTokenFile , roleArn : mockRoleArn } ) ;
104
+ try {
105
+ await fromTokenFile ( {
106
+ webIdentityTokenFile : mockTokenFile ,
107
+ roleArn : mockRoleArn ,
108
+ roleSessionName : mockRoleSessionName ,
109
+ roleAssumerWithWebIdentity : jest . fn ( ) ,
110
+ } ) ( ) ;
111
+ fail ( `Expected error to be thrown` ) ;
112
+ } catch ( error ) {
113
+ expect ( error ) . toEqual ( readFileSyncError ) ;
114
+ }
115
+ expect ( readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
179
116
} ) ;
180
117
} ) ;
181
118
} ) ;
0 commit comments