diff --git a/__tests__/requests/bo.spec.js b/__tests__/requests/bo.spec.js index ea0dca564ea26d8e3b0f9e7bbad12400492cc8e2..5300d07413f4d696a85d4c65b1f979b139c73cf8 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 e1fdc9bc5ae544e4b470ca908228fec2dde5765c..9be6341e9e5902dc86609f55c8b343535569280b 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) } }