diff --git a/__tests__/findUserPdl.spec.js b/__tests__/findUserPdl.spec.js deleted file mode 100644 index 6907f93c23af4c76c6ea3ec9c0b04860e24ceb67..0000000000000000000000000000000000000000 --- a/__tests__/findUserPdl.spec.js +++ /dev/null @@ -1,19 +0,0 @@ -const { errors } = require('cozy-konnector-libs') -const { findUserPdl } = require('../src') - -describe('recherchePoint', () => { - // mock soap request with result - // mock soap request to throw error - it('should throw LOGIN_FAILED for too many responses', () => { - expect( - findUserPdl() - // - // .env - ).toBe(errors.LOGIN_FAILED) - }) - it('should throw LOGIN_FAILED for empty response', () => {}) - - it('should return a correct pdl number', () => { - // Test with a given address ? - }) -}) diff --git a/__tests__/findUserPdl.spec.ts b/__tests__/findUserPdl.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..2c521a09dda96b68fca98c7934f2fdc137d6259b --- /dev/null +++ b/__tests__/findUserPdl.spec.ts @@ -0,0 +1,48 @@ +const { errors } = require('cozy-konnector-libs') +const { findUserPdl } = require('../src/findUserPdl') + +var mockParsePdl: jest.Mock + +jest.mock('../src/parsing', () => ({ + parseUserPdl: mockParsePdl = jest.fn(), +})) + +describe('recherchePoint', () => { + it('should throw LOGIN_FAILED for too many responses', async () => { + mockParsePdl.mockImplementationOnce(() => { + throw new Error('fail') + }) + + try { + const result = await findUserPdl( + 'azertyuiop', + 'apiKey', + 'login@user.com', + 'John', + '1 street', + '69069', + '69069' + ) + console.log('result: ' + result) + expect(true).toBe(false) + } catch (error) { + console.log('error in test file ' + error) + expect(error).toBe(errors.LOGIN_FAILED) + } + }) + + it('should return a correct pdl number', async () => { + mockParsePdl.mockResolvedValue('12345') + expect( + await findUserPdl( + 'azertyuiop', + 'apiKey', + 'login@user.com', + 'John', + '1 street', + '69069', + '69069' + ) + ).toBe('12345') + }) +}) diff --git a/__tests__/verifyUserIdentity.spec.js b/__tests__/verifyUserIdentity.spec.js index d691e3f88a02aa44aa3ebbb3dfce49da62c76416..6e838b8b8bb6c85e9d8826fd3c799c9cbe3f3ae7 100644 --- a/__tests__/verifyUserIdentity.spec.js +++ b/__tests__/verifyUserIdentity.spec.js @@ -5,7 +5,6 @@ jest.mock('../src/requests/insee', () => ({ getInseeCode: jest.fn().mockResolvedValue(69), })) -// This mock doenst work somehow jest.mock('../src/findUserPdl', () => ({ findUserPdl: jest.fn().mockResolvedValue('12345'), })) diff --git a/src/findUserPdl.js b/src/findUserPdl.js index d1bfb11e565d804efa02a3a032f059486663da09..69154781afed80dd69a6dffbcd92f63b43713ac3 100644 --- a/src/findUserPdl.js +++ b/src/findUserPdl.js @@ -6,6 +6,13 @@ const { rechercherPoint } = require('./requests/sge') const xml2js = require('xml2js') /** + * @param {string} url + * @param {string} apiAuthKey + * @param {string} appLogin + * @param {string} name + * @param {string} address + * @param {string} postalCode + * @param {string} inseeCode * @return {Promise<string | null>} User Pdl */ async function findUserPdl( @@ -22,24 +29,27 @@ async function findUserPdl( '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 { 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, - }) + // const parsedReply = await xml2js.parseStringPromise(response.body, { + // tagNameProcessors: [parseTags], + // valueProcessors: [parseValue], + // explicitArray: false, + // }) try { - return parseUserPdl(parsedReply) + console.log('parsedReply') + const pdl = parseUserPdl('parsedReply') + console.log('pdl: ' + pdl) + return pdl } catch (error) { throw errors.LOGIN_FAILED }