adding keycloak config and unit test

This commit is contained in:
KurtisMelkisedec 2025-10-30 11:35:03 +00:00
parent bf468ba8ae
commit 120990f89f
3 changed files with 146 additions and 9 deletions

View File

@ -14,10 +14,10 @@ export class RabbitMQService implements OnModuleInit {
} }
async connect(): Promise<void> { async connect(): Promise<void> {
const user = this.configService.get<string>('rabbitmq.user'); const user = this.configService.get<string>('appConfig.user');
const pass = this.configService.get<string>('rabbitmq.pass'); const pass = this.configService.get<string>('appConfig.pass');
const host = this.configService.get<string>('rabbitmq.host'); const host = this.configService.get<string>('appConfig.host');
const port = this.configService.get<string>('rabbitmq.port'); const port = this.configService.get<string>('appConfig.port');
this.connection = await connect(`amqp://${user}:${pass}@${host}:${port}`); this.connection = await connect(`amqp://${user}:${pass}@${host}:${port}`);
this.channel = await this.connection.createChannel(); this.channel = await this.connection.createChannel();

View File

@ -0,0 +1,117 @@
import { Test, TestingModule } from '@nestjs/testing';
import { WebhookService } from './webhook.service';
import { RabbitMQService } from './rabbit.service';
import { ConfigService } from '@nestjs/config';
import { InboundSMSMessageNotificationWrapperDto } from 'src/dtos/sms.mo.dto';
import {
EventType,
OrderState,
SubscriptionDto,
} from 'src/dtos/subscription.dto';
describe('WebhookService', () => {
let service: WebhookService;
const mockRabbitMQ = { sendToQueue: jest.fn() };
const mockConfigService = {
get: jest.fn((key: string) => {
const config = {
RABBITMQ_QUEUE_WEBHOOK: 'RABBITMQ_QUEUE_WEBHOOK',
RABBITMQ_QUEUE_NOTIFICATION: 'RABBITMQ_QUEUE_NOTIFICATION',
RABBITMQ_QUEUE_PAYMENT: 'RABBITMQ_QUEUE_PAYMENT',
};
return config[key];
}),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
WebhookService,
{ provide: RabbitMQService, useValue: mockRabbitMQ },
{ provide: ConfigService, useValue: mockConfigService },
],
}).compile();
service = module.get<WebhookService>(WebhookService);
jest.clearAllMocks();
});
//unit test for sms-mo notification
it('should send SMS MO payload to the correct queue', async () => {
const dto: InboundSMSMessageNotificationWrapperDto = {
inboundSMSMessageNotification: {
callbackData: 'cb-123',
inboundSMSMessage: {
dateTime: new Date().toISOString(),
destinationAddress: '12345',
messageId: 'msg-001',
message: 'Hello world!',
senderAddress: '987654321',
},
},
};
await service.smsMoNotification('FR', 'Orange', '123', dto);
expect(mockRabbitMQ.sendToQueue).toHaveBeenCalledWith(
'webhook_queue',
expect.objectContaining({
operator: 'Orange',
country: 'FR',
ise2: '123',
data: dto,
}),
);
});
//unit test subscription mangement
it('should send subscription payload to the correct queue', async () => {
const dto: SubscriptionDto = {
note: { text: 'User subscribed' },
event: {
id: 1,
relatedParty: [{ id: '123', name: 'ISE2', role: 'subscriber' }],
order: {
id: 1,
state: OrderState.Completed,
orderItem: {
product: { id: 'WIDO access' },
},
},
},
eventType: EventType.creation,
eventTime: new Date().toISOString(),
};
await service.manageSubscription('FR', 'Orange', dto);
expect(mockRabbitMQ.sendToQueue).toHaveBeenCalledWith(
'payment_queue',
expect.objectContaining({
operator: 'Orange',
country: 'FR',
data: dto,
}),
);
});
//unit test for he notification
it('should send HE notification payload to the correct queue', async () => {
await service.handleHeNotification(
'FR',
'Orange',
'callbackURL',
'ISE2CODE',
);
expect(mockRabbitMQ.sendToQueue).toHaveBeenCalledWith(
'notification_queue',
expect.objectContaining({
operator: 'Orange',
country: 'FR',
callback: 'callbackURL',
ise2: 'ISE2CODE',
}),
);
});
});

View File

@ -2,10 +2,27 @@ import { Injectable } from '@nestjs/common';
import { RabbitMQService } from 'src/services/rabbit.service'; import { RabbitMQService } from 'src/services/rabbit.service';
import { InboundSMSMessageNotificationWrapperDto } from '../dtos/sms.mo.dto'; import { InboundSMSMessageNotificationWrapperDto } from '../dtos/sms.mo.dto';
import { SubscriptionDto } from '../dtos/subscription.dto'; import { SubscriptionDto } from '../dtos/subscription.dto';
import { ConfigService } from '@nestjs/config';
@Injectable() @Injectable()
export class WebhookService { export class WebhookService {
constructor(private readonly rabbitMQService: RabbitMQService) {} private smsMoQueue: string;
private heQueue: string;
private subscriptionEventQueue: string;
constructor(
private readonly rabbitMQService: RabbitMQService,
private configService: ConfigService,
) {
this.smsMoQueue = this.configService.get(
'RABBITMQ_QUEUE_WEBHOOK',
) as string;
this.heQueue = this.configService.get(
'RABBITMQ_QUEUE_NOTIFICATION',
) as string;
this.subscriptionEventQueue = this.configService.get(
'RABBITMQ_QUEUE_PAYMENT',
) as string;
}
async smsMoNotification( async smsMoNotification(
country: string, country: string,
@ -21,7 +38,7 @@ export class WebhookService {
receivedAt: new Date().toISOString(), receivedAt: new Date().toISOString(),
}; };
await this.rabbitMQService.sendToQueue('sms_mo', payload); await this.rabbitMQService.sendToQueue(this.smsMoQueue, payload);
} }
async manageSubscription( async manageSubscription(
@ -37,7 +54,10 @@ export class WebhookService {
}; };
// send message to queue "subscription_events" // send message to queue "subscription_events"
await this.rabbitMQService.sendToQueue('subscription_events', payload); await this.rabbitMQService.sendToQueue(
this.subscriptionEventQueue,
payload,
);
console.log('payload sent to rabbitMQ'); console.log('payload sent to rabbitMQ');
} }
@ -54,7 +74,7 @@ export class WebhookService {
callback, callback,
receivedAt: new Date().toISOString(), receivedAt: new Date().toISOString(),
}; };
await this.rabbitMQService.sendToQueue('he_notifications', payload); await this.rabbitMQService.sendToQueue(this.heQueue, payload);
console.log('payload sent to rabbitMQ'); console.log('payload sent to rabbitMQ');
} }
} }