forked from meanjs/mean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.client.controller.tests.js
More file actions
200 lines (160 loc) · 6.98 KB
/
Copy pathauthentication.client.controller.tests.js
File metadata and controls
200 lines (160 loc) · 6.98 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
(function () {
// Authentication controller Spec
describe('AuthenticationController', function () {
// Initialize global variables
var AuthenticationController,
scope,
$httpBackend,
$stateParams,
$state,
$location,
Notification;
beforeEach(function () {
jasmine.addMatchers({
toEqualData: function (util, customEqualityTesters) {
return {
compare: function (actual, expected) {
return {
pass: angular.equals(actual, expected)
};
}
};
}
});
});
// Load the main application module
beforeEach(module(ApplicationConfiguration.applicationModuleName));
describe('Logged out user', function () {
// The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
// This allows us to inject a service but then attach it to a variable
// with the same name as the service.
beforeEach(inject(function ($controller, $rootScope, _$location_, _$stateParams_, _$httpBackend_, _Notification_) {
// Set a new global scope
scope = $rootScope.$new();
// Point global variables to injected services
$stateParams = _$stateParams_;
$httpBackend = _$httpBackend_;
$location = _$location_;
Notification = _Notification_;
// Spy on Notification
spyOn(Notification, 'error');
spyOn(Notification, 'success');
// Ignore parent template get on state transitions
$httpBackend.whenGET('/modules/core/client/views/home.client.view.html').respond(200);
$httpBackend.whenGET('/modules/core/client/views/400.client.view.html').respond(200);
// Initialize the Authentication controller
AuthenticationController = $controller('AuthenticationController as vm', {
$scope: scope
});
}));
describe('$scope.signin()', function () {
it('should login with a correct user and password', inject(function ($templateCache) {
$templateCache.put('/modules/core/client/views/home.client.view.html', '');
// Test expected GET request
$httpBackend.when('POST', '/api/auth/signin').respond(200, { username: 'Fred' });
scope.vm.signin(true);
$httpBackend.flush();
// Test scope value
expect(scope.vm.authentication.user.username).toEqual('Fred');
expect($location.url()).toEqual('/');
}));
it('should login with a correct email and password', inject(function ($templateCache) {
$templateCache.put('/modules/core/client/views/home.client.view.html', '');
// Test expected GET request
$httpBackend.when('POST', '/api/auth/signin').respond(200, { email: 'Fred@email.com' });
scope.vm.signin(true);
$httpBackend.flush();
// Test scope value
expect(scope.vm.authentication.user.email).toEqual('Fred@email.com');
expect($location.url()).toEqual('/');
}));
it('should be redirected to previous state after successful login',
inject(function (_$state_) {
$state = _$state_;
$state.previous = {
state: {
name: 'articles.create'
},
params: {},
href: '/articles/create'
};
spyOn($state, 'transitionTo');
spyOn($state, 'go');
// Test expected GET request
$httpBackend.when('POST', '/api/auth/signin').respond(200, 'Fred');
scope.vm.signin(true);
$httpBackend.flush();
// Test scope value
expect($state.go).toHaveBeenCalled();
expect($state.go).toHaveBeenCalledWith($state.previous.state.name, $state.previous.params);
}));
it('should fail to log in with nothing', function () {
// Test expected POST request
$httpBackend.expectPOST('/api/auth/signin').respond(400, {
'message': 'Missing credentials'
});
scope.vm.signin(true);
$httpBackend.flush();
// Test Notification.error is called
expect(Notification.error).toHaveBeenCalledWith({ message: 'Missing credentials', title: '<i class="glyphicon glyphicon-remove"></i> Signin Error!', delay: 6000 });
});
it('should fail to log in with wrong credentials', function () {
// Foo/Bar combo assumed to not exist
scope.vm.authentication.user = { username: 'Foo' };
scope.vm.credentials = 'Bar';
// Test expected POST request
$httpBackend.expectPOST('/api/auth/signin').respond(400, {
'message': 'Unknown user'
});
scope.vm.signin(true);
$httpBackend.flush();
// Test Notification.error is called
expect(Notification.error).toHaveBeenCalledWith({ message: 'Unknown user', title: '<i class="glyphicon glyphicon-remove"></i> Signin Error!', delay: 6000 });
});
});
describe('$scope.signup()', function () {
it('should register with correct data', inject(function ($templateCache) {
$templateCache.put('/modules/core/client/views/home.client.view.html', '');
// Test expected GET request
scope.vm.authentication.user = 'Fred';
$httpBackend.when('POST', '/api/auth/signup').respond(200, { username: 'Fred' });
scope.vm.signup(true);
$httpBackend.flush();
// test scope value
expect(scope.vm.authentication.user.username).toBe('Fred');
expect(Notification.success).toHaveBeenCalledWith({ message: '<i class="glyphicon glyphicon-ok"></i> Signup successful!' });
expect($location.url()).toBe('/');
}));
it('should fail to register with duplicate Username', function () {
// Test expected POST request
$httpBackend.when('POST', '/api/auth/signup').respond(400, {
'message': 'Username already exists'
});
scope.vm.signup(true);
$httpBackend.flush();
// Test Notification.error is called
expect(Notification.error).toHaveBeenCalledWith({ message: 'Username already exists', title: '<i class="glyphicon glyphicon-remove"></i> Signup Error!', delay: 6000 });
});
});
});
describe('Logged in user', function () {
beforeEach(inject(function ($controller, $rootScope, _$location_, _Authentication_) {
scope = $rootScope.$new();
$location = _$location_;
$location.path = jasmine.createSpy().and.returnValue(true);
// Mock logged in user
_Authentication_.user = {
username: 'test',
roles: ['user']
};
AuthenticationController = $controller('AuthenticationController as vm', {
$scope: scope
});
}));
it('should be redirected to home', function () {
expect($location.path).toHaveBeenCalledWith('/');
});
});
});
}());