1 | const Policy = require('../classes/policy');
|
2 | const Controller = require('../classes/controller');
|
3 | const assert = require('assert');
|
4 | const redis = require("redis");
|
5 | const client = redis.createClient();
|
6 | const policies = require('./mocks/policies');
|
7 | const server = { redis: client };
|
8 | const chai = require("chai");
|
9 | const sinon = require("sinon");
|
10 | const sinonChai = require("sinon-chai");
|
11 | const expect = chai.expect;
|
12 | chai.use(sinonChai);
|
13 |
|
14 | Promise = require('bluebird').Promise;
|
15 |
|
16 |
|
17 |
|
18 | describe("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 | });
|