Unit tests
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { SseController } from './sse.controller';
|
||||
import { SseTicketService } from './sse-ticket.service';
|
||||
import { UserService } from 'src/modules/user/user.service';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { AuthService } from 'src/modules/auth/auth.service';
|
||||
import { SseService } from './sse.service';
|
||||
|
||||
describe('SseController', () => {
|
||||
let controller: SseController;
|
||||
@@ -7,6 +13,16 @@ describe('SseController', () => {
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [SseController],
|
||||
imports: [ConfigModule],
|
||||
providers: [
|
||||
ConfigService,
|
||||
{ provide: JwtService, useClass: MockJwTService },
|
||||
{ provide: SseTicketService, useClass: MockSseTicketService },
|
||||
{ provide: UserService, useClass: MockUserService },
|
||||
{ provide: AuthService, useClass: MockAuthService },
|
||||
{ provide: SseService, useClass: MockSSEService }
|
||||
|
||||
]
|
||||
}).compile();
|
||||
|
||||
controller = module.get<SseController>(SseController);
|
||||
@@ -15,4 +31,36 @@ describe('SseController', () => {
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
|
||||
it('should generate a Ticket', () => {
|
||||
const t = controller.getTicket({ user: { id: 123}} as any);
|
||||
expect(controller['ticketService'].generateTicket).toHaveBeenCalled()
|
||||
});
|
||||
|
||||
it('should generate a SSE stream', async () => {
|
||||
await controller.sse('abc');
|
||||
expect(controller["sseService"].register).toHaveBeenCalled();
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
class MockSseTicketService {
|
||||
generateTicket = jest.fn().mockImplementation( (id) => {
|
||||
return {ticket: 'test-ticket-id'}
|
||||
});
|
||||
|
||||
getUserIdToTicket = jest.fn().mockImplementation( (ticket) => {
|
||||
return 99;
|
||||
})
|
||||
}
|
||||
|
||||
class MockSSEService {
|
||||
register = jest.fn().mockImplementation( (id) => {
|
||||
return new Promise(resolve => resolve(null))
|
||||
})
|
||||
}
|
||||
|
||||
class MockUserService { }
|
||||
|
||||
class MockJwTService {}
|
||||
class MockAuthService {}
|
||||
@@ -2,17 +2,13 @@ import { Controller, Get, Param, Query, Req, Sse, UnauthorizedException, UseGuar
|
||||
import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.interface';
|
||||
import { SseTicketService } from './sse-ticket.service';
|
||||
import { AuthGuard } from 'src/core/guards/auth.guard';
|
||||
import { Observable, interval, map } from 'rxjs';
|
||||
import { KeyService } from 'src/modules/key/key.service';
|
||||
import { AuthService } from 'src/modules/auth/auth.service';
|
||||
import { User } from 'src/model/entitites';
|
||||
import { UserService } from 'src/modules/user/user.service';
|
||||
import { Observable } from 'rxjs';
|
||||
import { SseService } from './sse.service';
|
||||
|
||||
@Controller('sse')
|
||||
export class SseController {
|
||||
|
||||
constructor(private ticketService: SseTicketService, private sseService: SseService, private userService: UserService) {}
|
||||
constructor(private ticketService: SseTicketService, private sseService: SseService) {}
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Get('ticket')
|
||||
@@ -24,15 +20,9 @@ export class SseController {
|
||||
async sse(@Query('ticket') ticket: string): Promise<Observable<any>> {
|
||||
const userId = this.ticketService.getUserIdToTicket(ticket);
|
||||
if (!userId) throw new UnauthorizedException('Invalid/expired ticket');
|
||||
const user = await this.getUserToId(userId);
|
||||
if (!userId) throw new UnauthorizedException('Invalid/expired ticket');
|
||||
|
||||
|
||||
return this.sseService.register(userId);
|
||||
}
|
||||
|
||||
private async getUserToId(userId: string): Promise<User> {
|
||||
const user = await this.userService.getUserById(userId);
|
||||
return Promise.resolve(user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,12 @@ import { SseTicketService } from './sse-ticket.service';
|
||||
import { AuthModule } from 'src/modules/auth/auth.module';
|
||||
import { SharedServiceModule } from 'src/shared/service/shared.service.module';
|
||||
import { MailModule } from 'src/modules/mail/mail.module';
|
||||
import { UserService } from 'src/modules/user/user.service';
|
||||
import { SseService } from './sse.service';
|
||||
|
||||
@Module({
|
||||
controllers: [SseController],
|
||||
imports: [DatabaseModule, AuthModule, SharedServiceModule, MailModule],
|
||||
providers: [SseTicketService, UserService, SseService],
|
||||
providers: [SseTicketService, SseService],
|
||||
exports: [SseService]
|
||||
})
|
||||
export class SseModule {}
|
||||
|
||||
Reference in New Issue
Block a user