All files / tests index.js

100% Statements 35/35
100% Branches 0/0
100% Functions 12/12
100% Lines 33/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51      1x 1x 1x     1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x   1x 1x      
import test from 'tape';
import {hash, compare, reset, update} from '../src';
 
test('hash() errors if no password given', t => {
  t.throws(hash.bind(undefined, {}), 'No password provided', 'Errors when no password is provided');
  t.end();
});
 
test('hash() produces a salted hash and overrides user password with it', t => {
  const user = {password: 'password'};
  hash(user).then(hash => {
    t.equal(hash.substr(0, 4), '$2a$', 'Hash begins with correct prefix');
    t.equal(user.password, hash, 'User password field is set to hash');
    t.end();
  });
});
 
test('compare() correctly compares password to salted hash', t => {
  const password = 'password';
  const user = {password};
  hash(user).then(() => compare(user, password)).then(result => {
    t.ok(result, 'User password field equals password hash');
    t.end();
  });
});
 
test('reset() generates a token and adds to user with expire', t => {
  const user = {};
  const future = Date.now() + 3600000;
  reset(user).then(token => {
    t.equals(token.length, 40, 'Token length is 40 chars');
    t.ok(user.resetPasswordToken, 'User resetPasswordToken field is set');
    t.ok(user.resetPasswordExpires, 'User resetPasswordExpires field is set');
    t.ok(user.resetPasswordExpires >= future, 'User resetPasswordExpires field is set to 3600000 seconds in the future');
    t.end();
  });
});
 
test('update() sets hash of given password on user and removes previously generated token and expiration values', t => {
  const user = {};
  const password = 'password';
  reset(user).then(() => update(user, password)).then(() => {
    t.notOk(user.resetPasswordToken, 'User resetPasswordToken field is unset');
    t.notOk(user.resetPasswordExpires, 'User resetPasswordExpires field is unset');
    return compare(user, password);
  }).then(result => {
    t.ok(result, 'User password is updated correctly');
    t.end();
  });
});