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
@@ -0,0 +1,60 @@
import { describe, expect, it } from '@jest/globals';
import { extractTeamOwnership, getSeverityPerRule } from '../../metrics/utils/metricCollectionUtils.js';

describe('tools/spectral/ipa/metrics/utils/metricCollectionUtils.js', () => {
describe('getSeverityPerRule', () => {
const testRuleSet = {
rules: {
rule1: {
definition: {
severity: 1,
},
},
rule2: {
definition: {
severity: 2,
},
},
},
};
it('maps the rule severities correctly', () => {
expect(getSeverityPerRule(testRuleSet)).toEqual({
rule1: 1,
rule2: 2,
});
});
});

describe('extractTeamOwnership', () => {
const testOas = {
paths: {
'/resource1': {
get: {
description: 'get resource',
'x-xgen-owner-team': 'team1',
},
post: {
description: 'create resource',
'x-xgen-owner-team': 'team1',
},
},
'/resource2': {
get: {
description: 'get resource',
'x-xgen-owner-team': 'team2',
},
post: {
description: 'create resource',
'x-xgen-owner-team': 'team2',
},
},
},
};
it('maps the path ownership correctly', () => {
expect(extractTeamOwnership(testOas)).toEqual({
'/resource1': 'team1',
'/resource2': 'team2',
});
});
});
});
149 changes: 149 additions & 0 deletions tools/spectral/ipa/__tests__/utils/componentUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { describe, expect, it } from '@jest/globals';
import {
isPathParam,
pathIsForRequestVersion,
pathIsForResponseVersion,
resolveObject,
} from '../../rulesets/functions/utils/componentUtils.js';

describe('tools/spectral/ipa/rulesets/functions/utils/componentUtils.js', () => {
describe('isPathParam', () => {
it('returns true if the string is a path param', () => {
expect(isPathParam('{id}')).toEqual(true);
});
it('returns true if the string is a path param with a custom method', () => {
expect(isPathParam('{id}:custom')).toEqual(true);
});
it('returns false if the string is not a path param', () => {
expect(isPathParam('resource')).toEqual(false);
expect(isPathParam('resource:custom')).toEqual(false);
});
});

describe('resolveObject', () => {
const testOas = {
paths: {
'/resource': {
get: {
description: 'get resource',
},
},
},
components: {
schemas: {
MySchema: {
properties: {
fieldName: { type: 'string' },
},
},
},
},
};

it('resolves the OAS component based on the path', () => {
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema'])).toEqual(
testOas.components.schemas.MySchema
);
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema', 'properties', 'fieldName'])).toEqual(
testOas.components.schemas.MySchema.properties.fieldName
);
expect(resolveObject(testOas, ['paths', '/resource', 'get'])).toEqual(testOas.paths['/resource'].get);
});
it('returns undefined if the OAS does not contain the component based on the path', () => {
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema2'])).toEqual(undefined);
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema', 'properties', 'fieldName2'])).toEqual(
undefined
);
expect(resolveObject(testOas, ['paths', '/resource/{id}', 'get'])).toEqual(undefined);
});
});

describe('pathIsForResponseVersion', () => {
it('returns true for a path for a response version', () => {
expect(
pathIsForResponseVersion([
'paths',
'/resource/{id}',
'get',
'responses',
'200',
'content',
'application/vnd.atlas.2023-08-05+json',
])
).toEqual(true);
});
it('returns true for a path for a schema in a response version', () => {
expect(
pathIsForResponseVersion([
'paths',
'/resource/{id}',
'get',
'responses',
'200',
'content',
'application/vnd.atlas.2023-08-05+json',
'schema',
])
).toEqual(true);
});
it('returns false for a path for a schema in a request version', () => {
expect(
pathIsForResponseVersion([
'paths',
'/resource/{id}',
'get',
'requestBody',
'content',
'application/vnd.atlas.2023-08-05+json',
])
).toEqual(false);
});
it('returns false for a path for a schema in components', () => {
expect(pathIsForResponseVersion(['components', 'schemas', 'ExampleSchema'])).toEqual(false);
});
});

