-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAuthorizerModuleTest.js
More file actions
98 lines (80 loc) · 2.81 KB
/
Copy pathAuthorizerModuleTest.js
File metadata and controls
98 lines (80 loc) · 2.81 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*!
* Expanse, LLC
* http://expansellc.io
*
* Copyright 2016
* Released under the Apache 2 license
* https://www.apache.org/licenses/LICENSE-2.0
*
* @authors Meghan Erickson
*/
'use strict';
const assert = require('chai').assert;
const expect = require('chai').expect;
const sinon = require('sinon');
const AuthorizerModule = require('../lib/AuthorizerModule');
const event = {
type: 'TOKEN',
authorizationToken: 'fooToken',
methodArn: 'arn:aws:execute-api:us-west-2:123456789012:0h00jda672/*/GET/'
};
const context = {};
const googPrincipalId = 'jenkypenky@gmail.com';
const fbPrincipalId = 'jenkypenky@facebook.com';
const amznPrincipalId = 'jenkypenky@amazon.com';
const idProviderFunction = 'callIdProvider';
const badPromise = new Promise((resolve) => {
resolve(0);
});
describe('Authorizer Unit Tests on authorize()', () => {
let sandbox;
let buildPolicySpy = null;
const authMod = new AuthorizerModule();
beforeEach('reset the spy', () => {
sandbox = sinon.sandbox.create();
buildPolicySpy = sandbox.spy(authMod, 'buildPolicy');
});
it('Authorizer authorize() flow with valid Google', () => {
const goodPromise = new Promise((resolve) => {
resolve(googPrincipalId);
});
sandbox.stub(authMod.googleMod, idProviderFunction, () => goodPromise);
sandbox.stub(authMod.amznMod, idProviderFunction, () => badPromise);
sandbox.stub(authMod.facebookMod, idProviderFunction, () => badPromise);
return authMod.authorize(event, context)
.then((result) => {
assert(buildPolicySpy.called);
expect(result.principalId).to.equal(googPrincipalId);
});
});
it('Authorizer authorize() flow with valid Amazon', () => {
const goodPromise = new Promise((resolve) => {
resolve(amznPrincipalId);
});
sandbox.stub(authMod.googleMod, idProviderFunction, () => badPromise);
sandbox.stub(authMod.amznMod, idProviderFunction, () => goodPromise);
sandbox.stub(authMod.facebookMod, idProviderFunction, () => badPromise);
return authMod.authorize(event, context)
.then((result) => {
assert(buildPolicySpy.called);
expect(result.principalId).to.equal(amznPrincipalId);
});
});
it('Authorizer authorize() flow with valid Facebook', () => {
const goodPromise = new Promise((resolve) => {
resolve(fbPrincipalId);
});
sandbox.stub(authMod.googleMod, idProviderFunction, () => badPromise);
sandbox.stub(authMod.amznMod, idProviderFunction, () => badPromise);
sandbox.stub(authMod.facebookMod, idProviderFunction, () => goodPromise);
return authMod.authorize(event, context)
.then((result) => {
assert(buildPolicySpy.called);
expect(result.principalId).to.equal(fbPrincipalId);
});
});
afterEach(() => {
buildPolicySpy.restore();
sandbox.restore();
});
});