Rename 'commit_parent' in 'commits' table to 'commit_previous_commit'.
[WebKit-https.git] / Websites / perf.webkit.org / server-tests / api-report-commits-tests.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/report-commits/", function () {
10     this.timeout(1000);
11     TestServer.inject();
12     connectToDatabaseInEveryTest();
13
14     const emptyReport = {
15         "slaveName": "someSlave",
16         "slavePassword": "somePassword",
17     };
18     const subversionCommit = {
19         "slaveName": "someSlave",
20         "slavePassword": "somePassword",
21         "commits": [
22             {
23                 "repository": "WebKit",
24                 "revision": "141977",
25                 "time": "2013-02-06T08:55:20.9Z",
26                 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
27                 "message": "some message",
28             }
29         ],
30     };
31     const subversionInvalidCommit = {
32         "slaveName": "someSlave",
33         "slavePassword": "somePassword",
34         "commits": [
35             {
36                 "repository": "WebKit",
37                 "revision": "_141977",
38                 "time": "2013-02-06T08:55:20.9Z",
39                 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
40                 "message": "some message",
41             }
42         ],
43     };
44     const subversionTwoCommits = {
45         "slaveName": "someSlave",
46         "slavePassword": "somePassword",
47         "commits": [
48             {
49                 "repository": "WebKit",
50                 "revision": "141977",
51                 "time": "2013-02-06T08:55:20.9Z",
52                 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
53                 "message": "some message",
54             },
55             {
56                 "repository": "WebKit",
57                 "previousCommit": "141977",
58                 "revision": "141978",
59                 "time": "2013-02-06T09:54:56.0Z",
60                 "author": {"name": "Mikhail Pozdnyakov", "account": "mikhail.pozdnyakov@intel.com"},
61                 "message": "another message",
62             }
63         ]
64     };
65
66     const subversionInvalidPreviousCommit = {
67         "slaveName": "someSlave",
68         "slavePassword": "somePassword",
69         "commits": [
70             {
71                 "repository": "WebKit",
72                 "previousCommit": "99999",
73                 "revision": "12345",
74                 "time": "2013-02-06T08:55:20.9Z",
75                 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
76                 "message": "some message",
77             }
78         ]
79     }
80
81     it("should reject error when slave name is missing", function (done) {
82         TestServer.remoteAPI().postJSON('/api/report-commits/', {}).then(function (response) {
83             assert.equal(response['status'], 'MissingSlaveName');
84             done();
85         }).catch(done);
86     });
87
88     it("should reject when there are no slaves", function (done) {
89         TestServer.remoteAPI().postJSON('/api/report-commits/', emptyReport).then(function (response) {
90             assert.equal(response['status'], 'SlaveNotFound');
91             return TestServer.database().selectAll('commits');
92         }).then(function (rows) {
93             assert.equal(rows.length, 0);
94             done();
95         }).catch(done);
96     });
97
98     it("should accept an empty report", function (done) {
99         addSlaveForReport(emptyReport).then(function () {
100             return TestServer.remoteAPI().postJSON('/api/report-commits/', emptyReport);
101         }).then(function (response) {
102             assert.equal(response['status'], 'OK');
103             done();
104         }).catch(done);
105     });
106
107     it("should add a missing repository", function (done) {
108         const db = TestServer.database();
109         addSlaveForReport(subversionCommit).then(function () {
110             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommit);
111         }).then(function (response) {
112             assert.equal(response['status'], 'OK');
113             return db.selectAll('repositories');
114         }).then(function (rows) {
115             assert.equal(rows.length, 1);
116             assert.equal(rows[0]['name'], subversionCommit.commits[0]['repository']);
117             done();
118         }).catch(done);
119     });
120
121     it("should store a commit from a valid slave", function (done) {
122         const db = TestServer.database();
123         addSlaveForReport(subversionCommit).then(function () {
124             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommit);
125         }).then(function (response) {
126             assert.equal(response['status'], 'OK');
127             return Promise.all([db.selectAll('commits'), db.selectAll('committers')]);
128         }).then(function (result) {
129             let commits = result[0];
130             let committers = result[1];
131             let reportedData = subversionCommit.commits[0];
132
133             assert.equal(commits.length, 1);
134             assert.equal(committers.length, 1);
135             assert.equal(commits[0]['revision'], reportedData['revision']);
136             assert.equal(commits[0]['time'].toString(), new Date('2013-02-06 08:55:20.9').toString());
137             assert.equal(commits[0]['message'], reportedData['message']);
138             assert.equal(commits[0]['committer'], committers[0]['id']);
139             assert.equal(committers[0]['name'], reportedData['author']['name']);
140             assert.equal(committers[0]['account'], reportedData['author']['account']);
141
142             done();
143         }).catch(done);
144     });
145
146     it("should reject an invalid revision number", function (done) {
147         addSlaveForReport(subversionCommit).then(function () {
148             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionInvalidCommit);
149         }).then(function (response) {
150             assert.equal(response['status'], 'InvalidRevision');
151             return TestServer.database().selectAll('commits');
152         }).then(function (rows) {
153             assert.equal(rows.length, 0);
154             done();
155         }).catch(done);
156     });
157
158     it("should store two commits from a valid slave", function (done) {
159         const db = TestServer.database();
160         addSlaveForReport(subversionTwoCommits).then(function () {
161             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionTwoCommits);
162         }).then(function (response) {
163             assert.equal(response['status'], 'OK');
164             return Promise.all([db.selectAll('commits'), db.selectAll('committers')]);
165         }).then(function (result) {
166             const commits = result[0];
167             const committers = result[1];
168             assert.equal(commits.length, 2);
169             assert.equal(committers.length, 2);
170
171             let reportedData = subversionTwoCommits.commits[0];
172             assert.equal(commits[0]['revision'], reportedData['revision']);
173             assert.equal(commits[0]['time'].toString(), new Date('2013-02-06 08:55:20.9').toString());
174             assert.equal(commits[0]['message'], reportedData['message']);
175             assert.equal(commits[0]['committer'], committers[0]['id']);
176             assert.equal(commits[0]['previous_commit'], null);
177             assert.equal(committers[0]['name'], reportedData['author']['name']);
178             assert.equal(committers[0]['account'], reportedData['author']['account']);
179
180             reportedData = subversionTwoCommits.commits[1];
181             assert.equal(commits[1]['revision'], reportedData['revision']);
182             assert.equal(commits[1]['time'].toString(), new Date('2013-02-06 09:54:56.0').toString());
183             assert.equal(commits[1]['message'], reportedData['message']);
184             assert.equal(commits[1]['committer'], committers[1]['id']);
185             assert.equal(commits[1]['previous_commit'], commits[0]['id']);
186             assert.equal(committers[1]['name'], reportedData['author']['name']);
187             assert.equal(committers[1]['account'], reportedData['author']['account']);
188
189             done();
190         }).catch(done);
191     });
192
193     it("should fail if previous commit is invalid", function (done) {
194         const db = TestServer.database();
195         addSlaveForReport(subversionInvalidPreviousCommit).then(function () {
196             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionInvalidPreviousCommit);
197         }).then(function (response) {
198             assert.equal(response['status'], 'FailedToFindPreviousCommit');
199             return db.selectAll('commits');
200         }).then(function (result) {
201             assert.equal(result.length, 0);
202             done();
203         }).catch(done);
204     });
205
206     it("should update an existing commit if there is one", function (done) {
207         const db = TestServer.database();
208         const reportedData = subversionCommit.commits[0];
209         addSlaveForReport(subversionCommit).then(function () {
210             return Promise.all([
211                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
212                 db.insert('commits', {'repository': 1, 'revision': reportedData['revision'], 'time': reportedData['time']})
213             ]);
214         }).then(function () {
215             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommit);
216         }).then(function (response) {
217             assert.equal(response['status'], 'OK');
218             return Promise.all([db.selectAll('commits'), db.selectAll('committers')]);
219         }).then(function (result) {
220             const commits = result[0];
221             const committers = result[1];
222
223             assert.equal(commits.length, 1);
224             assert.equal(committers.length, 1);
225             assert.equal(commits[0]['message'], reportedData['message']);
226             assert.equal(commits[0]['committer'], committers[0]['id']);
227             assert.equal(committers[0]['name'], reportedData['author']['name']);
228             assert.equal(committers[0]['account'], reportedData['author']['account']);
229
230             done();
231         }).catch(done);
232     });
233
234     it("should not update an unrelated commit", function (done) {
235         const db = TestServer.database();
236         const firstData = subversionTwoCommits.commits[0];
237         const secondData = subversionTwoCommits.commits[1];
238         addSlaveForReport(subversionCommit).then(function () {
239             return Promise.all([
240                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
241                 db.insert('commits', {'id': 2, 'repository': 1, 'revision': firstData['revision'], 'time': firstData['time']}),
242                 db.insert('commits', {'id': 3, 'repository': 1, 'revision': secondData['revision'], 'time': secondData['time']})
243             ]);
244         }).then(function () {
245             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommit);
246         }).then(function (response) {
247             assert.equal(response['status'], 'OK');
248             return Promise.all([db.selectAll('commits'), db.selectAll('committers')]);
249         }).then(function (result) {
250             const commits = result[0];
251             const committers = result[1];
252
253             assert.equal(commits.length, 2);
254             assert.equal(committers.length, 1);
255             assert.equal(commits[0]['id'], 2);
256             assert.equal(commits[0]['message'], firstData['message']);
257             assert.equal(commits[0]['committer'], committers[0]['id']);
258             assert.equal(committers[0]['name'], firstData['author']['name']);
259             assert.equal(committers[0]['account'], firstData['author']['account']);
260
261             assert.equal(commits[1]['id'], 3);
262             assert.equal(commits[1]['message'], null);
263             assert.equal(commits[1]['committer'], null);
264
265             done();
266         }).catch(done);
267     });
268
269     it("should update an existing committer if there is one", function (done) {
270         const db = TestServer.database();
271         const author = subversionCommit.commits[0]['author'];
272         addSlaveForReport(subversionCommit).then(function () {
273             return Promise.all([
274                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
275                 db.insert('committers', {'repository': 1, 'account': author['account']}),
276             ]);
277         }).then(function () {
278             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommit);
279         }).then(function (response) {
280             assert.equal(response['status'], 'OK');
281             return db.selectAll('committers');
282         }).then(function (committers) {
283             assert.equal(committers.length, 1);
284             assert.equal(committers[0]['name'], author['name']);
285             assert.equal(committers[0]['account'], author['account']);
286             done();
287         }).catch(done);
288     });
289
290 });