From 4333ea580685dba9cb1bcf65a8b0e12d99034832 Mon Sep 17 00:00:00 2001 From: Orlando Ureta Date: Tue, 19 Jan 2016 20:37:02 +0800 Subject: [PATCH] refactor: auth.prelogin and tests --- .env.json.dist | 1 + dist/auth.js | 8 ++++++-- gulpfile.babel.js | 3 ++- package.json | 3 ++- src/auth.js | 6 +++++- test-dist/auth-test.js | 22 -------------------- test-dist/auth/prelogin.js | 41 ++++++++++++++++++++++++++++++++++++++ test/auth-test.js | 20 ------------------- test/auth/prelogin.js | 41 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 98 insertions(+), 47 deletions(-) delete mode 100644 test-dist/auth-test.js create mode 100644 test-dist/auth/prelogin.js delete mode 100644 test/auth-test.js create mode 100644 test/auth/prelogin.js diff --git a/.env.json.dist b/.env.json.dist index a70f49f..0149d4c 100644 --- a/.env.json.dist +++ b/.env.json.dist @@ -1,5 +1,6 @@ { "BASE_URL": "https://staging.player.me/api/v1", + "NON_EXISTING_USERNAME": "", "USERNAME": "", "PASSWORD": "" } diff --git a/dist/auth.js b/dist/auth.js index 1608e96..7a0f383 100644 --- a/dist/auth.js +++ b/dist/auth.js @@ -7,8 +7,12 @@ exports.prelogin = prelogin; var _fetch = require('./lib/fetch'); -function prelogin(_ref) { - var login = _ref.login; +function prelogin() { + var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + + if (!args.login) return false; + + var login = args.login; return new Promise(function (resolve, reject) { (0, _fetch.post)('auth/pre-login', { login: login }).then(function (response) { diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 9ca4182..377296e 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -6,6 +6,7 @@ import eslint from 'gulp-eslint'; import del from 'del'; import runSequence from 'run-sequence'; import plumber from 'gulp-plumber'; +import yargs from 'yargs'; const config = { paths: { @@ -72,7 +73,7 @@ gulp.task('test', ['build'], () => gulp.src([config.paths.test.run]) .pipe(envs) .pipe(plumber()) - .pipe(mocha({ reporter: 'spec' })) + .pipe(mocha({ reporter: 'spec', grep: yargs.argv['mocha-grep'] })) .pipe(envs.reset) ); diff --git a/package.json b/package.json index c73ddef..ea36d2c 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,8 @@ "gulp-plumber": "^1.0.1", "gulp-util": "^3.0.7", "mocha": "^2.3.4", - "run-sequence": "^1.1.5" + "run-sequence": "^1.1.5", + "yargs": "^3.32.0" }, "dependencies": { "isomorphic-fetch": "^2.2.1" diff --git a/src/auth.js b/src/auth.js index 402b891..58d6a7e 100644 --- a/src/auth.js +++ b/src/auth.js @@ -1,6 +1,10 @@ import { post } from './lib/fetch'; -export function prelogin({ login }) { +export function prelogin(args = {}) { + if (!args.login) return false; + + const { login } = args; + return new Promise((resolve, reject) => { post('auth/pre-login', { login }) .then((response) => { diff --git a/test-dist/auth-test.js b/test-dist/auth-test.js deleted file mode 100644 index 38ee827..0000000 --- a/test-dist/auth-test.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict'; - -var _mocha = require('mocha'); - -var _chai = require('chai'); - -var _auth = require('../dist/auth'); - -(0, _mocha.describe)('auth tests', function () { - var username = process.env.USERNAME; - - (0, _mocha.it)('prelogin', function (done) { - (0, _auth.prelogin)({ login: username }).then(function (user) { - _chai.assert.typeOf(user, 'object'); - _chai.assert.ok(user.id, 'user has an id'); - _chai.assert.equal(user.username, username, 'username is equal to env username'); - done(); - }).catch(function () { - done(new Error('env user does not exist')); - }); - }); -}); \ No newline at end of file diff --git a/test-dist/auth/prelogin.js b/test-dist/auth/prelogin.js new file mode 100644 index 0000000..db0e376 --- /dev/null +++ b/test-dist/auth/prelogin.js @@ -0,0 +1,41 @@ +'use strict'; + +var _mocha = require('mocha'); + +var _chai = require('chai'); + +var _auth = require('../../dist/auth'); + +(0, _mocha.describe)('auth.prelogin', 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 () { + _chai.assert.ok(_auth.prelogin); + _chai.assert.ok(!(0, _auth.prelogin)()); + }); + + (0, _mocha.it)('should return a Promise if login parameter is given', function () { + _chai.assert.ok((0, _auth.prelogin)({ login: username }) instanceof Promise); + }); + + (0, _mocha.it)('should resolve to an object if user exists', function (done) { + (0, _auth.prelogin)({ login: username }).then(function (user) { + _chai.assert.typeOf(user, 'object'); + _chai.assert.ok(user.id); + _chai.assert.equal(user.username, username); + done(); + }).catch(function () { + done(new Error('env user does not exist')); + }); + }); + + (0, _mocha.it)('should reject with an error if user does not exist', function (done) { + (0, _auth.prelogin)({ login: nonExistingUsername }).then(function () { + done(new Error('env non-existing user exists')); + }).catch(function (error) { + _chai.assert.ok(error.message); + done(); + }); + }); +}); \ No newline at end of file diff --git a/test/auth-test.js b/test/auth-test.js deleted file mode 100644 index 43f1316..0000000 --- a/test/auth-test.js +++ /dev/null @@ -1,20 +0,0 @@ -import { describe, it } from 'mocha'; -import { assert } from 'chai'; - -import { prelogin } from '../dist/auth'; - -describe('auth tests', () => { - const username = process.env.USERNAME; - - it('prelogin', (done) => { - prelogin({ login: username }) - .then((user) => { - assert.typeOf(user, 'object'); - assert.ok(user.id, 'user has an id'); - assert.equal(user.username, username, 'username is equal to env username'); - done(); - }).catch(() => { - done(new Error('env user does not exist')); - }); - }); -}); diff --git a/test/auth/prelogin.js b/test/auth/prelogin.js new file mode 100644 index 0000000..18069b2 --- /dev/null +++ b/test/auth/prelogin.js @@ -0,0 +1,41 @@ +import { describe, it } from 'mocha'; +import { assert } from 'chai'; + +import { prelogin } from '../../dist/auth'; + +describe('auth.prelogin', () => { + const nonExistingUsername = process.env.NON_EXISTING_USERNAME; + const username = process.env.USERNAME; + + it('should be defined and return false if parameter is not valid', () => { + assert.ok(prelogin); + assert.ok(!prelogin()); + }); + + it('should return a Promise if login parameter is given', () => { + assert.ok(prelogin({ login: username }) instanceof Promise); + }); + + it('should resolve to an object if user exists', (done) => { + prelogin({ login: username }) + .then((user) => { + assert.typeOf(user, 'object'); + assert.ok(user.id); + assert.equal(user.username, username); + done(); + }) + .catch(() => { + done(new Error('env user does not exist')); + }); + }); + + it('should reject with an error if user does not exist', (done) => { + prelogin({ login: nonExistingUsername }) + .then(() => { + done(new Error('env non-existing user exists')); + }).catch((error) => { + assert.ok(error.message); + done(); + }); + }); +});