feat: add token related tests to report and history endpoints

Signed-off-by: Anton Ganhammar <ganhammar@gmail.com>
This commit is contained in:
Anton Ganhammar
2023-08-31 15:10:40 +02:00
parent 82141ae65a
commit f86328fee6
@@ -25,6 +25,35 @@ import {
} from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
import { createRouter } from './router';
import { CatalogRequestOptions } from '@backstage/catalog-client';
jest.mock('./CodeCoverageDatabase');
import { CodeCoverageDatabase } from './CodeCoverageDatabase';
CodeCoverageDatabase.create = jest.fn(
async () =>
({
getCodeCoverage: async () => ({
files: [],
metadata: {
generationTime: 1,
},
}),
getHistory: async () => ({}),
} as any),
);
let catalogRequestOptions: CatalogRequestOptions;
jest.mock('@backstage/catalog-client', () => ({
CatalogClient: jest.fn().mockImplementation(() => ({
getEntityByRef: async (_: string, options: CatalogRequestOptions) => {
catalogRequestOptions = options;
return {};
},
})),
}));
function createDatabase(): PluginDatabaseManager {
return DatabaseManager.fromConfig(
@@ -76,4 +105,28 @@ describe('createRouter', () => {
expect(response.body).toEqual({ status: 'ok' });
});
});
[
'/report?entity=component:default/mycomponent',
'/history?entity=component:default/mycomponent',
].forEach(uri => {
describe(`GET ${uri}`, () => {
it('does not send token when calling catalog api and request is unauthenticated', async () => {
const response = await request(app).get(uri);
expect(response.status).toEqual(200);
expect(catalogRequestOptions.token).toBeUndefined();
});
it('includes auth token when calling catalog api', async () => {
const token = 'my-auth-token';
const response = await request(app)
.get(uri)
.set('Authorization', `Bearer ${token}`);
expect(response.status).toEqual(200);
expect(catalogRequestOptions.token).toEqual(token);
});
});
});
});