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

Commit

Permalink
refactor: auth.prelogin and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oureta committed Jan 19, 2016
1 parent 2951ab8 commit 4333ea5
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 47 deletions.
1 change: 1 addition & 0 deletions .env.json.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"BASE_URL": "https://staging.player.me/api/v1",
"NON_EXISTING_USERNAME": "<non-existing-username>",
"USERNAME": "<username>",
"PASSWORD": "<password>"
}
8 changes: 6 additions & 2 deletions dist/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion gulpfile.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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)
);

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion src/auth.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
22 changes: 0 additions & 22 deletions test-dist/auth-test.js

This file was deleted.

41 changes: 41 additions & 0 deletions test-dist/auth/prelogin.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
20 changes: 0 additions & 20 deletions test/auth-test.js

This file was deleted.

41 changes: 41 additions & 0 deletions test/auth/prelogin.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit 4333ea5

Please sign in to comment.