salix/modules/ticket/back/methods/ticket/specs/restore.spec.js

105 lines
3.3 KiB
JavaScript

const app = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context');
const models = app.models;
describe('ticket restore()', () => {
const employeeUser = 1110;
const ticketId = 9;
const activeCtx = {
accessToken: {userId: employeeUser},
headers: {
origin: 'http://localhost:5000'
},
__: () => {}
};
const ctx = {req: activeCtx};
beforeEach(() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
it('should throw an error if the given ticket has past the deletion time', async() => {
let error;
const tx = await app.models.Ticket.beginTransaction({});
const now = Date.vnNew();
now.setHours(now.getHours() - 1.1);
try {
const options = {transaction: tx};
const ticket = await models.Ticket.findById(ticketId, null, options);
await ticket.updateAttributes({
isDeleted: true,
updated: now
}, options);
await models.TicketLog.create({
originFk: ticketId,
userFk: employeeUser,
action: 'update',
changedModel: 'Ticket',
creationDate: new Date('2001-01-01 10:59:00'),
newInstance: '{"isDeleted":true}'
}, options);
await app.models.Ticket.restore(ctx, ticketId, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e;
}
expect(error.message).toContain('You can only restore a ticket within the first hour after deletion');
});
it('should restore the ticket making its state no longer deleted', async() => {
const tx = await app.models.Ticket.beginTransaction({});
const now = Date.vnNew();
try {
const options = {transaction: tx};
const ticketBeforeUpdate = await models.Ticket.findById(ticketId, null, options);
await ticketBeforeUpdate.updateAttributes({
isDeleted: true,
updated: now
}, options);
await models.TicketLog.create({
originFk: ticketId,
userFk: employeeUser,
action: 'update',
changedModel: 'Ticket',
creationDate: new Date('2001-01-01 11:01:00'),
newInstance: '{"isDeleted":true}'
}, options);
const ticketAfterUpdate = await models.Ticket.findById(ticketId, null, options);
expect(ticketAfterUpdate.isDeleted).toBeTruthy();
await models.Ticket.restore(ctx, ticketId, options);
const ticketAfterRestore = await models.Ticket.findById(ticketId, null, options);
const fullYear = now.getFullYear();
const shippedFullYear = ticketAfterRestore.shipped.getFullYear();
const landedFullYear = ticketAfterRestore.landed.getFullYear();
expect(ticketAfterRestore.isDeleted).toBeFalsy();
expect(shippedFullYear).toEqual(fullYear);
expect(landedFullYear).toEqual(fullYear);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});