const models = require('vn-loopback/server/server').models; const axios = require('axios'); const fs = require('fs'); const mockBase64Binary = 'base64BinaryString'; const ticket1 = { 'id': '44', 'clientFk': 1101, 'shipped': Date.vnNew(), 'nickname': 'MRW', 'addressFk': 1, 'agencyModeFk': 999 }; const expedition1 = { 'id': 17, 'agencyModeFk': 999, 'ticketFk': 44, 'freightItemFk': 71, 'created': '2001-01-01', 'counter': 1, 'workerFk': 18, 'packagingFk': '94', 'hostFk': '', 'stateTypeFk': 3, 'hasNewRoute': 0, 'isBox': 71, 'editorFk': 100 }; let tx; let options; describe('MRWConfig createShipment()', () => { beforeEach(async() => { options = tx = undefined; tx = await models.MrwConfig.beginTransaction({}); options = {transaction: tx}; await models.Agency.create( {'id': 999, 'name': 'mrw'}, options ); await models.AgencyMode.create( {'id': 999, 'name': 'mrw', 'agencyFk': 999, 'code': 'mrw'}, options ); await models.MrwConfig.create( { 'id': 1, 'url': 'https://url.com', 'user': 'user', 'password': 'password', 'franchiseCode': 'franchiseCode', 'subscriberCode': 'subscriberCode' }, options ); await models.Application.rawSql( `INSERT INTO vn.mrwService SET agencyModeCodeFk = 'mrw', clientType = 1, serviceType = 1, kg = 1`, null, options ); await models.Ticket.create(ticket1, options); await models.Expedition.create(expedition1, options); }); afterEach(async() => { await tx.rollback(); }); it('should create a shipment and return a base64Binary label', async() => { 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())); const base64Binary = await models.MrwConfig.createShipment(expedition1.id, options); expect(base64Binary).toEqual(mockBase64Binary); }); 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`); }); expect(error).toBeDefined(); }); 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`); }); }); });