Skip to content

Commit cfdf627

Browse files
Add test coverage for AWS_IAM auth mode
1 parent c2762e2 commit cfdf627

File tree

1 file changed

+76
-17
lines changed

1 file changed

+76
-17
lines changed

packages/aws-appsync/__tests__/client.ts

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,25 @@ jest.mock("@redux-offline/redux-offline/lib/defaults/detectNetwork", () => (call
2121
jest.mock('apollo-link-http', () => ({
2222
createHttpLink: jest.fn(),
2323
}));
24-
let mockHttpResponse;
25-
let factory;
24+
let mockHttpResponse: (responses: any[] | any, delay?: number) => void;
25+
let factory: (opts: AWSAppSyncClientOptions) => AWSAppSyncClient<any>;
2626
let isOptimistic;
27+
let Signer;
2728
beforeEach(() => {
28-
let AWSAppSyncClient;
2929
let createHttpLink;
3030
jest.resetModules();
3131
jest.isolateModules(() => {
32-
({ AWSAppSyncClient } = require('../src/client'));
32+
const { AWSAppSyncClient } = require('../src/client');
3333
({ isOptimistic } = require("../src/link/offline-link"));
3434
({ createHttpLink } = require("apollo-link-http"));
35+
({ Signer } = require("../src/link/signer"));
3536

3637
factory = (opts) => {
3738
return new AWSAppSyncClient(opts);
3839
};
3940
});
4041

41-
mockHttpResponse = (responses: any[] | any, delay = 0) => {
42+
mockHttpResponse = (responses: any[] | any, delay:number = 0) => {
4243
const mock = (createHttpLink as jest.Mock);
4344

4445
const requestMock = jest.fn();
@@ -133,7 +134,7 @@ const getClient = (options?: Partial<AWSAppSyncClientOptions>) => {
133134
},
134135
};
135136

136-
const client = factory({
137+
const client: AWSAppSyncClient<any> = factory({
137138
...defaultOptions,
138139
...options,
139140
offlineConfig: {
@@ -994,22 +995,80 @@ describe("Multi client", () => {
994995
allKeys.forEach(key => expect(key).toMatch(new RegExp(`^${keyPrefix}:.+`)));
995996
};
996997
});
997-
});
998-
999-
test('Cannot use same keyPrefix more than once', () => {
1000-
getClient({
1001-
disableOffline: false,
1002-
offlineConfig: {
1003-
keyPrefix: 'myPrefix',
1004-
}
1005-
});
1006998

1007-
expect(() => {
999+
test('Cannot use same keyPrefix more than once', () => {
10081000
getClient({
10091001
disableOffline: false,
10101002
offlineConfig: {
10111003
keyPrefix: 'myPrefix',
10121004
}
10131005
});
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(/^AWS4\-HMAC\-SHA256 Credential=/),
1069+
'X-Amz-Security-Token': 'session',
1070+
'x-amz-date': expect.stringMatching(/^\d{8}T\d{6}Z$/),
1071+
})
1072+
}));
1073+
});
10151074
});

0 commit comments

Comments
 (0)