Skip to content

Commit

Permalink
Merge pull request #1184 from dukeofsussex/include_tests
Browse files Browse the repository at this point in the history
Some more tests and tweaks
  • Loading branch information
brianhyder committed Dec 4, 2016
2 parents 48341f4 + baff2bf commit c42de24
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 11 deletions.
2 changes: 1 addition & 1 deletion include/access_management.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ module.exports = function(pb) {
SecurityService.generatePassword = function(length) {

//ensure a length
if (pb.validation.isInt(length, true, true)) {
if (!pb.validation.isInt(length, true, true) || length < 8) {
length = 8;
}

Expand Down
6 changes: 3 additions & 3 deletions include/client_js.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ module.exports = function ClientJsModule(pb) {

var angularController = 'var pencilblueApp = angular.module("pencilblueApp", ' + JSON.stringify(modules) + ')';
if(!util.isNullOrUndefined(directiveJS)) {
angularController += '.directive("onFinishRender", function($timeout){return {restrict: "A",link: function(scope, element, attr){if (scope.$last === true){$timeout(function(){' + directiveJS + '})}}}})';
angularController += '.directive("onFinishRender", function($timeout) {return {restrict: "A", link: function(scope, element, attr) {if (scope.$last === true){$timeout(function() {' + directiveJS + '})}}}})';
}

var scopeString = ClientJs.getAngularObjects(objects);
angularController = angularController.concat('.controller("PencilBlueController", function($scope, $sce) {' + scopeString + "});\n");
angularController = angularController.concat('pencilblueApp.config(["$compileProvider",function(e){e.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|javascript):/)}]);');
angularController = angularController.concat('pencilblueApp.config(["$compileProvider",function(e) {e.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|javascript):/)}]);');
return ClientJs.getJSTag(angularController);
};

Expand All @@ -68,7 +68,7 @@ module.exports = function ClientJsModule(pb) {
scopeString = scopeString.concat('$scope.' + key + ' = ' + objects[key] + ";\n");
return;
}
scopeString = scopeString.concat('$scope.' + key + '=' + JSON.stringify(objects[key], null, pb.log.isSilly() ? ' ' : undefined) + ";\n");
scopeString = scopeString.concat('$scope.' + key + ' = ' + JSON.stringify(objects[key], null, pb.log.isSilly() ? ' ' : undefined) + ";\n");
});

return scopeString;
Expand Down
136 changes: 130 additions & 6 deletions test/include/access_management_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('SecurityService', function() {

var pb = null;
var SecurityService = null;
var sessions = null;
before('Initialize the Environment with the default configuration', function(next) {

//travis gets slow so we bump the timeout just a little here to get around the BS
Expand All @@ -17,6 +18,34 @@ describe('SecurityService', function() {
pb = new Lib(Configuration.getBaseConfig());
SecurityService = pb.SecurityService;

sessions = {
ADMIN: {
authentication: {
user: {
id: '12345',
name: 'Admin',
admin_level: 4
},
user_id: pb.util.uniqueId(),
admin_level: 4
}
},
USER: {
authentication: {
user: 'USER',
user_id: pb.util.uniqueId(),
admin_level: 0
}
},
GUEST: {
authentication: {
user: null,
user_id: null,
admin_level: 0
}
}
};

console.log('Completed in %sms', (new Date()).getTime() - start);
next();
});
Expand Down Expand Up @@ -48,16 +77,111 @@ describe('SecurityService', function() {
var result = SecurityService.getRoleNames(ls);

result.should.be.instanceof(Array);
should(result.length === 5).be.ok;
should(result.indexOf('generic.ACCESS_ADMINISTRATOR') >= 0).be.ok;
should(result.indexOf('generic.ACCESS_MANAGING_EDITOR') >= 0).be.ok;
should(result.indexOf('generic.ACCESS_EDITOR') >= 0).be.ok;
should(result.indexOf('generic.ACCESS_WRITER') >= 0).be.ok;
should(result.indexOf('generic.ACCESS_USER') >= 0).be.ok;
should(result.length === 5).be.ok();
should(result.indexOf('generic.ACCESS_ADMINISTRATOR') >= 0).be.ok();
should(result.indexOf('generic.ACCESS_MANAGING_EDITOR') >= 0).be.ok();
should(result.indexOf('generic.ACCESS_EDITOR') >= 0).be.ok();
should(result.indexOf('generic.ACCESS_WRITER') >= 0).be.ok();
should(result.indexOf('generic.ACCESS_USER') >= 0).be.ok();
});

it('should throw when no localization instance is provided', function() {
SecurityService.getRoleNames.bind(null).should.throwError();
});
});

describe('SecurityService.getRoleName', function() {
it('should return the correct role name for provided access level', function() {
SecurityService.getRoleName(0).should.equal('ACCESS_USER');
SecurityService.getRoleName(1).should.equal('ACCESS_WRITER');
SecurityService.getRoleName(2).should.equal('ACCESS_EDITOR');
SecurityService.getRoleName(3).should.equal('ACCESS_MANAGING_EDITOR');
SecurityService.getRoleName(4).should.equal('ACCESS_ADMINISTRATOR');
});

it('should throw when no access level is provided', function() {
SecurityService.getRoleName.bind(null).should.throwError();
SecurityService.getRoleName.bind(undefined).should.throwError();
SecurityService.getRoleName.bind(-1).should.throwError();
});
});

describe('SecurityService.isAuthorized', function() {
it('should correctly check for required authentication', function() {
var requirements = {};

requirements[SecurityService.AUTHENTICATED] = false;
SecurityService.isAuthorized(sessions.ADMIN, requirements).should.be.true();
SecurityService.isAuthorized(sessions.USER, requirements).should.be.true();
SecurityService.isAuthorized(sessions.GUEST, requirements).should.be.true();

requirements[SecurityService.AUTHENTICATED] = true;
SecurityService.isAuthorized(sessions.ADMIN, requirements).should.be.true();
SecurityService.isAuthorized(sessions.USER, requirements).should.be.true();
SecurityService.isAuthorized(sessions.GUEST, requirements).should.be.false();

requirements[SecurityService.ADMIN_LEVEL] = SecurityService.ACCESS_ADMINISTRATOR;
SecurityService.isAuthorized(sessions.ADMIN, requirements).should.be.true();
SecurityService.isAuthorized(sessions.USER, requirements).should.be.false();
SecurityService.isAuthorized(sessions.GUEST, requirements).should.be.false();

requirements[SecurityService.ADMIN_LEVEL] = SecurityService.ACCESS_USER;
SecurityService.isAuthorized(sessions.ADMIN, requirements).should.be.true();
SecurityService.isAuthorized(sessions.USER, requirements).should.be.true();
SecurityService.isAuthorized(sessions.GUEST, requirements).should.be.false();
});
});

describe('SecurityService.isAuthenticated', function() {
it('should check whether a session is authentic', function() {
SecurityService.isAuthenticated(sessions.ADMIN).should.be.true();
SecurityService.isAuthenticated(sessions.USER).should.be.true();
SecurityService.isAuthenticated(sessions.GUEST).should.be.false();
SecurityService.isAuthenticated(pb.util.uniqueId()).should.be.false();
});
});

describe('SecurityService.encrypt', function() {
it('should encrypt a password', function() {
var encrypted = SecurityService.encrypt('abcd');
encrypted.should.not.equal('abcd');
SecurityService.encrypt('abcd').should.equal(encrypted);
SecurityService.encrypt('dcba').should.not.equal(encrypted);
});
});

describe('SecurityService.generatePassword', function() {
it('should generate a password with a minimum length of 8', function() {
SecurityService.generatePassword(null).should.have.length(8);
SecurityService.generatePassword(undefined).should.have.length(8);
SecurityService.generatePassword(0).should.have.length(8);
SecurityService.generatePassword(7).should.have.length(8);
SecurityService.generatePassword(20).should.have.length(20);
});

it('should generate 25 different passwords that are all unique', function() {
var passwords = [];
for (var i = 0; i < 25; i++) {
passwords.push(SecurityService.generatePassword(8));
for (var j = 0; j < passwords.length; j++) {
if (i !== j) {
passwords[i].should.not.equal(passwords[j]);
};
}
}
});
});

describe('SecurityService.getPrincipal', function() {
it('should return the user principal', function() {
SecurityService.getPrincipal(sessions.ADMIN).should.deepEqual({
id: '12345',
name: 'Admin',
admin_level: 4
});
SecurityService.getPrincipal(sessions.USER).should.equal('USER');
(SecurityService.getPrincipal(sessions.GUEST) === null).should.be.true();
(SecurityService.getPrincipal(pb.util.uniqueId()) === null).should.be.true();
});
});
});
64 changes: 64 additions & 0 deletions test/include/client_js_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//dependencies
var should = require('should');
var Configuration = require('../../include/config.js');
var Lib = require('../../lib');

describe('AdminNavigation', function() {

var pb = null;
var ClientJs = null;
before('Initialize the Environment with the default configuration', function() {
this.timeout(10000);

pb = new Lib(Configuration.getBaseConfig());
ClientJs = pb.ClientJs;
});

describe('ClientJS.getAngularController', function() {

it('should return a valid Angular controller', function() {
var expectedResult = '<script type="text/javascript">\n'
+ 'var pencilblueApp = angular.module("pencilblueApp", ["ngRoute"])'
+ '.controller("PencilBlueController", function($scope, $sce) {});\n'
+ 'pencilblueApp.config(["$compileProvider",function(e) {'
+ 'e.aHrefSanitizationWhitelist(/^s*(https?|ftp|mailto|javascript):/)}]);\n</script>';

ClientJs.getAngularController({}, []).should.have.property('raw').which.equal(expectedResult);

expectedResult = '<script type="text/javascript">\n'
+ 'var pencilblueApp = angular.module("pencilblueApp", ["module"])'
+ '.controller("PencilBlueController", function($scope, $sce) {});\n'
+ 'pencilblueApp.config(["$compileProvider",function(e) {'
+ 'e.aHrefSanitizationWhitelist(/^s*(https?|ftp|mailto|javascript):/)}]);\n</script>';

ClientJs.getAngularController({}, ['module']).should.have.property('raw').which.equal(expectedResult);
});
});

describe('ClientJS.getAngularObjects', function() {

it('should return a valid script tag containing the passed code', function() {
var expectedResult = '$scope.foo = "bar";\n$scope.true = function(val){return true;};\n';

ClientJs.getAngularObjects({foo: 'bar', true: 'function(val){return true;}'}).should.equal(expectedResult);
});
});

describe('ClientJS.includeJS', function() {

it('should return a valid script tag containing the passed url as source', function() {
var expectedResult = '<script type="text/javascript" src="public/js/script.js"></script>';

ClientJs.includeJS('public/js/script.js').should.have.property('raw').which.equal(expectedResult);
});
});

describe('ClientJS.getJSTag', function() {

it('should return a valid script tag containing the passed code', function() {
var expectedResult = '<script type="text/javascript">\nalert(\'Hello World\')\n</script>';

ClientJs.getJSTag('alert(\'Hello World\')').should.have.property('raw').which.equal(expectedResult);
});
});
});
43 changes: 43 additions & 0 deletions test/include/content_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//dependencies
var should = require('should');
var Configuration = require('../../include/config.js');
var Lib = require('../../lib');

describe('Content', function() {

var pb = null;
var ContentService = null;
before('Initialize the Environment with the default configuration', function() {
this.timeout(10000);

pb = new Lib(Configuration.getBaseConfig());
ContentService = pb.ContentService;
});

describe('ContentService.getDefaultSettings', function() {

it('should return the correct default settings', function() {
var settings = ContentService.getDefaultSettings();

settings.should.be.an.Object();
settings.should.deepEqual({
articles_per_page: 5,
auto_break_articles: 0,
read_more_text: 'Read more',
display_timestamp: 1,
date_format: 'M dd, YYYY',
two_digit_date: 0,
display_hours_minutes: 1,
time_format: '12',
two_digit_time: 0,
display_bylines: 1,
display_author_photo: 1,
display_author_position: 1,
allow_comments: 1,
default_comments: 1,
require_account: 0,
require_verification: 0
});
});
});
});
14 changes: 13 additions & 1 deletion test/include/dao/dao_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('DAO', function() {

it('should return _id', function() {
DAO.getIdField().should.eql('_id');
})
});
});

