Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/repositories/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ exports.register = function (server, options, next) {
}
// ... otherwise skip over the test
} else {
if (test.testID) {
if (isOwn && test.testID) {
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]));
} else {
queries.push(db.genericQuery(`INSERT INTO ?? (??) VALUES (${pageID}, ${db.escape(test.title)}, ${db.escape(test.defer)}, ${db.escape(test.code)})`, [table, columns]));
Expand Down
22 changes: 20 additions & 2 deletions test/unit/server/repositories/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ lab.experiment('Tests Repository', function () {
});
});

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

lab.test('inserts new test if user is not the original owner', function (done) {
genericQueryStub.returns(Promise.resolve({ affectedRows: 1 }));
let tClone = Hoek.clone(t);
tClone[0].testID = 123;
tClone[1].testID = 321;
tests.bulkUpdate(pageID, tClone, false)
.then(results => {
const call1 = genericQueryStub.getCall(0).args;
const call2 = genericQueryStub.getCall(1).args;
Code.expect(call2[0]).to.equal('INSERT INTO ?? (??) VALUES (1, `t2`, `n`, `a = 2`)');
Code.expect(call2[1]).to.equal([ 'tests', [ 'pageID', 'title', 'defer', 'code' ] ]);

Code.expect(call1[0]).to.equal('INSERT INTO ?? (??) VALUES (1, `t1`, `n`, `a = 1`)');
Code.expect(call1[1]).to.equal([ 'tests', [ 'pageID', 'title', 'defer', 'code' ] ]);
done();
});
});

lab.test('deletes existing test if no title and no code', function (done) {
genericQueryStub.returns(Promise.resolve({ affectedRows: 1 }));
let tClone = Hoek.clone(t);
Expand Down