Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit 6514044

Browse files
committed
adding tests for directives
1 parent f733efb commit 6514044

File tree

7 files changed

+765
-5
lines changed

7 files changed

+765
-5
lines changed

gruntfile.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,26 @@ module.exports = function (grunt) {
258258
});
259259
});
260260

261+
// Drops the MongoDB database, used in e2e testing
262+
grunt.task.registerTask('dropdb', 'drop the database', function () {
263+
// async mode
264+
var done = this.async();
265+
266+
// Use mongoose configuration
267+
var mongoose = require('./config/lib/mongoose.js');
268+
269+
mongoose.connect(function (db) {
270+
db.connection.db.dropDatabase(function (err) {
271+
if (err) {
272+
console.log(err);
273+
} else {
274+
console.log('Successfully dropped db: ', db.connection.db.databaseName);
275+
}
276+
db.connection.db.close(done);
277+
});
278+
});
279+
});
280+
261281
grunt.task.registerTask('server', 'Starting the server', function () {
262282
// Get the callback
263283
var done = this.async();
@@ -279,7 +299,7 @@ module.exports = function (grunt) {
279299
grunt.registerTask('test', ['env:test', 'lint', 'mkdir:upload', 'copy:localConfig', 'server', 'mochaTest', 'karma:unit', 'protractor']);
280300
grunt.registerTask('test:server', ['env:test', 'lint', 'server', 'mochaTest']);
281301
grunt.registerTask('test:client', ['env:test', 'lint', 'server', 'karma:unit']);
282-
grunt.registerTask('test:e2e', ['env:test', 'lint', 'server', 'protractor']);
302+
grunt.registerTask('test:e2e', ['env:test', 'lint', 'dropdb', 'server', 'protractor']);
283303

284304
// Run project coverage
285305
grunt.registerTask('coverage', ['env:test', 'lint', 'mocha_istanbul:coverage']);

gulpfile.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ gulp.task('karma', function (done) {
199199
}));
200200
});
201201

202+
// Drops the MongoDB database, used in e2e testing
203+
gulp.task('dropdb', function (done) {
204+
// Use mongoose configuration
205+
var mongoose = require('./config/lib/mongoose.js');
206+
207+
mongoose.connect(function (db) {
208+
db.connection.db.dropDatabase(function (err) {
209+
if(err) {
210+
console.log(err);
211+
} else {
212+
console.log('Successfully dropped db: ', db.connection.db.databaseName);
213+
}
214+
db.connection.db.close(done);
215+
});
216+
});
217+
});
218+
202219
// Downloads the selenium webdriver
203220
gulp.task('webdriver_update', webdriver_update);
204221

@@ -248,7 +265,7 @@ gulp.task('test:client', function (done) {
248265
});
249266

250267
gulp.task('test:e2e', function (done) {
251-
runSequence('env:test', 'lint', 'nodemon', 'protractor', done);
268+
runSequence('env:test', 'lint', 'dropdb', 'nodemon', 'protractor', done);
252269
});
253270

254271
// Run the project in development mode

modules/users/client/directives/password-validator.client.directive.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ angular.module('users')
1111
var result = PasswordValidator.getResult(password);
1212
var requirementsIdx = 0;
1313

