Skip to content
Snippets Groups Projects
Commit 00e8b2a0 authored by Bastien DUMONT's avatar Bastien DUMONT :angel:
Browse files

fix: throw errors when no contracts are found

parent 8487f77b
No related branches found
No related tags found
1 merge request!13fix: no contract found throw errors
......@@ -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
......
......@@ -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()
})
})
// @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)) {
......
......@@ -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,
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment