Rename 'commit_parent' in 'commits' table to 'commit_previous_commit'.
[WebKit-https.git] / Websites / perf.webkit.org / server-tests / api-commits.js
1 'use strict';
2
3 const assert = require('assert');
4
5 const TestServer = require('./resources/test-server.js');
6 const addSlaveForReport = require('./resources/common-operations.js').addSlaveForReport;
7 const connectToDatabaseInEveryTest = require('./resources/common-operations.js').connectToDatabaseInEveryTest;
8
9 describe("/api/commits/", () => {
10     TestServer.inject();
11     connectToDatabaseInEveryTest();
12
13     const subversionCommits = {
14         "slaveName": "someSlave",
15         "slavePassword": "somePassword",
16         "commits": [
17             {
18                 "repository": "WebKit",
19                 "revision": "210948",
20                 "time": "2017-01-20T02:52:34.577Z",
21                 "author": {"name": "Zalan Bujtas", "account": "zalan@apple.com"},
22                 "message": "a message",
23             },
24             {
25                 "repository": "WebKit",
26                 "revision": "210949",
27                 "time": "2017-01-20T03:23:50.645Z",
28                 "author": {"name": "Chris Dumez", "account": "cdumez@apple.com"},
29                 "message": "some message",
30             },
31             {
32                 "repository": "WebKit",
33                 "previousCommit": "210949",
34                 "revision": "210950",
35                 "time": "2017-01-20T03:49:37.887Z",
36                 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
37                 "message": "another message",
38             }
39         ]
40     }
41
42     const notYetReportedCommit = {
43         revision: '210951',
44         time: '2017-01-20T03:56:20.045Z'
45     }
46
47     function assertCommitIsSameAsOneSubmitted(commit, submitted)
48     {
49         assert.equal(commit['revision'], submitted['revision']);
50         assert.equal(new Date(commit['time']).toISOString(), submitted['time']);
51         assert.equal(commit['message'], submitted['message']);
52         assert.equal(commit['authorName'], submitted['author']['name']);
53         assert.equal(commit['authorEmail'], submitted['author']['account']);
54         if(submitted['previousCommit']) {
55             assert.ok(commit['previousCommit']);
56         } else {
57             assert.equal(commit['previousCommit'], null);
58         }
59     }
60
61     describe('/api/commits/<repository>/', () => {
62         it("should return RepositoryNotFound when there are no matching repository", () => {
63             return TestServer.remoteAPI().getJSON('/api/commits/WebKit').then((response) => {
64                 assert.equal(response['status'], 'RepositoryNotFound');
65             });
66         });
67
68         it("should return an empty result when there are no reported commits", () => {
69             const db = TestServer.database();
70             return Promise.all([
71                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
72                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
73             ]).then(() => {
74                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit');
75             }).then((response) => {
76                 assert.equal(response['status'], 'OK');
77                 assert.deepEqual(response['commits'], []);
78             });
79         });
80
81         it("should return the list of all commits for a given repository", () => {
82             return addSlaveForReport(subversionCommits).then(() => {
83                 return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommits);
84             }).then(function (response) {
85                 assert.equal(response['status'], 'OK');
86                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/');
87             }).then(function (result) {
88                 assert.equal(result['status'], 'OK');
89
90                 const commits = result['commits'];
91                 assert.equal(commits.length, 3);
92                 const submittedCommits = subversionCommits['commits'];
93                 assertCommitIsSameAsOneSubmitted(commits[0], submittedCommits[0]);
94                 assertCommitIsSameAsOneSubmitted(commits[1], submittedCommits[1]);
95                 assertCommitIsSameAsOneSubmitted(commits[2], submittedCommits[2]);
96                 assert.equal(commits[2]['previousCommit'], commits[1]['id']);
97             });
98         });
99     });
100
101     describe('/api/commits/<repository>/oldest', () => {
102         it("should return RepositoryNotFound when there are no matching repository", () => {
103             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/oldest').then((response) => {
104                 assert.equal(response['status'], 'RepositoryNotFound');
105             });
106         });
107
108         it("should return an empty results when there are no commits", () => {
109             return TestServer.database().insert('repositories', {'id': 1, 'name': 'WebKit'}).then(() => {
110                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/oldest');
111             }).then((response) => {
112                 assert.equal(response['status'], 'OK');
113                 assert.deepEqual(response['commits'], []);
114             });
115         });
116
117         it("should return the oldest commit", () => {
118             const remote = TestServer.remoteAPI();
119             return addSlaveForReport(subversionCommits).then(() => {
120                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
121             }).then(() => {
122                 return remote.getJSON('/api/commits/WebKit/oldest');
123             }).then(function (result) {
124                 assert.equal(result['status'], 'OK');
125                 assert.equal(result['commits'].length, 1);
126                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'][0]);
127             });
128         });
129     });
130
131     describe('/api/commits/<repository>/latest', () => {
132         it("should return RepositoryNotFound when there are no matching repository", () => {
133             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/latest').then((response) => {
134                 assert.equal(response['status'], 'RepositoryNotFound');
135             });
136         });
137
138         it("should return an empty results when there are no commits", () => {
139             return TestServer.database().insert('repositories', {'id': 1, 'name': 'WebKit'}).then(() => {
140                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/latest');
141             }).then((response) => {
142                 assert.equal(response['status'], 'OK');
143                 assert.deepEqual(response['commits'], []);
144             });
145         });
146
147         it("should return the oldest commit", () => {
148             const remote = TestServer.remoteAPI();
149             return addSlaveForReport(subversionCommits).then(() => {
150                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
151             }).then(() => {
152                 return remote.getJSON('/api/commits/WebKit/latest');
153             }).then(function (result) {
154                 assert.equal(result['status'], 'OK');
155                 assert.equal(result['commits'].length, 1);
156                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'].slice().pop());
157             });
158         });
159     });
160
161     describe('/api/commits/<repository>/last-reported', () => {
162         it("should return RepositoryNotFound when there are no matching repository", () => {
163             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/last-reported').then((response) => {
164                 assert.equal(response['status'], 'RepositoryNotFound');
165             });
166         });
167
168         it("should return an empty result when there are no reported commits", () => {
169             const db = TestServer.database();
170             return Promise.all([
171                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
172                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
173             ]).then(() => {
174                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/last-reported');
175             }).then((response) => {
176                 assert.equal(response['status'], 'OK');
177                 assert.deepEqual(response['commits'], []);
178             });
179         });
180
181         it("should return an empty results when there are no reported commits", () => {
182             return TestServer.database().insert('repositories', {'id': 1, 'name': 'WebKit'}).then(() => {
183                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/last-reported');
184             }).then((response) => {
185                 assert.equal(response['status'], 'OK');
186                 assert.deepEqual(response['commits'], []);
187             });
188         });
189
190         it("should return the oldest reported commit", () => {
191             const db = TestServer.database();
192             const remote = TestServer.remoteAPI();
193             return Promise.all([
194                 addSlaveForReport(subversionCommits),
195                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
196                 db.insert('commits', {'repository': 1, 'revision': notYetReportedCommit.revision, 'time': notYetReportedCommit.time}),
197             ]).then(() => {
198                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
199             }).then(() => {
200                 return remote.getJSON('/api/commits/WebKit/last-reported');
201             }).then(function (result) {
202                 assert.equal(result['status'], 'OK');
203                 assert.equal(result['commits'].length, 1);
204                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'].slice().pop());
205             });
206         });
207     });
208
209     describe('/api/commits/<repository>/<commit>', () => {
210         it("should return RepositoryNotFound when there are no matching repository", () => {
211             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/210949').then((response) => {
212                 assert.equal(response['status'], 'RepositoryNotFound');
213             });
214         });
215
216         it("should return UnknownCommit when one of the specified commit does not exist in the database", () => {
217             const db = TestServer.database();
218             return Promise.all([
219                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
220                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
221             ]).then(() => {
222                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/210949');
223             }).then((response) => {
224                 assert.equal(response['status'], 'UnknownCommit');
225             });
226         });
227
228         it("should return the commit even if it had not been reported", () => {
229             const db = TestServer.database();
230             return Promise.all([
231                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
232                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
233             ]).then(() => {
234                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/210950');
235             }).then((result) => {
236                 assert.equal(result['status'], 'OK');
237                 assert.equal(result['commits'].length, 1);
238                 assertCommitIsSameAsOneSubmitted(result['commits'][0], {
239                     previousCommit: null,
240                     revision: '210950',
241                     time: '2017-01-20T03:49:37.887Z',
242                     author: {name: null, account: null},
243                     message: null,
244                 });
245             });
246         });
247
248         it("should return the full result for a reported commit", () => {
249             const remote = TestServer.remoteAPI();
250             return addSlaveForReport(subversionCommits).then(() => {
251                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
252             }).then(() => {
253                 return remote.getJSON('/api/commits/WebKit/210949');
254             }).then((result) => {
255                 assert.equal(result['status'], 'OK');
256                 assert.deepEqual(result['commits'].length, 1);
257                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'][1]);
258             });
259         });
260
261     });
262
263     describe('/api/commits/<repository>/?from=<commit-1>&to=<commit-2>', () => {
264         it("should return RepositoryNotFound when there are no matching repository", () => {
265             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210900&to=211000').then((response) => {
266                 assert.equal(response['status'], 'RepositoryNotFound');
267             });
268         });
269
270         it("should return UnknownCommit when one of the specified commit does not exist in the database", () => {
271             const db = TestServer.database();
272             return Promise.all([
273                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
274                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
275             ]).then(() => {
276                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210900&to=211000');
277             }).then((response) => {
278                 assert.equal(response['status'], 'UnknownCommit');
279             });
280         });
281
282         it("should return an empty result when commits in the specified range have not reported", () => {
283             const db = TestServer.database();
284             return Promise.all([
285                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
286                 db.insert('commits', {'repository': 1, 'revision': '210949', 'time': '2017-01-20T03:23:50.645Z'}),
287                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'}),
288             ]).then(() => {
289                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210949&to=210950');
290             }).then((response) => {
291                 assert.equal(response['status'], 'OK');
292                 assert.deepEqual(response['commits'], []);
293             });
294         });
295
296         it("should return reported commits in the specified range", () => {
297             const db = TestServer.database();
298             return Promise.all([
299                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
300                 db.insert('commits', {'repository': 1, 'revision': '210949', 'time': '2017-01-20T03:23:50.645Z', 'reported': true}),
301                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z', 'reported': true}),
302             ]).then(() => {
303                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210949&to=210950');
304             }).then((result) => {
305                 assert.equal(result['status'], 'OK');
306                 assert.deepEqual(result['commits'].length, 2);
307                 assertCommitIsSameAsOneSubmitted(result['commits'][0], {
308                     previousCommit: null,
309                     revision: '210949',
310                     time: '2017-01-20T03:23:50.645Z',
311                     author: {name: null, account: null},
312                     message: null,
313                 });
314                 assertCommitIsSameAsOneSubmitted(result['commits'][1], {
315                     previousCommit: null,
316                     revision: '210950',
317                     time: '2017-01-20T03:49:37.887Z',
318                     author: {name: null, account: null},
319                     message: null,
320                 });
321             });
322         });
323
324         it("should not include a revision not within the specified range", () => {
325             const remote = TestServer.remoteAPI();
326             return addSlaveForReport(subversionCommits).then(() => {
327                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
328             }).then(() => {
329                 return remote.getJSON('/api/commits/WebKit/?from=210948&to=210949');
330             }).then((result) => {
331                 assert.equal(result['status'], 'OK');
332                 assert.deepEqual(result['commits'].length, 2);
333                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'][0]);
334                 assertCommitIsSameAsOneSubmitted(result['commits'][1], subversionCommits['commits'][1]);
335             });
336         });
337
338     });
339
340 });