From 00e8b2a0d5a73daedffeb8363cf9f4419f4ca41a Mon Sep 17 00:00:00 2001 From: Bastien DUMONT <bdumont@grandlyon.com> Date: Thu, 25 Aug 2022 07:12:22 +0000 Subject: [PATCH] fix: throw errors when no contracts are found --- .gitlab-ci.yml | 2 +- __tests__/core/contractVerification.spec.js | 30 +++++++++++++++++++++ src/core/contractVerification.js | 12 ++++++++- src/helpers/parsing.js | 13 +++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1298966..cd98c30 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -40,7 +40,7 @@ unit-test: - apk add bash script: - yarn - - yarn test --ci --reporters=default --reporters=jest-junit + - yarn test --ci --reporters=default --reporters=jest-junit --coverage coverage: "/All files[^|]*\\|[^|]*\\s+([\\d\\.]+)/" artifacts: when: always diff --git a/__tests__/core/contractVerification.spec.js b/__tests__/core/contractVerification.spec.js index 12eddf0..33b2ab7 100644 --- a/__tests__/core/contractVerification.spec.js +++ b/__tests__/core/contractVerification.spec.js @@ -6,8 +6,10 @@ const mockSoapRequest = jest.fn() jest.mock('easy-soap-request', () => async () => mockSoapRequest()) const mockParseContracts = jest.fn() +const mockCheckContractExists = jest.fn() jest.mock('../../src/helpers/parsing', () => ({ parseContracts: () => mockParseContracts(), + checkContractExists: () => mockCheckContractExists(), })) const responseMock = { @@ -33,6 +35,7 @@ describe('verifyContract', () => { 'Collecte de la courbe de charge au pas 30 min avec transmission quotidienne des données brutes en soutirage', }, ]) + mockCheckContractExists.mockReturnValueOnce(true) jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValue( { @@ -84,6 +87,7 @@ describe('verifyContract', () => { serviceSouscritLibelle: 'Collecte de la courbe de charge au pas 30 min avec transmission quotidienne des données brutes en soutirage', }) + mockCheckContractExists.mockReturnValueOnce(true) jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValue( { @@ -143,6 +147,7 @@ describe('verifyContract', () => { 'Collecte de la courbe de charge au pas 30 min avec transmission quotidienne des données brutes en soutirage', }, ]) + mockCheckContractExists.mockReturnValueOnce(true) jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValue( { @@ -212,6 +217,7 @@ describe('verifyContract', () => { }, }, }) + mockCheckContractExists.mockReturnValueOnce(true) try { await verifyContract( @@ -226,4 +232,28 @@ describe('verifyContract', () => { expect(error).toBe(errors.LOGIN_FAILED) } }) + it('should return NULL if no contract are found 🚫', async () => { + mockSoapRequest.mockResolvedValue(responseMock) + jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValueOnce({ + Envelope: { + Body: { + rechercherServicesSouscritsMesuresResponse: {}, + }, + }, + }) + + try { + const serviceId = await verifyContract( + 'http://test.com', + '111', + 'login@log.com', + '1234567', + '1111111111111' + ) + expect(serviceId).toBe(null) + } catch (error) { + expect(true).toBe(false) + } + mockParseContracts.mockRestore() + }) }) diff --git a/src/core/contractVerification.js b/src/core/contractVerification.js index abc62f0..6216819 100644 --- a/src/core/contractVerification.js +++ b/src/core/contractVerification.js @@ -1,7 +1,12 @@ // @ts-check const { log, errors } = require('cozy-konnector-libs') const soapRequest = require('easy-soap-request') -const { parseTags, parseValue, parseContracts } = require('../helpers/parsing') +const { + parseTags, + parseValue, + parseContracts, + checkContractExists, +} = require('../helpers/parsing') const { rechercherServicesSouscritsMesures } = require('../requests/sge') const xml2js = require('xml2js') const { contractState, contractLibelle } = require('./types/enum') @@ -37,6 +42,11 @@ async function verifyContract(url, apiAuthKey, appLogin, contractId, pointId) { }) try { + if (!checkContractExists(parsedReply)) { + log('error', 'no contract found') + return null + } + const currentContracts = parseContracts(parsedReply) let currentContract = null if (Array.isArray(currentContracts)) { diff --git a/src/helpers/parsing.js b/src/helpers/parsing.js index a1f9e8f..1d10a74 100644 --- a/src/helpers/parsing.js +++ b/src/helpers/parsing.js @@ -89,6 +89,18 @@ async function formateDataForDoctype(data) { }) } +/** + * Check if response contains contracts + * @param {string} parsedReply + * @return {boolean} + */ +function checkContractExists(parsedReply) { + const json = JSON.stringify(parsedReply) + return JSON.parse(json)['Envelope']['Body'][ + 'rechercherServicesSouscritsMesuresResponse' + ]['servicesSouscritsMesures'] +} + /** * Format tag in order to be manipulated easly * @param {string} name @@ -124,4 +136,5 @@ module.exports = { parseContracts, parseContractStartDate, parseServiceId, + checkContractExists, } -- GitLab