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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 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 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 {signUpController} from './sign-up-controller';
import {fake, assert, match, createSandbox} from 'sinon';
import Bluebird from 'bluebird';
import {UserDto, ProfilePic, User} from '../../../models';
import {expect} from 'chai';
import {UniqueConstraintError} from 'sequelize';
import UsernameAlreadyExistsError from
'../../../errors/user/username-already-exists-error';
import * as generatePic from '../../../util/generate-profile-pic';
import config from '../../../config';
import db from '../../../db';
const sandbox = createSandbox();
describe('SignUpController', function() {
let transactionStub: sinon.SinonStub;
let transaction: any;
beforeEach(function() {
transaction = {
commit: sandbox.stub().resolves(),
rollback: sandbox.stub().resolves(),
};
transactionStub = sandbox.stub(
db,
'transaction',
).resolves(transaction as any);
});
afterEach(function() {
sandbox.restore();
});
it('creates user', function(done) {
// Create fake user
const user = {
id: 5,
username: 'demo',
email: 'demo@mail.com',
password: 'password',
isBetaUser: false,
get() {
return this;
},
};
const jwt = {
isBetaUser: user.isBetaUser,
username: user.username,
};
// Mock profile pic create function
const picCreateStub = sandbox.stub(ProfilePic, 'create');
picCreateStub.usingPromise(Bluebird).resolves();
// Mock profile pic generation
const fakeData = 'DATA';
const generateStub = sandbox.stub(generatePic, 'default')
.resolves(fakeData as any);
// Creates stubs
const createStub = sandbox.stub(User, 'create');
createStub.usingPromise(Bluebird.Promise).resolves(user as any);
const requestStub: any = sandbox.stub();
requestStub.body = user;
const responseStub: any = sandbox.stub();
responseStub.status = sandbox.stub()
.withArgs(201).returns(responseStub as any);
responseStub.setJwtToken = fake();
responseStub.send = fake(() => {
expect(responseStub.send.called);
assert.notCalled(fakeNext);
sandbox.assert.calledWith(responseStub.send, match.instanceOf(UserDto));
sandbox.assert.calledWith(responseStub.status, 201);
sandbox.assert.calledOnce(responseStub.setJwtToken);
sandbox.assert.calledWith(responseStub.setJwtToken, match(jwt));
// Check generatePic call
const dim = config.user.pb.dimensions;
sandbox.assert.calledOnceWithExactly(generateStub, dim, user.username, 0);
sandbox.assert.calledOnce(picCreateStub);
sandbox.assert.calledOnce(transactionStub);
sandbox.assert.calledOnce(transaction.commit);
// Get user object
const responseUser = responseStub.send.args[0][0];
expect(responseUser.username).to.equal(user.username);
expect(responseUser.email).to.equal(user.email);
expect(responseUser.password).to.be.undefined;
done();
}) as any;
const fakeNext = fake();
signUpController(requestStub, responseStub, fakeNext);
});
it('fails to create user because username already exists', async function() {
const user = {
username: 'demo',
email: 'demo@mail.com',
password: 'password',
get() {
return this;
},
};
// Create stubs
const createStub = sandbox.stub(User, 'create');
createStub.usingPromise(Bluebird.Promise)
.rejects(new UniqueConstraintError());
const requestStub: any = sandbox.stub();
const responseStub: any = sandbox.stub();
requestStub.body = user;
try {
await signUpController(requestStub, responseStub, undefined as any);
} catch (err) {
expect(err).to.be.instanceOf(UsernameAlreadyExistsError);
expect(err).to.haveOwnProperty('username');
expect(err.username).to.equal(user.username);
sandbox.assert.calledOnce(transaction.rollback);
}
});
it('fails to create user due to some error', async function() {
// Fake user
const user = {
username: 'demo',
email: 'demo@mail.com',
password: 'password',
get() {
return this;
},
};
// Fake error
const error = new Error('CUSTOM ERROR');
// Create stubs
const createStub = sandbox.stub(User, 'create');
createStub.usingPromise(Bluebird.Promise)
.rejects(error);
const requestStub: any = sandbox.stub();
const responseStub: any = sandbox.stub();
requestStub.body = user;
try {
await signUpController(requestStub, responseStub, undefined as any);
} catch (err) {
expect(err).to.be.equal(error);
sandbox.assert.calledOnce(transaction.rollback);
}
});
});
|