From 145eb4bd7cc3afcc2d3b26e94cfe3941eca6bb8f Mon Sep 17 00:00:00 2001 From: Uxio Fuentefria Date: Fri, 8 Sep 2023 14:04:43 +0200 Subject: [PATCH] Add HttpService tests --- package-lock.json | 4 +- src/routes/events/events.service.spec.ts | 4 +- src/routes/webhook/webhook.service.spec.ts | 70 ++++++++++++++++++++-- test/events.e2e-spec.ts | 2 +- 4 files changed, 70 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6caa1c4..1dfc214 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "safe-events-service", - "version": "0.2.1", + "version": "0.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "safe-events-service", - "version": "0.2.1", + "version": "0.3.0", "license": "UNLICENSED", "dependencies": { "@adminjs/express": "^6.0.0", diff --git a/src/routes/events/events.service.spec.ts b/src/routes/events/events.service.spec.ts index 1bb90d0..2955f27 100644 --- a/src/routes/events/events.service.spec.ts +++ b/src/routes/events/events.service.spec.ts @@ -17,7 +17,7 @@ describe('EventsService', () => { beforeEach(async () => { const webhookServiceMock = { - postEveryWebhook: async (_: object) => ({ + postEveryWebhook: async () => ({ data: {}, status: 200, statusText: 'OK', @@ -36,8 +36,6 @@ describe('EventsService', () => { webhookService = module.get(WebhookService); }); - - describe('listenToEvents', () => { it('should return consumer tag', async () => { const expected = 'exampleTag'; diff --git a/src/routes/webhook/webhook.service.spec.ts b/src/routes/webhook/webhook.service.spec.ts index a4c4b18..3ed4987 100644 --- a/src/routes/webhook/webhook.service.spec.ts +++ b/src/routes/webhook/webhook.service.spec.ts @@ -5,8 +5,12 @@ import { WebhookModule } from './webhook.module'; import { DatabaseModule } from '../../datasources/db/database.module'; import { ConfigModule } from '@nestjs/config'; import { TxServiceEventType } from '../events/event.dto'; +import { HttpService } from '@nestjs/axios'; +import { Observable } from 'rxjs'; +import { AxiosResponse } from 'axios'; describe('Webhook service', () => { + let httpService: HttpService; let webhookService: WebhookService; beforeEach(async () => { @@ -14,6 +18,7 @@ describe('Webhook service', () => { imports: [ConfigModule.forRoot(), WebhookModule, DatabaseModule], }).compile(); + httpService = moduleRef.get(HttpService); webhookService = moduleRef.get(WebhookService); }); @@ -64,10 +69,10 @@ describe('Webhook service', () => { }); it('should post if webhooks are defined', async () => { const webhooks: Webhook[] = [new Webhook(), new Webhook(), new Webhook()]; - webhooks[0].url = 'localhost:4815'; - webhooks[0].authorization = 'basic 1234'; - webhooks[1].url = 'localhost:1623'; - webhooks[2].url = 'localhost:42108'; + webhooks[0].url = 'http://localhost:4815'; + webhooks[0].authorization = 'Basic 1234'; + webhooks[1].url = 'http://localhost:1623'; + webhooks[2].url = 'http://localhost:42108'; webhooks[2].authorization = ''; webhooks[0].sendSafeCreations = true; webhooks[1].sendSafeCreations = false; @@ -110,4 +115,61 @@ describe('Webhook service', () => { ); }); }); + + describe('postWebhook', () => { + it('should post without authentication', async () => { + const url = 'http://localhost:4815'; + const msg = { + chainId: '1', + type: 'SAFE_CREATED' as TxServiceEventType, + text: 'hello', + address: '0x0275FC2adfF11270F3EcC4D2F7Aa0a9784601Ca6', + }; + + const axiosResponseMocked = { status: 200 }; + const httpServicePostSpy = jest + .spyOn(httpService, 'post') + .mockImplementation(() => { + const observableResponse: Observable> = + new Observable((subscriber) => { + subscriber.next(axiosResponseMocked); + }); + return observableResponse; + }); + const results = await webhookService.postWebhook(msg, url, ''); + expect(results).toBe(axiosResponseMocked); + expect(httpServicePostSpy).toHaveBeenCalledTimes(1); + expect(httpServicePostSpy).toHaveBeenCalledWith(url, msg, { + headers: {}, + }); + }); + + it('shoud post with authentication', async () => { + const url = 'http://localhost:4815'; + const msg = { + chainId: '1', + type: 'SAFE_CREATED' as TxServiceEventType, + text: 'hello', + address: '0x0275FC2adfF11270F3EcC4D2F7Aa0a9784601Ca6', + }; + const authorization = 'Basic 1234'; + + const axiosResponseMocked = { status: 200 }; + const httpServicePostSpy = jest + .spyOn(httpService, 'post') + .mockImplementation(() => { + const observableResponse: Observable> = + new Observable((subscriber) => { + subscriber.next(axiosResponseMocked); + }); + return observableResponse; + }); + const results = await webhookService.postWebhook(msg, url, authorization); + expect(results).toBe(axiosResponseMocked); + expect(httpServicePostSpy).toHaveBeenCalledTimes(1); + expect(httpServicePostSpy).toHaveBeenCalledWith(url, msg, { + headers: { Authorization: authorization }, + }); + }); + }); }); diff --git a/test/events.e2e-spec.ts b/test/events.e2e-spec.ts index 342ce36..d4aa7ca 100644 --- a/test/events.e2e-spec.ts +++ b/test/events.e2e-spec.ts @@ -20,8 +20,8 @@ describe('Events handling', () => { }).compile(); eventsService = moduleFixture.get(EventsService); - webhookService = moduleFixture.get(WebhookService); queueProvider = moduleFixture.get(QueueProvider); + webhookService = moduleFixture.get(WebhookService); // Wait for queue provider connection to be established, as it could take a little const { channel } = await queueProvider.getConnection();