Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
refactor: standardize error and success handling and clean test suites
Browse files Browse the repository at this point in the history
  • Loading branch information
oureta committed Jan 21, 2016
1 parent 5b87d7a commit e841890
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 277 deletions.
48 changes: 36 additions & 12 deletions dist/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ exports.register = register;

var _fetch = require('./lib/fetch');

var _error = require('./lib/error');

var _error2 = _interopRequireDefault(_error);

var _cookie = require('cookie');

var _cookie2 = _interopRequireDefault(_cookie);
Expand Down Expand Up @@ -41,14 +45,16 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
* @example
* auth
* .prelogin({ login: '<login>' })
* .then((user) => user )
* .catch((error) => error.message )
* .then((user) => user)
* .catch((error) => error.message)
*/
function prelogin() {
var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var login = args.login;

if (!login) return false;
if (!login) {
return Promise.reject({ message: _error2.default.INVALID_ARGUMENTS });
}

return (0, _fetch.post)('auth/pre-login', { login: login }).then(_fetch.postProcess);
}
Expand All @@ -68,15 +74,17 @@ function prelogin() {
* @example
* auth
* .check({ login: '<login>', password: '<password>' })
* .then((user) => user )
* .catch((error) => error.message )
* .then((user) => user)
* .catch((error) => error.message)
*/
function check() {
var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var login = args.login;
var password = args.password;

if (!login || !password) return false;
if (!login || !password) {
return Promise.reject({ message: _error2.default.INVALID_ARGUMENTS });
}

return (0, _fetch.post)('auth/login', { login: login, password: password }).then(function (response) {
var cookies = _cookie2.default.parse(response.headers.get('set-cookie'));
Expand Down Expand Up @@ -112,14 +120,24 @@ function check() {
* @param {String} args.username The username
*
* @return {Promise<Boolean>} Resolves to <code>true</code> if successful
*
* @example
* auth
* .forgot({ username: '<username>' })
* .then((success) => success)
* .catch((error) => error.message)
*/
function forgot() {
var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var username = args.username;

if (!username) return false;
if (!username) {
return Promise.reject({ message: _error2.default.INVALID_ARGUMENTS });
}

return (0, _fetch.post)('auth/forgot', { username: username }).then(_fetch.postProcess);
return (0, _fetch.post)('auth/forgot', { username: username }).then(_fetch.postProcess).then(function () {
return Promise.resolve({ message: 'Successful!' });
});
}

/**
Expand All @@ -131,7 +149,7 @@ function forgot() {
* @param {String} password The password (min: 8)
* @param {String} confirm This should be the same as password
*
* @return {Promise<String>} Resolves to a success message
* @return {Promise<Object>} Resolves to a success message
*
* @example
* auth
Expand All @@ -141,8 +159,8 @@ function forgot() {
* password: '<password>',
* confirm: '<confirm>'
* })
* .then((success) => success.message )
* .catch((error) => error.message )
* .then((success) => success.message)
* .catch((error) => error.message)
*/
function register() {
var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
Expand All @@ -151,7 +169,13 @@ function register() {
var password = args.password;
var confirm = args.confirm;

if (!(username && email && password && confirm)) return false;
if (!(username && email && password && confirm)) {
return Promise.reject({ message: _error2.default.INVALID_ARGUMENTS });
}

if (password !== confirm) {
return Promise.reject({ message: _error2.default.PASSWORD_CONFIRM_NOT_MATCHED });
}

return (0, _fetch.post)('auth/register', { username: username, email: email, password: password, confirm: confirm }).then(_fetch.postProcess).then(function (message) {
return Promise.resolve({ message: message });
Expand Down
10 changes: 10 additions & 0 deletions dist/lib/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = {
DEFAULT: 'Error has been encountered',
INVALID_ARGUMENTS: 'Insufficient arguments',
PASSWORD_CONFIRM_NOT_MATCHED: 'Password and confirm are not the same'
};
54 changes: 37 additions & 17 deletions src/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { post, postProcess } from './lib/fetch';
import error from './lib/error';

import Cookie from 'cookie';

/**
Expand All @@ -24,13 +26,15 @@ import Cookie from 'cookie';
* @example
* auth
* .prelogin({ login: '<login>' })
* .then((user) => user )
* .catch((error) => error.message )
* .then((user) => user)
* .catch((error) => error.message)
*/
export function prelogin(args = {}) {
const { login } = args;

if (!login) return false;
if (!login) {
return Promise.reject({ message: error.INVALID_ARGUMENTS });
}

return post('auth/pre-login', { login }).then(postProcess);
}
Expand All @@ -50,16 +54,18 @@ export function prelogin(args = {}) {
* @example
* auth
* .check({ login: '<login>', password: '<password>' })
* .then((user) => user )
* .catch((error) => error.message )
* .then((user) => user)
* .catch((error) => error.message)
*/
export function check(args = {}) {
const { login, password } = args;

if (!login || !password) return false;
if (!login || !password) {
return Promise.reject({ message: error.INVALID_ARGUMENTS });
}

return post('auth/login', { login, password })
.then((response) => {
.then(response => {
const cookies = Cookie.parse(response.headers.get('set-cookie'));

// get subdomain / environemnt
Expand All @@ -73,7 +79,7 @@ export function check(args = {}) {

const playermeSession = cookies[sessionName];

return response.json().then((responseJSON) => {
return response.json().then(responseJSON => {
// inject session key into response result
const resultWithSessionKey = {
...responseJSON,
Expand All @@ -96,13 +102,23 @@ export function check(args = {}) {
* @param {String} args.username The username
*
* @return {Promise<Boolean>} Resolves to <code>true</code> if successful
*
* @example
* auth
* .forgot({ username: '<username>' })
* .then((success) => success)
* .catch((error) => error.message)
*/
export function forgot(args = {}) {
const { username } = args;

if (!username) return false;
if (!username) {
return Promise.reject({ message: error.INVALID_ARGUMENTS });
}

return post('auth/forgot', { username }).then(postProcess);
return post('auth/forgot', { username })
.then(postProcess)
.then(() => Promise.resolve({ message: 'Successful!' }));
}

/**
Expand All @@ -114,7 +130,7 @@ export function forgot(args = {}) {
* @param {String} password The password (min: 8)
* @param {String} confirm This should be the same as password
*
* @return {Promise<String>} Resolves to a success message
* @return {Promise<Object>} Resolves to a success message
*
* @example
* auth
Expand All @@ -124,17 +140,21 @@ export function forgot(args = {}) {
* password: '<password>',
* confirm: '<confirm>'
* })
* .then((success) => success.message )
* .catch((error) => error.message )
* .then((success) => success.message)
* .catch((error) => error.message)
*/
export function register(args = {}) {
const { username, email, password, confirm } = args;

if (!(username && email && password && confirm)) return false;
if (!(username && email && password && confirm)) {
return Promise.reject({ message: error.INVALID_ARGUMENTS });
}

if (password !== confirm) {
return Promise.reject({ message: error.PASSWORD_CONFIRM_NOT_MATCHED });
}

return post('auth/register', { username, email, password, confirm })
.then(postProcess)
.then((message) => {
return Promise.resolve({ message });
});
.then(message => Promise.resolve({ message }));
}
5 changes: 5 additions & 0 deletions src/lib/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
DEFAULT: 'Error has been encountered',
INVALID_ARGUMENTS: 'Insufficient arguments',
PASSWORD_CONFIRM_NOT_MATCHED: 'Password and confirm are not the same'
};
34 changes: 13 additions & 21 deletions test-dist/auth/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,31 @@ var _mocha = require('mocha');

var _chai = require('chai');

var _utils = require('../lib/utils');

var _auth = require('../../dist/auth');

(0, _mocha.describe)('auth.check', function () {
var nonExistingUsername = process.env.NON_EXISTING_USERNAME;
var username = process.env.USERNAME;
var login = process.env.USERNAME;
var password = process.env.PASSWORD;

(0, _mocha.it)('should be defined and return false if parameter is not valid', function () {
(0, _mocha.it)('should be defined,\n return a Promise,\n and fail if arguments are not valid', function (done) {
_chai.assert.ok(_auth.check);
_chai.assert.ok(!(0, _auth.check)());
_chai.assert.typeOf((0, _auth.check)(), 'Promise');
(0, _utils.shouldFail)((0, _auth.check)(), done);
});

(0, _mocha.it)('should resolve to an object with playerme_session if valid', function (done) {
var promise = (0, _auth.check)({ login: username, password: password });
_chai.assert.typeOf(promise, 'Promise');
(0, _mocha.it)('should fail if username does not exist', function (done) {
return (0, _utils.shouldFail)((0, _auth.check)({ login: nonExistingUsername, password: password }), done);
});

promise.then(function (user) {
(0, _mocha.it)('should resolve to a user object\n { id, username, playerme_session }\n if successful', function (done) {
return (0, _utils.shouldSucceed)((0, _auth.check)({ login: login, password: password }), function (user) {
_chai.assert.typeOf(user, 'object');
_chai.assert.ok(user.id);
_chai.assert.equal(user.username, username);
_chai.assert.equal(user.username, login);
_chai.assert.ok(user.playerme_session);
done();
}).catch(function () {
done(new Error('env username and or password is incorrect'));
});
});

(0, _mocha.it)('should reject with an error if not valid', function (done) {
(0, _auth.check)({ login: nonExistingUsername, password: password }).then(function () {
done(new Error('env non-existing user exists'));
}).catch(function (error) {
_chai.assert.ok(error.message);
done();
});
}, done);
});
});
30 changes: 11 additions & 19 deletions test-dist/auth/forgot.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,27 @@ var _mocha = require('mocha');

var _chai = require('chai');

var _utils = require('../lib/utils');

var _auth = require('../../dist/auth');

(0, _mocha.describe)('auth.forgot', function () {
var nonExistingUsername = process.env.NON_EXISTING_USERNAME;
var username = process.env.USERNAME;

(0, _mocha.it)('should be defined and return false if parameter is not valid', function () {
(0, _mocha.it)('should be defined,\n return a Promise,\n and fail if arguments are not valid', function (done) {
_chai.assert.ok(_auth.forgot);
_chai.assert.ok(!(0, _auth.forgot)());
_chai.assert.typeOf((0, _auth.forgot)(), 'Promise');
(0, _utils.shouldFail)((0, _auth.forgot)(), done);
});

(0, _mocha.it)('should resolve if user exists', function (done) {
var promise = (0, _auth.forgot)({ username: username });
_chai.assert.typeOf(promise, 'Promise');

promise.then(function (success) {
_chai.assert.isTrue(success);
done();
}).catch(function () {
done(new Error('env user does not exist'));
});
(0, _mocha.it)('should fail if user does not exist', function (done) {
return (0, _utils.shouldFail)((0, _auth.forgot)({ username: nonExistingUsername }), done);
});

(0, _mocha.it)('should reject with an error if user does not exist', function (done) {
(0, _auth.forgot)({ username: nonExistingUsername }).then(function () {
done(new Error('env non-existing user exists'));
}).catch(function (error) {
_chai.assert.ok(error.message);
done();
});
(0, _mocha.it)('should resolve with a success message if user exists', function (done) {
return (0, _utils.shouldSucceed)((0, _auth.forgot)({ username: username }), function (success) {
return _chai.assert.ok(success.message);
}, done);
});
});
Loading

0 comments on commit e841890

Please sign in to comment.