Skip to content

Commit 2eb861d

Browse files
blachadrew-gross
authored andcommitted
Fixing typo in _loadRoles which prevents caching of roles. (#2063)
1 parent ae0ba7b commit 2eb861d

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

spec/Auth.spec.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
});

src/Auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Auth.prototype._loadRoles = function() {
9898
var cacheAdapter = this.config.cacheController;
9999
return cacheAdapter.role.get(this.user.id).then((cachedRoles) => {
100100
if (cachedRoles != null) {
101-
this.fetchedroles = true;
101+
this.fetchedRoles = true;
102102
this.userRoles = cachedRoles;
103103
return Promise.resolve(cachedRoles);
104104
}

0 commit comments

Comments
 (0)