Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 1x 1x 1x 1x 1x 1x 1x 1x 3x 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 1x 1x 1x | import User, {hashPasswordOfUser} from './user';
import bcrypt from 'bcrypt';
import {match, assert, createSandbox} from 'sinon';
import {expect} from 'chai';
import * as config from '../../config';
const sandbox = createSandbox();
describe('User model', function() {
afterEach(function() {
sandbox.restore();
});
describe('hashPasswordOfUser', function() {
it('can hash string', function() {
const password = '1234';
const fakeUser = {
password,
};
// Overwrite saltRounds in config
const saltRounds = 2;
config.default.auth.saltRounds = saltRounds;
const fakeHash = 'some hash';
const hashStub = sandbox.stub(bcrypt)
.hash
.withArgs(match.string, saltRounds)
.resolves(fakeHash);
return hashPasswordOfUser(fakeUser as User).then(() => {
expect(fakeUser.password).to.equal(fakeHash);
assert.calledOnce(hashStub);
assert.calledWith(hashStub, password, saltRounds);
});
});
it('can hash number', function() {
const password = 1234;
const fakeUser = {
password,
};
// Overwrite saltRounds in config
const saltRounds = 2;
config.default.auth.saltRounds = saltRounds;
const fakeHash = 'some hash';
const hashStub = sandbox.stub(bcrypt)
.hash
.withArgs(match.string, match.number)
.resolves(fakeHash);
return hashPasswordOfUser(fakeUser as unknown as User).then(() => {
expect(fakeUser.password).to.equal(fakeHash);
assert.calledOnce(hashStub);
assert.calledWith(hashStub, password.toString(), saltRounds);
});
});
it('throws PasswordNotHashableError if bcrypt can\'t hash', function() {
const fakeUser = {
username: 'demo',
password: 1234,
};
const expectedMessage =
`Couldn't hash the password for user "${fakeUser.username}"`;
// Overwrite saltRounds in config
const saltRounds = 2;
config.default.auth.saltRounds = saltRounds;
const hashStub = sandbox.stub(bcrypt, 'hash')
.withArgs(match.string, match.number)
.rejects();
return hashPasswordOfUser(fakeUser as unknown as User).catch((error) => {
expect(fakeUser.password).to.equal(fakeUser.password);
assert.calledOnce(hashStub);
assert.calledWith(hashStub, fakeUser.password.toString(), saltRounds);
expect(error).to.have.property('message', expectedMessage);
expect(error).to.be.nested
.property('constructor.name', 'PasswordNotHashableError');
expect(error).to.have.property('user', fakeUser.username);
});
});
});
});
|