From 7708460faff0b4f3e8bf24ef91b4b04e4e6e9726 Mon Sep 17 00:00:00 2001 From: John McLear Date: Sat, 27 Dec 2014 17:14:40 +0100 Subject: [PATCH] fix commenting --- src/node/db/API.js | 16 +++++--- src/node/utils/ImportHtml.js | 6 +-- tests/backend/specs/api/pad.js | 39 ++++++++++++++++++++ tests/backend/specs/api/sessionsAndGroups.js | 1 - 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/node/db/API.js b/src/node/db/API.js index a9df2a12f..bf8afacf0 100644 --- a/src/node/db/API.js +++ b/src/node/db/API.js @@ -408,12 +408,18 @@ exports.setHTML = function(padID, html, callback) getPadSafe(padID, true, function(err, pad) { if(ERR(err, callback)) return; - // add a new changeset with the new html to the pad - importHtml.setPadHTML(pad, cleanText(html), callback); - - //update the clients on the pad - padMessageHandler.updatePadClients(pad, callback); + importHtml.setPadHTML(pad, cleanText(html), function(e){ + if(e){ + callback(new customError("HTML is malformed","apierror")); + return; + }else{ + //update the clients on the pad + padMessageHandler.updatePadClients(pad, callback); + callback(); + return; + } + }); }); } diff --git a/src/node/utils/ImportHtml.js b/src/node/utils/ImportHtml.js index 59802f9bf..6bcf078d3 100644 --- a/src/node/utils/ImportHtml.js +++ b/src/node/utils/ImportHtml.js @@ -22,9 +22,8 @@ var cheerio = require("cheerio"); function setPadHTML(pad, html, callback) { var apiLogger = log4js.getLogger("ImportHtml"); - +console.warn("setting padhtml"); var $ = cheerio.load(html); - // Appends a line break, used by Etherpad to ensure a caret is available // below the last line of an import $('body').append("

"); @@ -40,7 +39,7 @@ function setPadHTML(pad, html, callback) cc.collectContent(doc); }catch(e){ apiLogger.warn("HTML was not properly formed", e); - return; // We don't process the HTML because it was bad.. + return callback(e); // We don't process the HTML because it was bad.. } var result = cc.finish(); @@ -91,6 +90,7 @@ function setPadHTML(pad, html, callback) apiLogger.debug('The changeset: ' + theChangeset); pad.setText(""); pad.appendRevision(theChangeset); + callback(); } exports.setPadHTML = setPadHTML; diff --git a/tests/backend/specs/api/pad.js b/tests/backend/specs/api/pad.js index 700c498f6..a5634d605 100644 --- a/tests/backend/specs/api/pad.js +++ b/tests/backend/specs/api/pad.js @@ -61,6 +61,9 @@ describe('Permission', function(){ -> setText(padId) -> getLastEdited(padID) -- Should be when setText was performed -> padUsers(padID) -- Should be when setText was performed + -> setHTML(padID) -- Should fail on invalid HTML + -> setHTML(padID) -- Should fail on invalid HTML + -> getHTML(padID) -- Should return HTML close to posted HTML */ describe('deletePad', function(){ @@ -265,6 +268,42 @@ describe('padUsers', function(){ }); }) +describe('setHTML', function(){ + it('Sets the HTML of a Pad', function(done) { + var html = "Hello HTMLHello World!

"; + api.get(endPoint('setHTML')+"&padID="+testPadId+"&html="+html) + .expect(function(res){ + if(res.body.code !== 0) throw new Error("Error importing HTML") + }) + .expect('Content-Type', /json/) + .expect(200, done) + }); +}) + +describe('setHTML', function(){ + it('Sets the HTML of a Pad attempting to pass ugly HTML', function(done) { + var html = "
Hello HTML
"; + api.get(endPoint('setHTML')+"&padID="+testPadId+"&html="+html) + .expect(function(res){ + if(res.body.code !== 1) throw new Error("Allowing crappy HTML to be imported") + }) + .expect('Content-Type', /json/) + .expect(200, done) + }); +}) + +describe('setHTML', function(){ + it('Sets the HTML of a Pad attempting to pass ugly HTML', function(done) { + var html = "
  • Hello World!
  • foo
"; + api.get(endPoint('setHTML')+"&padID=test&html="+html) + .expect(function(res){ + if(res.body.code !== 0) throw new Error("List HTML cant be imported") + }) + .expect('Content-Type', /json/) + .expect(200, done) + }); +}) + var endPoint = function(point){ diff --git a/tests/backend/specs/api/sessionsAndGroups.js b/tests/backend/specs/api/sessionsAndGroups.js index 4742852ff..a2a3b3ef5 100644 --- a/tests/backend/specs/api/sessionsAndGroups.js +++ b/tests/backend/specs/api/sessionsAndGroups.js @@ -5,7 +5,6 @@ var assert = require('assert') path = require('path'); var filePath = path.join(__dirname, '../../../../APIKEY.txt'); - var apiKey = fs.readFileSync(filePath, {encoding: 'utf-8'}); apiKey = apiKey.replace(/\n$/, ""); var apiVersion = 1;