From 18e3d9591854be50a5d83f2c1afdf5d0340ce690 Mon Sep 17 00:00:00 2001
From: hsubtil <ext.sopra.husubtil@grandlyon.com>
Date: Wed, 3 Aug 2022 09:42:42 +0000
Subject: [PATCH] feat: update TU

---
 __tests__/requests/bo.spec.js | 79 +++++++++++++++++++++++++++++++----
 src/types.js                  | 22 +++++++---
 2 files changed, 89 insertions(+), 12 deletions(-)

diff --git a/__tests__/requests/bo.spec.js b/__tests__/requests/bo.spec.js
index 92fb02e..7e21029 100644
--- a/__tests__/requests/bo.spec.js
+++ b/__tests__/requests/bo.spec.js
@@ -1,9 +1,12 @@
-const { createBoConsent } = require('../../src/requests/bo')
-import * as axios from 'axios'
+const { createBoConsent, getBoConsent } = require('../../src/requests/bo')
+const { default: axios } = require('axios')
+const { errors } = require('cozy-konnector-libs')
+
 jest.mock('axios')
 describe('Backoffice routes', () => {
   describe('createBoConsent', () => {
     it('should send consent to BO', async () => {
+      axios.post.mockResolvedValueOnce({ id: 1 })
       const consent = await createBoConsent({
         pdl: 11111111111111,
         name: 'POUET',
@@ -11,18 +14,80 @@ describe('Backoffice routes', () => {
         postalCode: '69003',
         inseeCode: '69383',
       })
-      expect(consent).toBe({ id: 'abce23' })
+      expect(consent).toBe({ id: 1 })
     })
     it('should handle unavailable BO', async () => {
-      axios.post.mockImplementation(() => Promise.reject({ status: 500 }))
-      const consent = await createBoConsent({
-        pdl: 11111111111111,
+      axios.post.mockImplementationOnce(() =>
+        Promise.reject(errors.MAINTENANCE)
+      )
+      try {
+        await createBoConsent({
+          pdl: 11111111111111,
+          name: 'POUET',
+          adresse: '20 rue du lac',
+          postalCode: '69003',
+          inseeCode: '69383',
+        })
+        expect(true).toBe(false)
+      } catch (e) {
+        expect(e).toBe(errors.MAINTENANCE)
+      }
+    })
+  })
+  describe('getBoConsent', () => {
+    it('should get consent from BO', async () => {
+      axios.get.mockResolvedValueOnce({
+        id: 1,
+        pointId: 11111111111111,
+        name: 'POUET',
+        adresse: '20 rue du lac',
+        postalCode: '69003',
+        inseeCode: '69383',
+      })
+      const consent = await getBoConsent(1)
+      expect(consent).toBe({
+        pointId: 11111111111111,
+        name: 'POUET',
+        adresse: '20 rue du lac',
+        postalCode: '69003',
+        inseeCode: '69383',
+      })
+    })
+    it('should get consent from BO with service id', async () => {
+      axios.get.mockResolvedValueOnce({
+        id: 1,
+        pointId: 11111111111111,
         name: 'POUET',
         adresse: '20 rue du lac',
         postalCode: '69003',
         inseeCode: '69383',
+        serviceId: 'abcde',
       })
-      expect(consent).toBe(null)
+      const consent = await getBoConsent(1)
+      expect(consent.serviceId).toBeTruthy()
+      expect(consent).toBe({
+        pointId: 11111111111111,
+        name: 'POUET',
+        adresse: '20 rue du lac',
+        postalCode: '69003',
+        inseeCode: '69383',
+        serviceId: 'abcde',
+      })
+    })
+    it('should handle unavailable BO', async () => {
+      axios.get.mockImplementationOnce(() => Promise.reject(errors.MAINTENANCE))
+      try {
+        await createBoConsent({
+          pointId: 11111111111111,
+          name: 'POUET',
+          adresse: '20 rue du lac',
+          postalCode: '69003',
+          inseeCode: '69383',
+        })
+        expect(true).toBe(false)
+      } catch (e) {
+        expect(e).toBe(errors.MAINTENANCE)
+      }
     })
   })
 })
diff --git a/src/types.js b/src/types.js
index b2d1f40..b5cff38 100644
--- a/src/types.js
+++ b/src/types.js
@@ -15,12 +15,24 @@
  * @property {string} d
  */
 
+// /**
+//  * User definition
+//  * @typedef {object} User
+//  * @property {string} name
+//  * @property {string} address
+//  * @property {string} postalCode
+//  * @property {string} pointId
+//  * @property {string} [inseeCode]
+//  */
+
 /**
- * User definition
- * @typedef {object} User
+ * Consent definition
+ * @typedef {object} Consent
+ * @property {number} pointId
  * @property {string} name
- * @property {string} address
+ * @property {string} adresse
  * @property {string} postalCode
- * @property {string} pointId
- * @property {string} [inseeCode]
+ * @property {string} inseeCode
+ * @property {string} [serviceId]
+ * @property {number} [id]
  */
-- 
GitLab