salix/services/loopback/common/methods/client/specs/createWithUser.spec.js

87 lines
3.0 KiB
JavaScript
Raw Normal View History

const app = require('../../../../../client/server/server');
const catchErrors = require('../../../../../../services/utils/jasmineHelpers').catchErrors;
const restoreFixtures = require('../../../../../../services/db/testing_fixtures');
describe('Client Create', () => {
let sqlStatements = {deletes: `
DELETE FROM vn.address WHERE nickname = "Wade";
DELETE FROM vn.client WHERE name = "Wade";
DELETE FROM account.user WHERE name = "Deadpool";
`, inserts: ``, updates: ``};
beforeAll(() => {
restoreFixtures(sqlStatements);
});
afterAll(() => {
restoreFixtures(sqlStatements);
});
let newAccountData = {
active: true,
name: 'Wade',
userName: 'Deadpool',
email: 'Deadpool@marvel.com',
2018-02-27 11:56:03 +00:00
fi: '16195279J',
socialName: 'Deadpool Marvel',
salesPersonFk: 1
};
it('should find Charles Xavier', done => {
app.models.Account.findOne({where: {name: 'CharlesXavier'}})
.then(account => {
expect(account.name).toEqual('CharlesXavier');
done();
});
});
it(`should not find Deadpool as he's not created yet`, done => {
app.models.Account.findOne({where: {name: newAccountData.userName}})
.then(account => {
expect(account).toEqual(null);
app.models.Client.findOne({where: {name: newAccountData.name}})
.then(client => {
expect(client).toEqual(null);
done();
});
})
.catch(catchErrors(done));
});
it('should not be able to create a user if exists', async() => {
let client = await app.models.Client.findOne({where: {name: 'Charles Xavier'}});
let account = await app.models.Account.findOne({where: {id: client.id}});
let formerAccountData = {
name: client.name,
userName: account.name,
email: client.email,
fi: client.fi,
socialName: client.socialName
};
try {
let client = await app.models.Client.createWithUser(formerAccountData);
expect(client).toBeNull();
} catch (err) {
expect(err.details.codes.name[0]).toEqual('uniqueness');
}
});
it('should create a new account', async() => {
let client = await app.models.Client.createWithUser(newAccountData);
let account = await app.models.Account.findOne({where: {name: newAccountData.userName}});
expect(account.name).toEqual(newAccountData.userName);
client = await app.models.Client.findOne({where: {name: newAccountData.name}});
expect(client.id).toEqual(account.id);
expect(client.name).toEqual(newAccountData.name);
expect(client.email).toEqual(newAccountData.email);
expect(client.fi).toEqual(newAccountData.fi);
expect(client.socialName).toEqual(newAccountData.socialName);
});
});