Skip to content

Commit f144e66

Browse files
swangmathiasbynens
authored andcommitted
Fix issue with bad updates causing disappearing tests (#464)
Fixes #236 Pretty much similar to #414, but with an added test to show issue.
1 parent 3a1b91a commit f144e66

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

server/repositories/tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ exports.register = function (server, options, next) {
3838
}
3939
// ... otherwise skip over the test
4040
} else {
41-
if (test.testID) {
41+
if (isOwn && test.testID) {
4242
queries.push(db.genericQuery(`UPDATE ?? SET title = ${db.escape(test.title)}, defer = ${db.escape(test.defer)} , code = ${db.escape(test.code)} WHERE pageID = ${pageID} AND testID = ${test.testID}`, [table]));
4343
} else {
4444
queries.push(db.genericQuery(`INSERT INTO ?? (??) VALUES (${pageID}, ${db.escape(test.title)}, ${db.escape(test.defer)}, ${db.escape(test.code)})`, [table, columns]));

test/unit/server/repositories/tests.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ lab.experiment('Tests Repository', function () {
168168
});
169169
});
170170

171-
lab.test('updates test if it is an existing test', function (done) {
171+
lab.test('updates test if it is an existing test and the same owner', function (done) {
172172
genericQueryStub.returns(Promise.resolve({ affectedRows: 1 }));
173173
let tClone = Hoek.clone(t);
174174
tClone[0].testID = 123;
175175
tClone[1].testID = 321;
176-
tests.bulkUpdate(pageID, tClone, false)
176+
tests.bulkUpdate(pageID, tClone, true)
177177
.then(results => {
178178
const call1 = genericQueryStub.getCall(0).args;
179179
const call2 = genericQueryStub.getCall(1).args;
@@ -186,6 +186,24 @@ lab.experiment('Tests Repository', function () {
186186
});
187187
});
188188

189+
lab.test('inserts new test if user is not the original owner', function (done) {
190+
genericQueryStub.returns(Promise.resolve({ affectedRows: 1 }));
191+
let tClone = Hoek.clone(t);
192+
tClone[0].testID = 123;
193+
tClone[1].testID = 321;
194+
tests.bulkUpdate(pageID, tClone, false)
195+
.then(results => {
196+
const call1 = genericQueryStub.getCall(0).args;
197+
const call2 = genericQueryStub.getCall(1).args;
198+
Code.expect(call2[0]).to.equal('INSERT INTO ?? (??) VALUES (1, `t2`, `n`, `a = 2`)');
199+
Code.expect(call2[1]).to.equal([ 'tests', [ 'pageID', 'title', 'defer', 'code' ] ]);
200+
201+
Code.expect(call1[0]).to.equal('INSERT INTO ?? (??) VALUES (1, `t1`, `n`, `a = 1`)');
202+
Code.expect(call1[1]).to.equal([ 'tests', [ 'pageID', 'title', 'defer', 'code' ] ]);
203+
done();
204+
});
205+
});
206+
189207
lab.test('deletes existing test if no title and no code', function (done) {
190208
genericQueryStub.returns(Promise.resolve({ affectedRows: 1 }));
191209
let tClone = Hoek.clone(t);

0 commit comments

Comments
 (0)