diff --git a/__tests__/requests/bo.spec.js b/__tests__/requests/bo.spec.js index 92fb02e6aa205e42edff8aae15ba434559da6595..7e2102956c86c644a604a5de28d3e6aafcbca11b 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 b2d1f4016528c80d44ce27bc9b6c461fd1c170c6..b5cff384d39d1698486456f6abbf49fce321ca55 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] */