Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tools/spectral/ipa/__tests__/utils/resourceEvaluation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isRootLevelResource,
isSingleResourceIdentifier,
isSingletonResource,
resourceBelongsToSingleParent,
} from '../../rulesets/functions/utils/resourceEvaluation';

const resource = {
Expand Down Expand Up @@ -608,4 +609,38 @@ describe('tools/spectral/ipa/rulesets/functions/utils/resourceEvaluation.js', ()
expect(isRootLevelResource('/api/atlas/v2/resourceName/{pathParam}/operations/{operationId}')).toBe(false);
});
});

describe('resourceBelongsToSingleParent', () => {
it('recognizes nested collection paths', () => {
expect(resourceBelongsToSingleParent('/groups/{groupId}/clusters')).toBe(true);
expect(resourceBelongsToSingleParent('/api/atlas/v2/groups/{groupId}/clusters')).toBe(true);
expect(resourceBelongsToSingleParent('/api/atlas/v2/unauth/groups/{groupId}/clusters')).toBe(true);
expect(resourceBelongsToSingleParent('/groups/{groupId}/clusters/{clusterName}/processArgs')).toBe(true);
});

it('recognizes nested instance paths', () => {
expect(resourceBelongsToSingleParent('/groups/{groupId}/clusters/{clusterId}')).toBe(true);
expect(resourceBelongsToSingleParent('/groups/{g}/clusters/{c}/processes/{p}')).toBe(true);
});

it('rejects root-level resources', () => {
expect(resourceBelongsToSingleParent('/groups')).toBe(false);
expect(resourceBelongsToSingleParent('/groups/{groupId}')).toBe(false);
expect(resourceBelongsToSingleParent('/api/atlas/v2/groups/{groupId}')).toBe(false);
});

it('rejects resources nested under a non-parameterized segment', () => {
expect(resourceBelongsToSingleParent('/groups/settings')).toBe(false);
expect(resourceBelongsToSingleParent('/groups/settings/{settingId}')).toBe(false);
});

it('handles paths that are only the API prefix', () => {
expect(resourceBelongsToSingleParent('/api/atlas/v2')).toBe(true);
expect(resourceBelongsToSingleParent('/api/atlas/v2/unauth')).toBe(true);
});

it('rejects consecutive path params', () => {
expect(resourceBelongsToSingleParent('/a/{b}/{c}')).toBe(false);
});
});
});
25 changes: 10 additions & 15 deletions tools/spectral/ipa/rulesets/functions/utils/resourceEvaluation.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,32 +207,27 @@ export function getResourcePathItems(resourceCollectionPath, allPathItems) {
/**
* Checks whether a resource belongs to one parent resource.
* For example, '/resource' returns false, '/resource/{id}/child' returns true.
* Instance paths are normalized to their collection path first, so
* '/parent/{parentId}/child/{childId}' returns true.
*
* @param {string} resourcePath a path for a resource
* @returns {boolean}
*/
function resourceBelongsToSingleParent(resourcePath) {
export function resourceBelongsToSingleParent(resourcePath) {
// Ignore /api/atlas/v2 and /api/atlas/v2/unauth
const path = removePrefix(resourcePath);
if (path === '') {
return true;
}

let resourcePathSections = path.split('/');
if (resourcePathSections[0] === '') {
resourcePathSections.shift();
}
if (resourcePathSections.length < 2) {
return false;
}
if (isPathParam(resourcePathSections[resourcePathSections.length - 1])) {
resourcePathSections = resourcePathSections.slice(0, resourcePathSections.length - 2);
}
if (resourcePathSections.length === 1) {
return false;
const sections = path.split('/').filter((section) => section.length > 0);
// Normalize an instance path down to its collection path, so that
// '/parent/{parentId}/child/{childId}' is evaluated as '/parent/{parentId}/child'
if (sections.length > 0 && isPathParam(sections[sections.length - 1])) {
sections.pop();
}
const parentResourceSection = resourcePathSections[resourcePathSections.length - 2];
return isPathParam(parentResourceSection);
// The resource name is last, so the section before it holds the parent's id if there is a parent
return sections.length > 1 && isPathParam(sections[sections.length - 2]);
}

// TODO move prefixes to be rule arguments
Expand Down
Loading