From 915167e950b7e28c86f8aa9260d82ae67fdf9525 Mon Sep 17 00:00:00 2001 From: Wojciech Adaszynski Date: Mon, 18 May 2020 11:52:21 +0200 Subject: [PATCH] Fix Sentry plugin tests --- .../src/service/sentry-api.test.ts | 34 +++++++++++++++++++ .../sentry-backend/src/service/sentry-api.ts | 19 +++++++---- .../SentryPluginPage.test.tsx | 11 ++++-- plugins/sentry/src/data/api-factory.ts | 2 +- 4 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 plugins/sentry-backend/src/service/sentry-api.test.ts diff --git a/plugins/sentry-backend/src/service/sentry-api.test.ts b/plugins/sentry-backend/src/service/sentry-api.test.ts new file mode 100644 index 0000000000..fd4c2581b5 --- /dev/null +++ b/plugins/sentry-backend/src/service/sentry-api.test.ts @@ -0,0 +1,34 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 { SentryApiForwarder } from './sentry-api'; +import axios from 'axios'; + +jest.mock('axios', () => ({ + default: { + post: jest.fn(), + }, +})); +describe('SentryApiForwarder', () => { + it('should generate headers based on token passed in constructor', () => { + const forwarder = new SentryApiForwarder('testtoken'); + + expect(forwarder.getRequestHeaders()).toEqual({ + headers: { + Authorization: `Bearer testtoken`, + }, + }); + }); +}); diff --git a/plugins/sentry-backend/src/service/sentry-api.ts b/plugins/sentry-backend/src/service/sentry-api.ts index f80ad5ea7f..861ad52227 100644 --- a/plugins/sentry-backend/src/service/sentry-api.ts +++ b/plugins/sentry-backend/src/service/sentry-api.ts @@ -18,18 +18,23 @@ import axios from 'axios'; export class SentryApiForwarder { constructor(private token: string) {} + + // public for testing + public getRequestHeaders() { + return { + headers: { + Authorization: `Bearer ${this.token}`, + }, + }; + } public fowardRequest(request: express.Request, response: express.Response) { const sentryUrl = request.path; axios - .get(`https://sentry.io/${sentryUrl}`, { - headers: { - Authorization: `Bearer ${this.token}`, - }, - }) - .then(res => { + .get(`https://sentry.io/${sentryUrl}`, this.getRequestHeaders()) + .then((res) => { response.send(res.data); }) - .catch(err => { + .catch((err) => { return response.status(err.response.status).json({ detail: err.response.statusText, }); diff --git a/plugins/sentry/src/components/SentryPluginPage/SentryPluginPage.test.tsx b/plugins/sentry/src/components/SentryPluginPage/SentryPluginPage.test.tsx index cb22885861..400c75e065 100644 --- a/plugins/sentry/src/components/SentryPluginPage/SentryPluginPage.test.tsx +++ b/plugins/sentry/src/components/SentryPluginPage/SentryPluginPage.test.tsx @@ -20,14 +20,19 @@ import mockFetch from 'jest-fetch-mock'; import SentryPluginPage from './SentryPluginPage'; import { ThemeProvider } from '@material-ui/core'; import { lightTheme } from '@backstage/theme'; +import { ApiProvider, ApiRegistry, errorApiRef } from '@backstage/core'; + +const errorApi = { post: () => {} }; describe('SentryPluginPage', () => { it('should render header and time switched', () => { mockFetch.mockResponse(() => new Promise(() => {})); const rendered = render( - - - , + + + + + , ); expect(rendered.getByText('Sentry issues')).toBeInTheDocument(); expect(rendered.getByText('24H')).toBeInTheDocument(); diff --git a/plugins/sentry/src/data/api-factory.ts b/plugins/sentry/src/data/api-factory.ts index 0b82637cb4..32ff4bde5c 100644 --- a/plugins/sentry/src/data/api-factory.ts +++ b/plugins/sentry/src/data/api-factory.ts @@ -18,7 +18,7 @@ import { MockSentryApi } from './mock-api'; import { ProductionSentryApi } from './production-api'; export function sentryApiFactory(organization: string): SentryApi { - if (process.env.NODE_ENV !== 'production') { + if (process.env.NODE_ENV === 'production') { return new ProductionSentryApi(organization); } return new MockSentryApi();