describe('pathIsForRequestVersion', () => {
it('returns true for a request version', () => {
expect(
pathIsForRequestVersion([
'paths',
'/resource/{id}',
'get',
'requestBody',
'content',
'application/vnd.atlas.2023-08-05+json',
])
).toEqual(true);
});
it('returns true for a path for a schema in a request version', () => {
expect(
pathIsForRequestVersion([
'paths',
'/resource/{id}',
'get',
'requestBody',
'content',
'application/vnd.atlas.2023-08-05+json',
'schema',
])
).toEqual(true);
});
it('returns false for a path for a response version', () => {
expect(
pathIsForRequestVersion([
'paths',
'/resource/{id}',
'get',
'responses',
'200',
'content',
'application/vnd.atlas.2023-08-05+json',
])
).toEqual(false);
});
it('returns false for a path for a schema in components', () => {
expect(pathIsForRequestVersion(['components', 'schemas', 'ExampleSchema'])).toEqual(false);
});
});
});
39 changes: 20 additions & 19 deletions tools/spectral/ipa/__tests__/utils/extensions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,23 @@ import {
hasMethodVerbOverride,
hasOperationIdOverride,
getOperationIdOverride,
hasVerbOverride,
} from '../../rulesets/functions/utils/extensions';

const methodWithExtension = {
const operationWithVerbOverride = {
'x-xgen-method-verb-override': {
verb: 'get',
customMethod: false,
},
};

const customMethod = {
const operationWithCustomMethodVerbOverride = {
'x-xgen-method-verb-override': {
verb: 'add',
customMethod: true,
},
};

const endpointWithMethodExtension = {
delete: {
'x-xgen-method-verb-override': { verb: 'remove', customMethod: true },
},
};

const endpointWithNoMethodExtension = {
exception: true,
};

const operationWithOperationIdOverride = {
operationId: 'operationId',
'x-xgen-operation-id-override': 'customOperationId',
Expand All @@ -40,32 +31,32 @@ const operationWithEmptyOperationIdOverride = {
'x-xgen-operation-id-override': '',
};

const operationWithNoOperationIdOverride = {
const operationWithNoOverrides = {
operationId: 'operationId',
};

describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
describe('hasCustomMethodOverride', () => {
it('returns true if the method has the extension with the customMethod boolean set to true', () => {
expect(hasCustomMethodOverride(customMethod)).toBe(true);
expect(hasCustomMethodOverride(operationWithCustomMethodVerbOverride)).toBe(true);
});
it('returns false if the method does not have the extension', () => {
expect(hasCustomMethodOverride({})).toBe(false);
});
it('returns false if the method has the extension but is not a custom method', () => {
expect(hasCustomMethodOverride(methodWithExtension)).toBe(false);
expect(hasCustomMethodOverride(operationWithVerbOverride)).toBe(false);
});
});

describe('hasMethodVerbOverride', () => {
it('returns true if the method has the extension with the expected verb', () => {
expect(hasMethodVerbOverride(methodWithExtension, 'get')).toBe(true);
expect(hasMethodVerbOverride(operationWithVerbOverride, 'get')).toBe(true);
});
it('returns false if the method does not have the extension', () => {
expect(hasMethodVerbOverride({}, 'get')).toBe(false);
});
it('returns false if the method has the extension but with an unexpected verb', () => {
expect(hasMethodVerbOverride(methodWithExtension, 'put')).toBe(false);
expect(hasMethodVerbOverride(operationWithVerbOverride, 'put')).toBe(false);
});
});

Expand All @@ -77,7 +68,7 @@ describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
expect(hasOperationIdOverride(operationWithEmptyOperationIdOverride)).toBe(true);
});
it('returns false if the method does not have the extension', () => {
expect(hasOperationIdOverride(operationWithNoOperationIdOverride)).toBe(false);
expect(hasOperationIdOverride(operationWithNoOverrides)).toBe(false);
});
});

Expand All @@ -89,7 +80,17 @@ describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
expect(getOperationIdOverride(operationWithEmptyOperationIdOverride)).toBe('');
});
it('returns undefined if the method does not have the extension', () => {
expect(getOperationIdOverride(operationWithNoOperationIdOverride)).toBe(undefined);
expect(getOperationIdOverride(operationWithNoOverrides)).toBe(undefined);
});
});

describe('hasVerbOverride', () => {
it('returns true if the method has the extension', () => {
expect(hasVerbOverride(operationWithVerbOverride)).toBe(true);
expect(hasVerbOverride(operationWithCustomMethodVerbOverride)).toBe(true);
});
it('returns false if the method does not have the extension', () => {
expect(hasVerbOverride(operationWithNoOverrides)).toBe(false);
});
});
});
Loading
Loading