bc078174ab2dbe9ba99b8ff048085d96e356f2ec
[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                 "parent": "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     it("should reject error when slave name is missing", function (done) {
67         TestServer.remoteAPI().postJSON('/api/report-commits/', {}).then(function (response) {
68             assert.equal(response['status'], 'MissingSlaveName');
69             done();
70         }).catch(done);
71     });
72
73     it("should reject when there are no slaves", function (done) {
74         TestServer.remoteAPI().postJSON('/api/report-commits/', emptyReport).then(function (response) {
75             assert.equal(response['status'], 'SlaveNotFound');
76             return TestServer.database().selectAll('commits');
77         }).then(function (rows) {
78             assert.equal(rows.length, 0);
79             done();
80         }).catch(done);
81     });
82
83     it("should accept an empty report", function (done) {
84         addSlaveForReport(emptyReport).then(function () {
85             return TestServer.remoteAPI().postJSON('/api/report-commits/', emptyReport);
86         }).then(function (response) {
87             assert.equal(response['status'], 'OK');
88             done();
89         }).catch(done);
90     });
91
92     it("should add a missing repository", function (done) {
93         const db = TestServer.database();
94         addSlaveForReport(subversionCommit).then(function () {
95             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommit);
96         }).then(function (response) {
97             assert.equal(response['status'], 'OK');
98             return db.selectAll('repositories');
99         }).then(function (rows) {
100             assert.equal(rows.length, 1);
101             assert.equal(rows[0]['name'], subversionCommit.commits[0]['repository']);
102             done();
103         }).catch(done);
104     });
105
106     it("should store a commit from a valid slave", function (done) {
107         const db = TestServer.database();
108         addSlaveForReport(subversionCommit).then(function () {
109             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommit);
110         }).then(function (response) {
111             assert.equal(response['status'], 'OK');
112             return Promise.all([db.selectAll('commits'), db.selectAll('committers')]);
113         }).then(function (result) {
114             let commits = result[0];
115             let committers = result[1];
116             let reportedData = subversionCommit.commits[0];
117
118             assert.equal(commits.length, 1);
119             assert.equal(committers.length, 1);
120             assert.equal(commits[0]['revision'], reportedData['revision']);
121             assert.equal(commits[0]['time'].toString(), new Date('2013-02-06 08:55:20.9').toString());
122             assert.equal(commits[0]['message'], reportedData['message']);
123             assert.equal(commits[0]['committer'], committers[0]['id']);
124             assert.equal(committers[0]['name'], reportedData['author']['name']);
125             assert.equal(committers[0]['account'], reportedData['author']['account']);
126
127             done();
128         }).catch(done);
129     });
130
131     it("should reject an invalid revision number", function (done) {
132         addSlaveForReport(subversionCommit).then(function () {
133             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionInvalidCommit);
134         }).then(function (response) {
135             assert.equal(response['status'], 'InvalidRevision');
136             return TestServer.database().selectAll('commits');
137         }).then(function (rows) {
138             assert.equal(rows.length, 0);
139             done();
140         }).catch(done);
141     });
142
143     it("should store two commits from a valid slave", function (done) {
144         const db = TestServer.database();
145         addSlaveForReport(subversionTwoCommits).then(function () {
146             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionTwoCommits);
147         }).then(function (response) {
148             assert.equal(response['status'], 'OK');
149             return Promise.all([db.selectAll('commits'), db.selectAll('committers')]);
150         }).then(function (result) {
151             const commits = result[0];
152             const committers = result[1];
153             assert.equal(commits.length, 2);
154             assert.equal(committers.length, 2);
155
156             let reportedData = subversionTwoCommits.commits[0];
157             assert.equal(commits[0]['revision'], reportedData['revision']);
158             assert.equal(commits[0]['time'].toString(), new Date('2013-02-06 08:55:20.9').toString());
159             assert.equal(commits[0]['message'], reportedData['message']);
160             assert.equal(commits[0]['committer'], committers[0]['id']);
161             assert.equal(committers[0]['name'], reportedData['author']['name']);
162             assert.equal(committers[0]['account'], reportedData['author']['account']);
163
164             reportedData = subversionTwoCommits.commits[1];
165             assert.equal(commits[1]['revision'], reportedData['revision']);
166             assert.equal(commits[1]['time'].toString(), new Date('2013-02-06 09:54:56.0').toString());
167             assert.equal(commits[1]['message'], reportedData['message']);
168             assert.equal(commits[1]['committer'], committers[1]['id']);
169             assert.equal(committers[1]['name'], reportedData['author']['name']);
170             assert.equal(committers[1]['account'], reportedData['author']['account']);
171
172             done();
173         }).catch(done);
174     });
175
176     it("should update an existing commit if there is one", function (done) {
177         const db = TestServer.database();
178         const reportedData = subversionCommit.commits[0];
179         addSlaveForReport(subversionCommit).then(function () {
180             return Promise.all([
181                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
182                 db.insert('commits', {'repository': 1, 'revision': reportedData['revision'], 'time': reportedData['time']})
183             ]);
184         }).then(function () {
185             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommit);
186         }).then(function (response) {
187             assert.equal(response['status'], 'OK');
188             return Promise.all([db.selectAll('commits'), db.selectAll('committers')]);
189         }).then(function (result) {
190             const commits = result[0];
191             const committers = result[1];
192
193             assert.equal(commits.length, 1);
194             assert.equal(committers.length, 1);
195             assert.equal(commits[0]['message'], reportedData['message']);
196             assert.equal(commits[0]['committer'], committers[0]['id']);
197             assert.equal(committers[0]['name'], reportedData['author']['name']);
198             assert.equal(committers[0]['account'], reportedData['author']['account']);
199
200             done();
201         }).catch(done);
202     });
203
204     it("should not update an unrelated commit", function (done) {
205         const db = TestServer.database();
206         const firstData = subversionTwoCommits.commits[0];
207         const secondData = subversionTwoCommits.commits[1];
208         addSlaveForReport(subversionCommit).then(function () {
209             return Promise.all([
210                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
211                 db.insert('commits', {'id': 2, 'repository': 1, 'revision': firstData['revision'], 'time': firstData['time']}),
212                 db.insert('commits', {'id': 3, 'repository': 1, 'revision': secondData['revision'], 'time': secondData['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, 2);
224             assert.equal(committers.length, 1);
225             assert.equal(commits[0]['id'], 2);
226             assert.equal(commits[0]['message'], firstData['message']);
227             assert.equal(commits[0]['committer'], committers[0]['id']);
228             assert.equal(committers[0]['name'], firstData['author']['name']);
229             assert.equal(committers[0]['account'], firstData['author']['account']);
230             
231             assert.equal(commits[1]['id'], 3);
232             assert.equal(commits[1]['message'], null);
233             assert.equal(commits[1]['committer'], null);
234
235             done();
236         }).catch(done);
237     });
238
239     it("should update an existing committer if there is one", function (done) {
240         const db = TestServer.database();
241         const author = subversionCommit.commits[0]['author'];
242         addSlaveForReport(subversionCommit).then(function () {
243             return Promise.all([
244                 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
245                 db.insert('committers', {'repository': 1, 'account': author['account']}),
246             ]);
247         }).then(function () {
248             return TestServer.remoteAPI().postJSON('/api/report-commits/', subversionCommit);
249         }).then(function (response) {
250             assert.equal(response['status'], 'OK');
251             return db.selectAll('committers');
252         }).then(function (committers) {
253             assert.equal(committers.length, 1);
254             assert.equal(committers[0]['name'], author['name']);
255             assert.equal(committers[0]['account'], author['account']);
256             done();
257         }).catch(done);
258     });
259
260 });