11import { $TSContext } from '@aws-amplify/amplify-cli-core' ;
22import fs from 'fs-extra' ;
3- import { CredentialProviderChain , ProcessCredentials } from 'aws-sdk' ;
3+ import { fromProcess } from '@ aws-sdk/credential-providers ' ;
44import { getProfileCredentials , getProfiledAwsConfig } from '../system-config-manager' ;
55
66jest . setTimeout ( 15000 ) ;
@@ -11,9 +11,8 @@ jest.mock('../utils/aws-logger', () => ({
1111jest . mock ( 'fs-extra' ) ;
1212const fs_mock = fs as jest . Mocked < typeof fs > ;
1313
14- jest . mock ( 'aws-sdk' ) ;
15- const ProcessCredentialsMock = ProcessCredentials as jest . MockedClass < typeof ProcessCredentials > ;
16- const CredentialProviderChainMock = CredentialProviderChain as jest . MockedClass < typeof CredentialProviderChain > ;
14+ jest . mock ( '@aws-sdk/credential-providers' ) ;
15+ const fromProcessMock = fromProcess as jest . MockedFunction < typeof fromProcess > ;
1716
1817const context_stub = {
1918 print : {
@@ -29,14 +28,14 @@ describe('profile tests', () => {
2928 fs_mock . existsSync . mockReturnValue ( true ) ;
3029
3130 describe ( 'credential process loading' , ( ) => {
32- const mockResolvePromise = jest . fn ( ) . mockReturnValue (
33- Promise . resolve ( {
34- accessKeyId : 'chainTestAccessKey ' ,
35- secretAccessKey : 'chainTestSecret ' ,
36- sessionToken : 'chainTestSessionToken' ,
37- expireTime : new Date ( 1234 ) ,
38- } ) ,
39- ) ;
31+ const mockCredentials = {
32+ accessKeyId : 'chainTestAccessKey' ,
33+ secretAccessKey : 'chainTestSecret ' ,
34+ sessionToken : 'chainTestSessionToken ' ,
35+ expiration : new Date ( 1234 ) ,
36+ } ;
37+
38+ const mockCredentialProvider = jest . fn ( ) . mockResolvedValue ( mockCredentials ) ;
4039
4140 beforeEach ( ( ) => {
4241 // setup
@@ -47,24 +46,7 @@ describe('profile tests', () => {
4746
4847 fs_mock . readFileSync . mockReturnValue ( awsConfigContent ) ;
4948
50- CredentialProviderChainMock . mockImplementation ( ( ) => ( {
51- providers : [ ] ,
52- resolvePromise : mockResolvePromise ,
53- resolve : jest . fn ( ) ,
54- } ) ) ;
55-
56- ProcessCredentialsMock . mockImplementation ( ( ) => ( {
57- accessKeyId : 'testAccessKey' ,
58- secretAccessKey : 'testSecret' ,
59- sessionToken : 'testSessionToken' ,
60- expireTime : new Date ( 1234 ) ,
61- expired : false ,
62- get : jest . fn ( ) ,
63- getPromise : jest . fn ( ) ,
64- needsRefresh : jest . fn ( ) ,
65- refresh : jest . fn ( ) ,
66- refreshPromise : jest . fn ( ) ,
67- } ) ) ;
49+ fromProcessMock . mockReturnValue ( mockCredentialProvider ) ;
6850 } ) ;
6951
7052 it ( 'should use credential_process defined in config file' , async ( ) => {
@@ -73,24 +55,29 @@ describe('profile tests', () => {
7355
7456 // expect
7557 expect ( profile_config ) . toBeDefined ( ) ;
76- expect ( mockResolvePromise ) . toBeCalled ( ) ;
58+ expect ( fromProcessMock ) . toHaveBeenCalledWith ( { profile : 'fake' } ) ;
59+ expect ( mockCredentialProvider ) . toHaveBeenCalled ( ) ;
7760 expect ( profile_config . accessKeyId ) . toBe ( 'chainTestAccessKey' ) ;
7861 expect ( profile_config . secretAccessKey ) . toBe ( 'chainTestSecret' ) ;
7962 expect ( profile_config . sessionToken ) . toBe ( 'chainTestSessionToken' ) ;
8063 expect ( profile_config . expiration ) . toEqual ( new Date ( 1234 ) ) ;
8164 } ) ;
8265
83- it ( 'sets AWS_SDK_LOAD_CONFIG while ProcessCredentials executes' , async ( ) => {
66+ it ( 'sets AWS_SDK_LOAD_CONFIG while credential provider executes' , async ( ) => {
8467 const sdkLoadConfigOriginal = process . env . AWS_SDK_LOAD_CONFIG ;
85- mockResolvePromise . mockImplementationOnce ( ( ) => {
68+ mockCredentialProvider . mockImplementationOnce ( ( ) => {
8669 expect ( process . env . AWS_SDK_LOAD_CONFIG ) . toBeTruthy ( ) ;
87-
8870 return Promise . resolve ( {
8971 accessKeyId : 'chainTestAccessKey' ,
72+ secretAccessKey : 'chainTestSecret' ,
73+ sessionToken : 'chainTestSessionToken' ,
74+ expiration : new Date ( 1234 ) ,
9075 } ) ;
9176 } ) ;
77+
9278 await getProfiledAwsConfig ( context_stub , 'fake' ) ;
93- expect ( mockResolvePromise ) . toBeCalled ( ) ;
79+ expect ( fromProcessMock ) . toHaveBeenCalledWith ( { profile : 'fake' } ) ;
80+ expect ( mockCredentialProvider ) . toHaveBeenCalled ( ) ;
9481 expect ( process . env . AWS_SDK_LOAD_CONFIG ) . toStrictEqual ( sdkLoadConfigOriginal ) ;
9582 } ) ;
9683 } ) ;
0 commit comments