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