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
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,169 @@ testRule('xgen-IPA-106-readonly-resource-should-not-have-create-method', [
},
errors: [],
},
{
name: 'valid: resource with create method and request body parity exception',
document: {
openapi: '3.0.0',
paths: {
'/resource': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
post: {
requestBody: {
content: {
'application/vnd.atlas.2023-01-01+json': {
'x-xgen-IPA-exception': {
'xgen-IPA-106-create-method-request-body-is-get-method-response': 'Request body takes firstName',
},
schema: {
type: 'object',
properties: {
firstName: { type: 'integer' },
},
},
},
},
},
responses: {
201: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
},
'/resource/{id}': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
},
},
},
errors: [],
},
{
name: 'invalid: read-only resource with create method and an unrelated exception on the request body content',
document: {
openapi: '3.0.0',
paths: {
'/resource': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
post: {
requestBody: {
content: {
'application/vnd.atlas.2023-01-01+json': {
'x-xgen-IPA-exception': {
'xgen-IPA-106-create-method-request-has-no-readonly-fields': 'Unrelated exception',
},
schema: {
type: 'object',
properties: {
firstName: { type: 'integer' },
},
},
},
},
},
responses: {
201: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
},
'/resource/{id}': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-106-readonly-resource-should-not-have-create-method',
message: 'Read-only resources must not define the Create method.',
path: ['paths', '/resource', 'post'],
severity: DiagnosticSeverity.Error,
},
],
},
]);
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,169 @@ testRule('xgen-IPA-108-readonly-resource-should-not-have-delete-method', [
},
errors: [],
},
{
name: 'valid: resource with create is allowed a delete method',
document: {
openapi: '3.0.0',
paths: {
'/resource': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
post: {
requestBody: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: { type: 'object', properties: { name: { type: 'string' } } },
},
},
},
responses: {
201: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
},
'/resource/{id}': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
delete: {
responses: {
204: {
description: 'No Content',
},
},
},
},
},
},
errors: [],
},
{
name: 'invalid: read-only resource with only a custom method should not have a delete method',
document: {
openapi: '3.0.0',
paths: {
'/readOnlyResource': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
},
'/readOnlyResource/{id}': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
delete: {
responses: {
204: {
description: 'No Content',
},
},
},
},
'/readOnlyResource/{id}:reset': {
post: {
requestBody: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: { type: 'object', properties: { name: { type: 'string' } } },
},
},
},
responses: {
201: {
content: {
'application/vnd.atlas.2023-01-01+json': {
schema: {
type: 'object',
properties: {
id: { type: 'string', readOnly: true },
name: { type: 'string', readOnly: true },
},
},
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-108-readonly-resource-should-not-have-delete-method',
message: 'Read-only resources must not define the Delete method.',
path: ['paths', '/readOnlyResource/{id}', 'delete'],
severity: DiagnosticSeverity.Error,
},
],
},
]);
44 changes: 44 additions & 0 deletions tools/spectral/ipa/__tests__/utils/resourceEvaluation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from '@jest/globals';
import {
allPropertiesAreReadOnly,
getResourcePathItems,
hasStandardCreateMethod,
isReadOnlyResource,
isResourceCollectionIdentifier,
isRootLevelResource,
Expand Down Expand Up @@ -643,4 +644,47 @@ describe('tools/spectral/ipa/rulesets/functions/utils/resourceEvaluation.js', ()
expect(resourceBelongsToSingleParent('/a/{b}/{c}')).toBe(false);
});
});

describe('hasStandardCreateMethod', () => {
const paths = {
'/resource': { get: {}, post: {} },
'/resource/{id}': { get: {}, delete: {} },
'/resource/{id}:reset': { post: {} },
'/resourceWithoutCreate': { get: {} },
'/resourceWithoutCreate/{id}': { get: {}, delete: {} },
'/resourceWithoutCreate/{id}:reset': { post: {} },
'/resourceWithoutCreate:search': { post: {} },
'/groups/{groupId}/singleton': { get: {}, patch: {} },
'/groups/{groupId}/nested': { get: {}, post: {} },
'/groups/{groupId}/nested/{nestedId}': { get: {}, delete: {} },
};

it('returns true for a resource collection path with a POST method', () => {
expect(hasStandardCreateMethod('/resource', paths)).toBe(true);
});

it('returns true for a single resource path when its collection path has a POST method', () => {
expect(hasStandardCreateMethod('/resource/{id}', paths)).toBe(true);
expect(hasStandardCreateMethod('/groups/{groupId}/nested/{nestedId}', paths)).toBe(true);
});

it('returns false when the collection path has no POST method', () => {
expect(hasStandardCreateMethod('/resourceWithoutCreate', paths)).toBe(false);
expect(hasStandardCreateMethod('/resourceWithoutCreate/{id}', paths)).toBe(false);
});

it('does not treat custom methods as a Create method', () => {
expect(hasStandardCreateMethod('/resource/{id}:reset', paths)).toBe(false);
expect(hasStandardCreateMethod('/resourceWithoutCreate/{id}:reset', paths)).toBe(false);
expect(hasStandardCreateMethod('/resourceWithoutCreate:search', paths)).toBe(false);
});

it('returns false for a singleton path without a POST method', () => {
expect(hasStandardCreateMethod('/groups/{groupId}/singleton', paths)).toBe(false);
});

it('returns false when the collection path does not exist', () => {
expect(hasStandardCreateMethod('/missing/{id}', paths)).toBe(false);
});
});
});
Loading
Loading