14-
// requirements Meter - visual indicator for users
14+
// Requirements Meter - visual indicator for users
1515
var requirementsMeter = [
1616
{ color: 'danger', progress: '20' },
1717
{ color: 'warning', progress: '40' },
@@ -33,6 +33,7 @@ angular.module('users')
3333
status = false;
3434
} else {
3535
scope.popoverMsg = '';
36+
scope.passwordErrors = [];
3637
status = true;
3738
}
3839
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
'use strict';
2+
3+
(function() {
4+
// Password Validator Directive Spec
5+
describe('PasswordValidatorDirective', function() {
6+
// Initialize global variables
7+
var scope,
8+
element,
9+
$compile,
10+
form;
11+
12+
// Load the main application module
13+
beforeEach(module(ApplicationConfiguration.applicationModuleName));
14+
15+
beforeEach(inject(function(_$rootScope_, _$compile_) {
16+
// Set a new global scope
17+
scope = _$rootScope_.$new();
18+
$compile = _$compile_;
19+
20+
scope.passwordMock = {
21+
password: 'P@ssw0rd!!'
22+
};
23+
}));
24+
25+
function compileDirective(template) {
26+
// function to compile a fresh directive with the given template, or a default one
27+
// input form with directive
28+
if (!template) template = '<input type="password" id="password" name="password" ng-model="passwordMock.password" password-validator required>';
29+
template = '<form name="form"><div>' + template + '<input type="submit">submit form</input></div></form>';
30+
31+
// inject allows you to use AngularJS dependency injection
32+
// to retrieve and use other services
33+
inject(function($compile) {
34+
var form = $compile(template)(scope);
35+
element = form.find('div');
36+
37+
// $digest is necessary to finalize the directive generation
38+
scope.$digest();
39+
});
40+
}
41+
42+
describe('Initialize', function() {
43+
beforeEach(function () {
44+
compileDirective();
45+
});
46+
47+
it('should produce the password input', function () {
48+
expect(element.find('input').length).toEqual(2);
49+
});
50+
51+
it('should check form validity upon initializing', function () {
52+
expect(scope.form.$valid).toBeTruthy();
53+
});
54+
55+
});
56+
57+
it('should set form to invalid with empty password', function () {
58+
scope.passwordMock.password = '';
59+
compileDirective();
60+
scope.$digest();
61+
62+
expect(scope.form.password.$valid).toBeFalsy();
63+
expect(scope.form.password.$error.required).toBeTruthy();
64+
expect(scope.requirementsColor).toEqual(undefined);
65+
expect(scope.requirementsProgress).toEqual(undefined);
66+
});
67+
68+
it('should be valid when password meets requirements - "P@ssw0rd!!""', function() {
69+
scope.passwordMock.password = 'P@ssw0rd!!';
70+
compileDirective();
71+
scope.$digest();
72+
73+
expect(scope.form.password.$valid).toBeTruthy();
74+
expect(scope.form.password.$error).toEqual({});
75+
expect(scope.requirementsColor).toEqual('success');
76+
expect(scope.requirementsProgress).toEqual('100');
77+
});
78+
79+
it('should be valid when password meets requirements with a passphrase', function() {
80+
scope.passwordMock.password = 'Open-Source Full-Stack Solution for MEAN';
81+
compileDirective();
82+
scope.$digest();
83+
84+
expect(scope.form.password.$valid).toBeTruthy();
85+
expect(scope.form.password.$error).toEqual({});
86+
expect(scope.requirementsColor).toEqual('success');
87+
expect(scope.requirementsProgress).toEqual('100');
88+
});
89+
90+
it('should not allow a less than 10 characters long - "P@$$w0rd!"', function() {
91+
scope.passwordMock.password = 'P@$$w0rd!';
92+
compileDirective();
93+
scope.$digest();
94+
95+
expect(scope.form.password.$valid).toBeFalsy();
96+
expect(scope.form.password.$error.required).toBeFalsy();
97+
expect(scope.passwordErrors).toEqual(['The password must be at least 10 characters long.']);
98+
expect(scope.requirementsColor).toEqual('primary');
99+
expect(scope.requirementsProgress).toEqual('80');
100+
});
101+
102+
it('should not allow a greater than 128 characters long', function() {
103+
scope.passwordMock.password = ')!/uLT="lh&:`6X!]|15o!$!TJf,.13l?vG].-j],lFPe/QhwN#{Z<[*1nX@n1^?WW-%_.*D)m$toB+N7z}kcN#B_d(f41h%w@0F!]igtSQ1gl~6sEV&r~}~1ub>If1c+';
104+
compileDirective();
105+
scope.$digest();
106+
107+
expect(scope.form.password.$valid).toBeFalsy();
108+
expect(scope.form.password.$error.required).toBeFalsy();
109+
expect(scope.passwordErrors).toEqual(['The password must be fewer than 128 characters.']);
110+
expect(scope.requirementsColor).toEqual('primary');
111+
expect(scope.requirementsProgress).toEqual('80');
112+
});
113+
114+
it('should not allow more than 3 or more repeating characters - "P@$$w0rd!!!"', function() {
115+
scope.passwordMock.password = 'P@$$w0rd!!!';
116+
compileDirective();
117+
scope.$digest();
118+
119+
expect(scope.form.password.$valid).toBeFalsy();
120+
expect(scope.form.password.$error.required).toBeFalsy();
121+
expect(scope.passwordErrors).toEqual(['The password may not contain sequences of three or more repeated characters.']);
122+
expect(scope.requirementsColor).toEqual('primary');
123+
expect(scope.requirementsProgress).toEqual('80');
124+
});
125+
126+
it('should not allow a password with no uppercase letters - "p@$$w0rd!!"', function() {
127+
scope.passwordMock.password = 'p@$$w0rd!!';
128+
compileDirective();
129+
scope.$digest();
130+
131+
expect(scope.form.password.$valid).toBeFalsy();
132+
expect(scope.form.password.$error.required).toBeFalsy();
133+
expect(scope.passwordErrors).toEqual(['The password must contain at least one uppercase letter.']);
134+
expect(scope.requirementsColor).toEqual('primary');
135+
expect(scope.requirementsProgress).toEqual('80');
136+
});
137+
138+
it('should not allow a password with less than one number - "P@$$word!!"', function() {
139+
scope.passwordMock.password = 'P@$$word!!';
140+
compileDirective();
141+
scope.$digest();
142+
143+
expect(scope.form.password.$valid).toBeFalsy();
144+
expect(scope.form.password.$error.required).toBeFalsy();
145+
expect(scope.passwordErrors).toEqual(['The password must contain at least one number.']);
146+
expect(scope.requirementsColor).toEqual('primary');
147+
expect(scope.requirementsProgress).toEqual('80');
148+
});
149+
150+
it('should not allow a password with less than one special character - "Passw0rdss"', function() {
151+
scope.passwordMock.password = 'Passw0rdss';
152+
compileDirective();
153+
scope.$digest();
154+
155+
expect(scope.form.password.$valid).toBeFalsy();
156+
expect(scope.form.password.$error.required).toBeFalsy();
157+
expect(scope.passwordErrors).toEqual(['The password must contain at least one special character.']);
158+
expect(scope.requirementsColor).toEqual('primary');
159+
expect(scope.requirementsProgress).toEqual('80');
160+
});
161+
162+
it('should show 20% progress and "danger" color', function() {
163+
scope.passwordMock.password = 'P';
164+
compileDirective();
165+
scope.$digest();
166+
167+
expect(scope.requirementsColor).toEqual('danger');
168+
expect(scope.requirementsProgress).toEqual('20');
169+
});
170+
171+
it('should show 40% progress and "warning" color', function() {
172+
scope.passwordMock.password = 'Pa';
173+
compileDirective();
174+
scope.$digest();
175+
176+
expect(scope.requirementsColor).toEqual('warning');
177+
expect(scope.requirementsProgress).toEqual('40');
178+
});
179+
180+
it('should show 60% progress and "info" color', function() {
181+
scope.passwordMock.password = 'Pa$';
182+
compileDirective();
183+
scope.$digest();
184+
185+
expect(scope.requirementsColor).toEqual('info');
186+
expect(scope.requirementsProgress).toEqual('60');
187+
});
188+
189+
it('should show 80% progress and "primary" color', function() {
190+
scope.passwordMock.password = 'Pa$$w0rd';
191+
compileDirective();
192+
scope.$digest();
193+
194+
expect(scope.requirementsColor).toEqual('primary');
195+
expect(scope.requirementsProgress).toEqual('80');
196+
});
197+
198+
it('should show 100% progress and "success" color', function() {
199+
scope.passwordMock.password = 'Pa$$w0rd!!';
200+
compileDirective();
201+
scope.$digest();
202+
203+
expect(scope.requirementsColor).toEqual('success');
204+
expect(scope.requirementsProgress).toEqual('100');
205+
});
206+
207+
});
208+
}());
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
(function() {
4+
// Password Verify Directive Spec
5+
describe('PasswordVerifyDirective', function() {
6+
// Initialize global variables
7+
var scope,
8+
element,
9+
$compile,
10+
form;
11+
12+
// Load the main application module
13+
beforeEach(module(ApplicationConfiguration.applicationModuleName));
14+
15+
beforeEach(inject(function(_$rootScope_, _$compile_) {
16+
// Set a new global scope
17+
scope = _$rootScope_.$new();
18+
$compile = _$compile_;
19+
20+
scope.passwordMock = {
21+
newPassword: 'P@ssw0rd!!',
22+
verifyPassword: 'P@ssw0rd!!'
23+
};
24+
}));
25+
26+
function compileDirective(template) {
27+
// function to compile a fresh directive with the given template, or a default one
28+
// input form with directive
29+
if (!template) template = '<input type="password" id="newPassword" name="newPassword" class="form-control" ng-model="passwordMock.newPassword" placeholder="New Password" autocomplete="new-password" popover="{{popoverMsg}}" popover-trigger="focus" popover-placement="top" password-validator required>' +
30+
'<input type="password" id="verifyPassword" name="verifyPassword" class="form-control" ng-model="passwordMock.verifyPassword" placeholder="Verify Password" password-verify="passwordMock.newPassword" required>';
31+
template = '<form name="form"><div>' + template + '<input type="submit">submit form</input></div></form>';
32+
33+
// inject allows you to use AngularJS dependency injection
34+
// to retrieve and use other services
35+
inject(function($compile) {
36+
var form = $compile(template)(scope);
37+
element = form.find('div');
38+
39+
// $digest is necessary to finalize the directive generation
40+
scope.$digest();
41+
});
42+
}
43+
44+
describe('Initialize', function() {
45+
beforeEach(function () {
46+
compileDirective();
47+
});
48+
49+
it('should produce the password input', function () {
50+
expect(element.find('input').length).toEqual(3);
51+
});
52+
53+
it('should check form validity upon initializing', function () {
54+
expect(scope.form.$valid).toBeTruthy();
55+
});
56+
57+
});
58+
59+
it('should not show error when passwords match', function () {
60+
compileDirective();
61+
scope.passwordMock.newPassword = 'P@ssw0rd!!';
62+
scope.passwordMock.verifyPassword = 'P@ssw0rd!!';
63+
scope.$digest();
64+
65+
expect(scope.form.newPassword.$valid).toBeTruthy();
66+
expect(scope.form.newPassword.$error).toEqual({});
67+
expect(scope.form.verifyPassword.$valid).toBeTruthy();
68+
expect(scope.form.verifyPassword.$error).toEqual({});
69+
expect(scope.form.$valid).toBeTruthy();
70+
});
71+
72+
it('should show error when passwords do not match', function () {
73+
compileDirective();
74+
scope.passwordMock.newPassword = 'P@ssw0rd!!';
75+
scope.passwordMock.verifyPassword = 'P@ssw0rd!';
76+
scope.$digest();
77+
78+
expect(scope.form.newPassword.$valid).toBeTruthy();
79+
expect(scope.form.newPassword.$error).toEqual({});
80+
expect(scope.form.verifyPassword.$valid).toBeFalsy();
81+
expect(scope.form.verifyPassword.$error.passwordVerify).toBeTruthy();
82+
expect(scope.form.$valid).toBeFalsy();
83+
});
84+
85+
});
86+
}());

modules/users/tests/client/password.client.controller.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
(function() {
4-
// Authentication controller Spec
4+
// Password controller Spec
55
describe('PasswordController', function() {
66
// Initialize global variables
77
var PasswordController,

0 commit comments

Comments
 (0)