Skip to content

Commit

Permalink
Merge pull request #935 from dimagi/dm/266075-fix-overwrite-without-p…
Browse files Browse the repository at this point in the history
…rompt

Fix overwrite conflicting form without prompt
  • Loading branch information
snopoke committed Feb 26, 2018
2 parents db8b6f4 + d22a76c commit f574f15
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
17 changes: 7 additions & 10 deletions src/core.js
Expand Up @@ -2144,6 +2144,7 @@ define([
var CryptoJS = require('CryptoJS'),
_this = this,
opts = this.opts().core,
checkForConflict = false,
patch, data;
saveType = saveType || opts.saveType;

Expand All @@ -2152,6 +2153,7 @@ define([
showPageSpinner();

if (saveType === 'patch') {
checkForConflict = true;
var diff_match_patch = require('diff-match-patch'),
dmp = new diff_match_patch();
patch = dmp.patch_toText(
Expand All @@ -2164,16 +2166,11 @@ define([
}
}

if (saveType === 'patch') {
data = {
patch: patch,
sha1: CryptoJS.SHA1(this.data.core.lastSavedXForm).toString()
};
} else {
data = {xform: formText};
}

data = saveType === 'patch' ? {patch: patch} : {xform: formText};
data.case_references = JSON.stringify(this.data.core.form._logicManager.caseReferences());
if (checkForConflict) {
data.sha1 = CryptoJS.SHA1(this.data.core.lastSavedXForm).toString();
}

this.data.core.saveButton.ajax({
type: "POST",
Expand All @@ -2184,7 +2181,7 @@ define([
hidePageSpinner();
},
success: function (data) {
if (saveType === 'patch') {
if (checkForConflict) {
if (data.status === 'conflict') {
// reset save button to unsaved state
_this.data.core.saveButton.fire("change");
Expand Down
51 changes: 47 additions & 4 deletions tests/core.js
Expand Up @@ -487,7 +487,9 @@ define([
describe("save conflict resolution logic", function() {
var SRV_FORM = " line one\n line two\n line three\n line four\n line five\n",
NEW_FORM = SRV_FORM.replace("two", "2"),
vellum, originalShowOverwriteWarning, onOverwrite, response;
conflictResponse = {status: "conflict", xform: SRV_FORM},
savedAs = null,
vellum, originalShowOverwriteWarning, onOverwrite;
before(function (done) {
util.init({
core: {
Expand All @@ -496,11 +498,20 @@ define([
this.showOverwriteWarning = function (send, newForm, oldForm) {
onOverwrite(send, newForm, oldForm);
};
this.data.core.lastSavedXForm = SRV_FORM;
vellum = this;
done();
},
patchUrl: function () { return response; },
patchUrl: function () {
savedAs = "patch";
return conflictResponse;
},
saveUrl: function (data) {
savedAs = "full";
if (data.sha1) {
return conflictResponse;
}
return {xform: data.xform};
},
},
features: {full_save_on_missing_conflict_xform: false},
});
Expand All @@ -510,8 +521,8 @@ define([
});
beforeEach(function () {
onOverwrite = undefined;
response = {status: "conflict", xform: SRV_FORM};
vellum.data.core.saveButton.fire("change");
vellum.data.core.lastSavedXForm = SRV_FORM;
});

it("should re-enable save button before overwrite warning", function () {
Expand All @@ -525,6 +536,38 @@ define([
assert(util.saveButtonEnabled(), "save button is disabled (sanity check)");
vellum.send(NEW_FORM, "patch");
assert(didWarn, "overwrite warning not triggered");
assert.equal(savedAs, "patch");
assert.equal(vellum.data.core.lastSavedXForm, SRV_FORM);
});

it("should send sha1 checksum on patch switched to full form post", function () {
onOverwrite = function (send, newForm, oldForm) {
assert(util.saveButtonEnabled(), "save button is disabled");
assert.equal(newForm, newFormSrc);
assert.equal(oldForm, SRV_FORM);
didWarn = true;
};
var didWarn = false,
// diff too long -> will do full save instead of patch
newFormSrc = "abc";
assert(util.saveButtonEnabled(), "save button is disabled (sanity check)");
vellum.send(newFormSrc, null);
assert(didWarn, "overwrite warning not triggered");
assert.equal(savedAs, "full");
assert.equal(vellum.data.core.lastSavedXForm, SRV_FORM);
});

it("should not send sha1 checksum with full form post", function () {
onOverwrite = function (send, newForm, oldForm) {
didWarn = true;
};
var didWarn = false;
assert(util.saveButtonEnabled(), "save button is disabled (sanity check)");
vellum.send(NEW_FORM, "full");
assert(!didWarn, "should not prompt to overwrite on full save");
assert(!util.saveButtonEnabled(), "save button should be disabled");
assert.equal(savedAs, "full");
assert.equal(vellum.data.core.lastSavedXForm, NEW_FORM);
});
});

Expand Down
2 changes: 1 addition & 1 deletion tests/utils.js
Expand Up @@ -235,7 +235,7 @@ define([
vellum_options.core.saveUrl = function (data) {
savedForm = data.xform;
saveCount++;
originalSaveUrl(data);
return originalSaveUrl(data);
};
var vellum = $("#vellum"),
old = vellum.vellum("get");
Expand Down

0 comments on commit f574f15

Please sign in to comment.