All files / server/spec/expectations user.js

96.88% Statements 31/32
83.33% Branches 5/6
100% Functions 2/2
96.88% Lines 31/32
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 52 53 54 55 56 57 58 59 60 61 62 631x 1x   1x 1x   1x   7x 7x                 7x   7x 5x   2x     7x   7x   7x   7x     7x       7x     1x   7x 7x   7x 7x 7x 7x 7x 7x 7x   7x 2x 2x   5x      
const _ = require('lodash');
const bcrypt = require('bcryptjs');
 
const User = require('../../models/user');
const { checkRecord, expect, toArray } = require('../utils');
 
module.exports = function(actual, expected) {
 
  expect(actual, 'res.body').to.be.an('object');
  expect(actual, 'res.body').to.have.all.keys([
    'active',
    'createdAt',
    'id',
    'email',
    'roles',
    'updatedAt'
  ]);
 
  expect(actual.active, 'user.active').to.equal(_.get(expected, 'active', true));
 
  if (expected.id) {
    expect(actual.id, 'user.id').to.equal(expected.id);
  } else {
    expect(actual.id, 'user.id').to.be.a('string');
  }
 
  expect(actual.email, 'user.email').to.equal(expected.email);
 
  expect(actual.roles, 'user.roles').to.eql(_.get(expected, 'roles', []));
 
  expect(actual.createdAt, 'user.createdAt').to.be.iso8601(...toArray(expected.createdAt));
 
  Iif (expected.updatedAt == 'createdAt') {
    expect(actual.updatedAt, 'user.updatedAt').to.equal(actual.createdAt);
  } else {
    expect(actual.updatedAt, 'user.updatedAt').to.be.iso8601(...toArray(expected.updatedAt));
  }
 
  // Check that the corresponding user exists in the database.
  return module.exports.inDb(_.merge({}, actual, _.pick(expected, 'password')));
};
 
module.exports.inDb = async function(expected) {
 
  const user = await checkRecord(User, expected.id);
  expect(user, 'db.user').to.be.an.instanceof(User);
 
  expect(user.get('active'), 'db.user.active').to.equal(expected.active);
  expect(user.get('api_id'), 'db.user.api_id').to.equal(expected.id);
  expect(user.get('id'), 'db.user.id').to.be.a('string');
  expect(user.get('email'), 'db.user.email').to.equal(expected.email);
  expect(user.get('roles'), 'db.user.roles').to.eql(expected.roles);
  expect(user.get('created_at'), 'db.user.created_at').to.be.sameMoment(expected.createdAt);
  expect(user.get('updated_at'), 'db.user.updated_at').to.be.sameMoment(expected.updatedAt);
 
  if (expected.password) {
    expect(bcrypt.compareSync(expected.password, user.get('password_hash')), 'db.user.password_hash').to.equal(true);
    expect(user.hasPassword(expected.password), 'db.user.password_hash').to.equal(true);
  } else {
    expect(user.get('password_hash')).to.be.a('string');
  }
};