describe('DAO.areIdsEqual', function() {
Expand Down Expand Up @@ -76,4 +76,16 @@ describe('DAO', function() {
DAO.areIdsEqual(id1, id2).should.be.ok;
});
});

describe('DAO.getObjectId', function() {

it('should return object id', function() {
var id = pb.util.uniqueId();
DAO.getObjectId(id).should.be.String().and.equal(id);
DAO.getObjectId('').should.not.be.instanceOf(ObjectID);

id = new ObjectID();
DAO.getObjectId(id).should.be.instanceOf(ObjectID);
});
});
});
38 changes: 38 additions & 0 deletions test/include/email_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//dependencies
var should = require('should');
var Configuration = require('../../include/config.js');
var Lib = require('../../lib');

describe('Email', function() {

var pb = null;
var EmailService = null;
before('Initialize the Environment with the default configuration', function() {
this.timeout(10000);

pb = new Lib(Configuration.getBaseConfig());
EmailService = pb.EmailService;
});

describe('EmailService.getDefaultSettings', function() {

it('should return the correct default settings', function() {
var settings = EmailService.getDefaultSettings();

settings.should.be.an.Object();
settings.should.deepEqual({
from_name: pb.config.siteName,
from_address: 'no-reply@sample.com',
verification_subject: pb.config.siteName+' Account Confirmation',
verification_content: '',
template: 'admin/elements/default_verification_email',
service: 'Gmail',
host: '',
secure_connection: 1,
port: 465,
username: '',
password: ''
});
});
});
});
Loading

0 comments on commit c42de24

Please sign in to comment.