Skip to content
Snippets Groups Projects
Commit 88efae04 authored by Hugo NOUTS's avatar Hugo NOUTS
Browse files

init jest xml testing

scope out axios easy soap calls to unit test them
mock expected xml data
use mock data for parsing operations
parent 4741bf10
No related branches found
No related tags found
No related merge requests found
/**
* Returns a mock XML response for `GetContractData` SOAP action
* @param code Mock code
* @param description Mock description
*/
export const getContractDataMockResponse = (
pointId,
numeroEtNomVoie,
batiment,
) => `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<ns4:acquittement xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://www.erdf.fr/tube/exposition/finalisation" xmlns:ns4="http://www.enedis.fr/sge/b2b/technique/v1.0">
<resultat code="SGT200">Succès</resultat>
<infoFonctionnelles>
<pointId>${pointId}</pointId>
</infoFonctionnelles>
</ns4:acquittement>
</soapenv:Header>
<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<ns7:consulterDonneesTechniquesContractuellesResponse xmlns:ns0="http://www.erdf.fr/tube/exposition/finalisation" xmlns:ns7="http://www.enedis.fr/sge/b2b/services/consulterdonneestechniquescontractuelles/v1.0">
<point id="${pointId}">
<donneesGenerales>
<etatContractuel code="SERVC">
<libelle>En service</libelle>
</etatContractuel>
<adresseInstallation>
<escalierEtEtageEtAppartement>O2 205 </escalierEtEtageEtAppartement>
<batiment>${batiment}</batiment>
<numeroEtNomVoie>${numeroEtNomVoie}</numeroEtNomVoie>
<codePostal>69007</codePostal>
<commune code="69387">
<libelle>LYON 07</libelle>
</commune>
</adresseInstallation>
<dateDerniereModificationFormuleTarifaireAcheminement>2020-03-01+01:00</dateDerniereModificationFormuleTarifaireAcheminement>
<segment code="C5">
<libelle>C5</libelle>
</segment>
<niveauOuvertureServices>2</niveauOuvertureServices>
</donneesGenerales>
<situationAlimentation>
<alimentationPrincipale>
<domaineTension code="BTINF">
<libelle>BT&lt;=36kVA</libelle>
</domaineTension>
<puissanceRaccordementSoutirage>
<valeur>12</valeur>
<unite>kVA</unite>
</puissanceRaccordementSoutirage>
</alimentationPrincipale>
</situationAlimentation>
<situationComptage>
<dispositifComptage>
<typeComptage code="LINKY">
<libelle>Compteur Linky</libelle>
</typeComptage>
<compteurs>
<compteur>
<localisation code="LOCAL">
<libelle>Local</libelle>
</localisation>
<matricule>378</matricule>
<ticActivee>true</ticActivee>
<ticStandard>false</ticStandard>
<ticActivable>true</ticActivable>
</compteur>
</compteurs>
<disjoncteur>
<calibre code="15-45">
<libelle>15-45 A</libelle>
</calibre>
</disjoncteur>
</dispositifComptage>
<caracteristiquesReleve>
<periodicite code="MENSU">
<libelle>Mensuelle</libelle>
</periodicite>
</caracteristiquesReleve>
<futuresPlagesHeuresCreuses code="115">
<libelle>HC (23H54-7H54)</libelle>
</futuresPlagesHeuresCreuses>
</situationComptage>
<situationContractuelle>
<structureTarifaire>
<formuleTarifaireAcheminement code="BTINFCUST">
<libelle>Tarif BT&lt;=36kVA Courte Utilisation sans différenciation temporelle</libelle>
</formuleTarifaireAcheminement>
<puissanceSouscriteMax>
<valeur>6</valeur>
<unite>kVA</unite>
</puissanceSouscriteMax>
<calendrierFrn code="FC000063">
<libelle>Base</libelle>
</calendrierFrn>
</structureTarifaire>
</situationContractuelle>
</point>
</ns7:consulterDonneesTechniquesContractuellesResponse>
</soap:Body>
</soapenv:Envelope>`
......@@ -29,6 +29,7 @@
"dev": "cozy-konnector-dev",
"standalone": "cozy-konnector-standalone",
"pretest": "npm run clean",
"test": "jest",
"clean": "rm -rf ./data",
"build": "webpack",
"lint": "eslint --fix .",
......@@ -49,6 +50,8 @@
"cozy-konnector-build": "1.3.1",
"eslint-config-cozy-app": "1.3.3",
"eslint-plugin-prettier": "^4.0.0",
"git-directory-deploy": "1.5.1"
"git-directory-deploy": "1.5.1",
"jest": "^28.0.3",
"jest-xml-matcher": "^1.2.0"
}
}
......@@ -22,8 +22,10 @@ const {
userMesureDetailles,
userMaxPower,
} = require('./request')
const { getContractData } = require('./soapAxios')
moment.locale('fr') // set the language
moment.tz.setDefault('Europe/Paris') // set the timezone
const util = require('util')
/*** Connector Constants ***/
const manualExecution =
......@@ -94,30 +96,22 @@ async function start(fields, cozyParameters) {
*/
async function getDataStartDate(url, apiAuthKey, userLogin, pointId) {
log('info', 'Fetching data start date')
const sampleHeaders = {
'Content-Type': 'text/xml;charset=UTF-8',
apikey: apiAuthKey,
}
const { response } = await soapRequest({
url: url,
headers: sampleHeaders,
xml: userTechnicalData(pointId, userLogin),
}).catch(err => {
log('error', 'technicalDataResponse')
log('error', err)
return err
const { response } = await getContractData(url, apiAuthKey, userLogin, pointId)
const parsingData = await new Promise((resolve, reject) => {
xml2js.parseString(
response.body,
{
tagNameProcessors: [parseTags],
valueProcessors: [parseValue],
explicitArray: false,
},
(err, result) => {
if (err) reject(err)
else resolve(result)
}
)
})
xml2js.parseString(
response.body,
{
tagNameProcessors: [parseTags],
valueProcessors: [parseValue],
explicitArray: false,
},
processStartDate()
)
processStartDate(parsingData)
}
/**
......@@ -299,16 +293,10 @@ function processData(doctype = 'com.grandlyon.enedis.day') {
/**
* Store an accurate start date based on contrat start
*/
function processStartDate() {
return async (err, result) => {
if (err) {
log('error', err)
throw err
}
// update start Date with contract openning date
startDailyDate = moment(parseSgeXmlTechnicalData(result), 'YYYY-MM-DD')
startDailyDateString = startDailyDate.format('YYYY-MM-DD')
}
function processStartDate(result) {
// update start Date with contract openning date
startDailyDate = moment(parseSgeXmlTechnicalData(result), 'YYYY-MM-DD')
startDailyDateString = startDailyDate.format('YYYY-MM-DD')
}
/**
......
const parseValue = require('./parsing')
\ No newline at end of file
const easySoapRequest = require('easy-soap-request')
const { userTechnicalData } = require('./request')
async function soapAxios(url, headers, request) {
return easySoapRequest({
url: url,
headers: headers,
xml: request,
}).catch(err => {
log('error', url)
log('error', err)
return err
})
}
async function getContractData(url, apiAuthKey, userLogin, pointId) {
const sampleHeaders = {
'Content-Type': 'text/xml;charset=UTF-8',
apikey: apiAuthKey,
}
return soapAxios(
url,
sampleHeaders,
userTechnicalData(pointId, userLogin)
).catch(err => {
return err
})
}
async function getDetailedData(url, apiAuthKey, template) {
const sampleHeaders = {
'Content-Type': 'text/xml;charset=UTF-8',
apikey: apiAuthKey,
}
return soapAxios(
url,
sampleHeaders,
template
).catch(err => {
return err
})
}
module.exports = {
getContractData,
getDetailedData
}
const { getContractDataMockResponse } = require('../mocks/responseTechnicalData')
const getContractData = require('./soapAxios')
describe('getContractData', () => {
let baseUrl = fields.wso2BaseUrl
let apiAuthKey = fields.apiToken
let loginUtilisateur = fields.loginUtilisateur
let pointId = fields.pointId
it('should pass', async () => {
const data = getContractData(
`${baseUrl}/enedis_SGE_ConsultationDonneesTechniquesContractuelles/1.0`,
apiAuthKey,
loginUtilisateur,
fields.pointId
)
const expectedData = getContractDataMockResponse(pointId, "5 RUE CROIX BARRET", "SNC SO CITY - BATIMENT A");
expect(data).toEqualXML(expectedData)
})
it('should return an error', async () => {})
})
This diff is collapsed.
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