118 lines
3.2 KiB
TypeScript
118 lines
3.2 KiB
TypeScript
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',
|
|
}),
|
|
);
|
|
});
|
|
});
|