1
0
Fork 0

Cleaned up users app. Added missing error names.

This commit is contained in:
Alan Plum 2015-07-07 12:25:31 +02:00
parent 133b88f00d
commit 11d52b25b3
9 changed files with 109 additions and 157 deletions

View File

@ -1,19 +1,34 @@
'use strict'; 'use strict';
function SessionNotFound(sid) { function SessionNotFound(sid) {
this.message = 'Session with session id ' + sid + ' not found.'; this.message = 'Session with session id ' + sid + ' not found.';
this.name = this.constructor.name; this.name = this.name;
Error.captureStackTrace(this, SessionNotFound); Error.captureStackTrace(this, SessionNotFound);
} }
SessionNotFound.prototype = new Error(); SessionNotFound.prototype = new Error();
SessionNotFound.prototype.constructor = SessionNotFound; SessionNotFound.prototype.constructor = SessionNotFound;
Object.defineProperty(SessionNotFound.prototype, 'name', {
enumerable: true,
configurable: true,
get: function () {
return this.constructor.name;
}
});
function SessionExpired(sid) { function SessionExpired(sid) {
this.message = 'Session with session id ' + sid + ' has expired.'; this.message = 'Session with session id ' + sid + ' has expired.';
this.name = this.constructor.name; this.name = this.name;
Error.captureStackTrace(this, SessionExpired); Error.captureStackTrace(this, SessionExpired);
} }
SessionExpired.prototype = Object.create(SessionNotFound.prototype); SessionExpired.prototype = Object.create(SessionNotFound.prototype);
SessionExpired.prototype.constructor = SessionExpired; SessionExpired.prototype.constructor = SessionExpired;
SessionExpired.prototype.name = SessionExpired.name;
Object.defineProperty(SessionExpired.prototype, 'name', {
enumerable: true,
configurable: true,
get: function () {
return this.constructor.name;
}
});
exports.SessionNotFound = SessionNotFound; exports.SessionNotFound = SessionNotFound;
exports.SessionExpired = SessionExpired; exports.SessionExpired = SessionExpired;

View File

@ -1 +0,0 @@
node_modules

View File

@ -1,21 +1,35 @@
(function () { 'use strict';
'use strict'; function UserNotFound(uid) {
function UserNotFound(uid) {
this.message = 'User with user id ' + uid + ' not found.'; this.message = 'User with user id ' + uid + ' not found.';
var err = new Error(this.message); var err = new Error(this.message);
err.name = this.constructor.name; err.name = this.name;
this.stack = err.stack; this.stack = err.stack;
}
UserNotFound.prototype = new Error();
UserNotFound.prototype.constructor = UserNotFound;
Object.defineProperty(UserNotFound.prototype, 'name', {
enumerable: true,
configurable: true,
get: function () {
return this.constructor.name;
} }
UserNotFound.prototype = new Error(); });
function UsernameNotAvailable(username) { function UsernameNotAvailable(username) {
this.message = 'The username ' + username + ' is not available or already taken.'; this.message = 'The username ' + username + ' is not available or already taken.';
var err = new Error(this.message); var err = new Error(this.message);
err.name = this.constructor.name; err.name = this.name;
this.stack = err.stack; this.stack = err.stack;
}
UsernameNotAvailable.prototype = new Error();
UsernameNotAvailable.prototype.constructor = UsernameNotAvailable;
Object.defineProperty(UsernameNotAvailable.prototype, 'name', {
enumerable: true,
configurable: true,
get: function () {
return this.constructor.name;
} }
UsernameNotAvailable.prototype = new Error(); });
exports.UserNotFound = UserNotFound; exports.UserNotFound = UserNotFound;
exports.UsernameNotAvailable = UsernameNotAvailable; exports.UsernameNotAvailable = UsernameNotAvailable;
}());

View File

@ -2,7 +2,7 @@
"name": "users", "name": "users",
"description": "Username-based user storage for Foxx.", "description": "Username-based user storage for Foxx.",
"author": "ArangoDB GmbH", "author": "ArangoDB GmbH",
"version": "0.1", "version": "1.0.0",
"license": "Apache License, Version 2.0", "license": "Apache License, Version 2.0",
"isSystem": true, "isSystem": true,
@ -28,5 +28,7 @@
"scripts": { "scripts": {
"setup": "setup.js" "setup": "setup.js"
} },
"tests": "test/**"
} }

View File

@ -1,12 +0,0 @@
{
"private": true,
"devDependencies": {
"expect.js": "^0.3.1",
"mocha": "^1.21.4",
"mockuire": "^0.1.0",
"sinon": "^1.10.3"
},
"scripts": {
"test": "mocha --growl"
}
}

View File

