From 77eac423c7e80a70a355a72fee1af9a2667d3b1b Mon Sep 17 00:00:00 2001 From: Hayden Faulds Date: Wed, 29 Nov 2017 11:07:49 +0000 Subject: [PATCH] add tpds project structure acceptance tests --- .../coffee/ProjectStructureTests.coffee | 119 +++++++++++++++++- .../coffee/helpers/MockDocUpdaterApi.coffee | 16 ++- services/web/test/acceptance/files/test.tex | 1 + 3 files changed, 121 insertions(+), 15 deletions(-) create mode 100644 services/web/test/acceptance/files/test.tex diff --git a/services/web/test/acceptance/coffee/ProjectStructureTests.coffee b/services/web/test/acceptance/coffee/ProjectStructureTests.coffee index c7f429ac40..e54d4fac9d 100644 --- a/services/web/test/acceptance/coffee/ProjectStructureTests.coffee +++ b/services/web/test/acceptance/coffee/ProjectStructureTests.coffee @@ -1,7 +1,10 @@ -async = require("async") +async = require "async" expect = require("chai").expect +mkdirp = require "mkdirp" +ObjectId = require("mongojs").ObjectId Path = require "path" fs = require "fs" +Settings = require "settings-sharelatex" _ = require "underscore" ProjectGetter = require "../../../app/js/Features/Project/ProjectGetter.js" @@ -20,7 +23,7 @@ describe "ProjectStructureChanges", -> describe "creating a project from the example template", -> before (done) -> MockDocUpdaterApi.clearProjectStructureUpdates() - @owner.createProject "project", {template: "example"}, (error, project_id) => + @owner.createProject "example-project", {template: "example"}, (error, project_id) => throw error if error? @example_project_id = project_id done() @@ -133,12 +136,14 @@ describe "ProjectStructureChanges", -> describe "uploading a file", -> before (done) -> - MockDocUpdaterApi.clearProjectStructureUpdates() ProjectGetter.getProject @example_project_id, (error, projects) => throw error if error? @root_folder_id = projects[0].rootFolder[0]._id.toString() done() + beforeEach () -> + MockDocUpdaterApi.clearProjectStructureUpdates() + it "should version a newly uploaded file", (done) -> image_file = fs.createReadStream(Path.resolve(__dirname + '/../files/1pixel.png')) @@ -195,6 +200,108 @@ describe "ProjectStructureChanges", -> done() describe "tpds", -> - it "should version add a doc" - it "should version add a new file" - it "should version replacing a file" + before (done) -> + @tpds_project_name = "tpds-project-#{new ObjectId().toString()}" + @owner.createProject @tpds_project_name, (error, project_id) => + throw error if error? + @tpds_project_id = project_id + mkdirp Settings.path.dumpFolder, done + + beforeEach () -> + MockDocUpdaterApi.clearProjectStructureUpdates() + + it "should version adding a doc", (done) -> + tex_file = fs.createReadStream(Path.resolve(__dirname + '/../files/test.tex')) + + req = @owner.request.post { + uri: "/user/#{@owner._id}/update/#{@tpds_project_name}/test.tex", + auth: + user: _.keys(Settings.httpAuthUsers)[0] + pass: _.values(Settings.httpAuthUsers)[0] + sendImmediately: true + } + + tex_file.on "error", (err) -> + throw err + + req.on "error", (err) -> + throw err + + req.on "response", (res) => + if res.statusCode < 200 || res.statusCode >= 300 + throw new Error("failed to upload file #{res.statusCode}") + + updates = MockDocUpdaterApi.getProjectStructureUpdates(@tpds_project_id).docUpdates + expect(updates.length).to.equal(1) + update = updates[0] + expect(update.userId).to.equal(@owner._id) + expect(update.pathname).to.equal("/test.tex") + expect(update.docLines).to.equal("Test") + + done() + + tex_file.pipe(req) + + it "should version adding a new file", (done) -> + image_file = fs.createReadStream(Path.resolve(__dirname + '/../files/1pixel.png')) + + req = @owner.request.post { + uri: "/user/#{@owner._id}/update/#{@tpds_project_name}/1pixel.png", + auth: + user: _.keys(Settings.httpAuthUsers)[0] + pass: _.values(Settings.httpAuthUsers)[0] + sendImmediately: true + } + + image_file.on "error", (err) -> + throw err + + req.on "error", (err) -> + throw err + + req.on "response", (res) => + if res.statusCode < 200 || res.statusCode >= 300 + throw new Error("failed to upload file #{res.statusCode}") + + updates = MockDocUpdaterApi.getProjectStructureUpdates(@tpds_project_id).fileUpdates + expect(updates.length).to.equal(1) + update = updates[0] + expect(update.userId).to.equal(@owner._id) + expect(update.pathname).to.equal("/1pixel.png") + expect(update.url).to.be.a('string'); + + done() + + image_file.pipe(req) + + it "should version replacing a file", (done) -> + image_file = fs.createReadStream(Path.resolve(__dirname + '/../files/2pixel.png')) + + req = @owner.request.post { + uri: "/user/#{@owner._id}/update/#{@tpds_project_name}/1pixel.png", + auth: + user: _.keys(Settings.httpAuthUsers)[0] + pass: _.values(Settings.httpAuthUsers)[0] + sendImmediately: true + } + + image_file.on "error", (err) -> + throw err + + req.on "error", (err) -> + throw err + + req.on "response", (res) => + if res.statusCode < 200 || res.statusCode >= 300 + throw new Error("failed to upload file #{res.statusCode}") + + updates = MockDocUpdaterApi.getProjectStructureUpdates(@tpds_project_id).fileUpdates + expect(updates.length).to.equal(1) + update = updates[0] + expect(update.userId).to.equal(@owner._id) + expect(update.pathname).to.equal("/1pixel.png") + expect(update.url).to.be.a('string'); + + done() + + image_file.pipe(req) diff --git a/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee b/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee index 2147ac2691..b00cd6b173 100644 --- a/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee +++ b/services/web/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee @@ -4,26 +4,24 @@ bodyParser = require "body-parser" jsonParser = bodyParser.json() module.exports = MockDocUpdaterApi = - project_structures_updates: {} + updates: {} clearProjectStructureUpdates: () -> - @project_structures_updates = {} + @updates = {} getProjectStructureUpdates: (project_id) -> - @project_structures_updates[project_id] + @updates[project_id] || { docUpdates: [], fileUpdates: [] } addProjectStructureUpdates: (project_id, userId, docUpdates, fileUpdates) -> - @project_structures_updates[project_id] ||= { - docUpdates: [] - fileUpdates: [] - } + @updates[project_id] ||= { docUpdates: [], fileUpdates: [] } + for update in docUpdates update.userId = userId - @project_structures_updates[project_id].docUpdates.push(update) + @updates[project_id].docUpdates.push(update) for update in fileUpdates update.userId = userId - @project_structures_updates[project_id].fileUpdates.push(update) + @updates[project_id].fileUpdates.push(update) run: () -> app.post "/project/:project_id/flush", (req, res, next) => diff --git a/services/web/test/acceptance/files/test.tex b/services/web/test/acceptance/files/test.tex new file mode 100644 index 0000000000..8318c86b35 --- /dev/null +++ b/services/web/test/acceptance/files/test.tex @@ -0,0 +1 @@ +Test \ No newline at end of file