This repository has been archived by the owner on Nov 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-auth.spec.js
94 lines (68 loc) · 2.62 KB
/
angular-auth.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
describe('auth', function () {
var $rootScope, $state, auth;
beforeEach(function() {
module('boundstate.auth');
module(function ($stateProvider) {
$stateProvider
.state('login', {});
});
inject(function (_$rootScope_, _$state_, _auth_, $injector, localStorageService) {
$rootScope = _$rootScope_;
$state = _$state_;
auth = _auth_;
localStorageService.clearAll();
auth.initialize();
});
});
describe('login', function() {
it('sets user and token and emits "auth.login" event', inject(function () {
var loginEvent = jasmine.createSpy();
$rootScope.$on('auth.login', loginEvent);
auth.login('example-token', {name: 'Jon Snow'});
expect(loginEvent.calls.count()).toEqual(1);
expect(auth.getToken()).toEqual('example-token');
expect(auth.getUser().name).toEqual('Jon Snow');
}));
});
describe('logout', function() {
it('clears user and token, emits "auth.logout" event, and redirects to "login" state', inject(function () {
var logoutEvent = jasmine.createSpy();
$rootScope.$on('auth.logout', logoutEvent);
spyOn($state, 'transitionTo');
auth.login('example-token', {name: 'Jon Snow'});
auth.logout();
expect(auth.getToken()).toBeFalsy();
expect(auth.getUser()).toEqual({});
expect(logoutEvent.calls.count()).toEqual(1);
expect($state.transitionTo.calls.mostRecent().args[0]).toEqual('login');
}));
});
describe('updateUser', function() {
it('updates user data', inject(function () {
auth.login('example-token', {name: 'Jon Snow'});
auth.updateUser({name: 'Bill'});
expect(auth.getUser().name).toEqual('Bill');
}));
});
describe('isGuest', function() {
it('returns true if user is not logged in', inject(function() {
expect(auth.isGuest()).toEqual(true);
auth.login('example-token', {name: 'Jon Snow'});
expect(auth.isGuest()).toEqual(false);
}));
});
describe('hasRole', function() {
it('returns true if the user object contains the role', inject(function() {
auth.login('example-token', {name: 'Jon Snow', roles: ['president']});
expect(auth.hasRole('president')).toEqual(true);
expect(auth.hasRole('secretary')).toEqual(false);
}));
});
describe('hasAnyRole', function() {
it('returns true if the user object contains any role', inject(function() {
auth.login('example-token', {name: 'Jon Snow', roles: ['president']});
expect(auth.hasAnyRole(['janitor'])).toEqual(false);
expect(auth.hasAnyRole(['president', 'janitor'])).toEqual(true);
}));
});
});