UNPKG

2.08 kBJavaScriptView Raw
1const Policy = require('../classes/policy');
2const Controller = require('../classes/controller');
3const assert = require('assert');
4const redis = require("redis");
5const client = redis.createClient();
6const policies = require('./mocks/policies');
7const server = { redis: client };
8const chai = require("chai");
9const sinon = require("sinon");
10const sinonChai = require("sinon-chai");
11const expect = chai.expect;
12chai.use(sinonChai);
13
14Promise = require('bluebird').Promise;
15
16// policies, logging, server, policy, controller, action
17
18describe("Policies", function () {
19
20 let res = {
21 status () {
22 return { json (msg) { return msg } }
23 },
24 json () { return 'json' },
25 send () { return 'send'}
26 }
27
28 class testController extends Controller {
29 myMethod () {
30 return "OK";
31 }
32 }
33
34 let spy = sinon.spy(testController.prototype, "myMethod");
35 let policySpy = sinon.spy(policies, 'onFailure');
36
37 it('should call the controller method when the policy is accepted', function () {
38 spy.reset();
39 new Policy(policies, false, server, 'correctPolicy', testController, 'myMethod').restrict({}, res);
40 assert(spy.calledOnce);
41 });
42
43 it('should deny the request when the policy does not exist', function () {
44 spy.reset();
45 policySpy.reset();
46 let pol = new Policy(policies, false, server, 'notARealPolicy', testController, 'myMethod').restrict({}, res);
47 assert(spy.notCalled);
48 assert(policySpy.calledOnce);
49 });
50
51 it('should not blow up when an action does not exist on the controller, but it should warn', function () {
52 spy.reset();
53 policySpy.reset();
54 let pol = new Policy(policies, false, server, 'correctPolicy', testController, 'methodMissing').restrict({}, res);
55 assert(spy.notCalled);
56 assert(policySpy.calledOnce);
57 });
58
59 it('should call the controller method when no policy is given', function () {
60 spy.reset();
61 new Policy(policies, true, server, undefined, testController, 'myMethod').restrict({}, res);
62 assert(spy.calledOnce);
63 });
64
65
66});