chore: fixing mocking and tests

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-02-13 13:53:14 +01:00
parent 59ce34dc4d
commit 3d3c729728
2 changed files with 21 additions and 7 deletions
@@ -212,20 +212,30 @@ describe('createRouter', () => {
});
describe('GET /entity/:namespace/:kind/:name/obfuscated', () => {
catalog.getEntityByRef.mockResolvedValueOnce(entity);
catalog.getEntities.mockResolvedValueOnce({ items: entities });
beforeEach(() => {
catalog.getEntityByRef = jest.fn().mockResolvedValueOnce(entity);
catalog.getEntities = jest
.fn()
.mockResolvedValueOnce({ items: entities });
});
it('returns obfuscated 404 if the user token does not return a catalog entity', async () => {
catalog.getEntityByRef = jest.fn().mockResolvedValue(undefined);
it('returns obfuscated 401 if no auth', async () => {
const obfuscatedEntity = await request(app).get(
'/entity/default/component/test/obfuscated',
);
expect(obfuscatedEntity.status).toEqual(401);
expect(obfuscatedEntity.status).toEqual(404);
});
it('returns obfuscated entity and badges', async () => {
catalog.getEntityByRef = jest.fn().mockResolvedValue(entity);
const obfuscatedEntity = await request(app)
.get('/entity/default/component/test/obfuscated')
.set('Authorization', 'Bearer fakeToken');
expect(obfuscatedEntity.status).toEqual(200);
expect(obfuscatedEntity.body.uuid).toMatch(
new RegExp(
@@ -233,6 +243,11 @@ describe('createRouter', () => {
),
);
expect(catalog.getEntityByRef).toHaveBeenCalledWith(
{ namespace: 'default', kind: 'component', name: 'test' },
{ token: 'fakeToken' },
);
const uuid = obfuscatedEntity.body.uuid;
const url = `/entity/${uuid}/test-badge?format=json`;
let response = await request(app).get(url);
+2 -3
View File
@@ -60,7 +60,7 @@ export async function createRouter(
);
const router = Router();
const { config, logger, tokenManager, discovery, identity } = options;
const { config, logger, tokenManager, discovery } = options;
const baseUrl = await discovery.getExternalBaseUrl('badges');
if (config.getOptionalBoolean('app.badges.obfuscate')) {
@@ -72,7 +72,6 @@ export async function createRouter(
logger,
options,
config,
identity,
baseUrl,
);
}
@@ -94,7 +93,6 @@ async function obfuscatedRoute(
logger: Logger,
options: RouterOptions,
config: Config,
identity: IdentityApi,
baseUrl: string,
) {
logger.info('Badges obfuscation is enabled');
@@ -130,6 +128,7 @@ async function obfuscatedRoute(
},
token,
);
if (isNil(entity)) {
throw new NotFoundError(
`No ${kind} entity in ${namespace} named "${name}"`,