-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
120 lines (89 loc) · 4.62 KB
/
test.js
File metadata and controls
120 lines (89 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const SwaggerParser = require('swagger-parser');
const {
checkPathAndOperation,
checkResponseBodyExists,
checkRequestBodyExists,
checkRequestBodyProperties,
checkResponseBodyProperties,
checkResponseBodyIsAnArray,
verifyCustomerSchema
} = require('./validationHelpers');
const apiSpecPath = 'openapidesign.json';
describe('OpenAPI Specification Validation', () => {
let apiSpec = undefined;
test('Validate the entire OpenAPI Specification', async () => {
apiSpec = await SwaggerParser.parse(apiSpecPath);
expect(apiSpec).toBeTruthy(); // Assert that the API Spec is truthy
});
test('Check that path /customers exists with operation GET', async () => {
const paths = apiSpec.paths;
const result = checkPathAndOperation(paths, { method: 'GET', path: '/customers' });
expect(result).toBeTruthy(); // Assert that the operation exists
});
test('Check that operation GET /customers has a response body', async () => {
const paths = apiSpec.paths;
const result = checkResponseBodyExists(paths, { method: 'GET', path: '/customers' });
expect(result).toBeTruthy(); // Assert that the operation has a response body
});
test('Check that operation GET /customers response body is an array', async () => {
const paths = apiSpec.paths;
const result = checkResponseBodyIsAnArray(paths, { method: 'GET', path: '/customers' });
expect(result).toBeTruthy(); // Assert that the operation has a response body
});
test('Check that the document includes a schema object for Customer', async () => {
const schemas = apiSpec.components.schemas;
const result = schemas.Customer;
expect(result).toBeTruthy(); // Assert that the schema exists
});
test('Check that Customer schema object includes properties id, name, address and contactPerson object with properties name, phoneNumber, email and role', async () => {
const schemas = apiSpec.components.schemas;
const result = verifyCustomerSchema(schemas);
expect(result).toBeTruthy(); // Assert that the schema has the specified properties
});
test('check GET /customers/{id} is defined', async () => {
const paths = apiSpec.paths;
const result = checkPathAndOperation(paths, { method: 'GET', path: '/customers/{id}' });
expect(result).toBeTruthy(); // Assert that the operation exists
});
test('check GET /customers/{id} has a response body', async () => {
const paths = apiSpec.paths;
const result = checkResponseBodyExists(paths, { method: 'GET', path: '/customers/{id}' });
expect(result).toBeTruthy(); // Assert that the operation has a response body
});
test('check GET /customers/{id} responses with Customer schema', async () => {
const paths = apiSpec.paths;
// Check path response for this operation
const targetOperation = { method: 'GET', path: '/customers/{id}' };
const responseBody = paths[targetOperation.path][targetOperation.method.toLowerCase()].responses['200'];
const bodyContent = responseBody.content;
const bodyContentApplicationJson = bodyContent['application/json'];
const bodyContentApplicationJsonSchema = bodyContentApplicationJson.schema;
if(bodyContentApplicationJsonSchema.$ref === '#/components/schemas/Customer') {
expect(true).toBeTruthy(); // Assert that the operation has a response body
} else {
expect(false).toBeTruthy(); // Assert that the operation has a response body
}
});
test('check PUT /customers/{id} is defined', async () => {
const paths = apiSpec.paths;
const result = checkPathAndOperation(paths, { method: 'PUT', path: '/customers/{id}' });
expect(result).toBeTruthy(); // Assert that the operation exists
});
test('check PUT /customers/{id} has a request body', async () => {
const paths = apiSpec.paths;
const result = checkRequestBodyExists(paths, { method: 'PUT', path: '/customers/{id}' });
expect(result).toBeTruthy(); // Assert that the operation has a request body
});
test('check DELETE /customers/{id} is defined', async () => {
const paths = apiSpec.paths;
const result = checkPathAndOperation(paths, { method: 'DELETE', path: '/customers/{id}' });
expect(result).toBeTruthy(); // Assert that the operation exists
});
test('check DELETE /customers/{id} specifies a path parameter named id', async () => {
const paths = apiSpec.paths;
const targetOperation = { method: 'DELETE', path: '/customers/{id}' };
const parameters = paths[targetOperation.path][targetOperation.method.toLowerCase()].parameters;
const result = parameters[0].name === 'id';
expect(result).toBeTruthy(); // Assert that the operation has a request body
});
});