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

test verifyUserIdentity

parent 0840e453
No related branches found
No related tags found
2 merge requests!12Feat/donnes tech debut contrat,!10Feat/rechercher point
Pipeline #39769 failed
const { errors } = require('cozy-konnector-libs')
const { verifyUserIdentity } = require('../src/index')
const { verifyUserIdentity } = require('../src/verifyUserIdentity')
jest.mock('../src/requests/insee', () => ({
getInseeCode: jest.fn().mockResolvedValueOnce(69),
getInseeCode: jest.fn().mockResolvedValue(69),
}))
// This mock doenst work somehow
jest.mock('../src/findUserPdl', () => ({
findUserPdl: jest.fn().mockResolvedValue('12345'),
}))
describe('verifyUserIdentity', () => {
// mock insee code
// mock find User Pdl
// const mockGetInseeCode = jest.fn()
// const mockFindUserPdl = jest.fn()
it('should throw LOGIN_FAILED when pdl give and recieved are not matching', async () => {
const result = await verifyUserIdentity(
{
name: 'John',
address: '1 street',
pointId: 987654321,
postalCode: '69069',
},
'baseUrl',
'apiKey',
'login@user.com'
)
// jest
// .mock(verifyUserIdentity(), 'getInseeCode')
// .mockResolvedValueOnce(696969)
// jest.spyOn(verifyUserIdentity, 'findUserPdl').mockResolvedValueOnce(696969)
jest.mock('../src/index', () => ({
start: jest.fn(),
}))
describe('verifyUserIdentity', () => {
it('should throw LOGIN_FAILED when pdl give and recieved are NOT matching 🚫', async () => {
try {
await verifyUserIdentity(
{
name: 'John',
address: '1 street',
pointId: 987654321,
postalCode: '69069',
},
'azertyuiop',
'apiKey',
'login@user.com'
)
expect(true).toBe(false)
} catch (error) {
expect(error).toBe(errors.LOGIN_FAILED)
}
})
expect(result).toBe(errors.LOGIN_FAILED)
it('should return true when pdl give and recieved are matching ✅', async () => {
try {
const response = await verifyUserIdentity(
{
name: 'John',
address: '1 street',
pointId: '12345',
postalCode: '69069',
},
'azertyuiop',
'apiKey',
'login@user.com'
)
expect(response).toBe(true)
} catch (error) {
expect(true).toBe(false)
}
})
})
// @ts-check
const { log, errors } = require('cozy-konnector-libs')
const soapRequest = require('easy-soap-request')
const { parseUserPdl, parseTags, parseValue } = require('./parsing')
const { rechercherPoint } = require('./requests/sge')
const xml2js = require('xml2js')
/**
* @return {Promise<string>} User Pdl
*/
async function findUserPdl(
url,
apiAuthKey,
appLogin,
name,
address,
postalCode,
inseeCode
) {
log('info', 'Fetching user data')
const sgeHeaders = {
'Content-Type': 'text/xml;charset=UTF-8',
apikey: apiAuthKey,
}
const { response } = await soapRequest({
url: url,
headers: sgeHeaders,
xml: rechercherPoint(appLogin, name, postalCode, inseeCode, address),
}).catch(err => {
log('error', 'rechercherPointResponse')
log('error', err)
throw errors.LOGIN_FAILED
})
const parsedReply = await xml2js.parseStringPromise(response.body, {
tagNameProcessors: [parseTags],
valueProcessors: [parseValue],
explicitArray: false,
})
//TODO: handle errors
return parseUserPdl(parsedReply)
}
module.exports = { findUserPdl }
......@@ -17,13 +17,12 @@ const {
formateDataForDoctype,
parseTags,
parseValue,
parseUserPdl,
} = require('./parsing')
const {
consulterDonneesTechniquesContractuelles,
consultationMesuresDetailleesMaxPower,
consultationMesuresDetaillees,
rechercherPoint,
commanderCollectePublicationMesures,
commanderArretServiceSouscritMesures,
} = require('./requests/sge')
......@@ -33,7 +32,8 @@ const {
getBoConsent,
deleteBoConsent,
} = require('./requests/bo')
const { getInseeCode } = require('./requests/insee')
const verifyUserIdentity = require('./verifyUserIdentity')
moment.locale('fr') // set the language
moment.tz.setDefault('Europe/Paris') // set the timezone
......@@ -110,38 +110,6 @@ async function start(fields, cozyParameters) {
await gatherData(baseUrl, apiAuthKey, loginUtilisateur, fields.pointId)
}
/**
* Verify user identity
* @param {object} fields
* @param {string} baseUrl
* @param {string} apiAuthKey
* @param {string} loginUtilisateur
*/
async function verifyUserIdentity(
fields,
baseUrl,
apiAuthKey,
loginUtilisateur
) {
const inseeCode = await getInseeCode(fields.postalCode)
const pdl = await findUserPdl(
`${baseUrl}/enedis_SDE_recherche-point/1.0`,
apiAuthKey,
loginUtilisateur,
fields.name,
fields.address,
fields.postalCode,
inseeCode
)
if (fields.pointId != pdl) {
log('error', 'PointId does not match')
throw errors.LOGIN_FAILED
}
return true
}
/**
* Main method for gathering data
* @param {string} baseUrl
......@@ -476,45 +444,3 @@ function isFirstStart() {
//TODO: Implement
return true
}
/**
* @return {Promise<string>} User Pdl
*/
async function findUserPdl(
url,
apiAuthKey,
appLogin,
name,
address,
postalCode,
inseeCode
) {
log('info', 'Fetching user data')
const sgeHeaders = {
'Content-Type': 'text/xml;charset=UTF-8',
apikey: apiAuthKey,
}
const { response } = await soapRequest({
url: url,
headers: sgeHeaders,
xml: rechercherPoint(appLogin, name, postalCode, inseeCode, address),
}).catch(err => {
log('error', 'rechercherPointResponse')
log('error', err)
throw errors.LOGIN_FAILED
})
const parsedReply = await xml2js.parseStringPromise(response.body, {
tagNameProcessors: [parseTags],
valueProcessors: [parseValue],
explicitArray: false,
})
//TODO: handle errors
return parseUserPdl(parsedReply)
}
module.exports = {
verifyUserIdentity,
findUserPdl,
}
// @ts-check
const { log, errors } = require('cozy-konnector-libs')
const { findUserPdl } = require('./findUserPdl')
const { getInseeCode } = require('./requests/insee')
/**
* Verify user identity
* @param {object} fields
* @param {string} baseUrl
* @param {string} apiAuthKey
* @param {string} loginUtilisateur
*/
async function verifyUserIdentity(
fields,
baseUrl,
apiAuthKey,
loginUtilisateur
) {
const inseeCode = await getInseeCode(fields.postalCode)
const pdl = await findUserPdl(
`${baseUrl}/enedis_SDE_recherche-point/1.0`,
apiAuthKey,
loginUtilisateur,
fields.name,
fields.address,
fields.postalCode,
inseeCode
)
console.log(pdl)
console.log(fields.pointId)
if (fields.pointId != pdl) {
log('error', 'PointId does not match')
throw errors.LOGIN_FAILED
}
return true
}
module.exports = { verifyUserIdentity }
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