-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
228 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,69 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { EventsController } from './events.controller'; | ||
import { EventsService } from './events.service'; | ||
import { QueueProvider } from '../../datasources/queue/queue.provider'; | ||
import { WebhookService } from '../webhook/webhook.service'; | ||
import { firstValueFrom } from 'rxjs'; | ||
import { TxServiceEvent, TxServiceEventType } from './event.dto'; | ||
import { BadRequestException } from '@nestjs/common'; | ||
|
||
describe('EventsController', () => { | ||
let controller: EventsController; | ||
let service: EventsService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
const module = await Test.createTestingModule({ | ||
controllers: [EventsController], | ||
}).compile(); | ||
providers: [EventsService, QueueProvider, WebhookService], | ||
}) | ||
.overrideProvider(QueueProvider) | ||
.useValue({}) | ||
.overrideProvider(WebhookService) | ||
.useValue({}) | ||
.compile(); | ||
|
||
controller = module.get<EventsController>(EventsController); | ||
service = module.get<EventsService>(EventsService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
describe('SSE events', () => { | ||
it('should require an EIP55 address', async () => { | ||
const notValidAddress = '0x8618CE407F169ABB1388348A19632AaFA857CCB9'; | ||
const expectedError = new BadRequestException('Not valid EIP55 address', { | ||
description: `${notValidAddress} is not a valid EIP55 Safe address`, | ||
}); | ||
expect(() => { | ||
controller.sse(notValidAddress); | ||
}).toThrow(expectedError); | ||
}); | ||
it('should receive for a Safe', async () => { | ||
const relevantSafeAddress = '0x8618ce407F169ABB1388348A19632AaFA857CCB9'; | ||
const notRelevantSafeAddress = | ||
'0x3F6E283068Ded118459B56fC669A27C3a65e587D'; | ||
const txServiceEvents: Array<TxServiceEvent> = [ | ||
{ | ||
chainId: '1', | ||
type: 'SAFE_CREATED' as TxServiceEventType, | ||
hero: 'Saitama', | ||
address: notRelevantSafeAddress, | ||
}, | ||
{ | ||
chainId: '100', | ||
type: 'MESSAGE_CREATED' as TxServiceEventType, | ||
hero: 'Atomic Samurai', | ||
address: relevantSafeAddress, | ||
}, | ||
]; | ||
const observable = controller.sse(relevantSafeAddress); | ||
const firstValue = firstValueFrom(observable); | ||
txServiceEvents.map((txServiceEvent) => | ||
service.pushEventToEventsObservable(txServiceEvent), | ||
); | ||
|
||
// Not relevant event must be ignored by Safe filter | ||
const event = await firstValue; | ||
expect(event.data).toEqual(txServiceEvents[1]); | ||
expect(event.type).toEqual(txServiceEvents[1].type); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
import { Controller, Param, Sse } from '@nestjs/common'; | ||
import { BadRequestException, Controller, Param, Sse } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { EventsService } from './events.service'; | ||
import { getAddress, isAddress } from 'ethers'; | ||
|
||
@Controller('events') | ||
export class EventsController { | ||
constructor(private eventsService: EventsService) {} | ||
constructor(private readonly eventsService: EventsService) {} | ||
|
||
@Sse('/sse/:safe') | ||
sse(@Param('safe') safe: string): Observable<MessageEvent> { | ||
return interval(1000).pipe(map((_) => ({ data: { hello: 'world' } }))); | ||
if (isAddress(safe) && getAddress(safe) === safe) | ||
return this.eventsService.getEventsObservableForSafe(safe); | ||
|
||
throw new BadRequestException('Not valid EIP55 address', { | ||
description: `${safe} is not a valid EIP55 Safe address`, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { EventsController } from './events.controller'; | ||
import { EventsService } from './events.service'; | ||
import { WebhookModule } from '../webhook/webhook.module'; | ||
import { QueueModule } from '../../datasources/queue/queue.module'; | ||
import { WebhookModule } from '../webhook/webhook.module'; | ||
|
||
@Module({ | ||
imports: [QueueModule, WebhookModule], | ||
// controllers: [Controller], | ||
controllers: [EventsController], | ||
providers: [EventsService], | ||
}) | ||
export class EventsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters