3 const assert = require('assert');
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;
9 describe("/api/report-commits/", function () {
12 connectToDatabaseInEveryTest();
15 "slaveName": "someSlave",
16 "slavePassword": "somePassword",
18 const subversionCommit = {
19 "slaveName": "someSlave",
20 "slavePassword": "somePassword",
23 "repository": "WebKit",
25 "time": "2013-02-06T08:55:20.9Z",
26 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
27 "message": "some message",
31 const subversionInvalidCommit = {
32 "slaveName": "someSlave",
33 "slavePassword": "somePassword",
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",
44 const subversionTwoCommits = {
45 "slaveName": "someSlave",
46 "slavePassword": "somePassword",
49 "repository": "WebKit",
51 "time": "2013-02-06T08:55:20.9Z",
52 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
53 "message": "some message",
56 "repository": "WebKit",
57 "previousCommit": "141977",
59 "time": "2013-02-06T09:54:56.0Z",
60 "author": {"name": "Mikhail Pozdnyakov", "account": "mikhail.pozdnyakov@intel.com"},
61 "message": "another message",
66 const subversionInvalidPreviousCommit = {
67 "slaveName": "someSlave",
68 "slavePassword": "somePassword",
71 "repository": "WebKit",
72 "previousCommit": "99999",
74 "time": "2013-02-06T08:55:20.9Z",
75 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
76 "message": "some message",
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');
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);
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');
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']);
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];
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']);
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);
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);
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']);
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']);
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);
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 () {
211 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
212 db.insert('commits', {'repository': 1, 'revision': reportedData['revision'], 'time': reportedData['time']})
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];
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']);
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 () {
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']})
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];
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']);
261 assert.equal(commits[1]['id'], 3);
262 assert.equal(commits[1]['message'], null);
263 assert.equal(commits[1]['committer'], null);
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 () {
274 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
275 db.insert('committers', {'repository': 1, 'account': author['account']}),
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']);