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';
function SessionNotFound(sid) {
this.message = 'Session with session id ' + sid + ' not found.';
this.name = this.constructor.name;
this.name = this.name;
Error.captureStackTrace(this, SessionNotFound);
}
SessionNotFound.prototype = new Error();
SessionNotFound.prototype.constructor = SessionNotFound;
Object.defineProperty(SessionNotFound.prototype, 'name', {
enumerable: true,
configurable: true,
get: function () {
return this.constructor.name;
}
});
function SessionExpired(sid) {
this.message = 'Session with session id ' + sid + ' has expired.';
this.name = this.constructor.name;
this.name = this.name;
Error.captureStackTrace(this, SessionExpired);
}
SessionExpired.prototype = Object.create(SessionNotFound.prototype);
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.SessionExpired = SessionExpired;

View File

@ -1 +0,0 @@
node_modules

View File

@ -1,21 +1,35 @@
(function () {
'use strict';
function UserNotFound(uid) {
this.message = 'User with user id ' + uid + ' not found.';
var err = new Error(this.message);
err.name = this.constructor.name;
this.stack = err.stack;
'use strict';
function UserNotFound(uid) {
this.message = 'User with user id ' + uid + ' not found.';
var err = new Error(this.message);
err.name = this.name;
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) {
this.message = 'The username ' + username + ' is not available or already taken.';
var err = new Error(this.message);
err.name = this.constructor.name;
this.stack = err.stack;
function UsernameNotAvailable(username) {
this.message = 'The username ' + username + ' is not available or already taken.';
var err = new Error(this.message);
err.name = this.name;
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.UsernameNotAvailable = UsernameNotAvailable;
}());
exports.UserNotFound = UserNotFound;
exports.UsernameNotAvailable = UsernameNotAvailable;

View File

@ -2,7 +2,7 @@
"name": "users",
"description": "Username-based user storage for Foxx.",
"author": "ArangoDB GmbH",
"version": "0.1",
"version": "1.0.0",
"license": "Apache License, Version 2.0",
"isSystem": true,
@ -28,5 +28,7 @@
"scripts": {
"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 */
(function () {
'use strict';
var db = require('org/arangodb').db,
usersName = applicationContext.collectionName('users');
'use strict';
var db = require('org/arangodb').db;
var usersName = applicationContext.collectionName('users');
if (db._collection(usersName) === null) {
db._create(usersName, {isSystem: true});
}
}());
if (db._collection(usersName) === null) {
db._create(usersName, {isSystem: true});
}

View File

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

View File

@ -1,32 +1,30 @@
/*global describe, it */
(function () {
'use strict';
var expect = require('expect.js'),
errors = require('../errors');
'use strict';
var expect = require('expect.js');
var errors = require('../errors');
describe('errors', function () {
['UserNotFound', 'UsernameNotAvailable'].forEach(function (name) {
describe(name, function () {
var UserError = errors[name];
it('is a constructor', function () {
expect(new UserError()).to.be.a(UserError);
});
it('creates an Error', function () {
expect(new UserError()).to.be.an(Error);
});
it('uses its argument in its message', function () {
var err = new UserError('potato');
expect(err.message).to.contain('potato');
});
it('uses its message in its stack trace', function () {
var err = new UserError('potato');
expect(err.stack).to.contain(err.message);
});
it('uses its name in its stack trace', function () {
var err = new UserError();
expect(err.stack).to.contain(name);
});
describe('errors', function () {
['UserNotFound', 'UsernameNotAvailable'].forEach(function (name) {
describe(name, function () {
var UserError = errors[name];
it('is a constructor', function () {
expect(new UserError()).to.be.a(UserError);
});
it('creates an Error', function () {
expect(new UserError()).to.be.an(Error);
});
it('uses its argument in its message', function () {
var err = new UserError('potato');
expect(err.message).to.contain('potato');
});
it('uses its message in its stack trace', function () {
var err = new UserError('potato');
expect(err.stack).to.contain(err.message);
});
it('uses its name in its stack trace', function () {
var err = new UserError();
expect(err.stack).to.contain(name);
});
});
});
}());
});

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);
});
});
}());