@ -1,10 +1,8 @@
/*global applicationContext */ /*global applicationContext */
(function () { 'use strict';
'use strict'; var db = require('org/arangodb').db;
var db = require('org/arangodb').db, var usersName = applicationContext.collectionName('users');
usersName = applicationContext.collectionName('users');
if (db._collection(usersName) === null) { if (db._collection(usersName) === null) {
db._create(usersName, {isSystem: true}); db._create(usersName, {isSystem: true});
} }
}());

View File

@ -1,36 +1,26 @@
/*global applicationContext */ /*global applicationContext */
'use strict'; 'use strict';
const _ = require('underscore');
var _ = require('underscore'), const joi = require('joi');
joi = require('joi'), const arangodb = require('org/arangodb');
arangodb = require('org/arangodb'), const db = arangodb.db;
db = arangodb.db, const Foxx = require('org/arangodb/foxx');
Foxx = require('org/arangodb/foxx'), const errors = require('./errors');
errors = require('./errors'), const refreshUserCache = require('org/arangodb/users').reload;
User = Foxx.Model.extend({ const User = Foxx.Model.extend({
schema: { schema: {
user: joi.string().required(), user: joi.string().required(),
authData: joi.object().required(), authData: joi.object().required(),
userData: joi.object().required() userData: joi.object().required()
} }
}), });
users; const users = new Foxx.Repository(
if (applicationContext.mount.indexOf('/_system/') === 0) {
users = new Foxx.Repository(
db._collection('_users'), db._collection('_users'),
{model: User} {model: User}
); );
} else {
users = new Foxx.Repository(
applicationContext.collection('users'),
{model: User}
);
}
function resolve(username) { function resolve(username) {
var user = users.firstExample({user: username}); const user = users.firstExample({user: username});
if (!user.get('_key')) { if (!user.get('_key')) {
return null; return null;
} }
@ -60,7 +50,7 @@ function createUser(username, userData, authData) {
if (!username) { if (!username) {
throw new Error('Must provide username!'); throw new Error('Must provide username!');
} }
var user; let user;
db._executeTransaction({ db._executeTransaction({
collections: { collections: {
read: [users.collection.name()], read: [users.collection.name()],
@ -78,14 +68,12 @@ function createUser(username, userData, authData) {
users.save(user); users.save(user);
} }
}); });
if (applicationContext.mount.indexOf('/_system/') === 0) { refreshUserCache();
require('org/arangodb/users').reload();
}
return user; return user;
} }
function getUser(uid) { function getUser(uid) {
var user; let user;
try { try {
user = users.byId(uid); user = users.byId(uid);
} catch (err) { } catch (err) {
@ -112,19 +100,15 @@ function deleteUser(uid) {
} }
throw err; throw err;
} }
if (applicationContext.mount.indexOf('/_system/') === 0) { refreshUserCache();
require('org/arangodb/users').reload();
}
return null; return null;
} }
_.extend(User.prototype, { _.extend(User.prototype, {
save: function () { save: function () {
var user = this; const user = this;
users.replace(user); users.replace(user);
if (applicationContext.mount.indexOf('/_system/') === 0) { refreshUserCache();
require('org/arangodb/users').reload();
}
return user; return user;
}, },
delete: function () { delete: function () {
@ -136,9 +120,7 @@ _.extend(User.prototype, {
} }
throw e; throw e;
} }
if (applicationContext.mount.indexOf('/_system/') === 0) { refreshUserCache();
require('org/arangodb/users').reload();
}
return true; return true;
} }
}); });

View File

@ -1,10 +1,9 @@
/*global describe, it */ /*global describe, it */
(function () { 'use strict';
'use strict'; var expect = require('expect.js');
var expect = require('expect.js'), var errors = require('../errors');
errors = require('../errors');
describe('errors', function () { describe('errors', function () {
['UserNotFound', 'UsernameNotAvailable'].forEach(function (name) { ['UserNotFound', 'UsernameNotAvailable'].forEach(function (name) {
describe(name, function () { describe(name, function () {
var UserError = errors[name]; var UserError = errors[name];
@ -28,5 +27,4 @@
}); });
}); });
}); });
}); });
}());

View File

@ -1,44 +0,0 @@
/*global 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('setup.js', function () {
var db = {}, ctx = {};
beforeEach(function () {
ctx.collectionName = sinon.stub();
db._collection = sinon.stub();
db._create = sinon.stub();
});
it('creates a users collection if it does not exist', function () {
ctx.collectionName.withArgs('users').returns('magic');
db._collection.returns(null);
mockuire('../setup', {
applicationContext: ctx,
'org/arangodb': {db: db}
});
expect(db._create.callCount).to.equal(1);
expect(db._create.args[0]).to.eql(['magic']);
});
it('does not overwrite an existing collection', function () {
ctx.collectionName.returns('magic');
db._collection.returns({});
mockuire('../setup', {
applicationContext: ctx,
'org/arangodb': {db: db}
});
expect(db._create.callCount).to.equal(0);
});
});
}());