feat: move notifications origin resolving to backend

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2024-02-29 17:49:48 +02:00
parent 6ea413c7e8
commit a790a3dfa0
11 changed files with 62 additions and 82 deletions
+1 -3
View File
@@ -31,13 +31,11 @@ export const notificationService = createServiceRef<NotificationService>({
deps: {
auth: coreServices.auth,
discovery: coreServices.discovery,
pluginMetadata: coreServices.pluginMetadata,
},
factory({ auth, discovery, pluginMetadata }) {
factory({ auth, discovery }) {
return DefaultNotificationService.create({
auth,
discovery,
pluginId: pluginMetadata.getId(),
});
},
}),
@@ -42,7 +42,6 @@ describe('DefaultNotificationService', () => {
service = DefaultNotificationService.create({
auth,
discovery,
pluginId: 'test',
});
});
@@ -58,7 +57,7 @@ describe('DefaultNotificationService', () => {
`${await discovery.getBaseUrl('notifications')}/`,
async (req, res, ctx) => {
const json = await req.json();
expect(json).toEqual({ ...body, origin: 'plugin-test' });
expect(json).toEqual(body);
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
@@ -83,7 +82,7 @@ describe('DefaultNotificationService', () => {
`${await discovery.getBaseUrl('notifications')}/`,
async (req, res, ctx) => {
const json = await req.json();
expect(json).toEqual({ ...body, origin: 'plugin-test' });
expect(json).toEqual(body);
expect(req.headers.get('Authorization')).toBe(
mockCredentials.service.header({
onBehalfOf: await auth.getOwnServiceCredentials(),
@@ -22,7 +22,6 @@ import { NotificationPayload } from '@backstage/plugin-notifications-common';
export type NotificationServiceOptions = {
auth: AuthService;
discovery: DiscoveryService;
pluginId: string;
};
/** @public */
@@ -45,17 +44,12 @@ export class DefaultNotificationService implements NotificationService {
private constructor(
private readonly discovery: DiscoveryService,
private readonly auth: AuthService,
private readonly pluginId: string,
) {}
static create(
options: NotificationServiceOptions,
): DefaultNotificationService {
return new DefaultNotificationService(
options.discovery,
options.auth,
options.pluginId,
);
return new DefaultNotificationService(options.discovery, options.auth);
}
async send(notification: NotificationSendOptions): Promise<void> {
@@ -65,13 +59,10 @@ export class DefaultNotificationService implements NotificationService {
onBehalfOf: await this.auth.getOwnServiceCredentials(),
targetPluginId: 'notifications',
});
const response = await fetch(`${baseUrl}/`, {
method: 'POST',
body: JSON.stringify({
...notification,
// TODO: Should retrieve this in the backend from service auth instead
origin: `plugin-${this.pluginId}`,
}),
body: JSON.stringify(notification),
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',