1 | 'use strict';
|
2 |
|
3 | var expect = require('chai').expect;
|
4 | var winston = require('winston');
|
5 | var cache = require('../lib/cache');
|
6 | var authErrors = require('../lib/errors');
|
7 |
|
8 | describe('common', function(){
|
9 | var common, mockAuthentic, mockAuthScope;
|
10 | var endpointResponse, endpointToken, context;
|
11 |
|
12 | beforeEach(function(){
|
13 | cache.enable();
|
14 | cache.clear();
|
15 | endpointResponse = { statusCode: 200 };
|
16 | endpointToken = {result:{}};
|
17 | mockAuthentic = {
|
18 | getEndpointClaims: function(_, __, cb) { cb(null, endpointResponse, endpointToken); }
|
19 | };
|
20 | mockAuthScope = { verify: function(_, cb) { cb(null, context); } };
|
21 | context = {};
|
22 | common = require('../lib/common')(mockAuthScope, mockAuthentic, winston);
|
23 | });
|
24 |
|
25 | describe('#getEndpointClaims', function(){
|
26 | it('should return verified context', function(){
|
27 | return common.getEndpointClaims('en-US', 'some-principal')
|
28 | .then(function(result){
|
29 | expect(result).to.eql(context);
|
30 | });
|
31 | });
|
32 | it('should pass back error from authentic', function(){
|
33 | mockAuthentic.getEndpointClaims = function(_, __, cb) { cb(new Error('fake error')); };
|
34 | return common.getEndpointClaims('en-US', 'some-principal')
|
35 | .then(function(){
|
36 | throw new Error('Expected failure, got success');
|
37 | })
|
38 | .catch(function(err){
|
39 | expect(err).to.be.ok;
|
40 | expect(err.message).to.eql('fake error');
|
41 | });
|
42 | });
|
43 | it('should cache successful response', function(){
|
44 | return common.getEndpointClaims('en-US', 'some-principal')
|
45 | .then(function(result){
|
46 | var cachedResult = cache._cache.get('auth:some-principal');
|
47 | expect(cachedResult).to.be.ok;
|
48 | expect(cachedResult).to.eql(result);
|
49 | });
|
50 | });
|
51 | });
|
52 |
|
53 | describe('#verifyContext', function(){
|
54 | var auth;
|
55 |
|
56 | beforeEach(function(){
|
57 | auth = { verified: true, isExpired: false };
|
58 | });
|
59 |
|
60 | it('should return auth when it is verified and not expired', function(){
|
61 | var result = common.verifyContext(auth);
|
62 | expect(result).to.eql(auth);
|
63 | });
|
64 | it('should throw error when auth is not verified', function(){
|
65 | auth.verified = false;
|
66 | expect(function(){ common.verifyContext(auth); }).to.throw(authErrors.UnverifiedContextError);
|
67 | });
|
68 | it('should throw error when auth is expired', function(){
|
69 | auth.isExpired = true;
|
70 | expect(function(){ common.verifyContext(auth); }).to.throw(authErrors.ExpiredContextError);
|
71 | });
|
72 | });
|
73 | });
|