mirror of https://gitee.com/bigwinds/arangodb
Added tests for simple-auth app.
This commit is contained in:
parent
573fd51a5e
commit
19062d9d57
|
@ -0,0 +1 @@
|
||||||
|
node_modules
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
|
@ -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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}());
|
|
@ -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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}());
|
Loading…
Reference in New Issue