|
| 1 | +describe('Auth', () => { |
| 2 | + var Auth = require('../src/Auth.js').Auth; |
| 3 | + |
| 4 | + describe('getUserRoles', () => { |
| 5 | + var auth; |
| 6 | + var config; |
| 7 | + var cacheController; |
| 8 | + var currentRoles = null; |
| 9 | + var currentUserId = 'userId'; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + currentRoles = ['role:userId']; |
| 13 | + |
| 14 | + config = { |
| 15 | + cacheController: { |
| 16 | + role: { |
| 17 | + get: () => Promise.resolve(currentRoles), |
| 18 | + set: jasmine.createSpy('set') |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + spyOn(config.cacheController.role, 'get').and.callThrough(); |
| 23 | + |
| 24 | + auth = new Auth({ |
| 25 | + config: config, |
| 26 | + isMaster: false, |
| 27 | + user: { |
| 28 | + id: currentUserId |
| 29 | + }, |
| 30 | + installationId: 'installationId' |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should get user roles from the cache', (done) => { |
| 35 | + auth.getUserRoles() |
| 36 | + .then((roles) => { |
| 37 | + var firstSet = config.cacheController.role.set.calls.first(); |
| 38 | + expect(firstSet).toEqual(undefined); |
| 39 | + |
| 40 | + var firstGet = config.cacheController.role.get.calls.first(); |
| 41 | + expect(firstGet.args[0]).toEqual(currentUserId); |
| 42 | + expect(roles).toEqual(currentRoles); |
| 43 | + done(); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should only query the roles once', (done) => { |
| 48 | + var loadRolesSpy = spyOn(auth, '_loadRoles').and.callThrough(); |
| 49 | + auth.getUserRoles() |
| 50 | + .then((roles) => { |
| 51 | + expect(roles).toEqual(currentRoles); |
| 52 | + return auth.getUserRoles() |
| 53 | + }) |
| 54 | + .then((roles) => auth.getUserRoles()) |
| 55 | + .then((roles) => auth.getUserRoles()) |
| 56 | + .then((roles) => { |
| 57 | + // Should only call the cache adapter once. |
| 58 | + expect(config.cacheController.role.get.calls.count()).toEqual(1); |
| 59 | + expect(loadRolesSpy.calls.count()).toEqual(1); |
| 60 | + |
| 61 | + var firstGet = config.cacheController.role.get.calls.first(); |
| 62 | + expect(firstGet.args[0]).toEqual(currentUserId); |
| 63 | + expect(roles).toEqual(currentRoles); |
| 64 | + done(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should not have any roles with no user', (done) => { |
| 69 | + auth.user = null |
| 70 | + auth.getUserRoles() |
| 71 | + .then((roles) => expect(roles).toEqual([])) |
| 72 | + .then(() => done()); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should not have any user roles with master', (done) => { |
| 76 | + auth.isMaster = true |
| 77 | + auth.getUserRoles() |
| 78 | + .then((roles) => expect(roles).toEqual([])) |
| 79 | + .then(() => done()); |
| 80 | + }) |
| 81 | + |
| 82 | + }); |
| 83 | +}); |
0 commit comments