@@ -21,24 +21,25 @@ jest.mock("@redux-offline/redux-offline/lib/defaults/detectNetwork", () => (call
21
21
jest . mock ( 'apollo-link-http' , ( ) => ( {
22
22
createHttpLink : jest . fn ( ) ,
23
23
} ) ) ;
24
- let mockHttpResponse ;
25
- let factory ;
24
+ let mockHttpResponse : ( responses : any [ ] | any , delay ?: number ) => void ;
25
+ let factory : ( opts : AWSAppSyncClientOptions ) => AWSAppSyncClient < any > ;
26
26
let isOptimistic ;
27
+ let Signer ;
27
28
beforeEach ( ( ) => {
28
- let AWSAppSyncClient ;
29
29
let createHttpLink ;
30
30
jest . resetModules ( ) ;
31
31
jest . isolateModules ( ( ) => {
32
- ( { AWSAppSyncClient } = require ( '../src/client' ) ) ;
32
+ const { AWSAppSyncClient } = require ( '../src/client' ) ;
33
33
( { isOptimistic } = require ( "../src/link/offline-link" ) ) ;
34
34
( { createHttpLink } = require ( "apollo-link-http" ) ) ;
35
+ ( { Signer } = require ( "../src/link/signer" ) ) ;
35
36
36
37
factory = ( opts ) => {
37
38
return new AWSAppSyncClient ( opts ) ;
38
39
} ;
39
40
} ) ;
40
41
41
- mockHttpResponse = ( responses : any [ ] | any , delay = 0 ) => {
42
+ mockHttpResponse = ( responses : any [ ] | any , delay : number = 0 ) => {
42
43
const mock = ( createHttpLink as jest . Mock ) ;
43
44
44
45
const requestMock = jest . fn ( ) ;
@@ -133,7 +134,7 @@ const getClient = (options?: Partial<AWSAppSyncClientOptions>) => {
133
134
} ,
134
135
} ;
135
136
136
- const client = factory ( {
137
+ const client : AWSAppSyncClient < any > = factory ( {
137
138
...defaultOptions ,
138
139
...options ,
139
140
offlineConfig : {
@@ -994,22 +995,80 @@ describe("Multi client", () => {
994
995
allKeys . forEach ( key => expect ( key ) . toMatch ( new RegExp ( `^${ keyPrefix } :.+` ) ) ) ;
995
996
} ;
996
997
} ) ;
997
- } ) ;
998
-
999
- test ( 'Cannot use same keyPrefix more than once' , ( ) => {
1000
- getClient ( {
1001
- disableOffline : false ,
1002
- offlineConfig : {
1003
- keyPrefix : 'myPrefix' ,
1004
- }
1005
- } ) ;
1006
998
1007
- expect ( ( ) => {
999
+ test ( 'Cannot use same keyPrefix more than once' , ( ) => {
1008
1000
getClient ( {
1009
1001
disableOffline : false ,
1010
1002
offlineConfig : {
1011
1003
keyPrefix : 'myPrefix' ,
1012
1004
}
1013
1005
} ) ;
1014
- } ) . toThrowError ( 'The keyPrefix myPrefix is already in use. Multiple clients cannot share the same keyPrefix.' ) ;
1006
+
1007
+ expect ( ( ) => {
1008
+ getClient ( {
1009
+ disableOffline : false ,
1010
+ offlineConfig : {
1011
+ keyPrefix : 'myPrefix' ,
1012
+ }
1013
+ } ) ;
1014
+ } ) . toThrowError ( 'The keyPrefix myPrefix is already in use. Multiple clients cannot share the same keyPrefix.' ) ;
1015
+ } ) ;
1016
+ } ) ;
1017
+
1018
+ describe ( 'Auth modes' , ( ) => {
1019
+ test ( 'AWS_IAM calls signer' , async ( ) => {
1020
+ // Signer.sign = jest.fn().mockImplementation(x => x);
1021
+ const signerSpy = jest . spyOn ( Signer , 'sign' ) ;
1022
+
1023
+ mockHttpResponse ( {
1024
+ data : {
1025
+ someQuery : {
1026
+ __typename : 'someType' ,
1027
+ someField : 'someValue'
1028
+ }
1029
+ }
1030
+ } ) ;
1031
+
1032
+ const credentials = {
1033
+ accessKeyId : 'access' ,
1034
+ secretAccessKey : 'secret' ,
1035
+ sessionToken : 'session' ,
1036
+ } ;
1037
+
1038
+ const client = getClient ( {
1039
+ disableOffline : false ,
1040
+ url : 'https://somehost/graphql' ,
1041
+ auth : {
1042
+ type : AUTH_TYPE . AWS_IAM ,
1043
+ credentials : ( ) => credentials
1044
+ }
1045
+ } ) ;
1046
+
1047
+ await client . hydrated ( ) ;
1048
+
1049
+ await client . query ( {
1050
+ query : gql `query {
1051
+ someQuery {
1052
+ someField
1053
+ }
1054
+ }` ,
1055
+ fetchPolicy : "network-only"
1056
+ } ) ;
1057
+
1058
+ // Give it some time
1059
+ await new Promise ( r => setTimeout ( r , WAIT ) ) ;
1060
+
1061
+ expect ( signerSpy ) . toHaveBeenCalledWith ( expect . anything ( ) , {
1062
+ access_key : credentials . accessKeyId ,
1063
+ secret_key : credentials . secretAccessKey ,
1064
+ session_token : credentials . sessionToken ,
1065
+ } ) ;
1066
+ expect ( signerSpy ) . toReturnWith ( expect . objectContaining ( {
1067
+ headers : expect . objectContaining ( {
1068
+ Authorization : expect . stringMatching ( / ^ A W S 4 \- H M A C \- S H A 2 5 6 C r e d e n t i a l = / ) ,
1069
+ 'X-Amz-Security-Token' : 'session' ,
1070
+ 'x-amz-date' : expect . stringMatching ( / ^ \d { 8 } T \d { 6 } Z $ / ) ,
1071
+ } )
1072
+ } ) ) ;
1073
+ } ) ;
1015
1074
} ) ;
0 commit comments