-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-compliance.js
133 lines (121 loc) · 5.16 KB
/
api-compliance.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
import { check, fail, group } from 'k6';
import http from 'k6/http';
export const options = {
vus: 1,
thresholds: {
// Ensure we have 100% compliance on API tests
checks: [{ threshold: 'rate == 1.0', abortOnFail: true }],
},
};
var targetProtocol = "http"
if (__ENV.PROTOCOL !== undefined) {
targetProtocol = __ENV.PROTOCOL
}
var targetHost = "localhost"
if (__ENV.HOST !== undefined) {
targetHost = __ENV.HOST
}
var targetPort = "80"
if (__ENV.PORT !== undefined) {
targetPort = __ENV.PORT
}
const BASE_URL = `${targetProtocol}://${targetHost}:${targetPort}`;
export default () => {
const params = {
headers: {
'Content-Type': 'application/json',
},
};
let testId = -1;
const testName = `k6-${Date.now()}`;
const testDesc = 'API Compliance Test';
const testLat = 35.4183;
const testLon = 76.5517;
group('Initial listing check', function () {
const placesRes = http.get(`${BASE_URL}/api/places`)
check(placesRes, {
'fetch returns appropriate status': (resp) => resp.status === 200,
});
// Confirm we do not have a place having the testName
let places = placesRes.json();
for (var i = 0; i < places.length; i++) {
if (places[i].name === testName) {
fail(`Test named "${testName}" already exists`);
}
}
});
group('Create a new place', function () {
const createRes = http.post(`${BASE_URL}/api/places`, JSON.stringify({
name: testName,
description: testDesc,
latitude: testLat,
longitude: testLon,
}), params);
check(createRes, {
'create returns appropriate status': (resp) => resp.status === 200,
'and successfully creates a new place': (resp) => resp.json('id') !== '',
});
testId = createRes.json('id');
});
group('Retrieving a place', function () {
const placeRes = http.get(`${BASE_URL}/api/places/${testId}`);
check(placeRes, {
'retrieving by id is successful': (resp) => resp.status === 200,
});
check(placeRes.json(), {
'response provides attribute `id`': (place) => place.id === testId,
'response provides attribute `name`': (place) => place.name === testName,
'response provides attribute `description`': (place) => place.description === testDesc,
'response provides attribute `latitude`': (place) => place.latitude === testLat,
'response provides attribute `longitude`': (place) => place.longitude === testLon,
'response provides attribute `created_at``': (place) => place.created_at !== undefined && place.created_at !== '',
'response provides attribute `updated_at`': (place) => place.updated_at !== undefined && place.updated_at !== '',
});
// console.log("POST CREATE");
// console.log(JSON.stringify(placeRes.body));
// Ensure the place is returned in the list
const placesRes = http.get(`${BASE_URL}/api/places`)
let places = placesRes.json();
let found = false;
for (var i = 0; i < places.length; i++) {
if (places[i].id === testId) {
found = true;
break;
}
}
if (!found) {
fail('Test place was not returned when retrieving all places');
}
});
group('Update place by id', function () {
const patchRes = http.patch(`${BASE_URL}/api/places/${testId}`, JSON.stringify({
description: testDesc + " Updated"
}), params);
check(patchRes, {
'update returns appropriate status': (resp) => resp.status === 200,
});
check(patchRes.json(), {
'response provides attribute `id`': (place) => place.id === testId,
'response provides attribute `name`': (place) => place.name === testName,
'response provides modified attribute `description`': (place) => place.description === testDesc + " Updated",
'response provides attribute `latitude`': (place) => place.latitude === testLat,
'response provides attribute `longitude`': (place) => place.longitude === testLon,
'response provides attribute `created_at``': (place) => place.created_at !== undefined && place.created_at !== '',
'response provides attribute `updated_at`': (place) => place.updated_at !== undefined && place.updated_at !== '',
'update changes modification date': (place) => place.updated_at !== place.created_at,
});
// console.log("POST UPDATE");
// console.log(JSON.stringify(patchRes.body));
});
group('Delete place by id', function () {
const deleteRes = http.del(`${BASE_URL}/api/places/${testId}`)
check(deleteRes, {
'delete returns appropriate status': (resp) => resp.status === 200,
});
// Confirm that the place has been removed
const placeRes = http.get(`${BASE_URL}/api/places/${testId}`)
check(placeRes, {
'deleted place no longer available': (resp) => resp.status === 404,
});
});
}