From 19062d9d5713b7bfa69764f7b6fa97fb0417478b Mon Sep 17 00:00:00 2001 From: Alan Plum Date: Thu, 21 Aug 2014 15:22:39 +0200 Subject: [PATCH] Added tests for simple-auth app. --- js/apps/system/simple-auth/.gitignore | 1 + js/apps/system/simple-auth/package.json | 12 ++++ .../system/simple-auth/test/hashPassword.js | 54 +++++++++++++++ .../system/simple-auth/test/verifyPassword.js | 65 +++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 js/apps/system/simple-auth/.gitignore create mode 100644 js/apps/system/simple-auth/package.json create mode 100644 js/apps/system/simple-auth/test/hashPassword.js create mode 100644 js/apps/system/simple-auth/test/verifyPassword.js diff --git a/js/apps/system/simple-auth/.gitignore b/js/apps/system/simple-auth/.gitignore new file mode 100644 index 0000000000..3c3629e647 --- /dev/null +++ b/js/apps/system/simple-auth/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/js/apps/system/simple-auth/package.json b/js/apps/system/simple-auth/package.json new file mode 100644 index 0000000000..b0ca357fbe --- /dev/null +++ b/js/apps/system/simple-auth/package.json @@ -0,0 +1,12 @@ +{ + "private": true, + "devDependencies": { + "expect.js": "^0.3.1", + "mocha": "^1.21.4", + "mockuire": "^0.1.0", + "sinon": "^1.10.3" + }, + "scripts": { + "test": "mocha --growl" + } +} diff --git a/js/apps/system/simple-auth/test/hashPassword.js b/js/apps/system/simple-auth/test/hashPassword.js new file mode 100644 index 0000000000..fb7d48e1e3 --- /dev/null +++ b/js/apps/system/simple-auth/test/hashPassword.js @@ -0,0 +1,54 @@ +/*jslint indent: 2, nomen: true, maxlen: 120, es5: true */ +/*global require, module, describe, it, beforeEach */ +(function () { + 'use strict'; + var sinon = require('sinon'), + expect = require('expect.js'), + mockuire; + + mockuire = require('mockuire')(module, { + 'js': {compile: function (src) { + return 'var applicationContext = require("applicationContext");\n' + src; + }} + }); + + describe('hashPassword', function () { + var cfg = {}, crypto = {}, hashPassword; + + hashPassword = mockuire('../auth', { + applicationContext: {configuration: cfg}, + 'org/arangodb/crypto': crypto + }).hashPassword; + + beforeEach(function () { + cfg.hashMethod = 'bogohash'; + crypto.bogohash = sinon.stub(); + crypto.genRandomAlphaNumbers = sinon.stub(); + }); + + it('uses the hash method defined by the configuration', function () { + var result = hashPassword('secret'); + expect(result.method).to.equal(cfg.hashMethod); + }); + + it('uses the salt length defined by the configuration', function () { + cfg.saltLength = 42; + var result = hashPassword('secret'); + expect(crypto.genRandomAlphaNumbers.callCount).to.equal(1); + expect(crypto.genRandomAlphaNumbers.args[0]).to.eql([42]); + }); + + it('prefixes the password with the salt', function () { + crypto.genRandomAlphaNumbers.returns('keyboardcat'); + var result = hashPassword('secret'); + expect(crypto.bogohash.callCount).to.equal(1); + expect(crypto.bogohash.args[0]).to.eql(['keyboardcatsecret']); + }); + + it('retuns the hashed password', function () { + crypto.bogohash.returns('somekindofhash'); + var result = hashPassword(); + expect(result.hash).to.equal('somekindofhash'); + }); + }); +}()); \ No newline at end of file diff --git a/js/apps/system/simple-auth/test/verifyPassword.js b/js/apps/system/simple-auth/test/verifyPassword.js new file mode 100644 index 0000000000..76d924cdeb --- /dev/null +++ b/js/apps/system/simple-auth/test/verifyPassword.js @@ -0,0 +1,65 @@ +/*jslint indent: 2, nomen: true, maxlen: 120, es5: true */ +/*global require, module, exports, describe, it, beforeEach */ +(function () { + 'use strict'; + var sinon = require('sinon'), + expect = require('expect.js'), + mockuire; + + mockuire = require('mockuire')(module, { + 'js': {compile: function (src) { + return 'var applicationContext = require("applicationContext");\n' + src; + }} + }); + + describe('verifyPassword', function () { + var cfg = {}, crypto = {}, verifyPassword; + + verifyPassword = mockuire('../auth', { + applicationContext: {configuration: cfg}, + 'org/arangodb/crypto': crypto + }).verifyPassword; + + beforeEach(function () { + cfg.hashMethod = 'bogohash'; + crypto.bogohash = sinon.stub(); + crypto.constantEquals = sinon.stub(); + }); + + it('uses the hash method defined by the auth data', function () { + crypto.rot13 = sinon.spy(); + verifyPassword({method: 'rot13'}); + expect(crypto.rot13.callCount).to.equal(1); + expect(crypto.bogohash.callCount).to.equal(0); + }); + + it('falls back to the hash method defined by the configuration', function () { + verifyPassword({}); + expect(crypto.bogohash.callCount).to.equal(1); + crypto.bogohash.reset(); + verifyPassword(); + expect(crypto.bogohash.callCount).to.equal(1); + }); + + it('prefixes the password with the salt', function () { + verifyPassword({salt: 'keyboardcat'}, 'secret'); + expect(crypto.bogohash.callCount).to.equal(1); + expect(crypto.bogohash.args[0]).to.eql(['keyboardcatsecret']); + }); + + it('passes the hashed password and the stored hash to constantEquals', function () { + crypto.bogohash.returns('deadbeef'); + verifyPassword({hash: '8badf00d'}); + expect(crypto.constantEquals.callCount).to.equal(1); + expect(crypto.constantEquals.args[0].length).to.equal(2); + expect(crypto.constantEquals.args[0]).to.contain('deadbeef'); + expect(crypto.constantEquals.args[0]).to.contain('8badf00d'); + }); + + it('returns the result of constantEquals', function () { + crypto.constantEquals.returns('magic'); + var result = verifyPassword(); + expect(result).to.equal('magic'); + }); + }); +}()); \ No newline at end of file