From 479e84a32700b4caa1a96fe41cb75e3d8e372cb3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Pailharey?= <rpailharey@grandlyon.com>
Date: Thu, 2 Nov 2023 13:54:19 +0100
Subject: [PATCH] fix: tests

---
 __tests__/requests/bo.spec.js | 31 ++++++++++++++++++++-----------
 src/requests/bo.js            |  6 +++---
 2 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/__tests__/requests/bo.spec.js b/__tests__/requests/bo.spec.js
index ea0dca5..5300d07 100644
--- a/__tests__/requests/bo.spec.js
+++ b/__tests__/requests/bo.spec.js
@@ -45,7 +45,7 @@ describe('Backoffice routes', () => {
       })
     })
     it('should handle unavailable BO', async () => {
-      axios.post.mockImplementationOnce(() => Promise.reject('fail'))
+      axios.post.mockRejectedValueOnce(new Error('request failed'))
       try {
         await createBoConsent(
           'http://test.com',
@@ -105,7 +105,7 @@ describe('Backoffice routes', () => {
       })
     })
     it('should handle unavailable BO', async () => {
-      axios.put.mockImplementationOnce(() => Promise.reject('fail'))
+      axios.put.mockRejectedValueOnce(new Error('request failed'))
       try {
         await updateBoConsent(
           'http://test.com',
@@ -157,7 +157,7 @@ describe('Backoffice routes', () => {
       })
     })
     it('should handle unavailable BO', async () => {
-      axios.put.mockImplementationOnce(() => Promise.reject('fail'))
+      axios.put.mockRejectedValueOnce(new Error('request failed'))
       try {
         await deleteBoConsent('http://test.com', 'token', 1)
         expect(true).toBe(false)
@@ -192,19 +192,28 @@ describe('Backoffice routes', () => {
     })
 
     it('should handle unavailable BO', async () => {
-      axios.get.mockImplementationOnce(() => Promise.reject(errors.MAINTENANCE))
+      axios.get.mockRejectedValueOnce(new Error('request failed'))
       try {
-        await getBoConsent({
-          pointId: 11111111111111,
-          name: 'POUET',
-          adresse: '20 rue du lac',
-          postalCode: '69003',
-          inseeCode: '69383',
-        })
+        await getBoConsent('http://test.com', 'token', '1')
         expect(true).toBe(false)
       } catch (error) {
         expect(error.message).toBe(errors.MAINTENANCE)
       }
     })
+
+    it('should handle not found consent', async () => {
+      axios.get.mockRejectedValueOnce({
+        response: {
+          status: 404,
+          data: { code: 404, description: 'Not Found' },
+        },
+      })
+      try {
+        await getBoConsent('http://test.com', 'token', 'n0tF0unD')
+        expect(true).toBe(false)
+      } catch (error) {
+        expect(error.message).toBe(errors.LOGIN_FAILED)
+      }
+    })
   })
 })
diff --git a/src/requests/bo.js b/src/requests/bo.js
index e1fdc9b..9be6341 100644
--- a/src/requests/bo.js
+++ b/src/requests/bo.js
@@ -134,10 +134,10 @@ async function getBoConsent(url, token, consentId) {
         consentId: consentId,
       },
     })
-    if (err.response.status === 404) {
-      throw errors.LOGIN_FAILED
+    if (err.response?.status === 404) {
+      throw new Error(errors.LOGIN_FAILED)
     }
-    throw errors.MAINTENANCE
+    throw new Error(errors.MAINTENANCE)
   }
 }
 
-- 
GitLab