salix/back/methods/mrw-config/specs/createShipment.spec.js

121 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-02-07 15:52:43 +00:00
const models = require('vn-loopback/server/server').models;
const axios = require('axios');
2024-02-16 07:28:40 +00:00
const fs = require('fs');
2024-02-07 15:52:43 +00:00
const mockBase64Binary = 'base64BinaryString';
const ticket1 = {
'id': '44',
'clientFk': 1101,
'shipped': Date.vnNew(),
'nickname': 'MRW',
'addressFk': 1,
2024-02-12 08:24:37 +00:00
'agencyModeFk': 999
2024-02-07 15:52:43 +00:00
};
const expedition1 = {
2024-02-16 13:15:13 +00:00
'id': 17,
2024-02-12 08:24:37 +00:00
'agencyModeFk': 999,
2024-02-07 15:52:43 +00:00
'ticketFk': 44,
'freightItemFk': 71,
'created': '2001-01-01',
'counter': 1,
'workerFk': 18,
'packagingFk': '94',
'hostFk': '',
'stateTypeFk': 3,
'hasNewRoute': 0,
'isBox': 71,
'editorFk': 100
};
2024-02-16 13:15:13 +00:00
2024-02-19 10:33:56 +00:00
let tx;
2024-02-16 07:28:40 +00:00
let options;
2024-02-12 08:24:37 +00:00
2024-02-19 10:33:56 +00:00
describe('MRWConfig createShipment()', () => {
beforeEach(async() => {
options = tx = undefined;
tx = await models.MrwConfig.beginTransaction({});
options = {transaction: tx};
2024-02-12 08:24:37 +00:00
await models.Agency.create(
{'id': 999, 'name': 'mrw'},
options
);
await models.AgencyMode.create(
{'id': 999, 'name': 'mrw', 'agencyFk': 999, 'code': 'mrw'},
options
);
2024-02-07 15:52:43 +00:00
2024-02-12 08:24:37 +00:00
await models.MrwConfig.create(
{
'url': 'https://url.com',
'user': 'user',
'password': 'password',
'franchiseCode': 'franchiseCode',
'subscriberCode': 'subscriberCode'
}, options
);
2024-02-07 15:52:43 +00:00
2024-02-12 08:24:37 +00:00
await models.Application.rawSql(
`INSERT INTO vn.mrwService
2024-02-16 07:28:40 +00:00
SET agencyModeCodeFk = 'mrw',
clientType = 1,
serviceType = 1,
kg = 1`, null, options
2024-02-12 08:24:37 +00:00
);
2024-02-16 07:28:40 +00:00
await models.Ticket.create(ticket1, options);
await models.Expedition.create(expedition1, options);
});
2024-02-19 10:33:56 +00:00
afterEach(async() => {
await tx.rollback();
});
2024-02-16 07:28:40 +00:00
it('should create a shipment and return a base64Binary label', async() => {
2024-02-19 10:33:56 +00:00
const mockPostResponses = [
{data: fs.readFileSync(__dirname + '/mockGetLabel.xml', 'utf-8')},
{data: fs.readFileSync(__dirname + '/mockCreateShipment.xml', 'utf-8')}
];
spyOn(axios, 'post').and.callFake(() => Promise.resolve(mockPostResponses.pop()));
2024-02-07 15:52:43 +00:00
2024-02-16 13:15:13 +00:00
const base64Binary = await models.MrwConfig.createShipment(expedition1.id, options);
2024-02-07 15:52:43 +00:00
2024-02-16 13:15:13 +00:00
expect(base64Binary).toEqual(mockBase64Binary);
2024-02-07 15:52:43 +00:00
});
2024-02-12 08:24:37 +00:00
2024-02-16 13:15:13 +00:00
it('should fail if mrwConfig has no data', async() => {
let error;
await models.MrwConfig.createShipment(expedition1.id).catch(e => {
error = e;
}).finally(async() => {
expect(error.message).toEqual(`Some mrwConfig parameters are not set`);
});
2024-02-16 07:28:40 +00:00
2024-02-16 13:15:13 +00:00
expect(error).toBeDefined();
});
2024-02-16 07:28:40 +00:00
2024-02-16 13:15:13 +00:00
it('should fail if expeditionFk is not a MrwExpedition', async() => {
let error;
await models.MrwConfig.createShipment(undefined, options).catch(e => {
error = e;
}).finally(async() => {
expect(error.message).toEqual(`This expedition is not a MRW shipment`);
});
});
it(' should fail if the creation date of this ticket is before the current date it', async() => {
let error;
const yesterday = Date.vnNew();
yesterday.setDate(yesterday.getDate() - 1);
await models.Ticket.updateAll({id: ticket1.id}, {shipped: yesterday}, options);
await models.MrwConfig.createShipment(expedition1.id, options).catch(e => {
error = e;
}).finally(async() => {
expect(error.message).toEqual(`This ticket has a shipped date earlier than today`);
});
});
});