diff --git a/__tests__/requests/bo.spec.js b/__tests__/requests/bo.spec.js index ea0dca564ea26d8e3b0f9e7bbad12400492cc8e2..fb95043b7b4efac7959c13c3d97fa87de57dcc78 100644 --- a/__tests__/requests/bo.spec.js +++ b/__tests__/requests/bo.spec.js @@ -11,18 +11,16 @@ jest.mock('axios') describe('Backoffice routes', () => { describe('createBoConsent', () => { it('should send consent to BO', async () => { - axios.post.mockImplementationOnce(() => { - return { - data: { - ID: 1, - firstname: 'mr', - lastname: 'POUET', - pointId: 11111111111111, - postalCode: '69003', - address: '20 rue du lac', - inseeCode: '69383', - }, - } + axios.post.mockResolvedValueOnce({ + data: { + ID: 1, + firstname: 'mr', + lastname: 'POUET', + pointId: 11111111111111, + postalCode: '69003', + address: '20 rue du lac', + inseeCode: '69383', + }, }) const consent = await createBoConsent( 'http://test.com', @@ -45,7 +43,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', @@ -65,19 +63,17 @@ describe('Backoffice routes', () => { }) describe('updateBoConsent', () => { it('should update consent to BO', async () => { - axios.put.mockImplementationOnce(() => { - return { - data: { - ID: 1, - firstname: 'mr', - lastname: 'POUET', - pointId: 11111111111111, - postalCode: '69003', - address: '20 rue du lac', - inseeCode: '69383', - serviceId: '123456', - }, - } + axios.put.mockResolvedValueOnce({ + data: { + ID: 1, + firstname: 'mr', + lastname: 'POUET', + pointId: 11111111111111, + postalCode: '69003', + address: '20 rue du lac', + inseeCode: '69383', + serviceId: '123456', + }, }) const consent = await updateBoConsent( 'http://test.com', @@ -105,7 +101,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', @@ -130,19 +126,17 @@ describe('Backoffice routes', () => { describe('deleteBoConsent', () => { it('should delete consent to BO', async () => { - axios.delete.mockImplementationOnce(() => { - return { - data: { - ID: 1, - firstname: 'mr', - lastname: 'POUET', - pointId: 11111111111111, - postalCode: '69003', - address: '20 rue du lac', - inseeCode: '69383', - serviceId: '123456', - }, - } + axios.delete.mockResolvedValueOnce({ + data: { + ID: 1, + firstname: 'mr', + lastname: 'POUET', + pointId: 11111111111111, + postalCode: '69003', + address: '20 rue du lac', + inseeCode: '69383', + serviceId: '123456', + }, }) const consent = await deleteBoConsent('http://test.com', 'token', 1) expect(consent).toEqual({ @@ -157,7 +151,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) @@ -168,17 +162,15 @@ describe('Backoffice routes', () => { }) describe('getBoConsent', () => { it('should get consent from BO', async () => { - axios.get.mockImplementationOnce(() => { - return { - data: { - ID: 1, - pointId: 11111111111111, - name: 'POUET', - adresse: '20 rue du lac', - postalCode: '69003', - inseeCode: '69383', - }, - } + axios.get.mockResolvedValueOnce({ + data: { + ID: 1, + pointId: 11111111111111, + name: 'POUET', + adresse: '20 rue du lac', + postalCode: '69003', + inseeCode: '69383', + }, }) const consent = await getBoConsent('http://test.com', 'token', 1) expect(consent).toEqual({ @@ -192,19 +184,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: { 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/core/types/types.js b/src/core/types/types.js index 5a344f9061d4f856717f0df7e037aabfbdb3d401..0de983a05eea5135929ad1e11c871aa4c4c33f9c 100644 --- a/src/core/types/types.js +++ b/src/core/types/types.js @@ -40,7 +40,7 @@ * @property {string} inseeCode * @property {string} endDate * @property {number} [serviceID] - * @property {number} [ID] + * @property {string} [ID] */ /** @@ -87,7 +87,7 @@ /** * AccountData definition * @typedef {object} AccountData - * @property {number} consentId + * @property {string} consentId * @property {string} inseeCode */ diff --git a/src/requests/bo.js b/src/requests/bo.js index 8addf82e97d319c58719333856aa9bd57a9586b7..9be6341e9e5902dc86609f55c8b343535569280b 100644 --- a/src/requests/bo.js +++ b/src/requests/bo.js @@ -82,7 +82,7 @@ async function updateBoConsent(url, token, consent, serviceId) { let consentId = '' if (consent.ID) { - consentId = consent.ID.toString() + consentId = consent.ID } try { const { data } = await axios.put( @@ -110,7 +110,7 @@ async function updateBoConsent(url, token, consent, serviceId) { } /** - * @param {number} consentId + * @param {string} consentId * @returns {Promise<Consent>} */ async function getBoConsent(url, token, consentId) { @@ -134,6 +134,9 @@ async function getBoConsent(url, token, consentId) { consentId: consentId, }, }) + if (err.response?.status === 404) { + throw new Error(errors.LOGIN_FAILED) + } throw new Error(errors.MAINTENANCE) } } @@ -142,7 +145,7 @@ async function getBoConsent(url, token, consentId) { * Delete BO consent * @param {string} url * @param {string} token - * @param {number} consentId + * @param {string} consentId * @returns */ async function deleteBoConsent(url, token, consentId) {