From 11d52b25b3149e3051d82b22212caaed11060e0b Mon Sep 17 00:00:00 2001 From: Alan Plum Date: Tue, 7 Jul 2015 12:25:31 +0200 Subject: [PATCH] Cleaned up users app. Added missing error names. --- js/apps/system/_system/sessions/APP/errors.js | 19 ++++- js/apps/system/_system/users/APP/.gitignore | 1 - js/apps/system/_system/users/APP/errors.js | 48 ++++++++----- .../system/_system/users/APP/manifest.json | 6 +- js/apps/system/_system/users/APP/package.json | 12 ---- js/apps/system/_system/users/APP/setup.js | 14 ++-- js/apps/system/_system/users/APP/storage.js | 70 +++++++------------ .../system/_system/users/APP/test/errors.js | 52 +++++++------- .../system/_system/users/APP/test/setup.js | 44 ------------ 9 files changed, 109 insertions(+), 157 deletions(-) delete mode 100644 js/apps/system/_system/users/APP/.gitignore delete mode 100644 js/apps/system/_system/users/APP/package.json delete mode 100644 js/apps/system/_system/users/APP/test/setup.js diff --git a/js/apps/system/_system/sessions/APP/errors.js b/js/apps/system/_system/sessions/APP/errors.js index d43887f1d9..6f15aab5e6 100644 --- a/js/apps/system/_system/sessions/APP/errors.js +++ b/js/apps/system/_system/sessions/APP/errors.js @@ -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; \ No newline at end of file diff --git a/js/apps/system/_system/users/APP/.gitignore b/js/apps/system/_system/users/APP/.gitignore deleted file mode 100644 index 3c3629e647..0000000000 --- a/js/apps/system/_system/users/APP/.gitignore +++ /dev/null @@ -1 +0,0 @@ -node_modules diff --git a/js/apps/system/_system/users/APP/errors.js b/js/apps/system/_system/users/APP/errors.js index 74dcfb1ff1..6ef79241d5 100644 --- a/js/apps/system/_system/users/APP/errors.js +++ b/js/apps/system/_system/users/APP/errors.js @@ -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; -}()); \ No newline at end of file +exports.UserNotFound = UserNotFound; +exports.UsernameNotAvailable = UsernameNotAvailable; diff --git a/js/apps/system/_system/users/APP/manifest.json b/js/apps/system/_system/users/APP/manifest.json index 8680b31cae..5656f5982f 100644 --- a/js/apps/system/_system/users/APP/manifest.json +++ b/js/apps/system/_system/users/APP/manifest.json @@ -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/**" } diff --git a/js/apps/system/_system/users/APP/package.json b/js/apps/system/_system/users/APP/package.json deleted file mode 100644 index b0ca357fbe..0000000000 --- a/js/apps/system/_system/users/APP/package.json +++ /dev/null @@ -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" - } -} diff --git a/js/apps/system/_system/users/APP/setup.js b/js/apps/system/_system/users/APP/setup.js index 218713f926..45a8b7768e 100644 --- a/js/apps/system/_system/users/APP/setup.js +++ b/js/apps/system/_system/users/APP/setup.js @@ -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}); +} diff --git a/js/apps/system/_system/users/APP/storage.js b/js/apps/system/_system/users/APP/storage.js index d7a1b9bdd0..c6954fe6ca 100644 --- a/js/apps/system/_system/users/APP/storage.js +++ b/js/apps/system/_system/users/APP/storage.js @@ -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; } }); diff --git a/js/apps/system/_system/users/APP/test/errors.js b/js/apps/system/_system/users/APP/test/errors.js index 34bf08f756..d4bfe8aa03 100644 --- a/js/apps/system/_system/users/APP/test/errors.js +++ b/js/apps/system/_system/users/APP/test/errors.js @@ -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); }); }); }); -}()); \ No newline at end of file +}); diff --git a/js/apps/system/_system/users/APP/test/setup.js b/js/apps/system/_system/users/APP/test/setup.js deleted file mode 100644 index 8dd9064c45..0000000000 --- a/js/apps/system/_system/users/APP/test/setup.js +++ /dev/null @@ -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); - }); - }); -}()); \ No newline at end of file