notifications-backend: migrated to use new auth services
Co-authored-by: Fredrik Adelöw <freben@gmail.com> Co-authored-by: Carl-Erik Bergström <cbergstrom@spotify.com> Co-authored-by: blam <ben@blam.sh> Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -29,14 +29,14 @@ export const notificationService = createServiceRef<NotificationService>({
|
||||
createServiceFactory({
|
||||
service,
|
||||
deps: {
|
||||
auth: coreServices.auth,
|
||||
discovery: coreServices.discovery,
|
||||
tokenManager: coreServices.tokenManager,
|
||||
pluginMetadata: coreServices.pluginMetadata,
|
||||
},
|
||||
factory({ discovery, tokenManager, pluginMetadata }) {
|
||||
factory({ auth, discovery, pluginMetadata }) {
|
||||
return DefaultNotificationService.create({
|
||||
auth,
|
||||
discovery,
|
||||
tokenManager,
|
||||
pluginId: pluginMetadata.getId(),
|
||||
});
|
||||
},
|
||||
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
DefaultNotificationService,
|
||||
NotificationSendOptions,
|
||||
} from './DefaultNotificationService';
|
||||
import { mockCredentials, mockServices } from '@backstage/backend-test-utils';
|
||||
|
||||
const server = setupServer();
|
||||
|
||||
@@ -33,21 +34,14 @@ const testNotification: NotificationPayload = {
|
||||
|
||||
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(),
|
||||
};
|
||||
const discovery = mockServices.discovery();
|
||||
const auth = mockServices.auth();
|
||||
|
||||
let service: DefaultNotificationService;
|
||||
beforeEach(() => {
|
||||
beforeEach(async () => {
|
||||
service = DefaultNotificationService.create({
|
||||
discovery: discoveryApi,
|
||||
tokenManager,
|
||||
auth,
|
||||
discovery,
|
||||
pluginId: 'test',
|
||||
});
|
||||
});
|
||||
@@ -60,14 +54,22 @@ describe('DefaultNotificationService', () => {
|
||||
};
|
||||
|
||||
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));
|
||||
}),
|
||||
rest.post(
|
||||
`${await discovery.getBaseUrl('notifications')}/`,
|
||||
async (req, res, ctx) => {
|
||||
const json = await req.json();
|
||||
expect(json).toEqual({ ...body, origin: 'plugin-test' });
|
||||
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.not.toThrow();
|
||||
await expect(service.send(body)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('should throw error if failing', async () => {
|
||||
@@ -77,14 +79,24 @@ describe('DefaultNotificationService', () => {
|
||||
};
|
||||
|
||||
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));
|
||||
}),
|
||||
rest.post(
|
||||
`${await discovery.getBaseUrl('notifications')}/`,
|
||||
async (req, res, ctx) => {
|
||||
const json = await req.json();
|
||||
expect(json).toEqual({ ...body, origin: 'plugin-test' });
|
||||
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',
|
||||
);
|
||||
await expect(service.send(body)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,15 +13,15 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { TokenManager } from '@backstage/backend-common';
|
||||
|
||||
import { NotificationService } from './NotificationService';
|
||||
import { DiscoveryService } from '@backstage/backend-plugin-api';
|
||||
import { AuthService, DiscoveryService } from '@backstage/backend-plugin-api';
|
||||
import { NotificationPayload } from '@backstage/plugin-notifications-common';
|
||||
|
||||
/** @public */
|
||||
export type NotificationServiceOptions = {
|
||||
auth: AuthService;
|
||||
discovery: DiscoveryService;
|
||||
tokenManager: TokenManager;
|
||||
pluginId: string;
|
||||
};
|
||||
|
||||
@@ -44,22 +44,27 @@ export type NotificationSendOptions = {
|
||||
export class DefaultNotificationService implements NotificationService {
|
||||
private constructor(
|
||||
private readonly discovery: DiscoveryService,
|
||||
private readonly tokenManager: TokenManager,
|
||||
private readonly auth: AuthService,
|
||||
private readonly pluginId: string,
|
||||
) {}
|
||||
|
||||
static create({
|
||||
tokenManager,
|
||||
discovery,
|
||||
pluginId,
|
||||
}: NotificationServiceOptions): DefaultNotificationService {
|
||||
return new DefaultNotificationService(discovery, tokenManager, pluginId);
|
||||
static create(
|
||||
options: NotificationServiceOptions,
|
||||
): DefaultNotificationService {
|
||||
return new DefaultNotificationService(
|
||||
options.discovery,
|
||||
options.auth,
|
||||
options.pluginId,
|
||||
);
|
||||
}
|
||||
|
||||
async send(notification: NotificationSendOptions): Promise<void> {
|
||||
try {
|
||||
const baseUrl = await this.discovery.getBaseUrl('notifications');
|
||||
const { token } = await this.tokenManager.getToken();
|
||||
const { token } = await this.auth.getPluginRequestToken({
|
||||
onBehalfOf: await this.auth.getOwnServiceCredentials(),
|
||||
targetPluginId: 'notifications',
|
||||
});
|
||||
const response = await fetch(`${baseUrl}/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
|
||||
Reference in New Issue
Block a user