Skip to content

Instantly share code, notes, and snippets.

@joshtwist
Last active December 10, 2015 12:08
Show Gist options
  • Select an option

  • Save joshtwist/4431805 to your computer and use it in GitHub Desktop.

Select an option

Save joshtwist/4431805 to your computer and use it in GitHub Desktop.
Unit tests for script https://gist.github.com/4431800
var fs = require('fs');
var assert = require('assert');
var tables = require('./mockTable.js');
// read the insert function ready for testing
eval(fs.readFileSync("./table/accounts.insert.js", "utf8"));
suite('accounts tables', function() {
test('insert hashing and creating salt', function(done) {
var testUsername = "testusername";
var testPassword = "123456789";
// setup tables
tables.clear();
tables.addItem('accounts', {});
var item = { username: testUsername, password : testPassword }
var req = {
execute : function(options) {
assert.notEqual(item.password, testPassword);
assert(item.salt); // should be truthy, e.g. not null/undefined
options.success(); // need to invoke the success callback
},
respond: function() {
// password and salt should have been removed
assert(!item.password);
assert(!item.salt);
done();
},
parameters : {} // no parameters
}
insert(item, null, req);
});
test('existing username fails with 400', function(done) {
tables.clear();
tables.addItem('accounts', { username : "test" });
var req = {
execute : function(options) { options.success() },
respond: function(code, body) {
assert.equal(code, 400);
done();
},
parameters: {}
}
insert({ username: "test" }, null, req);
});
test('login matches password correctly', function(done) {
var testUsername = "josh";
var testPassword = "P@ssw0rd1";
var testSalt = "A rAndomLY GenERated ValuE";
hash(testPassword, testSalt, function(e, h){
// clear tables down and setup mock data
tables.clear();
tables.addItem('accounts', {
id: 1,
username: testUsername,
salt: testSalt,
password: h
});
var item = { username : 'josh', password : testPassword };
var req = {
respond: function(code, body) {
assert.equal(200, code);
assert.equal(body.user.userId, "Custom:1");
assert(body.token);
assert(body.token.indexOf(".") > 0);
done();
},
parameters : { login : 'true' }
}
insert(item, null, req);
})
});
test('unauthorized on invalid password', function(done) {
var testUsername = "josh";
var testPassword = "P@ssw0rd1";
var testSalt = "A rAndomLY GenERated ValuE";
hash(testPassword, testSalt, function(e, h) {
// clear tables down and setup mock data
tables.clear();
tables.addItem('accounts', {
id: 1,
username: testUsername,
salt: testSalt,
password: h
});
var item = { username : testUsername, password : "P@ssword1" };
var req = {
respond: function(code, body) {
assert.equal(401, code);
assert(!body.user);
assert(!body.token);
done();
},
parameters : { login : 'true' }
}
insert(item, null, req);
});
});
});
var internalTables = {};
function addItem(tableName, item)
{
var table = internalTables[tableName];
if (!table) {
table = [];
internalTables[tableName] = table;
}
table.push(item);
}
function clear()
{
internalTables = {};
}
function TableQuery(table, queryArgs) {
this.read = function(options) {
if (typeof(queryArgs[0]) === 'function') {
var fn = queryArgs[0];
var matches = table.filter(function(record) {
queryArgs.splice = Array.prototype.splice;
return fn.apply(record, queryArgs.splice(0,1));
});
options.success(matches);
}
else {
var matches = table.filter(function(record) {
var query = queryArgs[0];
for (var prop in query) {
if (!record[prop] || record[prop] != query[prop]) {
return false;
}
}
return true;
});
options.success(matches);
}
}
}
function getTable(tableName) {
var table = internalTables[tableName];
var where = function() {
var queryArgs = arguments;
return new TableQuery(table, queryArgs);
};
return { where : where };
}
module.exports.getTable = getTable;
module.exports.addItem = addItem;
module.exports.clear = clear;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment