Skip to content

Commit 50f7c4a

Browse files
test(ipa): add missing tests for IPA utils functions (#872)
1 parent 8f42476 commit 50f7c4a

File tree

9 files changed

+503
-46
lines changed

9 files changed

+503
-46
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { extractTeamOwnership, getSeverityPerRule } from '../../metrics/utils/metricCollectionUtils.js';
3+
4+
describe('tools/spectral/ipa/metrics/utils/metricCollectionUtils.js', () => {
5+
describe('getSeverityPerRule', () => {
6+
const testRuleSet = {
7+
rules: {
8+
rule1: {
9+
definition: {
10+
severity: 1,
11+
},
12+
},
13+
rule2: {
14+
definition: {
15+
severity: 2,
16+
},
17+
},
18+
},
19+
};
20+
it('maps the rule severities correctly', () => {
21+
expect(getSeverityPerRule(testRuleSet)).toEqual({
22+
rule1: 1,
23+
rule2: 2,
24+
});
25+
});
26+
});
27+
28+
describe('extractTeamOwnership', () => {
29+
const testOas = {
30+
paths: {
31+
'/resource1': {
32+
get: {
33+
description: 'get resource',
34+
'x-xgen-owner-team': 'team1',
35+
},
36+
post: {
37+
description: 'create resource',
38+
'x-xgen-owner-team': 'team1',
39+
},
40+
},
41+
'/resource2': {
42+
get: {
43+
description: 'get resource',
44+
'x-xgen-owner-team': 'team2',
45+
},
46+
post: {
47+
description: 'create resource',
48+
'x-xgen-owner-team': 'team2',
49+
},
50+
},
51+
},
52+
};
53+
it('maps the path ownership correctly', () => {
54+
expect(extractTeamOwnership(testOas)).toEqual({
55+
'/resource1': 'team1',
56+
'/resource2': 'team2',
57+
});
58+
});
59+
});
60+
});
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import {
3+
isPathParam,
4+
pathIsForRequestVersion,
5+
pathIsForResponseVersion,
6+
resolveObject,
7+
} from '../../rulesets/functions/utils/componentUtils.js';
8+
9+
describe('tools/spectral/ipa/rulesets/functions/utils/componentUtils.js', () => {
10+
describe('isPathParam', () => {
11+
it('returns true if the string is a path param', () => {
12+
expect(isPathParam('{id}')).toEqual(true);
13+
});
14+
it('returns true if the string is a path param with a custom method', () => {
15+
expect(isPathParam('{id}:custom')).toEqual(true);
16+
});
17+
it('returns false if the string is not a path param', () => {
18+
expect(isPathParam('resource')).toEqual(false);
19+
expect(isPathParam('resource:custom')).toEqual(false);
20+
});
21+
});
22+
23+
describe('resolveObject', () => {
24+
const testOas = {
25+
paths: {
26+
'/resource': {
27+
get: {
28+
description: 'get resource',
29+
},
30+
},
31+
},
32+
components: {
33+
schemas: {
34+
MySchema: {
35+
properties: {
36+
fieldName: { type: 'string' },
37+
},
38+
},
39+
},
40+
},
41+
};
42+
43+
it('resolves the OAS component based on the path', () => {
44+
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema'])).toEqual(
45+
testOas.components.schemas.MySchema
46+
);
47+
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema', 'properties', 'fieldName'])).toEqual(
48+
testOas.components.schemas.MySchema.properties.fieldName
49+
);
50+
expect(resolveObject(testOas, ['paths', '/resource', 'get'])).toEqual(testOas.paths['/resource'].get);
51+
});
52+
it('returns undefined if the OAS does not contain the component based on the path', () => {
53+
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema2'])).toEqual(undefined);
54+
expect(resolveObject(testOas, ['components', 'schemas', 'MySchema', 'properties', 'fieldName2'])).toEqual(
55+
undefined
56+
);
57+
expect(resolveObject(testOas, ['paths', '/resource/{id}', 'get'])).toEqual(undefined);
58+
});
59+
});
60+
61+
describe('pathIsForResponseVersion', () => {
62+
it('returns true for a path for a response version', () => {
63+
expect(
64+
pathIsForResponseVersion([
65+
'paths',
66+
'/resource/{id}',
67+
'get',
68+
'responses',
69+
'200',
70+
'content',
71+
'application/vnd.atlas.2023-08-05+json',
72+
])
73+
).toEqual(true);
74+
});
75+
it('returns true for a path for a schema in a response version', () => {
76+
expect(
77+
pathIsForResponseVersion([
78+
'paths',
79+
'/resource/{id}',
80+
'get',
81+
'responses',
82+
'200',
83+
'content',
84+
'application/vnd.atlas.2023-08-05+json',
85+
'schema',
86+
])
87+
).toEqual(true);
88+
});
89+
it('returns false for a path for a schema in a request version', () => {
90+
expect(
91+
pathIsForResponseVersion([
92+
'paths',
93+
'/resource/{id}',
94+
'get',
95+
'requestBody',
96+
'content',
97+
'application/vnd.atlas.2023-08-05+json',
98+
])
99+
).toEqual(false);
100+
});
101+
it('returns false for a path for a schema in components', () => {
102+
expect(pathIsForResponseVersion(['components', 'schemas', 'ExampleSchema'])).toEqual(false);
103+
});
104+
});
105+
106+
describe('pathIsForRequestVersion', () => {
107+
it('returns true for a request version', () => {
108+
expect(
109+
pathIsForRequestVersion([
110+
'paths',
111+
'/resource/{id}',
112+
'get',
113+
'requestBody',
114+
'content',
115+
'application/vnd.atlas.2023-08-05+json',
116+
])
117+
).toEqual(true);
118+
});
119+
it('returns true for a path for a schema in a request version', () => {
120+
expect(
121+
pathIsForRequestVersion([
122+
'paths',
123+
'/resource/{id}',
124+
'get',
125+
'requestBody',
126+
'content',
127+
'application/vnd.atlas.2023-08-05+json',
128+
'schema',
129+
])
130+
).toEqual(true);
131+
});
132+
it('returns false for a path for a response version', () => {
133+
expect(
134+
pathIsForRequestVersion([
135+
'paths',
136+
'/resource/{id}',
137+
'get',
138+
'responses',
139+
'200',
140+
'content',
141+
'application/vnd.atlas.2023-08-05+json',
142+
])
143+
).toEqual(false);
144+
});
145+
it('returns false for a path for a schema in components', () => {
146+
expect(pathIsForRequestVersion(['components', 'schemas', 'ExampleSchema'])).toEqual(false);
147+
});
148+
});
149+
});

tools/spectral/ipa/__tests__/utils/extensions.test.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,23 @@ import {
44
hasMethodVerbOverride,
55
hasOperationIdOverride,
66
getOperationIdOverride,
7+
hasVerbOverride,
78
} from '../../rulesets/functions/utils/extensions';
89

9-
const methodWithExtension = {
10+
const operationWithVerbOverride = {
1011
'x-xgen-method-verb-override': {
1112
verb: 'get',
1213
customMethod: false,
1314
},
1415
};
1516

16-
const customMethod = {
17+
const operationWithCustomMethodVerbOverride = {
1718
'x-xgen-method-verb-override': {
1819
verb: 'add',
1920
customMethod: true,
2021
},
2122
};
2223

23-
const endpointWithMethodExtension = {
24-
delete: {
25-
'x-xgen-method-verb-override': { verb: 'remove', customMethod: true },
26-
},
27-
};
28-
29-
const endpointWithNoMethodExtension = {
30-
exception: true,
31-
};
32-
3324
const operationWithOperationIdOverride = {
3425
operationId: 'operationId',
3526
'x-xgen-operation-id-override': 'customOperationId',
@@ -40,32 +31,32 @@ const operationWithEmptyOperationIdOverride = {
4031
'x-xgen-operation-id-override': '',
4132
};
4233

43-
const operationWithNoOperationIdOverride = {
34+
const operationWithNoOverrides = {
4435
operationId: 'operationId',
4536
};
4637

4738
describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
4839
describe('hasCustomMethodOverride', () => {
4940
it('returns true if the method has the extension with the customMethod boolean set to true', () => {
50-
expect(hasCustomMethodOverride(customMethod)).toBe(true);
41+
expect(hasCustomMethodOverride(operationWithCustomMethodVerbOverride)).toBe(true);
5142
});
5243
it('returns false if the method does not have the extension', () => {
5344
expect(hasCustomMethodOverride({})).toBe(false);
5445
});
5546
it('returns false if the method has the extension but is not a custom method', () => {
56-
expect(hasCustomMethodOverride(methodWithExtension)).toBe(false);
47+
expect(hasCustomMethodOverride(operationWithVerbOverride)).toBe(false);
5748
});
5849
});
5950

6051
describe('hasMethodVerbOverride', () => {
6152
it('returns true if the method has the extension with the expected verb', () => {
62-
expect(hasMethodVerbOverride(methodWithExtension, 'get')).toBe(true);
53+
expect(hasMethodVerbOverride(operationWithVerbOverride, 'get')).toBe(true);
6354
});
6455
it('returns false if the method does not have the extension', () => {
6556
expect(hasMethodVerbOverride({}, 'get')).toBe(false);
6657
});
6758
it('returns false if the method has the extension but with an unexpected verb', () => {
68-
expect(hasMethodVerbOverride(methodWithExtension, 'put')).toBe(false);
59+
expect(hasMethodVerbOverride(operationWithVerbOverride, 'put')).toBe(false);
6960
});
7061
});
7162

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

@@ -89,7 +80,17 @@ describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
8980
expect(getOperationIdOverride(operationWithEmptyOperationIdOverride)).toBe('');
9081
});
9182
it('returns undefined if the method does not have the extension', () => {
92-
expect(getOperationIdOverride(operationWithNoOperationIdOverride)).toBe(undefined);
83+
expect(getOperationIdOverride(operationWithNoOverrides)).toBe(undefined);
84+
});
85+
});
86+
87+
describe('hasVerbOverride', () => {
88+
it('returns true if the method has the extension', () => {
89+
expect(hasVerbOverride(operationWithVerbOverride)).toBe(true);
90+
expect(hasVerbOverride(operationWithCustomMethodVerbOverride)).toBe(true);
91+
});
92+
it('returns false if the method does not have the extension', () => {
93+
expect(hasVerbOverride(operationWithNoOverrides)).toBe(false);
9394
});
9495
});
9596
});

0 commit comments

Comments
 (0)