diff --git a/services/docstore/app.coffee b/services/docstore/app.coffee index e3acb4529f..8b89bb97ec 100644 --- a/services/docstore/app.coffee +++ b/services/docstore/app.coffee @@ -11,6 +11,7 @@ app = express() app.get '/project/:project_id/doc/:doc_id', HttpController.getDoc app.post '/project/:project_id/doc/:doc_id', bodyParser.json(), HttpController.updateDoc +app.del '/project/:project_id/doc/:doc_id', HttpController.deleteDoc app.get '/status', (req, res)-> res.send('docstore is alive') diff --git a/services/docstore/app/coffee/HttpController.coffee b/services/docstore/app/coffee/HttpController.coffee index fc30f1adb9..314d1490b1 100644 --- a/services/docstore/app/coffee/HttpController.coffee +++ b/services/docstore/app/coffee/HttpController.coffee @@ -30,4 +30,12 @@ module.exports = HttpController = return next(error) if error? res.json { modified: modified - } \ No newline at end of file + } + + deleteDoc: (req, res, next = (error) ->) -> + project_id = req.params.project_id + doc_id = req.params.doc_id + logger.log project_id: project_id, doc_id: doc_id, "deleting doc" + DocManager.deleteDoc project_id, doc_id, (error) -> + return next(error) if error? + res.send 204 \ No newline at end of file diff --git a/services/docstore/app/coffee/MongoManager.coffee b/services/docstore/app/coffee/MongoManager.coffee index a988c9bd01..23e98e69c3 100644 --- a/services/docstore/app/coffee/MongoManager.coffee +++ b/services/docstore/app/coffee/MongoManager.coffee @@ -17,4 +17,4 @@ module.exports = MongoManager = insertDoc: (project_id, doc_id, attributes, callback = (error) ->) -> attributes._id = ObjectId(doc_id) attributes.project_id = ObjectId(project_id) - db.projects.insert attributes, callback \ No newline at end of file + db.docs.insert attributes, callback \ No newline at end of file diff --git a/services/docstore/app/coffee/mongojs.coffee b/services/docstore/app/coffee/mongojs.coffee index 86ce73809a..aae672b8f3 100644 --- a/services/docstore/app/coffee/mongojs.coffee +++ b/services/docstore/app/coffee/mongojs.coffee @@ -1,6 +1,6 @@ Settings = require "settings-sharelatex" mongojs = require "mongojs" -db = mongojs.connect(Settings.mongo.url, ["projects"]) +db = mongojs.connect(Settings.mongo.url, ["projects", "docs"]) module.exports = db: db ObjectId: mongojs.ObjectId diff --git a/services/docstore/test/acceptance/coffee/DeletingDocsTests.coffee b/services/docstore/test/acceptance/coffee/DeletingDocsTests.coffee new file mode 100644 index 0000000000..a82906be89 --- /dev/null +++ b/services/docstore/test/acceptance/coffee/DeletingDocsTests.coffee @@ -0,0 +1,39 @@ +sinon = require "sinon" +chai = require("chai") +chai.should() +{db, ObjectId} = require "../../../app/js/mongojs" + +DocstoreClient = require "./helpers/DocstoreClient" + +describe "Applying updates to a doc", -> + beforeEach (done) -> + @project_id = ObjectId() + @lines = ["original", "lines"] + DocstoreClient.createDoc @project_id, @lines, (error, @doc_id) => + done() + + afterEach (done) -> + DocstoreClient.deleteProject @project_id, done + + describe "when the doc exists", -> + beforeEach (done) -> + DocstoreClient.deleteDoc @project_id, @doc_id, (error, @res, doc) => + done() + + afterEach (done) -> + db.docs.remove({_id: @doc_id}, done) + + it "should insert a deleted doc into the docs collection", (done) -> + db.docs.find _id: @doc_id, (error, docs) => + docs[0]._id.should.deep.equal @doc_id + docs[0].lines.should.deep.equal @lines + docs[0].deleted.should.equal true + done() + + describe "when the doc does not exist", -> + it "should return a 404", (done) -> + missing_doc_id = ObjectId() + DocstoreClient.deleteDoc @project_id, missing_doc_id, (error, res, doc) -> + res.statusCode.should.equal 404 + done() + diff --git a/services/docstore/test/acceptance/coffee/helpers/DocstoreClient.coffee b/services/docstore/test/acceptance/coffee/helpers/DocstoreClient.coffee index f80afcd1af..6aec513246 100644 --- a/services/docstore/test/acceptance/coffee/helpers/DocstoreClient.coffee +++ b/services/docstore/test/acceptance/coffee/helpers/DocstoreClient.coffee @@ -31,6 +31,11 @@ module.exports = DocstoreClient = json: lines: lines }, callback + + deleteDoc: (project_id, doc_id, callback = (error, res, body) ->) -> + request.del { + url: "http://localhost:3016/project/#{project_id}/doc/#{doc_id}" + }, callback diff --git a/services/docstore/test/unit/coffee/HttpControllerTests.coffee b/services/docstore/test/unit/coffee/HttpControllerTests.coffee index f942b4dbf0..576fd66122 100644 --- a/services/docstore/test/unit/coffee/HttpControllerTests.coffee +++ b/services/docstore/test/unit/coffee/HttpControllerTests.coffee @@ -86,3 +86,21 @@ describe "HttpController", -> @res.send .calledWith(400) .should.equal true + + describe "deleteDoc", -> + beforeEach -> + @req.params = + project_id: @project_id + doc_id: @doc_id + @DocManager.deleteDoc = sinon.stub().callsArg(2) + @HttpController.deleteDoc @req, @res, @next + + it "should delete the document", -> + @DocManager.deleteDoc + .calledWith(@project_id, @doc_id) + .should.equal true + + it "should return a 204 (No Content)", -> + @res.send + .calledWith(204) + .should.equal true diff --git a/services/docstore/test/unit/coffee/MongoManagerTests.coffee b/services/docstore/test/unit/coffee/MongoManagerTests.coffee index 63386d757c..d8c7411eec 100644 --- a/services/docstore/test/unit/coffee/MongoManagerTests.coffee +++ b/services/docstore/test/unit/coffee/MongoManagerTests.coffee @@ -8,7 +8,7 @@ describe "MongoManager", -> beforeEach -> @MongoManager = SandboxedModule.require modulePath, requires: "./mongojs": - db: @db = { projects: {} } + db: @db = { projects: {}, docs: {} } ObjectId: ObjectId @project_id = ObjectId().toString() @callback = sinon.stub() @@ -55,11 +55,11 @@ describe "MongoManager", -> beforeEach -> @doc_id = ObjectId().toString() @lines = ["mock-lines"] - @db.projects.insert = sinon.stub().callsArg(1) + @db.docs.insert = sinon.stub().callsArg(1) @MongoManager.insertDoc @project_id, @doc_id, lines: @lines, @callback it "should insert the attributes with the given doc and project id", -> - @db.projects.insert + @db.docs.insert .calledWith({ _id: ObjectId(@doc_id) project_id: ObjectId(@project_id)