|
| 1 | +const test = require('tape'); |
| 2 | +const { CapabilitiesController } = require('../dist'); |
| 3 | +const clone = require('clone'); |
| 4 | + |
| 5 | +function noop () {} |
| 6 | + |
| 7 | +const domain1 = 'foo.com'; |
| 8 | +const domain2 = 'bar.io'; |
| 9 | + |
| 10 | +const method1 = 'restricted'; |
| 11 | +const method2 = 'restricted2'; |
| 12 | + |
| 13 | +const domains = { |
| 14 | + [domain1]: { |
| 15 | + permissions: [ |
| 16 | + { |
| 17 | + parentCapability: method1, |
| 18 | + id: 'abc', |
| 19 | + }, |
| 20 | + ], |
| 21 | + }, |
| 22 | + [domain2]: { |
| 23 | + permissions: [ |
| 24 | + { |
| 25 | + parentCapability: method2, |
| 26 | + id: 'xyz', |
| 27 | + }, |
| 28 | + ], |
| 29 | + }, |
| 30 | +}; |
| 31 | + |
| 32 | +test('hasPermissions returns false if no permissions', (t) => { |
| 33 | + const ctrl = new CapabilitiesController({ |
| 34 | + requestUserApproval: noop, |
| 35 | + }); |
| 36 | + |
| 37 | + t.equal(ctrl.hasPermissions(domain1), false, 'should return false'); |
| 38 | + t.end(); |
| 39 | +}); |
| 40 | + |
| 41 | +test('hasPermission returns false if no permissions', (t) => { |
| 42 | + const ctrl = new CapabilitiesController({ |
| 43 | + requestUserApproval: noop, |
| 44 | + }); |
| 45 | + |
| 46 | + t.equal(ctrl.hasPermissions(domain1, method1), false, 'should return false'); |
| 47 | + t.end(); |
| 48 | +}); |
| 49 | + |
| 50 | +test('hasPermissions returns true with permissions', (t) => { |
| 51 | + const ctrl = new CapabilitiesController({ |
| 52 | + requestUserApproval: noop, |
| 53 | + }, |
| 54 | + { |
| 55 | + domains: clone(domains), |
| 56 | + }); |
| 57 | + |
| 58 | + t.equal(ctrl.hasPermissions(domain1), true, 'should return true for domain1'); |
| 59 | + t.equal(ctrl.hasPermissions(domain2), true, 'should return true for domain2'); |
| 60 | + t.end(); |
| 61 | +}); |
| 62 | + |
| 63 | +test('hasPermission returns true with permissions and correct method', (t) => { |
| 64 | + const ctrl = new CapabilitiesController({ |
| 65 | + requestUserApproval: noop, |
| 66 | + }, |
| 67 | + { |
| 68 | + domains: clone(domains), |
| 69 | + }); |
| 70 | + |
| 71 | + t.equal( |
| 72 | + ctrl.hasPermission(domain1, method1), |
| 73 | + true, |
| 74 | + 'should return true for domain1 and method1' |
| 75 | + ); |
| 76 | + t.equal( |
| 77 | + ctrl.hasPermission(domain2, method2), |
| 78 | + true, |
| 79 | + 'should return true for domain2 and method2' |
| 80 | + ); |
| 81 | + t.end(); |
| 82 | +}); |
| 83 | + |
| 84 | +test('hasPermissions returns with permissions but wrong domain', (t) => { |
| 85 | + const ctrl = new CapabilitiesController({ |
| 86 | + requestUserApproval: noop, |
| 87 | + }, |
| 88 | + { |
| 89 | + domains: { |
| 90 | + [domain1]: clone(domains[domain1]), |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + t.equal(ctrl.hasPermissions(domain1), true, 'should return true for domain1'); |
| 95 | + t.equal(ctrl.hasPermissions(domain2), false, 'should return false for domain2'); |
| 96 | + t.end(); |
| 97 | +}); |
| 98 | + |
| 99 | +test('hasPermission returns false with permissions but wrong method', (t) => { |
| 100 | + const ctrl = new CapabilitiesController({ |
| 101 | + requestUserApproval: noop, |
| 102 | + }, |
| 103 | + { |
| 104 | + domains: clone(domains), |
| 105 | + }); |
| 106 | + |
| 107 | + t.equal( |
| 108 | + ctrl.hasPermission(domain1, method2), |
| 109 | + false, |
| 110 | + 'should return false for domain1 and method2' |
| 111 | + ); |
| 112 | + t.equal( |
| 113 | + ctrl.hasPermission(domain2, method1), |
| 114 | + false, |
| 115 | + 'should return false for domain2 and method1' |
| 116 | + ); |
| 117 | + t.end(); |
| 118 | +}); |
0 commit comments