a91465317f553413a32c0b0cd61022b697f3d85d
[WebKit.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                 "parent": "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     }
55
56     describe('/api/commits/<repository>/', () => {
57         it("should return RepositoryNotFound when there are no matching repository", () => {
58             return TestServer.remoteAPI().getJSON('/api/commits/WebKit').then((response) => {
59                 assert.equal(response['status'], 'RepositoryNotFound');
60             });
61         });
62
63         it("should return an empty result when there are no reported commits", () => {
64             const db = TestServer.database();
65             return Promise.all([
66                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
67                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
68             ]).then(() => {
69                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit');
70             }).then((response) => {
71                 assert.equal(response['status'], 'OK');
72                 assert.deepEqual(response['commits'], []);
73             });
74         });
75
76         it("should return the list of all commits for a given repository", () => {
77             return addSlaveForReport(subversionCommits).then(() => {
78                 return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommits);
79             }).then(function (response) {
80                 assert.equal(response['status'], 'OK');
81                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/');
82             }).then(function (result) {
83                 assert.equal(result['status'], 'OK');
84
85                 const commits = result['commits'];
86                 assert.equal(commits.length, 3);
87                 const submittedCommits = subversionCommits['commits'];
88                 assertCommitIsSameAsOneSubmitted(commits[0], submittedCommits[0]);
89                 assertCommitIsSameAsOneSubmitted(commits[1], submittedCommits[1]);
90                 assertCommitIsSameAsOneSubmitted(commits[2], submittedCommits[2]);
91             });
92         });        
93     });
94
95     describe('/api/commits/<repository>/oldest', () => {
96         it("should return RepositoryNotFound when there are no matching repository", () => {
97             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/oldest').then((response) => {
98                 assert.equal(response['status'], 'RepositoryNotFound');
99             });
100         });
101
102         it("should return an empty results when there are no commits", () => {
103             return TestServer.database().insert('repositories', {'id': 1, 'name': 'WebKit'}).then(() => {
104                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/oldest');
105             }).then((response) => {
106                 assert.equal(response['status'], 'OK');
107                 assert.deepEqual(response['commits'], []);
108             });
109         });
110
111         it("should return the oldest commit", () => {
112             const remote = TestServer.remoteAPI();
113             return addSlaveForReport(subversionCommits).then(() => {
114                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
115             }).then(() => {
116                 return remote.getJSON('/api/commits/WebKit/oldest');
117             }).then(function (result) {
118                 assert.equal(result['status'], 'OK');
119                 assert.equal(result['commits'].length, 1);
120                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'][0]);
121             });
122         });
123     });
124
125     describe('/api/commits/<repository>/latest', () => {
126         it("should return RepositoryNotFound when there are no matching repository", () => {
127             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/latest').then((response) => {
128                 assert.equal(response['status'], 'RepositoryNotFound');
129             });
130         });
131
132         it("should return an empty results when there are no commits", () => {
133             return TestServer.database().insert('repositories', {'id': 1, 'name': 'WebKit'}).then(() => {
134                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/latest');
135             }).then((response) => {
136                 assert.equal(response['status'], 'OK');
137                 assert.deepEqual(response['commits'], []);
138             });
139         });
140
141         it("should return the oldest commit", () => {
142             const remote = TestServer.remoteAPI();
143             return addSlaveForReport(subversionCommits).then(() => {
144                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
145             }).then(() => {
146                 return remote.getJSON('/api/commits/WebKit/latest');
147             }).then(function (result) {
148                 assert.equal(result['status'], 'OK');
149                 assert.equal(result['commits'].length, 1);
150                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'].slice().pop());
151             });
152         });
153     });
154
155     describe('/api/commits/<repository>/last-reported', () => {
156         it("should return RepositoryNotFound when there are no matching repository", () => {
157             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/last-reported').then((response) => {
158                 assert.equal(response['status'], 'RepositoryNotFound');
159             });
160         });
161
162         it("should return an empty result when there are no reported commits", () => {
163             const db = TestServer.database();
164             return Promise.all([
165                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
166                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
167             ]).then(() => {
168                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/last-reported');
169             }).then((response) => {
170                 assert.equal(response['status'], 'OK');
171                 assert.deepEqual(response['commits'], []);
172             });
173         });
174
175         it("should return an empty results when there are no reported commits", () => {
176             return TestServer.database().insert('repositories', {'id': 1, 'name': 'WebKit'}).then(() => {
177                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/last-reported');
178             }).then((response) => {
179                 assert.equal(response['status'], 'OK');
180                 assert.deepEqual(response['commits'], []);
181             });
182         });
183
184         it("should return the oldest reported commit", () => {
185             const db = TestServer.database();
186             const remote = TestServer.remoteAPI();
187             return Promise.all([
188                 addSlaveForReport(subversionCommits),
189                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
190                 db.insert('commits', {'repository': 1, 'revision': notYetReportedCommit.revision, 'time': notYetReportedCommit.time}),
191             ]).then(() => {
192                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
193             }).then(() => {
194                 return remote.getJSON('/api/commits/WebKit/last-reported');
195             }).then(function (result) {
196                 assert.equal(result['status'], 'OK');
197                 assert.equal(result['commits'].length, 1);
198                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'].slice().pop());
199             });
200         });
201     });
202
203     describe('/api/commits/<repository>/<commit>', () => {
204         it("should return RepositoryNotFound when there are no matching repository", () => {
205             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/210949').then((response) => {
206                 assert.equal(response['status'], 'RepositoryNotFound');
207             });
208         });
209
210         it("should return UnknownCommit when one of the specified commit does not exist in the database", () => {
211             const db = TestServer.database();
212             return Promise.all([
213                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
214                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
215             ]).then(() => {
216                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/210949');
217             }).then((response) => {
218                 assert.equal(response['status'], 'UnknownCommit');
219             });
220         });
221
222         it("should return the commit even if it had not been reported", () => {
223             const db = TestServer.database();
224             return Promise.all([
225                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
226                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
227             ]).then(() => {
228                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/210950');
229             }).then((result) => {
230                 assert.equal(result['status'], 'OK');
231                 assert.equal(result['commits'].length, 1);
232                 assertCommitIsSameAsOneSubmitted(result['commits'][0], {
233                     parent: null,
234                     revision: '210950',
235                     time: '2017-01-20T03:49:37.887Z',
236                     author: {name: null, account: null},
237                     message: null,
238                 });
239             });
240         });
241
242         it("should return the full result for a reported commit", () => {
243             const remote = TestServer.remoteAPI();
244             return addSlaveForReport(subversionCommits).then(() => {
245                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
246             }).then(() => {
247                 return remote.getJSON('/api/commits/WebKit/210949');
248             }).then((result) => {
249                 assert.equal(result['status'], 'OK');
250                 assert.deepEqual(result['commits'].length, 1);
251                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'][1]);
252             });
253         });
254
255     });
256
257     describe('/api/commits/<repository>/?from=<commit-1>&to=<commit-2>', () => {
258         it("should return RepositoryNotFound when there are no matching repository", () => {
259             return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210900&to=211000').then((response) => {
260                 assert.equal(response['status'], 'RepositoryNotFound');
261             });
262         });
263
264         it("should return UnknownCommit when one of the specified commit does not exist in the database", () => {
265             const db = TestServer.database();
266             return Promise.all([
267                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
268                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'})
269             ]).then(() => {
270                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210900&to=211000');
271             }).then((response) => {
272                 assert.equal(response['status'], 'UnknownCommit');
273             });
274         });
275
276         it("should return an empty result when commits in the specified range have not reported", () => {
277             const db = TestServer.database();
278             return Promise.all([
279                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
280                 db.insert('commits', {'repository': 1, 'revision': '210949', 'time': '2017-01-20T03:23:50.645Z'}),
281                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z'}),
282             ]).then(() => {
283                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210949&to=210950');
284             }).then((response) => {
285                 assert.equal(response['status'], 'OK');
286                 assert.deepEqual(response['commits'], []);
287             });
288         });
289
290         it("should return reported commits in the specified range", () => {
291             const db = TestServer.database();
292             return Promise.all([
293                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
294                 db.insert('commits', {'repository': 1, 'revision': '210949', 'time': '2017-01-20T03:23:50.645Z', 'reported': true}),
295                 db.insert('commits', {'repository': 1, 'revision': '210950', 'time': '2017-01-20T03:49:37.887Z', 'reported': true}),
296             ]).then(() => {
297                 return TestServer.remoteAPI().getJSON('/api/commits/WebKit/?from=210949&to=210950');
298             }).then((result) => {
299                 assert.equal(result['status'], 'OK');
300                 assert.deepEqual(result['commits'].length, 2);
301                 assertCommitIsSameAsOneSubmitted(result['commits'][0], {
302                     parent: null,
303                     revision: '210949',
304                     time: '2017-01-20T03:23:50.645Z',
305                     author: {name: null, account: null},
306                     message: null,
307                 });
308                 assertCommitIsSameAsOneSubmitted(result['commits'][1], {
309                     parent: null,
310                     revision: '210950',
311                     time: '2017-01-20T03:49:37.887Z',
312                     author: {name: null, account: null},
313                     message: null,
314                 });
315             });
316         });
317
318         it("should not include a revision not within the specified range", () => {
319             const remote = TestServer.remoteAPI();
320             return addSlaveForReport(subversionCommits).then(() => {
321                 return remote.postJSONWithStatus('/api/report-commits/', subversionCommits);
322             }).then(() => {
323                 return remote.getJSON('/api/commits/WebKit/?from=210948&to=210949');
324             }).then((result) => {
325                 assert.equal(result['status'], 'OK');
326                 assert.deepEqual(result['commits'].length, 2);
327                 assertCommitIsSameAsOneSubmitted(result['commits'][0], subversionCommits['commits'][0]);
328                 assertCommitIsSameAsOneSubmitted(result['commits'][1], subversionCommits['commits'][1]);
329             });
330         });
331
332     });
333
334 });