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

Commit

Permalink
adding tests for directives
Browse files Browse the repository at this point in the history
  • Loading branch information
jloveland committed Oct 14, 2015
1 parent f733efb commit 6514044
Show file tree
Hide file tree
Showing 7 changed files with 765 additions and 5 deletions.
22 changes: 21 additions & 1 deletion gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,26 @@ module.exports = function (grunt) {
});
});

// Drops the MongoDB database, used in e2e testing
grunt.task.registerTask('dropdb', 'drop the database', function () {
// async mode
var done = this.async();

// Use mongoose configuration
var mongoose = require('./config/lib/mongoose.js');

mongoose.connect(function (db) {
db.connection.db.dropDatabase(function (err) {
if (err) {
console.log(err);
} else {
console.log('Successfully dropped db: ', db.connection.db.databaseName);
}
db.connection.db.close(done);
});
});
});

grunt.task.registerTask('server', 'Starting the server', function () {
// Get the callback
var done = this.async();
Expand All @@ -279,7 +299,7 @@ module.exports = function (grunt) {
grunt.registerTask('test', ['env:test', 'lint', 'mkdir:upload', 'copy:localConfig', 'server', 'mochaTest', 'karma:unit', 'protractor']);
grunt.registerTask('test:server', ['env:test', 'lint', 'server', 'mochaTest']);
grunt.registerTask('test:client', ['env:test', 'lint', 'server', 'karma:unit']);
grunt.registerTask('test:e2e', ['env:test', 'lint', 'server', 'protractor']);
grunt.registerTask('test:e2e', ['env:test', 'lint', 'dropdb', 'server', 'protractor']);

// Run project coverage
grunt.registerTask('coverage', ['env:test', 'lint', 'mocha_istanbul:coverage']);
Expand Down
19 changes: 18 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,23 @@ gulp.task('karma', function (done) {
}));
});

// Drops the MongoDB database, used in e2e testing
gulp.task('dropdb', function (done) {
// Use mongoose configuration
var mongoose = require('./config/lib/mongoose.js');

mongoose.connect(function (db) {
db.connection.db.dropDatabase(function (err) {
if(err) {
console.log(err);
} else {
console.log('Successfully dropped db: ', db.connection.db.databaseName);
}
db.connection.db.close(done);
});
});
});

// Downloads the selenium webdriver
gulp.task('webdriver_update', webdriver_update);

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

gulp.task('test:e2e', function (done) {
runSequence('env:test', 'lint', 'nodemon', 'protractor', done);
runSequence('env:test', 'lint', 'dropdb', 'nodemon', 'protractor', done);
});

// Run the project in development mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ angular.module('users')
var result = PasswordValidator.getResult(password);
var requirementsIdx = 0;

