Skip to content

Commit

Permalink
Fix kuery node builder for deleteLegacyUrlAliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Portner committed Mar 22, 2022
1 parent d2529f2 commit 6cc86ef
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ jest.mock('../../../../elasticsearch', () => {
return { getErrorMessage: mockGetEsErrorMessage };
});

// Mock these functions to return empty results, as this simplifies test cases and we don't need to exercise alternate code paths for these
jest.mock('@kbn/es-query', () => {
return { nodeTypes: { function: { buildNode: jest.fn() } } };
});
// Mock this function to return empty results, as this simplifies test cases and we don't need to exercise alternate code paths for these
jest.mock('../search_dsl', () => {
return { getSearchDsl: jest.fn() };
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('deleteLegacyUrlAliases', () => {
}

const type = 'obj-type';
const id = 'obj-id';
const id = 'id-1:'; // include a colon (special character) in the object ID to implicitly assert that the kuery node builder handles it gracefully

it('throws an error if namespaces includes the "all namespaces" string', async () => {
const namespaces = [ALL_NAMESPACES_STRING];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export async function deleteLegacyUrlAliases(params: DeleteLegacyUrlAliasesParam
}

const { buildNode } = esKuery.nodeTypes.function;
const match1 = buildNode('is', `${LEGACY_URL_ALIAS_TYPE}.targetType`, type);
const match2 = buildNode('is', `${LEGACY_URL_ALIAS_TYPE}.targetId`, id);
const match1 = buildNode('is', getKueryKey('targetType'), type);
const match2 = buildNode('is', getKueryKey('targetId'), `"${id}"`); // Enclose in quotes to escape any special characters like ':' and prevent parsing errors (Technically an object ID can contain a colon)
const kueryNode = buildNode('and', [match1, match2]);

try {
Expand Down Expand Up @@ -107,3 +107,8 @@ export async function deleteLegacyUrlAliases(params: DeleteLegacyUrlAliasesParam
function throwError(type: string, id: string, message: string) {
throw new Error(`Failed to delete legacy URL aliases for ${type}/${id}: ${message}`);
}

function getKueryKey(attribute: string) {
// Note: these node keys do NOT include '.attributes' for type-level fields because we are using the query in the ES client (instead of the SO client)
return `${LEGACY_URL_ALIAS_TYPE}.${attribute}`;
}

0 comments on commit 6cc86ef

Please sign in to comment.