Make identityApi required

This commit is contained in:
Erik Larsson
2021-01-19 18:34:21 +01:00
parent 570884ece6
commit b52baa518b
2 changed files with 27 additions and 9 deletions
+22 -3
View File
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { UrlPatternDiscovery } from '@backstage/core';
import { UrlPatternDiscovery, IdentityApi } from '@backstage/core';
import { msw } from '@backstage/test-utils';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
@@ -22,6 +22,21 @@ import { FindingSummary, FossaApi, FossaClient } from './index';
const server = setupServer();
const identityApi: IdentityApi = {
getUserId() {
return 'jane-fonda';
},
getProfile() {
return { email: 'jane-fonda@spotify.com' };
},
async getIdToken() {
return Promise.resolve('fake-id-token');
},
async signOut() {
return Promise.resolve();
},
};
describe('FossaClient', () => {
msw.setupDefaultHandlers(server);
@@ -30,7 +45,11 @@ describe('FossaClient', () => {
let client: FossaApi;
beforeEach(() => {
client = new FossaClient({ discoveryApi, organizationId: '8736' });
client = new FossaClient({
discoveryApi,
identityApi,
organizationId: '8736',
});
});
it('should report finding summary', async () => {
@@ -137,7 +156,7 @@ describe('FossaClient', () => {
});
it('should skip organizationId', async () => {
client = new FossaClient({ discoveryApi });
client = new FossaClient({ discoveryApi, identityApi });
server.use(
rest.get(`${mockBaseUrl}/fossa/projects`, (req, res, ctx) => {
+5 -6
View File
@@ -20,7 +20,7 @@ import { FindingSummary, FossaApi } from './FossaApi';
export class FossaClient implements FossaApi {
discoveryApi: DiscoveryApi;
identityApi?: IdentityApi;
identityApi: IdentityApi;
organizationId?: string;
constructor({
@@ -29,7 +29,7 @@ export class FossaClient implements FossaApi {
organizationId,
}: {
discoveryApi: DiscoveryApi;
identityApi?: IdentityApi;
identityApi: IdentityApi;
organizationId?: string;
}) {
this.discoveryApi = discoveryApi;
@@ -39,10 +39,9 @@ export class FossaClient implements FossaApi {
private async callApi(path: string): Promise<any> {
const apiUrl = `${await this.discoveryApi.getBaseUrl('proxy')}/fossa`;
const headers: Record<string, string> = {};
if (this.identityApi) {
headers.authorization = `Bearer ${this.identityApi.getIdToken()}`;
}
const headers: Record<string, string> = {
authorization: await `Bearer ${this.identityApi.getIdToken()}`,
};
const response = await fetch(`${apiUrl}/${path}`, {
headers,
});