-
Notifications
You must be signed in to change notification settings - Fork 60
/
test-apis.js
80 lines (71 loc) · 2.55 KB
/
test-apis.js
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
const { readdirSync, statSync } = require('fs')
const { join } = require('path')
const assert = require('assert');
// All the directories in specification
const dirs = readdirSync(__dirname).filter(f => statSync(join(__dirname, f)).isDirectory());
dirs.forEach(dir => {
const apiSpec = require(join(__dirname, dir, 'api.json'));
// Check service name
assert(
apiSpec.name
&& apiSpec.name === apiSpec.name.toLowerCase().replace(/\s/g, '')
&& !apiSpec.name.includes('_')
&& !apiSpec.name.endsWith('s')
&& apiSpec.name === dir,
'The service name must be in singular kebab case, ' +
'it must also be the same as the directory name'
);
// Check models
Object.keys(apiSpec.models).forEach(key => {
// Check model name is in snake case
assert(
key === key.toLowerCase().replace(/\s/g, '')
&& !key.includes('-'),
'The model names must be in snake case'
);
apiSpec.models[key].fields.forEach(field => {
// Check model fields are in camel case
assert(
field.name
&& field.name === field.name.replace(/\s/g, '')
&& !field.name.includes('_')
&& !field.name.includes('-'),
'The field names must be in camel case'
);
});
});
// Check resources
Object.keys(apiSpec.resources).forEach(key => {
apiSpec.resources[key].operations.forEach(operation => {
// Check description contain the information about the methodName to use
const regex = RegExp('.*\\; methodName: .*');
assert(
operation.description
&& regex.test(operation.description),
'The operation description must contains a description ' +
'following by "; methodName: <method name>" where <method name> is the ' +
'method name that will be used in the controller of the service for this operation'
);
if (operation.path && operation.path.includes('/:id')) {
// Check if we have a 404 response
let found = false;
Object.keys(operation.responses).forEach(response => {
if (response === '404') {
found = true;
}
});
assert(found, 'When looking for a resource by id, it must contains a 404 response');
}
if (operation.body) {
// Check if we have a 400 response
let found = false;
Object.keys(operation.responses).forEach(response => {
if (response === '400') {
found = true;
}
});
assert(found, 'When sending a body in an operation, it must contains a 400 response');
}
});
});
});