Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tenant-management): add post webhook handler #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {ILogger, LOGGER, STATUS_CODE} from '@sourceloop/core';
import {createHmac, randomBytes} from 'crypto';
import {TenantMgmtServiceApplication} from '../../application';
import {TenantStatus} from '../../enums';
import {WEBHOOK_CONFIG} from '../../keys';
import {PostWebhookHandlerServiceKey, WEBHOOK_CONFIG} from '../../keys';
import {
ContactRepository,
ResourceRepository,
Expand All @@ -24,6 +24,7 @@ describe('WebhookController', () => {
let tenantRepo: TenantRepository;
let contactRepo: ContactRepository;
let webhookPayload: WebhookPayload;
let postWebhookHandlerServiceStub: sinon.SinonStub;
const nonExistantTenant = 'non-existant-tenant';
const notifStub = sinon.stub();

Expand All @@ -49,6 +50,11 @@ describe('WebhookController', () => {
createNotification: notifStub,
getTemplateByName: (name: string) => testTemplates[name],
});

postWebhookHandlerServiceStub = sinon.stub();
app.bind(PostWebhookHandlerServiceKey).to({
postWebhookHandler: postWebhookHandlerServiceStub,
});
});

after(async () => {
Expand All @@ -70,6 +76,19 @@ describe('WebhookController', () => {
});

describe('Common', () => {
it('should call postWebhookHandler on successful webhook processing', async () => {
const headers = await buildHeaders(webhookPayload);
await client
.post('/webhook')
.set(webhookConfig.signatureHeaderName, headers.signature)
.set(webhookConfig.timestampHeaderName, headers.timestamp)
.send(webhookPayload)
.expect(STATUS_CODE.NO_CONTENT);

// Verify that postWebhookHandler is called
sinon.assert.calledOnce(postWebhookHandlerServiceStub);
sinon.assert.calledWith(postWebhookHandlerServiceStub, webhookPayload);
});
it('should return 204 status for a webhook call with valid token', async () => {
const headers = await buildHeaders(webhookPayload);
await client
Expand Down
6 changes: 6 additions & 0 deletions services/tenant-management-service/src/keys.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {VerifyFunction} from 'loopback4-authentication';
import {
IPostWebhookHandlerService,
ITenantManagementServiceConfig,
LeadUser,
ResourceProvisionedWebhookPayload,
WebhookConfig,
WebhookNotificationServiceType,
} from './types';
Expand Down Expand Up @@ -73,3 +75,7 @@ export const WebhookNotificationService =
BindingKey.create<WebhookNotificationServiceType>(
'sf.webhook.handler.notification.service',
);

export const PostWebhookHandlerServiceKey = BindingKey.create<
IPostWebhookHandlerService<ResourceProvisionedWebhookPayload>
>('services.PostWebhookHandlerService');
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
} from '../../types';
import {CryptoHelperService} from '../crypto-helper.service';
import {NotificationService} from '../notifications';
import {IPostWebhookHandlerService} from '../../types/i-post-webhook-handler-service.interface';
import {PostWebhookHandlerServiceKey} from '../../keys';

/**
* Handler for provisioning webhooks.
Expand All @@ -41,6 +43,8 @@ export class ProvisioningWebhookHandler implements IWebhookHandler {
public tenantRepository: TenantRepository,
@inject('services.NotificationService')
private notificationService: NotificationService,
@inject(PostWebhookHandlerServiceKey)
private postWebhookHandlerService: IPostWebhookHandlerService<ResourceProvisionedWebhookPayload>,
@service(CryptoHelperService)
private cryptoHelperService: CryptoHelperService,
@inject(LOGGER.LOGGER_INJECT)
Expand Down Expand Up @@ -97,6 +101,7 @@ export class ProvisioningWebhookHandler implements IWebhookHandler {
{transaction},
);
}
await this.postWebhookHandlerService.postWebhookHandler(payload);

await transaction.commit();
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {ResourceProvisionedWebhookPayload} from './webhook-payload.type';

export interface IPostWebhookHandlerService<
T extends ResourceProvisionedWebhookPayload,
> {
postWebhookHandler(dto: T): Promise<void>;
}
1 change: 1 addition & 0 deletions services/tenant-management-service/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ export * from './webhook-payload.type';
export * from './resource.type';
export * from './i-provisioning-service.interface';
export * from './i-subscription.interface';
export * from './i-post-webhook-handler-service.interface';
Loading