Skip to content

Commit

Permalink
Add HttpService tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Uxio0 committed Sep 8, 2023
1 parent b6dacf4 commit 92556ae
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/routes/events/events.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ describe('EventsService', () => {
webhookService = module.get<WebhookService>(WebhookService);
});



describe('listenToEvents', () => {
it('should return consumer tag', async () => {
const expected = 'exampleTag';
Expand Down
70 changes: 66 additions & 4 deletions src/routes/webhook/webhook.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ 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 () => {
const moduleRef = await Test.createTestingModule({
imports: [ConfigModule.forRoot(), WebhookModule, DatabaseModule],
}).compile();

httpService = moduleRef.get<HttpService>(HttpService);
webhookService = moduleRef.get<WebhookService>(WebhookService);
});

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = <AxiosResponse>{ status: 200 };
const httpServicePostSpy = jest
.spyOn(httpService, 'post')
.mockImplementation(() => {
const observableResponse: Observable<AxiosResponse<unknown, any>> =
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 = <AxiosResponse>{ status: 200 };
const httpServicePostSpy = jest
.spyOn(httpService, 'post')
.mockImplementation(() => {
const observableResponse: Observable<AxiosResponse<unknown, any>> =
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 },
});
});
});
});
2 changes: 1 addition & 1 deletion test/events.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('Events handling', () => {
}).compile();

eventsService = moduleFixture.get<EventsService>(EventsService);
webhookService = moduleFixture.get<WebhookService>(WebhookService);
queueProvider = moduleFixture.get<QueueProvider>(QueueProvider);
webhookService = moduleFixture.get<WebhookService>(WebhookService);

// Wait for queue provider connection to be established, as it could take a little
const { channel } = await queueProvider.getConnection();
Expand Down

0 comments on commit 92556ae

Please sign in to comment.