diff --git a/express-api/src/services/ches/chesServices.ts b/express-api/src/services/ches/chesServices.ts index 5f564dc0d..1ab194f99 100644 --- a/express-api/src/services/ches/chesServices.ts +++ b/express-api/src/services/ches/chesServices.ts @@ -97,11 +97,19 @@ const sendAsync = async (endpoint: string, method: string, data: Record { text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}', })); _fetch.mockImplementationOnce(() => ({ + ok: true, text: () => `{ "messages": [{}], "txId": "${randomUUID()}" }`, })); const response = await chesServices.sendEmailAsync(email, keycloak); @@ -35,10 +36,37 @@ describe('UNIT - Ches Services', () => { text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}', })); _fetch.mockImplementationOnce(() => ({ + ok: false, text: () => `{ "messages": [{}], "txId": "${randomUUID()}" }`, })); expect(async () => await chesServices.sendEmailAsync(email, keycloak)).rejects.toThrow(); }); + it('should return null if the CHES response is not OK', async () => { + const email = produceEmail({ cc: ['john@doe.com'], bcc: ['john@doe.com'] }); + const keycloak = producePimsRequestUser(); + _fetch.mockImplementationOnce(() => ({ + text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}', + })); + _fetch.mockImplementationOnce(() => ({ + ok: false, + text: () => `{ "messages": [{}], "txId": "${randomUUID()}" }`, + })); + const result = await chesServices.sendEmailAsync(email, keycloak); + expect(result).toBeNull(); + }); + it('should return null if the CHES response has no text length', async () => { + const email = produceEmail({ cc: ['john@doe.com'], bcc: ['john@doe.com'] }); + const keycloak = producePimsRequestUser(); + _fetch.mockImplementationOnce(() => ({ + text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}', + })); + _fetch.mockImplementationOnce(() => ({ + ok: true, + text: () => ``, + })); + const result = await chesServices.sendEmailAsync(email, keycloak); + expect(result).toBeNull(); + }); it('should send email with extra config', async () => { const email: IEmail = produceEmail({}); const keycloak = producePimsRequestUser(); @@ -55,6 +83,7 @@ describe('UNIT - Ches Services', () => { text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}', })); _fetch.mockImplementationOnce(() => ({ + ok: true, text: () => `{ "messages": [{}], "txId": "${randomUUID()}" }`, })); const response = await chesServices.sendEmailAsync(email, keycloak); @@ -67,8 +96,8 @@ describe('UNIT - Ches Services', () => { text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}', })); _fetch.mockImplementationOnce(() => ({ + ok: true, text: () => ` - { "createdTS": 1560000000, @@ -102,6 +131,7 @@ describe('UNIT - Ches Services', () => { text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}', })); _fetch.mockImplementationOnce(() => ({ + ok: true, text: () => ` [ { @@ -143,6 +173,7 @@ describe('UNIT - Ches Services', () => { text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}', })); _fetch.mockImplementationOnce(() => ({ + ok: true, text: () => ` { @@ -177,6 +208,7 @@ describe('UNIT - Ches Services', () => { text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}', })); _fetch.mockImplementationOnce(() => ({ + ok: true, text: () => ` { @@ -202,6 +234,7 @@ describe('UNIT - Ches Services', () => { text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}', })); _fetch.mockImplementationOnce(() => ({ + ok: true, text: () => ` { @@ -238,6 +271,7 @@ describe('UNIT - Ches Services', () => { text: () => '{"access_token":"eyAiYSI6IDEgfQ==.ewoiZXhwIjoxCn0="}', })); _fetch.mockImplementationOnce(() => ({ + ok: true, text: () => ` [ {