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",
59 "time": "2013-02-06T09:54:56.0Z",
60 "author": {"name": "Mikhail Pozdnyakov", "account": "mikhail.pozdnyakov@intel.com"},
61 "message": "another message",
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');
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);
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');
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']);
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];
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']);
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);
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);
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']);
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']);
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 () {
181 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
182 db.insert('commits', {'repository': 1, 'revision': reportedData['revision'], 'time': reportedData['time']})
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];
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']);
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 () {
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']})
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, 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']);
231 assert.equal(commits[1]['id'], 3);
232 assert.equal(commits[1]['message'], null);
233 assert.equal(commits[1]['committer'], null);
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 () {
244 db.insert('repositories', {'id': 1, 'name': 'WebKit'}),
245 db.insert('committers', {'repository': 1, 'account': author['account']}),
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']);