From c97ec9fcbaf8a493828ecdb450d9600d0096e9ee Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Tue, 4 Oct 2022 13:28:44 +0200
Subject: [PATCH] feat: add TU

---
 __tests__/core/findUserPdl.spec.js | 75 +++++++++++++++++++++---------
 importedData.json                  | 26 ++++++++++-
 src/core/findUserPdl.js            |  1 -
 src/core/verifyUserIdentity.js     |  4 +-
 4 files changed, 79 insertions(+), 27 deletions(-)

diff --git a/__tests__/core/findUserPdl.spec.js b/__tests__/core/findUserPdl.spec.js
index 803b82f..447a3fe 100644
--- a/__tests__/core/findUserPdl.spec.js
+++ b/__tests__/core/findUserPdl.spec.js
@@ -5,20 +5,6 @@ const { findUserPdl } = require('../../src/core/findUserPdl')
 const mockSoapRequest = jest.fn()
 jest.mock('easy-soap-request', () => async () => mockSoapRequest())
 
-jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValue({
-  Envelope: {
-    Body: {
-      Fault: { detail: { erreur: { resultat: { $: { code: 401 } } } } },
-      faultstring: 'Mock error',
-    },
-  },
-})
-
-const mockParseUserPdl = jest.fn()
-jest.mock('../../src/helpers/parsing', () => ({
-  parseUserPdl: () => mockParseUserPdl(),
-}))
-
 const responseMock = {
   response: {
     body: 'mockedBody',
@@ -26,7 +12,23 @@ const responseMock = {
 }
 
 describe('recherchePoint', () => {
+  afterEach(() => {
+    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()
@@ -37,21 +39,48 @@ describe('recherchePoint', () => {
   })
 
   it('should return a correct pdl number', async () => {
+    const mockParseUserPdl = jest.fn()
+    jest.mock('../../src/helpers/parsing', () => ({
+      parseUserPdl: () => mockParseUserPdl(),
+    }))
+    jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValueOnce({
+      Envelope: {
+        Body: {
+          rechercherPointResponse: {
+            points: { point: { $: { id: '191919119' } } },
+          },
+        },
+      },
+    })
     mockSoapRequest.mockResolvedValueOnce(responseMock)
-    mockParseUserPdl.mockResolvedValueOnce('12345')
 
-    expect(await findUserPdl()).toBe('12345')
+    expect(await findUserPdl()).toBe('191919119')
   })
 
   it('should handle issue on parsing', async () => {
+    const cozyKonnectorsLib = require('cozy-konnector-libs')
+    const spyer = jest.spyOn(cozyKonnectorsLib, 'log')
+    jest.spyOn(xml2js, 'parseStringPromise').mockResolvedValueOnce({
+      Envelope: {
+        Body: {
+          Fault: {
+            detail: {
+              erreur: {
+                resultat: {
+                  $: {
+                    code: 401,
+                  },
+                },
+              },
+            },
+          },
+        },
+      },
+    })
     mockSoapRequest.mockResolvedValueOnce(responseMock)
-    mockParseUserPdl.mockRejectedValue(new Error('Async error'))
 
-    try {
-      await findUserPdl()
-      expect(true).toBe(false)
-    } catch (error) {
-      expect(error).toBe(errors.LOGIN_FAILED)
-    }
+    await findUserPdl()
+    // Only verifing that we are going through logs
+    expect(spyer).toBeCalledTimes(3)
   })
 })
diff --git a/importedData.json b/importedData.json
index 5b3d3e9..cfcc1e5 100644
--- a/importedData.json
+++ b/importedData.json
@@ -2,5 +2,29 @@
   "io.cozy.files": [],
   "com.grandlyon.enedis.year": [],
   "com.grandlyon.enedis.month": [],
-  "com.grandlyon.enedis.minute": []
+  "com.grandlyon.enedis.minute": [],
+  "io.cozy.accounts": [
+    {
+      "cozyMetadata": {
+        "doctypeVersion": 1,
+        "metadataVersion": 1,
+        "createdAt": "2022-10-04T11:27:21.432Z",
+        "createdByApp": "enedis-sge-grandlyon",
+        "createdByAppVersion": "1.0.1",
+        "updatedAt": "2022-10-04T11:27:21.432Z",
+        "updatedByApps": [
+          {
+            "slug": "enedis-sge-grandlyon",
+            "date": "2022-10-04T11:27:21.432Z",
+            "version": "1.0.1"
+          }
+        ]
+      },
+      "_id": "1111111",
+      "account_type": "1111111",
+      "data": {
+        "name": "pouet"
+      }
+    }
+  ]
 }
\ No newline at end of file
diff --git a/src/core/findUserPdl.js b/src/core/findUserPdl.js
index 1c06be2..318d955 100644
--- a/src/core/findUserPdl.js
+++ b/src/core/findUserPdl.js
@@ -58,7 +58,6 @@ async function findUserPdl(
     return parseUserPdl(parsedReply)
   } catch (error) {
     log('error', 'Error while parsing user PDL: ' + error)
-    console.log(error)
     if (parsedReply.Envelope.Body.Fault) {
       log(
         'error',
diff --git a/src/core/verifyUserIdentity.js b/src/core/verifyUserIdentity.js
index e093c4c..73d7d14 100644
--- a/src/core/verifyUserIdentity.js
+++ b/src/core/verifyUserIdentity.js
@@ -43,7 +43,7 @@ async function verifyUserIdentity(
   )
 
   if (!pdl) {
-    log('warn', 'Second change for sge onboarding')
+    log('warn', 'Second chance for sge onboarding')
     // Backup verification
     const userAddress = await findUserAddress(
       baseUrl,
@@ -69,7 +69,7 @@ async function verifyUserIdentity(
 
     // Third try, remove address number because it's buggy on SGE side
     if (!pdl) {
-      log('warn', 'Lastchance onboarding for sge')
+      log('warn', 'Last chance onboarding for sge')
       pdl = await findUserPdl(
         `${baseUrl}/enedis_SDE_recherche-point/1.0`,
         apiAuthKey,
-- 
GitLab