// requirements Meter - visual indicator for users
// Requirements Meter - visual indicator for users
var requirementsMeter = [
{ color: 'danger', progress: '20' },
{ color: 'warning', progress: '40' },
Expand All @@ -33,6 +33,7 @@ angular.module('users')
status = false;
} else {
scope.popoverMsg = '';
scope.passwordErrors = [];
status = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
'use strict';

(function() {
// Password Validator Directive Spec
describe('PasswordValidatorDirective', function() {
// Initialize global variables
var scope,
element,
$compile,
form;

// Load the main application module
beforeEach(module(ApplicationConfiguration.applicationModuleName));

beforeEach(inject(function(_$rootScope_, _$compile_) {
// Set a new global scope
scope = _$rootScope_.$new();
$compile = _$compile_;

scope.passwordMock = {
password: 'P@ssw0rd!!'
};
}));

function compileDirective(template) {
// function to compile a fresh directive with the given template, or a default one
// input form with directive
if (!template) template = '<input type="password" id="password" name="password" ng-model="passwordMock.password" password-validator required>';
template = '<form name="form"><div>' + template + '<input type="submit">submit form</input></div></form>';

// inject allows you to use AngularJS dependency injection
// to retrieve and use other services
inject(function($compile) {
var form = $compile(template)(scope);
element = form.find('div');

// $digest is necessary to finalize the directive generation
scope.$digest();
});
}

describe('Initialize', function() {
beforeEach(function () {
compileDirective();
});

it('should produce the password input', function () {
expect(element.find('input').length).toEqual(2);
});

it('should check form validity upon initializing', function () {
expect(scope.form.$valid).toBeTruthy();
});

});

it('should set form to invalid with empty password', function () {
scope.passwordMock.password = '';
compileDirective();
scope.$digest();

expect(scope.form.password.$valid).toBeFalsy();
expect(scope.form.password.$error.required).toBeTruthy();
expect(scope.requirementsColor).toEqual(undefined);
expect(scope.requirementsProgress).toEqual(undefined);
});

it('should be valid when password meets requirements - "P@ssw0rd!!""', function() {
scope.passwordMock.password = 'P@ssw0rd!!';
compileDirective();
scope.$digest();

expect(scope.form.password.$valid).toBeTruthy();
expect(scope.form.password.$error).toEqual({});
expect(scope.requirementsColor).toEqual('success');
expect(scope.requirementsProgress).toEqual('100');
});

it('should be valid when password meets requirements with a passphrase', function() {
scope.passwordMock.password = 'Open-Source Full-Stack Solution for MEAN';
compileDirective();
scope.$digest();

expect(scope.form.password.$valid).toBeTruthy();
expect(scope.form.password.$error).toEqual({});
expect(scope.requirementsColor).toEqual('success');
expect(scope.requirementsProgress).toEqual('100');
});

it('should not allow a less than 10 characters long - "P@$$w0rd!"', function() {
scope.passwordMock.password = 'P@$$w0rd!';
compileDirective();
scope.$digest();

expect(scope.form.password.$valid).toBeFalsy();
expect(scope.form.password.$error.required).toBeFalsy();
expect(scope.passwordErrors).toEqual(['The password must be at least 10 characters long.']);
expect(scope.requirementsColor).toEqual('primary');
expect(scope.requirementsProgress).toEqual('80');
});

it('should not allow a greater than 128 characters long', function() {
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+';
compileDirective();
scope.$digest();

expect(scope.form.password.$valid).toBeFalsy();
expect(scope.form.password.$error.required).toBeFalsy();
expect(scope.passwordErrors).toEqual(['The password must be fewer than 128 characters.']);
expect(scope.requirementsColor).toEqual('primary');
expect(scope.requirementsProgress).toEqual('80');
});

it('should not allow more than 3 or more repeating characters - "P@$$w0rd!!!"', function() {
scope.passwordMock.password = 'P@$$w0rd!!!';
compileDirective();
scope.$digest();

expect(scope.form.password.$valid).toBeFalsy();
expect(scope.form.password.$error.required).toBeFalsy();
expect(scope.passwordErrors).toEqual(['The password may not contain sequences of three or more repeated characters.']);
expect(scope.requirementsColor).toEqual('primary');
expect(scope.requirementsProgress).toEqual('80');
});

it('should not allow a password with no uppercase letters - "p@$$w0rd!!"', function() {
scope.passwordMock.password = 'p@$$w0rd!!';
compileDirective();
scope.$digest();

expect(scope.form.password.$valid).toBeFalsy();
expect(scope.form.password.$error.required).toBeFalsy();
expect(scope.passwordErrors).toEqual(['The password must contain at least one uppercase letter.']);
expect(scope.requirementsColor).toEqual('primary');
expect(scope.requirementsProgress).toEqual('80');
});

it('should not allow a password with less than one number - "P@$$word!!"', function() {
scope.passwordMock.password = 'P@$$word!!';
compileDirective();
scope.$digest();

expect(scope.form.password.$valid).toBeFalsy();
expect(scope.form.password.$error.required).toBeFalsy();
expect(scope.passwordErrors).toEqual(['The password must contain at least one number.']);
expect(scope.requirementsColor).toEqual('primary');
expect(scope.requirementsProgress).toEqual('80');
});

it('should not allow a password with less than one special character - "Passw0rdss"', function() {
scope.passwordMock.password = 'Passw0rdss';
compileDirective();
scope.$digest();

expect(scope.form.password.$valid).toBeFalsy();
expect(scope.form.password.$error.required).toBeFalsy();
expect(scope.passwordErrors).toEqual(['The password must contain at least one special character.']);
expect(scope.requirementsColor).toEqual('primary');
expect(scope.requirementsProgress).toEqual('80');
});

it('should show 20% progress and "danger" color', function() {
scope.passwordMock.password = 'P';
compileDirective();
scope.$digest();

expect(scope.requirementsColor).toEqual('danger');
expect(scope.requirementsProgress).toEqual('20');
});

it('should show 40% progress and "warning" color', function() {
scope.passwordMock.password = 'Pa';
compileDirective();
scope.$digest();

expect(scope.requirementsColor).toEqual('warning');
expect(scope.requirementsProgress).toEqual('40');
});

it('should show 60% progress and "info" color', function() {
scope.passwordMock.password = 'Pa$';
compileDirective();
scope.$digest();

expect(scope.requirementsColor).toEqual('info');
expect(scope.requirementsProgress).toEqual('60');
});

it('should show 80% progress and "primary" color', function() {
scope.passwordMock.password = 'Pa$$w0rd';
compileDirective();
scope.$digest();

expect(scope.requirementsColor).toEqual('primary');
expect(scope.requirementsProgress).toEqual('80');
});

it('should show 100% progress and "success" color', function() {
scope.passwordMock.password = 'Pa$$w0rd!!';
compileDirective();
scope.$digest();

expect(scope.requirementsColor).toEqual('success');
expect(scope.requirementsProgress).toEqual('100');
});

});
}());
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';

(function() {
// Password Verify Directive Spec
describe('PasswordVerifyDirective', function() {
// Initialize global variables
var scope,
element,
$compile,
form;

// Load the main application module
beforeEach(module(ApplicationConfiguration.applicationModuleName));

beforeEach(inject(function(_$rootScope_, _$compile_) {
// Set a new global scope
scope = _$rootScope_.$new();
$compile = _$compile_;

scope.passwordMock = {
newPassword: 'P@ssw0rd!!',
verifyPassword: 'P@ssw0rd!!'
};
}));

function compileDirective(template) {
// function to compile a fresh directive with the given template, or a default one
// input form with directive
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>' +
'<input type="password" id="verifyPassword" name="verifyPassword" class="form-control" ng-model="passwordMock.verifyPassword" placeholder="Verify Password" password-verify="passwordMock.newPassword" required>';
template = '<form name="form"><div>' + template + '<input type="submit">submit form</input></div></form>';

// inject allows you to use AngularJS dependency injection
// to retrieve and use other services
inject(function($compile) {
var form = $compile(template)(scope);
element = form.find('div');

// $digest is necessary to finalize the directive generation
scope.$digest();
});
}

describe('Initialize', function() {
beforeEach(function () {
compileDirective();
});

it('should produce the password input', function () {
expect(element.find('input').length).toEqual(3);
});

it('should check form validity upon initializing', function () {
expect(scope.form.$valid).toBeTruthy();
});

});

it('should not show error when passwords match', function () {
compileDirective();
scope.passwordMock.newPassword = 'P@ssw0rd!!';
scope.passwordMock.verifyPassword = 'P@ssw0rd!!';
scope.$digest();

expect(scope.form.newPassword.$valid).toBeTruthy();
expect(scope.form.newPassword.$error).toEqual({});
expect(scope.form.verifyPassword.$valid).toBeTruthy();
expect(scope.form.verifyPassword.$error).toEqual({});
expect(scope.form.$valid).toBeTruthy();
});

it('should show error when passwords do not match', function () {
compileDirective();
scope.passwordMock.newPassword = 'P@ssw0rd!!';
scope.passwordMock.verifyPassword = 'P@ssw0rd!';
scope.$digest();

expect(scope.form.newPassword.$valid).toBeTruthy();
expect(scope.form.newPassword.$error).toEqual({});
expect(scope.form.verifyPassword.$valid).toBeFalsy();
expect(scope.form.verifyPassword.$error.passwordVerify).toBeTruthy();
expect(scope.form.$valid).toBeFalsy();
});

});
}());
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

(function() {
// Authentication controller Spec
// Password controller Spec
describe('PasswordController', function() {
// Initialize global variables
var PasswordController,
Expand Down
Loading

0 comments on commit 6514044

Please sign in to comment.