only emulate entities with by-query if disableRelationsCompatibility is set

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-12-15 19:27:02 +01:00
parent 3d2e95f71c
commit 83b366eca1
2 changed files with 77 additions and 10 deletions
@@ -53,7 +53,7 @@ describe('createRouter readonly disabled', () => {
let locationAnalyzer: jest.Mocked<LocationAnalyzer>;
let permissionsService: jest.Mocked<PermissionsService>;
beforeAll(async () => {
beforeEach(async () => {
entitiesCatalog = {
entities: jest.fn(),
entitiesBatch: jest.fn(),
@@ -135,6 +135,38 @@ describe('createRouter readonly disabled', () => {
{ apiVersion: 'a', kind: 'b', metadata: { name: 'n' } },
];
entitiesCatalog.entities.mockResolvedValueOnce({
entities: { type: 'object', entities: [entities[0]] },
pageInfo: { hasNextPage: false },
});
const response = await request(app).get('/entities');
expect(response.status).toEqual(200);
expect(response.body).toEqual(entities);
});
it('happy path: lists entities when by-entities emulation is enabled', async () => {
const router = await createRouter({
entitiesCatalog,
locationService,
orchestrator,
logger: mockServices.logger.mock(),
refreshService,
config: new ConfigReader(undefined),
permissionIntegrationRouter: express.Router(),
auth: mockServices.auth(),
httpAuth: mockServices.httpAuth(),
locationAnalyzer,
permissionsService: permissionsService,
disableRelationsCompatibility: true, // added
});
app = await wrapServer(express().use(router));
const entities: Entity[] = [
{ apiVersion: 'a', kind: 'b', metadata: { name: 'n' } },
];
entitiesCatalog.queryEntities.mockResolvedValueOnce({
items: { type: 'object', entities: [entities[0]] },
pageInfo: {},
@@ -148,6 +180,49 @@ describe('createRouter readonly disabled', () => {
});
it('parses single and multiple request parameters and passes them down', async () => {
entitiesCatalog.entities.mockResolvedValueOnce({
entities: { type: 'object', entities: [] },
pageInfo: { hasNextPage: false },
});
const response = await request(app).get(
'/entities?filter=a=1,a=2,b=3&filter=c=4',
);
expect(response.status).toEqual(200);
expect(entitiesCatalog.entities).toHaveBeenCalledTimes(1);
expect(entitiesCatalog.entities).toHaveBeenCalledWith({
filter: {
anyOf: [
{
allOf: [
{ key: 'a', values: ['1', '2'] },
{ key: 'b', values: ['3'] },
],
},
{ key: 'c', values: ['4'] },
],
},
credentials: mockCredentials.user(),
});
});
it('parses single and multiple request parameters and passes them down when by-entities emulation is enabled', async () => {
const router = await createRouter({
entitiesCatalog,
locationService,
orchestrator,
logger: mockServices.logger.mock(),
refreshService,
config: new ConfigReader(undefined),
permissionIntegrationRouter: express.Router(),
auth: mockServices.auth(),
httpAuth: mockServices.httpAuth(),
locationAnalyzer,
permissionsService: permissionsService,
disableRelationsCompatibility: true, // added
});
app = await wrapServer(express().use(router));
entitiesCatalog.queryEntities.mockResolvedValueOnce({
items: { type: 'object', entities: [] },
pageInfo: {},
@@ -43,7 +43,6 @@ import { LocationService, RefreshService } from './types';
import {
disallowReadonlyMode,
encodeCursor,
expandLegacyCompoundRelationsInEntity,
locationInput,
validateRequestBody,
} from './util';
@@ -60,7 +59,6 @@ import { LocationAnalyzer } from '@backstage/plugin-catalog-node';
import { AuthorizedValidationService } from './AuthorizedValidationService';
import {
createEntityArrayJsonStream,
processEntitiesResponseItems,
writeEntitiesResponse,
writeSingleEntityResponse,
} from './response';
@@ -153,7 +151,7 @@ export async function createRouter(
// When pagination parameters are passed in, use the legacy slow path
// that loads all entities into memory
if (pagination) {
if (pagination || disableRelationsCompatibility !== true) {
const { entities, pageInfo } = await entitiesCatalog.entities({
filter,
fields,
@@ -194,12 +192,6 @@ export async function createRouter(
);
if (result.items.entities.length) {
if (!disableRelationsCompatibility) {
result.items = processEntitiesResponseItems(
result.items,
expandLegacyCompoundRelationsInEntity,
);
}
if (await responseStream.send(result.items)) {
return; // Client closed connection
}