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

Commit

Permalink
feat: implement auth.reset
Browse files Browse the repository at this point in the history
  • Loading branch information
oureta committed Jan 21, 2016
1 parent e841890 commit 722e0b7
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
"REGISTER_EMAIL": "<register_email>",
"REGISTER_USERNAME": "<register_username>",
"REGISTER_PASSWORD": "<register_password>",
"RESET_CODE": "<reset_code>"
}
36 changes: 36 additions & 0 deletions dist/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exports.prelogin = prelogin;
exports.check = check;
exports.forgot = forgot;
exports.register = register;
exports.reset = reset;

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

Expand Down Expand Up @@ -180,4 +181,39 @@ function register() {
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 });
});
}

/**
* Resets the password
*
* @param {Object} args
* @param {String} args.code Password reset code
* @param {String} args.password The password (min: 8)
* @param {String} args.confirm This should be the same as password
*
* @return {Promise<Boolean>} Resolves <code>true</code> if successful
*
* @example
* auth
* .reset({ code: '<code>', password: '<password>', confirm: '<confirm>' })
* .then((success) => success)
* .catch((error) => error.message)
*/
function reset() {
var args = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var code = args.code;
var password = args.password;
var confirm = args.confirm;

if (!(code && 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/reset/' + code, { password: password, confirm: confirm }).then(_fetch.postProcess).then(function () {
return Promise.resolve({ message: 'Successful!' });
});
}
32 changes: 32 additions & 0 deletions src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,35 @@ export function register(args = {}) {
.then(postProcess)
.then(message => Promise.resolve({ message }));
}

/**
* Resets the password
*
* @param {Object} args
* @param {String} args.code Password reset code
* @param {String} args.password The password (min: 8)
* @param {String} args.confirm This should be the same as password
*
* @return {Promise<Boolean>} Resolves <code>true</code> if successful
*
* @example
* auth
* .reset({ code: '<code>', password: '<password>', confirm: '<confirm>' })
* .then((success) => success)
* .catch((error) => error.message)
*/
export function reset(args = {}) {
const { code, password, confirm } = args;

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

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

return post(`auth/reset/${code}`, { password, confirm })
.then(postProcess)
.then(() => Promise.resolve({ message: 'Successful!' }));
}
2 changes: 2 additions & 0 deletions test-dist/auth/check.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

require('./reset');

var _mocha = require('mocha');

var _chai = require('chai');
Expand Down
2 changes: 2 additions & 0 deletions test-dist/auth/forgot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

require('./reset');

var _mocha = require('mocha');

var _chai = require('chai');
Expand Down
37 changes: 37 additions & 0 deletions test-dist/auth/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

var _mocha = require('mocha');

var _chai = require('chai');

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

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

(0, _mocha.describe)('auth.reset', function () {
var code = process.env.RESET_CODE;
var password = process.env.PASSWORD;
var confirm = password;

(0, _mocha.it)('should be defined,\n returns a Promise,\n and reject if arguments are not valid', function (done) {
_chai.assert.ok(_auth.reset);
_chai.assert.typeOf((0, _auth.reset)(), 'Promise');
(0, _utils.shouldFail)((0, _auth.reset)(), done);
});

(0, _mocha.it)('should fail if code is invalid', function (done) {
return (0, _utils.shouldFail)((0, _auth.reset)({ code: 'invalidcode', password: password, confirm: confirm }), done);
});

(0, _mocha.it)('should fail if password and confirm are not the same', function (done) {
return (0, _utils.shouldFail)((0, _auth.reset)({ code: code, password: password, confirm: confirm + 1 }), done);
});

if (!code) return; // skip the tests below if there is no valid code

(0, _mocha.it)('should resolve with a success message if successful', function (done) {
return (0, _utils.shouldSucceed)((0, _auth.reset)({ code: code, password: password, confirm: confirm }), function (success) {
return _chai.assert.ok(success.message);
}, done);
});
});
2 changes: 2 additions & 0 deletions test/auth/check.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './reset';

import { describe, it } from 'mocha';
import { assert } from 'chai';
import { shouldFail, shouldSucceed } from '../lib/utils';
Expand Down
2 changes: 2 additions & 0 deletions test/auth/forgot.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './reset';

import { describe, it } from 'mocha';
import { assert } from 'chai';
import { shouldFail, shouldSucceed } from '../lib/utils';
Expand Down
49 changes: 49 additions & 0 deletions test/auth/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, it } from 'mocha';
import { assert } from 'chai';
import { shouldFail, shouldSucceed } from '../lib/utils';

import { reset } from '../../dist/auth';

describe('auth.reset', () => {
const code = process.env.RESET_CODE;
const password = process.env.PASSWORD;
const confirm = password;

it(
`should be defined,
returns a Promise,
and reject if arguments are not valid`,
done => {
assert.ok(reset);
assert.typeOf(reset(), 'Promise');
shouldFail(reset(), done);
}
);

it(
`should fail if code is invalid`,
done => shouldFail(
reset({ code: 'invalidcode', password, confirm }),
done
)
);

it(
`should fail if password and confirm are not the same`,
done => shouldFail(
reset({ code, password, confirm: confirm + 1 }),
done
)
);

if (!code) return; // skip the tests below if there is no valid code

it(
`should resolve with a success message if successful`,
done => shouldSucceed(
reset({ code, password, confirm }),
success => assert.ok(success.message),
done
)
);
});

0 comments on commit 722e0b7

Please sign in to comment.