Updated back unit test

This commit is contained in:
Joan Sanchez 2022-01-21 16:16:39 +01:00
parent f5217c3ebc
commit ddf62d2496
3 changed files with 21 additions and 14 deletions

View File

@ -8,7 +8,7 @@ ALTER TABLE `vn`.`ticket` AUTO_INCREMENT = 1;
INSERT INTO `salix`.`AccessToken` (`id`, `ttl`, `created`, `userId`)
VALUES
('TOTALLY_SECURE_TOKEN', '1209600', CURDATE(), 66);
('DEFAULT_TOKEN', '1209600', CURDATE(), 66);
INSERT INTO `vn`.`ticketConfig` (`id`, `scopeDays`)

View File

@ -80,9 +80,11 @@ module.exports = Self => {
if (tx) await tx.commit();
response.data.pipe(fs.createWriteStream(fileSrc));
}).catch(async() => {
}).catch(async e => {
if (fs.existsSync(fileSrc))
await fs.unlink(fileSrc);
throw e;
});
} catch (e) {
if (tx) await tx.rollback();

View File

@ -1,24 +1,28 @@
const models = require('vn-loopback/server/server').models;
const got = require('got');
const LoopBackContext = require('loopback-context');
const fs = require('fs-extra');
const axios = require('axios');
describe('InvoiceOut createPdf()', () => {
const userId = 1;
const ctx = {
req: {
accessToken: {userId: userId},
headers: {origin: 'http://localhost:5000'},
}
const activeCtx = {
accessToken: {userId: userId, id: 'DEFAULT_TOKEN'},
headers: {origin: 'http://localhost:5000'}
};
const ctx = {req: activeCtx};
it('should create a new PDF file and set true the hasPdf property', async() => {
const invoiceId = 1;
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
const response = {
pipe: () => {},
on: () => {},
data: {
pipe: () => {},
on: () => {},
}
};
spyOn(got, 'stream').and.returnValue(response);
spyOn(axios, 'get').and.returnValue(new Promise(resolve => resolve(response)));
spyOn(models.InvoiceContainer, 'container').and.returnValue({
client: {root: '/path'}
});
@ -32,9 +36,10 @@ describe('InvoiceOut createPdf()', () => {
const options = {transaction: tx};
try {
const result = await models.InvoiceOut.createPdf(ctx, invoiceId, options);
await models.InvoiceOut.createPdf(ctx, invoiceId, options);
const invoiceOut = await models.InvoiceOut.findById(invoiceId, null, options);
expect(result.hasPdf).toBe(true);
expect(invoiceOut.hasPdf).toBe(true);
await tx.rollback();
} catch (e) {