Increase code coverage

This commit is contained in:
Erik Larsson
2021-01-27 02:30:59 +01:00
parent 97d13a8889
commit 1476950a12
3 changed files with 162 additions and 1 deletions
@@ -120,4 +120,51 @@ describe('CatalogClient', () => {
expect(response.items).toEqual([]);
});
});
describe('getLocationById', () => {
const defaultResponse = {
data: {
id: '42',
},
};
beforeEach(() => {
server.use(
rest.get(`${mockBaseUrl}/locations/42`, (_, res, ctx) => {
return res(ctx.json(defaultResponse));
}),
);
});
it('should locations from correct endpoint', async () => {
const response = await client.getLocationById('42', { token });
expect(response).toEqual(defaultResponse);
});
it('forwards authorization token', async () => {
expect.assertions(1);
server.use(
rest.get(`${mockBaseUrl}/locations/42`, (req, res, ctx) => {
expect(req.headers.get('authorization')).toBe(`Bearer ${token}`);
return res(ctx.json(defaultResponse));
}),
);
await client.getLocationById('42', { token });
});
it('skips authorization header if token is omitted', async () => {
expect.assertions(1);
server.use(
rest.get(`${mockBaseUrl}/locations/42`, (req, res, ctx) => {
expect(req.headers.get('authorization')).toBeNull();
return res(ctx.json(defaultResponse));
}),
);
await client.getLocationById('42');
});
});
});
@@ -0,0 +1,19 @@
/*
* 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.
*/
export type CatalogIdentityRequestOptions = {
token: string | undefined;
};
@@ -41,9 +41,24 @@ const identityApi: IdentityApi = {
return Promise.resolve();
},
};
const guestIdentityApi: IdentityApi = {
getUserId() {
return 'guest';
},
getProfile() {
return {};
},
async getIdToken() {
return Promise.resolve(undefined);
},
async signOut() {
return Promise.resolve();
},
};
describe('CatalogClientWrapper', () => {
let client: CatalogClientWrapper;
let client;
let guestClient;
beforeEach(() => {
MockedCatalogClient.mockClear();
@@ -51,6 +66,10 @@ describe('CatalogClientWrapper', () => {
client: new MockedCatalogClient({ discoveryApi }),
identityApi,
});
guestClient = new CatalogClientWrapper({
client: new MockedCatalogClient({ discoveryApi }),
identityApi: guestIdentityApi,
});
});
describe('getEntities', () => {
@@ -64,4 +83,80 @@ describe('CatalogClientWrapper', () => {
expect(getEntities).toHaveBeenCalledTimes(1);
});
});
describe('getLocationById', () => {
it('omits authorization token when guest', async () => {
expect.assertions(2);
await guestClient.getLocationById('42');
const getLocationById =
MockedCatalogClient.mock.instances[0].getLocationById;
expect(getLocationById).toHaveBeenCalledWith('42', {});
expect(getLocationById).toHaveBeenCalledTimes(1);
});
});
describe('getEntityByName', () => {
const name = {
kind: 'kind',
namespace: 'namespace',
name: 'name',
};
it('injects authorization token', async () => {
expect.assertions(2);
await client.getEntityByName(name);
const getEntityByName =
MockedCatalogClient.mock.instances[0].getEntityByName;
expect(getEntityByName).toHaveBeenCalledWith(name, {
token: 'fake-id-token',
});
expect(getEntityByName).toHaveBeenCalledTimes(1);
});
});
describe('addLocation', () => {
const location = { target: 'target' };
it('injects authorization token', async () => {
expect.assertions(2);
await client.addLocation(location);
const addLocation = MockedCatalogClient.mock.instances[0].addLocation;
expect(addLocation).toHaveBeenCalledWith(location, {
token: 'fake-id-token',
});
expect(addLocation).toHaveBeenCalledTimes(1);
});
});
describe('getLocationByEntity', () => {
const entity = {
apiVersion: 'apiVersion',
kind: 'kind',
metadata: {
name: 'name',
},
};
it('injects authorization token', async () => {
expect.assertions(2);
await client.getLocationByEntity(entity);
const getLocationByEntity =
MockedCatalogClient.mock.instances[0].getLocationByEntity;
expect(getLocationByEntity).toHaveBeenCalledWith(entity, {
token: 'fake-id-token',
});
expect(getLocationByEntity).toHaveBeenCalledTimes(1);
});
});
describe('removeEntityByUid', () => {
it('injects authorization token', async () => {
const uid = 'uid';
expect.assertions(2);
await client.removeEntityByUid(uid);
const removeEntityByUid =
MockedCatalogClient.mock.instances[0].removeEntityByUid;
expect(removeEntityByUid).toHaveBeenCalledWith(uid, {
token: 'fake-id-token',
});
expect(removeEntityByUid).toHaveBeenCalledTimes(1);
});
});
});