use node-fetch

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-05-25 11:38:55 +02:00
parent 09c05a10f2
commit 1354d81b86
9 changed files with 44 additions and 32 deletions
+1
View File
@@ -37,6 +37,7 @@
"@backstage/plugin-notifications-common": "workspace:^",
"@backstage/plugin-signals-node": "workspace:^",
"knex": "^3.0.0",
"node-fetch": "^2.6.7",
"uuid": "^9.0.0"
},
"devDependencies": {
@@ -24,8 +24,6 @@ import {
} from './DefaultNotificationService';
import { mockCredentials, mockServices } from '@backstage/backend-test-utils';
const server = setupServer();
const testNotification: NotificationPayload = {
title: 'Notification 1',
link: '/catalog',
@@ -33,8 +31,12 @@ const testNotification: NotificationPayload = {
};
describe('DefaultNotificationService', () => {
const server = setupServer();
setupRequestMockHandlers(server);
const discovery = mockServices.discovery();
const discovery = mockServices.discovery.mock({
getBaseUrl: jest.fn().mockResolvedValue('http://example.com'),
});
const auth = mockServices.auth();
let service: DefaultNotificationService;
@@ -53,20 +55,17 @@ describe('DefaultNotificationService', () => {
};
server.use(
rest.post(
`${await discovery.getBaseUrl('notifications')}/`,
async (req, res, ctx) => {
const json = await req.json();
expect(json).toEqual(body);
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'notifications',
}),
);
return res(ctx.status(200));
},
),
rest.post('http://example.com', async (req, res, ctx) => {
const json = await req.json();
expect(json).toEqual(body);
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'notifications',
}),
);
return res(ctx.status(200));
}),
);
await expect(service.send(body)).resolves.toBeUndefined();
});
@@ -78,20 +77,17 @@ describe('DefaultNotificationService', () => {
};
server.use(
rest.post(
`${await discovery.getBaseUrl('notifications')}/`,
async (req, res, ctx) => {
const json = await req.json();
expect(json).toEqual(body);
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'notifications',
}),
);
return res(ctx.status(400));
},
),
rest.post('http://example.com', async (req, res, ctx) => {
const json = await req.json();
expect(json).toEqual(body);
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
targetPluginId: 'notifications',
}),
);
return res(ctx.status(400));
}),
);
await expect(service.send(body)).rejects.toThrow(
'Request failed with status 400',
@@ -17,6 +17,7 @@
import { NotificationService } from './NotificationService';
import { AuthService, DiscoveryService } from '@backstage/backend-plugin-api';
import { NotificationPayload } from '@backstage/plugin-notifications-common';
import fetch from 'node-fetch';
/** @public */
export type NotificationServiceOptions = {
@@ -68,7 +69,7 @@ export class DefaultNotificationService implements NotificationService {
targetPluginId: 'notifications',
});
const response = await fetch(`${baseUrl}/`, {
const response = await fetch(baseUrl, {
method: 'POST',
body: JSON.stringify(notification),
headers: {