115 lines
4.1 KiB
JavaScript
115 lines
4.1 KiB
JavaScript
const models = require('vn-loopback/server/server').models;
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
describe('Client Model', () => {
|
|
const activeCtx = {
|
|
accessToken: {userId: 9},
|
|
http: {
|
|
req: {
|
|
headers: {origin: 'http://localhost'},
|
|
[`__`]: value => {
|
|
return value;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
const ctx = {req: activeCtx};
|
|
const chatModel = models.Chat;
|
|
const instance = {id: 1101, name: 'Bruce Banner'};
|
|
const previousWorkerId = 1106; // DavidCharlesHaller
|
|
const currentWorkerId = 1107; // HankPym
|
|
|
|
beforeEach(() => {
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
active: activeCtx
|
|
});
|
|
});
|
|
|
|
describe('notifyAssignment()', () => {
|
|
it('should call to the Chat send() method for both workers', async() => {
|
|
spyOn(chatModel, 'send').and.callThrough();
|
|
|
|
await models.Client.notifyAssignment(instance, previousWorkerId, currentWorkerId);
|
|
|
|
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@DavidCharlesHaller', `Client assignment has changed`);
|
|
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', `Client assignment has changed`);
|
|
});
|
|
|
|
it('should call to the Chat send() method for the previous worker', async() => {
|
|
spyOn(chatModel, 'send').and.callThrough();
|
|
|
|
await models.Client.notifyAssignment(instance, null, currentWorkerId);
|
|
|
|
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@HankPym', `Client assignment has changed`);
|
|
});
|
|
|
|
it('should call to the Chat send() method for the current worker', async() => {
|
|
spyOn(chatModel, 'send').and.callThrough();
|
|
|
|
await models.Client.notifyAssignment(instance, previousWorkerId, null);
|
|
|
|
expect(chatModel.send).toHaveBeenCalledWith(ctx, '@DavidCharlesHaller', `Client assignment has changed`);
|
|
});
|
|
});
|
|
|
|
describe('changeCredit()', () => {
|
|
it('should fail to change the credit as a salesAssistant set to zero by a manager', async() => {
|
|
const tx = await models.Client.beginTransaction({});
|
|
|
|
let error;
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
const context = {options};
|
|
|
|
// Set credit to zero by a manager
|
|
const financialBoss = await models.Account.findOne({
|
|
where: {name: 'financialBoss'}
|
|
}, options);
|
|
context.options.accessToken = {userId: financialBoss.id};
|
|
|
|
await models.Client.changeCredit(context, instance, {credit: 0});
|
|
|
|
const salesAssistant = await models.Account.findOne({
|
|
where: {name: 'salesAssistant'}
|
|
}, options);
|
|
context.options.accessToken = {userId: salesAssistant.id};
|
|
|
|
await models.Client.changeCredit(context, instance, {credit: 300});
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
error = e;
|
|
await tx.rollback();
|
|
}
|
|
|
|
expect(error.message).toEqual(`You can't change the credit set to zero from a manager`);
|
|
});
|
|
|
|
it('should fail to change to a high credit amount as a salesAssistant', async() => {
|
|
const tx = await models.Client.beginTransaction({});
|
|
|
|
let error;
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
const context = {options};
|
|
|
|
const salesAssistant = await models.Account.findOne({
|
|
where: {name: 'salesAssistant'}
|
|
}, options);
|
|
context.options.accessToken = {userId: salesAssistant.id};
|
|
|
|
await models.Client.changeCredit(context, instance, {credit: 99999});
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
error = e;
|
|
await tx.rollback();
|
|
}
|
|
|
|
expect(error.message).toEqual(`You don't have enough privileges to set this credit amount`);
|
|
});
|
|
});
|
|
});
|