From 3922b8b9165579c5675a0df59fba3c2384dda461 Mon Sep 17 00:00:00 2001 From: Michael Mazour Date: Thu, 22 Mar 2018 09:49:12 +0000 Subject: [PATCH] Add project export acceptance tests - Add acceptance tests - Add `MockV1Api` helper - Add flush endpoint to `MockProjectHistoryApi` helper --- .../acceptance/coffee/ExportsTests.coffee | 56 +++++++++++++++++++ .../helpers/MockProjectHistoryApi.coffee | 12 ++++ .../coffee/helpers/MockV1Api.coffee | 34 +++++++++++ 3 files changed, 102 insertions(+) create mode 100644 services/web/test/acceptance/coffee/ExportsTests.coffee create mode 100644 services/web/test/acceptance/coffee/helpers/MockV1Api.coffee diff --git a/services/web/test/acceptance/coffee/ExportsTests.coffee b/services/web/test/acceptance/coffee/ExportsTests.coffee new file mode 100644 index 0000000000..7a6784008d --- /dev/null +++ b/services/web/test/acceptance/coffee/ExportsTests.coffee @@ -0,0 +1,56 @@ +expect = require('chai').expect +request = require './helpers/request' +_ = require 'underscore' + + +User = require './helpers/User' +ProjectGetter = require '../../../app/js/Features/Project/ProjectGetter.js' +ExportsHandler = require '../../../app/js/Features/Exports/ExportsHandler.js' + +MockProjectHistoryApi = require './helpers/MockProjectHistoryApi' +MockV1Api = require './helpers/MockV1Api' + +describe 'Exports', -> + before (done) -> + @brand_variation_id = '18' + @owner = new User() + @owner.login (error) => + throw error if error? + @owner.createProject 'example-project', {template: 'example'}, (error, @project_id) => + throw error if error? + done() + + describe 'exporting a project', -> + beforeEach (done) -> + @version = Math.floor(Math.random() * 10000) + MockProjectHistoryApi.setProjectVersion(@project_id, @version) + @export_id = Math.floor(Math.random() * 10000) + MockV1Api.setExportId(@export_id) + MockV1Api.clearExportParams() + @owner.request { + method: 'POST', + url: "/project/#{@project_id}/export/#{@brand_variation_id}", + json: {}, + }, (error, response, body) => + throw error if error? + expect(response.statusCode).to.equal 200 + @exportResponseBody = body + done() + + it 'should have sent correct data to v1', (done) -> + {project, user, destination, options} = MockV1Api.getLastExportParams() + # project details should match + expect(project.id).to.equal @project_id + expect(project.rootDocPath).to.equal '/main.tex' + # version should match what was retrieved from project-history + expect(project.historyVersion).to.equal @version + # user details should match + expect(user.id).to.equal @owner.id + expect(user.email).to.equal @owner.email + # brand-variation should match + expect(destination.brandVariationId).to.equal @brand_variation_id + done() + + it 'should have returned the export ID provided by v1', (done) -> + expect(@exportResponseBody.export_v1_id).to.equal @export_id + done() diff --git a/services/web/test/acceptance/coffee/helpers/MockProjectHistoryApi.coffee b/services/web/test/acceptance/coffee/helpers/MockProjectHistoryApi.coffee index 381d7ab272..b7df202d72 100644 --- a/services/web/test/acceptance/coffee/helpers/MockProjectHistoryApi.coffee +++ b/services/web/test/acceptance/coffee/helpers/MockProjectHistoryApi.coffee @@ -6,9 +6,14 @@ module.exports = MockProjectHistoryApi = oldFiles: {} + projectVersions: {} + addOldFile: (project_id, version, pathname, content) -> @oldFiles["#{project_id}:#{version}:#{pathname}"] = content + setProjectVersion: (project_id, version) -> + @projectVersions[project_id] = version + run: () -> app.post "/project", (req, res, next) => res.json project: id: 1 @@ -21,6 +26,13 @@ module.exports = MockProjectHistoryApi = else res.send 404 + app.get "/project/:project_id/version", (req, res, next) => + {project_id} = req.params + if @projectVersions[project_id]? + res.json version: @projectVersions[project_id] + else + res.send 404 + app.listen 3054, (error) -> throw error if error? .on "error", (error) -> diff --git a/services/web/test/acceptance/coffee/helpers/MockV1Api.coffee b/services/web/test/acceptance/coffee/helpers/MockV1Api.coffee new file mode 100644 index 0000000000..f27a4e1662 --- /dev/null +++ b/services/web/test/acceptance/coffee/helpers/MockV1Api.coffee @@ -0,0 +1,34 @@ +express = require("express") +app = express() +bodyParser = require('body-parser') + +app.use(bodyParser.json()) + +module.exports = MockV1Api = + + exportId: null + + exportParams: null + + setExportId: (id) -> + @exportId = id + + getLastExportParams: () -> + @exportParams + + clearExportParams: () -> + @exportParams = null + + run: () -> + app.post "/api/v1/sharelatex/exports", (req, res, next) => + #{project, version, pathname} + @exportParams = Object.assign({}, req.body) + res.json exportId: @exportId + + app.listen 5000, (error) -> + throw error if error? + .on "error", (error) -> + console.error "error starting MockOverleafAPI:", error.message + process.exit(1) + +MockV1Api.run()