From aa58a3b32473fe8cf9c76bf799a14a1645fc247d Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Tue, 4 Oct 2022 18:01:14 +0200
Subject: [PATCH] fix: TU

---
 __tests__/core/findUserPdl.spec.js        | 57 ++++++++----------
 __tests__/core/verifyUserIdentity.spec.js | 72 +++++++++++++++++++++--
 __tests__/helpers/parsing.spec.js         | 40 +++++++++++++
 3 files changed, 134 insertions(+), 35 deletions(-)

diff --git a/__tests__/core/findUserPdl.spec.js b/__tests__/core/findUserPdl.spec.js
index 447a3fe..d79e167 100644
--- a/__tests__/core/findUserPdl.spec.js
+++ b/__tests__/core/findUserPdl.spec.js
@@ -12,37 +12,10 @@ const responseMock = {
 }
 
 describe('recherchePoint', () => {
-  afterEach(() => {
+  beforeEach(() => {
     jest.clearAllMocks()
   })
-
-  it('should throw LOGIN_FAIL if soapRequest fails', async () => {
-    const mockParseUserPdl = jest.fn()
-    jest.mock('../../src/helpers/parsing', () => ({
-      parseUserPdl: () => mockParseUserPdl(),
-    }))
-    jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValueOnce({
-      Envelope: {
-        Body: {
-          Fault: { detail: { erreur: { resultat: { $: { code: 401 } } } } },
-          faultstring: 'Mock error',
-        },
-      },
-    })
-    mockSoapRequest.mockRejectedValueOnce('reject')
-    try {
-      await findUserPdl()
-      expect(true).toBe(false)
-    } catch (error) {
-      expect(error).toBe(errors.LOGIN_FAILED)
-    }
-  })
-
-  it('should return a correct pdl number', async () => {
-    const mockParseUserPdl = jest.fn()
-    jest.mock('../../src/helpers/parsing', () => ({
-      parseUserPdl: () => mockParseUserPdl(),
-    }))
+  it('should return a correct pdl number ✅', async () => {
     jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValueOnce({
       Envelope: {
         Body: {
@@ -57,7 +30,7 @@ describe('recherchePoint', () => {
     expect(await findUserPdl()).toBe('191919119')
   })
 
-  it('should handle issue on parsing', async () => {
+  it('should handle issue on parsing ✅', async () => {
     const cozyKonnectorsLib = require('cozy-konnector-libs')
     const spyer = jest.spyOn(cozyKonnectorsLib, 'log')
     jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValueOnce({
@@ -81,6 +54,28 @@ describe('recherchePoint', () => {
 
     await findUserPdl()
     // Only verifing that we are going through logs
-    expect(spyer).toBeCalledTimes(3)
+    expect(spyer).toBeCalledTimes(5)
+  })
+
+  it('should throw LOGIN_FAIL if soapRequest fails 🚫', async () => {
+    const mockParseUserPdl = jest.fn()
+    jest.mock('../../src/helpers/parsing', () => ({
+      parseUserPdl: () => mockParseUserPdl(),
+    }))
+    jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValueOnce({
+      Envelope: {
+        Body: {
+          Fault: { detail: { erreur: { resultat: { $: { code: 401 } } } } },
+          faultstring: 'Mock error',
+        },
+      },
+    })
+    mockSoapRequest.mockRejectedValueOnce('reject')
+    try {
+      await findUserPdl()
+      expect(true).toBe(false)
+    } catch (error) {
+      expect(error).toBe(errors.LOGIN_FAILED)
+    }
   })
 })
diff --git a/__tests__/core/verifyUserIdentity.spec.js b/__tests__/core/verifyUserIdentity.spec.js
index 4c3dd13..390396d 100644
--- a/__tests__/core/verifyUserIdentity.spec.js
+++ b/__tests__/core/verifyUserIdentity.spec.js
@@ -1,20 +1,22 @@
 const { errors } = require('cozy-konnector-libs')
 const { verifyUserIdentity } = require('../../src/core/verifyUserIdentity')
+const { findUserPdl } = require('../../src/core/findUserPdl')
+const { findUserAddress } = require('../../src/core/findUserAddress')
+
+jest.mock('../../src/core/findUserPdl')
+jest.mock('../../src/core/findUserAddress')
 
 jest.mock('../../src/requests/insee', () => ({
   getInseeCode: jest.fn().mockResolvedValue(69),
 }))
 
-jest.mock('../../src/core/findUserPdl', () => ({
-  findUserPdl: jest.fn().mockResolvedValue('12345'),
-}))
-
 jest.mock('../../src/index', () => ({
   start: jest.fn(),
 }))
 
 describe('verifyUserIdentity', () => {
   it('should throw LOGIN_FAILED when pdl given and recieved are NOT matching 🚫', async () => {
+    findUserPdl.mockResolvedValueOnce('12345')
     try {
       await verifyUserIdentity(
         {
@@ -32,7 +34,9 @@ describe('verifyUserIdentity', () => {
       expect(error).toBe(errors.LOGIN_FAILED)
     }
   })
+
   it('should throw TERMS_VERSION_MISMATCH when pdl give and recieved are NOT matching on alternate start 🚫', async () => {
+    findUserPdl.mockResolvedValueOnce('12345')
     try {
       await verifyUserIdentity(
         {
@@ -53,6 +57,7 @@ describe('verifyUserIdentity', () => {
   })
 
   it('should return void when pdl give and recieved are matching ✅', async () => {
+    findUserPdl.mockResolvedValueOnce('12345')
     expect.assertions(1)
     try {
       await verifyUserIdentity(
@@ -72,6 +77,7 @@ describe('verifyUserIdentity', () => {
     }
   })
   it('should return void when pdl give and recieved are matching with stored inseecode ✅', async () => {
+    findUserPdl.mockResolvedValue('12345')
     expect.assertions(1)
     try {
       await verifyUserIdentity(
@@ -92,4 +98,62 @@ describe('verifyUserIdentity', () => {
       expect(true).toBe(false)
     }
   })
+
+  it('should return void when pdl give and recieved are matching with SGE second chance onboarding ✅ ', async () => {
+    findUserPdl.mockResolvedValueOnce(null).mockResolvedValueOnce('12345')
+    findUserAddress.mockResolvedValueOnce({
+      escalierEtEtageEtAppartement: '12',
+      codePostal: '69003',
+      numeroEtNomVoie: '20 rue du lac',
+      commune: { $: { code: '69383' } },
+    })
+
+    try {
+      await verifyUserIdentity(
+        {
+          lastname: 'John',
+          firstname: 'DOE',
+          address: '1 street',
+          pointId: '12345',
+          postalCode: '69069',
+        },
+        'azertyuiop',
+        'apiKey',
+        'login@user.com'
+      )
+      expect(true).toBeTruthy()
+    } catch (error) {
+      expect(true).toBe(false)
+    }
+  })
+
+  it('should return void when pdl give and recieved are matching with SGE last chance onboarding✅ ', async () => {
+    findUserPdl
+      .mockResolvedValueOnce(null)
+      .mockResolvedValueOnce(null)
+      .mockResolvedValueOnce('12345')
+    findUserAddress.mockResolvedValueOnce({
+      escalierEtEtageEtAppartement: '12',
+      codePostal: '69003',
+      numeroEtNomVoie: '20 rue du lac',
+      commune: { $: { code: '69383' } },
+    })
+
+    try {
+      await verifyUserIdentity(
+        {
+          name: 'John',
+          address: '1 street',
+          pointId: '12345',
+          postalCode: '69069',
+        },
+        'azertyuiop',
+        'apiKey',
+        'login@user.com'
+      )
+      expect(true).toBeTruthy()
+    } catch (error) {
+      expect(true).toBe(false)
+    }
+  })
 })
diff --git a/__tests__/helpers/parsing.spec.js b/__tests__/helpers/parsing.spec.js
index 37fefc0..742790a 100644
--- a/__tests__/helpers/parsing.spec.js
+++ b/__tests__/helpers/parsing.spec.js
@@ -4,6 +4,8 @@ const {
   parseContracts,
   parseServiceId,
   parseSgeXmlData,
+  checkContractExists,
+  parseUserAddress,
   formateDataForDoctype,
   parseTags,
   parseValue,
@@ -132,6 +134,44 @@ describe('parsing', () => {
     ])
   })
 
+  it('should format existing contract', async () => {
+    const result = {
+      Envelope: {
+        Body: {
+          rechercherServicesSouscritsMesuresResponse: {
+            servicesSouscritsMesures: {
+              v: 14361,
+              d: '2021-08-01T00:00:00.000+02:00',
+            },
+          },
+        },
+      },
+    }
+    const reply = checkContractExists(result)
+    expect(reply).toEqual({
+      v: 14361,
+      d: '2021-08-01T00:00:00.000+02:00',
+    })
+  })
+
+  it('should format user address', async () => {
+    const result = {
+      Envelope: {
+        Body: {
+          consulterDonneesTechniquesContractuellesResponse: {
+            point: {
+              donneesGenerales: { adresseInstallation: { numero: '12' } },
+            },
+          },
+        },
+      },
+    }
+    const reply = parseUserAddress(result)
+    expect(reply).toEqual({
+      numero: '12',
+    })
+  })
+
   it('should parseTag with :', () => {
     const reply = parseTags('test:tag')
     expect(reply).toBe('tag')
-- 
GitLab