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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | 1x 1x 1x 1x 1x 1x 1x 1x 4x 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 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 1x 1x 1x 1x 1x | /* eslint-disable @typescript-eslint/no-explicit-any */
import {stub, fake, assert, match, createSandbox} from 'sinon';
import {User, UserDto} from '../../../models';
import bcrypt from 'bcrypt';
import loginController from './login-controller';
import Bluebird from 'bluebird';
const sandbox = createSandbox();
describe('LoginController', function() {
afterEach(() => {
sandbox.restore();
});
it('logs in, if credentials correct', function(done) {
const requestBody = {
username: 'demo',
email: 'demo@mail.com',
password: '123456',
};
const dbUser = {
username: 'demo',
email: 'demo@mail.com',
password: 'hashed',
isBetaUser: false,
get() {
return this;
},
};
const expectedJwt = {
username: 'demo',
isBetaUser: false,
};
// Stub bcrypt comparing
const compareStub = sandbox.stub(bcrypt, 'compare');
compareStub.resolves(true);
// Stub database
const findByUsernameStub = sandbox.stub(User, 'findByUsername');
findByUsernameStub.usingPromise(Bluebird).resolves(dbUser as any);
// Stub express
const requestStub: any = sandbox.stub();
requestStub.body = requestBody;
const responseStub: any = sandbox.stub();
responseStub.setJwtToken = fake();
const next = fake();
responseStub.send = fake(() => {
sandbox.assert.calledWith(compareStub,
requestBody.password,
dbUser.password);
assert.notCalled(next);
sandbox.assert.calledOnce(responseStub.send);
sandbox.assert.calledWith(responseStub.send, match.instanceOf(UserDto));
sandbox.assert.calledWith(findByUsernameStub, requestBody.username);
sandbox.assert.calledOnce(responseStub.setJwtToken);
sandbox.assert.calledWith(responseStub.setJwtToken, match(expectedJwt));
done();
}) as any;
loginController(requestStub, responseStub, next);
});
describe('throws error', function() {
it('if database throws error while finding user', function(done) {
const requestBody = {
username: 'demo',
email: 'demo@mail.com',
password: '123456',
};
const dbError = new Error('SOME MESSAGE');
// Stub database
const findOneStub = sandbox.stub(User, 'findOne');
findOneStub.usingPromise(Bluebird).rejects(dbError);
// Stub express
const requestStub: any = sandbox.stub();
requestStub.body = requestBody;
const responseStub: any = sandbox.stub();
responseStub.send = sandbox.stub();
const next = fake(() => {
sandbox.assert.notCalled(responseStub.send);
assert.calledWith(next, match(dbError));
done();
});
loginController(requestStub, responseStub, next);
});
it('if given password doesn\'t match stored hash', function(done) {
const requestBody = {
username: 'demo',
email: 'demo@mail.com',
password: '123456',
};
const dbUser = {
username: 'demo',
email: 'demo@mail.com',
password: 'hashed',
};
// Stub bcrypt comparing
const compareStub = sandbox.stub(bcrypt, 'compare');
compareStub.resolves(false);
// Stub database
const findOneStub = sandbox.stub(User, 'findOne');
findOneStub.usingPromise(Bluebird).resolves(dbUser as any);
// Stub express
const requestStub: any = sandbox.stub();
requestStub.body = requestBody;
const responseStub: any = sandbox.stub();
responseStub.send = sandbox.stub();
const next = fake(() => {
assert.notCalled(responseStub.send);
assert.calledWith(compareStub, requestBody.password, dbUser.password);
assert.calledWith(findOneStub,
match({where: {username: requestBody.username}}));
assert.calledWith(next,
match.hasNested('constructor.name', 'InvalidLoginError'));
assert.calledWith(next,
match.has('message', 'Username or email is invalid'));
assert.calledWith(next, match.has('statusCode', 400));
done();
}) as any;
loginController(requestStub, responseStub, next);
});
it('if database can\'t find user', function(done) {
const requestBody = {
username: 'demo',
email: 'demo@mail.com',
password: '123456',
};
// Stub bcrypt comparing
const compareStub = sandbox.stub(bcrypt, 'compare');
// Stub database
const findOneStub = sandbox.stub(User, 'findOne');
findOneStub.usingPromise(Bluebird).resolves(null as any);
// Stub express
const requestStub: any = sandbox.stub();
requestStub.body = requestBody;
const responseStub: any = sandbox.stub();
responseStub.send = stub();
const next = fake(() => {
assert.notCalled(responseStub.send);
assert.notCalled(compareStub);
assert.calledWith(findOneStub,
match({where: {username: requestBody.username}}));
assert.calledWith(next,
match.hasNested('constructor.name', 'InvalidLoginError'));
assert.calledWith(next,
match.has('message', 'Username or email is invalid'));
assert.calledWith(next, match.has('statusCode', 400));
done();
}) as any;
loginController(requestStub, responseStub, next);
});
});
});
|