/
AmznIdModuleTest.js
80 lines (69 loc) · 2.42 KB
/
AmznIdModuleTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*!
* Expanse, LLC
* http://expansellc.io
*
* Copyright 2016
* Released under the Apache 2 license
* https://www.apache.org/licenses/LICENSE-2.0
*
* @authors Ryan Scott
*/
'use strict';
const expect = require('chai').expect;
const nock = require('nock');
const ProviderConfig = require('../config/ProviderConfig');
const AmznIdModule = require('../lib/AmznIdModule');
describe('AmznIdModule Unit Tests', () => {
const amznMod = new AmznIdModule(new ProviderConfig());
const goodToken = 'good';
const invalidToken = 'invalid';
const expectedPrincipalId = 'jenkypenky@gmail.com';
const validTokenPayload = '{"app_id": "amzn1.application.identifier","aud": "amzn1.application-oa2-client.identifier","exp": 818,"iat": 1475349936,"iss": "https://www.amazon.com","user_id": "amzn1.account.identifier"}';
const invalidTokenPayload = '{"error_description": "Invalid mock value"}';
const validProfilePayload = '{"email": "jenkypenky@gmail.com","name": "Jenky Penky","user_id": "amzn1.account.identifier"}';
const invalidProfilePayload = '{"error_description": "Invalid mock value"}';
it('Mock 200 response for both token and profile APIs', () => {
nock('https://api.amazon.com')
.get('/auth/o2/tokeninfo')
.query({
access_token: goodToken
})
.reply(200, validTokenPayload);
nock('https://api.amazon.com', {
reqheaders: {
Authorization: 'Bearer '.concat(goodToken)
}
})
.get('/user/profile')
.reply(200, validProfilePayload);
return amznMod.callIdProvider(goodToken)
.then(data => {
expect(data).to.equal(expectedPrincipalId);
nock.cleanAll();
});
});
it('Mock 400 response from token API to AmznIdModule', () => {
nock('https://api.amazon.com')
.get('/auth/o2/tokeninfo')
.query({
access_token: invalidToken
})
.reply(400, invalidTokenPayload);
nock('https://api.amazon.com', {
reqheaders: {
Authorization: 'Bearer '.concat(invalidToken)
}
})
.get('/user/profile')
.reply(400, invalidProfilePayload);
return amznMod.callIdProvider(invalidToken)
.catch(error => {
expect(error).to.equal(0);
nock.cleanAll();
});
});
it('A principalId should be derived from the token response', () => {
const principalId = AmznIdModule.retrievePrincipalId(validProfilePayload);
expect(principalId).to.equal(expectedPrincipalId);
});
});