Fix Sentry plugin tests

This commit is contained in:
Wojciech Adaszynski
2020-05-18 11:52:21 +02:00
parent 35dec7dc7c
commit 915167e950
4 changed files with 55 additions and 11 deletions
@@ -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`,
},
});
});
});
@@ -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,
});
@@ -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(
<ThemeProvider theme={lightTheme}>
<SentryPluginPage />
</ThemeProvider>,
<ApiProvider apis={ApiRegistry.from([[errorApiRef, errorApi]])}>
<ThemeProvider theme={lightTheme}>
<SentryPluginPage />
</ThemeProvider>
</ApiProvider>,
);
expect(rendered.getByText('Sentry issues')).toBeInTheDocument();
expect(rendered.getByText('24H')).toBeInTheDocument();
+1 -1
View File
@@ -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();