test: add tests for notification clients
Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
@@ -22,7 +22,9 @@
|
||||
"postpack": "backstage-cli package postpack"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@backstage/cli": "workspace:^"
|
||||
"@backstage/cli": "workspace:^",
|
||||
"@backstage/test-utils": "workspace:^",
|
||||
"msw": "^1.0.0"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2024 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { setupRequestMockHandlers } from '@backstage/test-utils';
|
||||
import { rest } from 'msw';
|
||||
import { setupServer } from 'msw/node';
|
||||
import { NotificationPayload } from '@backstage/plugin-notifications-common';
|
||||
import {
|
||||
DefaultNotificationService,
|
||||
NotificationSendOptions,
|
||||
} from './DefaultNotificationService';
|
||||
|
||||
const server = setupServer();
|
||||
|
||||
const testNotification: NotificationPayload = {
|
||||
title: 'Notification 1',
|
||||
link: '/catalog',
|
||||
severity: 'normal',
|
||||
};
|
||||
|
||||
describe('DefaultNotificationService', () => {
|
||||
setupRequestMockHandlers(server);
|
||||
const mockBaseUrl = 'http://backstage/api/notifications';
|
||||
const discoveryApi = {
|
||||
getBaseUrl: async () => mockBaseUrl,
|
||||
getExternalBaseUrl: async () => mockBaseUrl,
|
||||
};
|
||||
const tokenManager = {
|
||||
getToken: async () => ({ token: '1234' }),
|
||||
authenticate: jest.fn(),
|
||||
};
|
||||
|
||||
let service: DefaultNotificationService;
|
||||
beforeEach(() => {
|
||||
service = DefaultNotificationService.create({
|
||||
discovery: discoveryApi,
|
||||
tokenManager,
|
||||
pluginId: 'test',
|
||||
});
|
||||
});
|
||||
|
||||
describe('getNotifications', () => {
|
||||
it('should create notification', async () => {
|
||||
const body: NotificationSendOptions = {
|
||||
recipients: { type: 'entity', entityRef: ['user:default/john.doe'] },
|
||||
payload: testNotification,
|
||||
};
|
||||
|
||||
server.use(
|
||||
rest.post(`${mockBaseUrl}/`, async (req, res, ctx) => {
|
||||
const json = await req.json();
|
||||
expect(json).toEqual({ ...body, origin: 'plugin-test' });
|
||||
expect(req.headers.get('Authorization')).toEqual('Bearer 1234');
|
||||
return res(ctx.status(200));
|
||||
}),
|
||||
);
|
||||
await expect(service.send(body)).resolves.not.toThrow();
|
||||
});
|
||||
|
||||
it('should throw error if failing', async () => {
|
||||
const body: NotificationSendOptions = {
|
||||
recipients: { type: 'entity', entityRef: ['user:default/john.doe'] },
|
||||
payload: testNotification,
|
||||
};
|
||||
|
||||
server.use(
|
||||
rest.post(`${mockBaseUrl}/`, async (req, res, ctx) => {
|
||||
const json = await req.json();
|
||||
expect(json).toEqual({ ...body, origin: 'plugin-test' });
|
||||
expect(req.headers.get('Authorization')).toEqual('Bearer 1234');
|
||||
return res(ctx.status(400));
|
||||
}),
|
||||
);
|
||||
await expect(service.send(body)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -60,7 +60,7 @@ export class DefaultNotificationService implements NotificationService {
|
||||
try {
|
||||
const baseUrl = await this.discovery.getBaseUrl('notifications');
|
||||
const { token } = await this.tokenManager.getToken();
|
||||
await fetch(`${baseUrl}/notifications`, {
|
||||
const response = await fetch(`${baseUrl}/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
...notification,
|
||||
@@ -69,9 +69,14 @@ export class DefaultNotificationService implements NotificationService {
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Request failed with status ${response.status}`);
|
||||
}
|
||||
} catch (error) {
|
||||
// TODO: Should not throw in optimal case, see BEP
|
||||
throw new Error(`Failed to send notifications: ${error}`);
|
||||
|
||||
Reference in New Issue
Block a user