forked from meanjs/mean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword.client.controller.tests.js
More file actions
193 lines (160 loc) · 6.6 KB
/
Copy pathpassword.client.controller.tests.js
File metadata and controls
193 lines (160 loc) · 6.6 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
'use strict';
(function() {
// Password controller Spec
describe('PasswordController', function() {
// Initialize global variables
var PasswordController,
scope,
$httpBackend,
$stateParams,
$location,
$window,
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 in user', function() {
beforeEach(inject(function($controller, $rootScope, _UsersService_, _Authentication_, _$stateParams_, _$httpBackend_, _$location_) {
// Set a new global scope
scope = $rootScope.$new();
// Point global variables to injected services
$stateParams = _$stateParams_;
$httpBackend = _$httpBackend_;
$location = _$location_;
$location.path = jasmine.createSpy().and.returnValue(true);
// Ignore parent template gets on state transition
$httpBackend.whenGET('/modules/core/client/views/404.client.view.html').respond(200);
// Mock logged in user
_Authentication_.user = {
username: 'test',
roles: ['user']
};
// Initialize the Authentication controller
PasswordController = $controller('PasswordController as vm', {
$scope: scope
});
}));
it('should redirect logged in user to home', function() {
expect($location.path).toHaveBeenCalledWith('/');
});
});
describe('Logged out user', function() {
beforeEach(inject(function($controller, $rootScope, _$window_, _$stateParams_, _$httpBackend_, _$location_, _Notification_) {
// Set a new global scope
scope = $rootScope.$new();
// Point global variables to injected services
$stateParams = _$stateParams_;
$httpBackend = _$httpBackend_;
$location = _$location_;
$location.path = jasmine.createSpy().and.returnValue(true);
$window = _$window_;
$window.user = null;
Notification = _Notification_;
spyOn(Notification, 'error');
spyOn(Notification, 'success');
// Ignore parent template gets on state transition
$httpBackend.whenGET('/modules/core/client/views/404.client.view.html').respond(200);
$httpBackend.whenGET('/modules/core/client/views/400.client.view.html').respond(200);
// Initialize the Authentication controller
PasswordController = $controller('PasswordController as vm', {
$scope: scope
});
}));
it('should not redirect to home', function() {
expect($location.path).not.toHaveBeenCalledWith('/');
});
describe('askForPasswordReset', function() {
var credentials = {
username: 'test',
password: 'P@ssw0rd!!'
};
beforeEach(function() {
scope.vm.credentials = credentials;
});
describe('POST error', function() {
var errorMessage = 'No account with that username has been found';
beforeEach(function() {
$httpBackend.when('POST', '/api/auth/forgot', credentials).respond(400, {
'message': errorMessage
});
scope.vm.askForPasswordReset(true);
$httpBackend.flush();
});
it('should clear form', function() {
expect(scope.vm.credentials).toBe(null);
});
it('should call Notification.error with response message', function() {
expect(Notification.error).toHaveBeenCalledWith({ message: errorMessage, title: '<i class="glyphicon glyphicon-remove"></i> Failed to send password reset email!', delay: 4000 });
});
});
describe('POST success', function() {
var successMessage = 'An email has been sent to the provided email with further instructions.';
beforeEach(function() {
$httpBackend.when('POST', '/api/auth/forgot', credentials).respond({
'message': successMessage
});
scope.vm.askForPasswordReset(true);
$httpBackend.flush();
});
it('should clear form', function() {
expect(scope.vm.credentials).toBe(null);
});
it('should call Notification.success with response message', function() {
expect(Notification.success).toHaveBeenCalledWith({ message: successMessage, title: '<i class="glyphicon glyphicon-ok"></i> Password reset email sent successfully!' });
});
});
});
describe('resetUserPassword', function() {
var token = 'testToken';
var passwordDetails = {
password: 'test'
};
beforeEach(function() {
$stateParams.token = token;
scope.vm.passwordDetails = passwordDetails;
});
it('POST error should call Notification.error with response message', function() {
var errorMessage = 'Passwords do not match';
$httpBackend.when('POST', '/api/auth/reset/' + token, passwordDetails).respond(400, {
'message': errorMessage
});
scope.vm.resetUserPassword(true);
$httpBackend.flush();
expect(Notification.error).toHaveBeenCalledWith({ message: errorMessage, title: '<i class="glyphicon glyphicon-remove"></i> Password reset failed!', delay: 4000 });
});
describe('POST success', function() {
var user = {
username: 'test'
};
beforeEach(function() {
$httpBackend.when('POST', '/api/auth/reset/' + token, passwordDetails).respond(user);
scope.vm.resetUserPassword(true);
$httpBackend.flush();
});
it('should clear password form', function() {
expect(scope.vm.passwordDetails).toBe(null);
});
it('should attach user profile', function() {
expect(scope.vm.authentication.user.username).toEqual(user.username);
});
it('should redirect to password reset success view with Notification.success', function() {
expect(Notification.success).toHaveBeenCalledWith({ message: '<i class="glyphicon glyphicon-ok"></i> Password reset successful!' });
expect($location.path).toHaveBeenCalledWith('/password/reset/success');
});
});
});
});
});
}());