fix: ensure proper slashes in the email url
Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
+26
@@ -383,6 +383,32 @@ describe('NotificationsEmailProcessor', () => {
|
||||
text: 'https://example.org/catalog/user/default/john.doe',
|
||||
to: 'mock@backstage.io',
|
||||
});
|
||||
|
||||
await processor.postProcess(
|
||||
{
|
||||
origin: 'plugin',
|
||||
id: '1234',
|
||||
user: 'user:default/mock',
|
||||
created: new Date(),
|
||||
payload: {
|
||||
title: 'notification',
|
||||
link: '/catalog/user/default/jane.doe',
|
||||
},
|
||||
},
|
||||
{
|
||||
recipients: { type: 'entity', entityRef: 'user:default/mock' },
|
||||
payload: { title: 'notification' },
|
||||
},
|
||||
);
|
||||
|
||||
expect(sendmailMock).toHaveBeenCalledWith({
|
||||
from: 'backstage@backstage.io',
|
||||
html: '<p><a href="https://example.org/catalog/user/default/jane.doe">https://example.org/catalog/user/default/jane.doe</a></p>',
|
||||
replyTo: undefined,
|
||||
subject: 'notification',
|
||||
text: 'https://example.org/catalog/user/default/jane.doe',
|
||||
to: 'mock@backstage.io',
|
||||
});
|
||||
});
|
||||
|
||||
it('should send email with absolute link to given address', async () => {
|
||||
|
||||
+7
-1
@@ -219,8 +219,14 @@ export class NotificationsEmailProcessor implements NotificationProcessor {
|
||||
|
||||
private getNotificationLink(notification: Notification) {
|
||||
if (notification.payload.link) {
|
||||
const stripLeadingSlash = (s: string) => s.replace(/^\//, '');
|
||||
const ensureTrailingSlash = (s: string) => s.replace(/\/?$/, '/');
|
||||
|
||||
try {
|
||||
const url = new URL(notification.payload.link, this.frontendBaseUrl);
|
||||
const url = new URL(
|
||||
stripLeadingSlash(notification.payload.link),
|
||||
ensureTrailingSlash(this.frontendBaseUrl),
|
||||
);
|
||||
return url.toString();
|
||||
} catch (_e) {
|
||||
// noop: fallback to relative URL
|
||||
|
||||
Reference in New Issue
Block a user