From 916b4cb40b1dd34d68b2ee19d799152a1404ddbb Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 27 Feb 2015 15:38:57 +0000 Subject: [PATCH] move convert tests from middleware to restricted static server --- .../StaticServerForbidSymlinksTests.coffee | 82 +++++++++++++++++++ .../SymlinkCheckerMiddlewearTests.coffee | 60 -------------- 2 files changed, 82 insertions(+), 60 deletions(-) create mode 100644 test/unit/coffee/StaticServerForbidSymlinksTests.coffee delete mode 100644 test/unit/coffee/SymlinkCheckerMiddlewearTests.coffee diff --git a/test/unit/coffee/StaticServerForbidSymlinksTests.coffee b/test/unit/coffee/StaticServerForbidSymlinksTests.coffee new file mode 100644 index 0000000..e17bace --- /dev/null +++ b/test/unit/coffee/StaticServerForbidSymlinksTests.coffee @@ -0,0 +1,82 @@ +should = require('chai').should() +SandboxedModule = require('sandboxed-module') +assert = require('assert') +path = require('path') +sinon = require('sinon') +modulePath = path.join __dirname, "../../../app/js/StaticServerForbidSymlinks" +expect = require("chai").expect + +describe "StaticServerForbidSymlinks", -> + + beforeEach -> + + @settings = + path: + compilesDir: "/compiles/here" + + @fs = {} + @ForbidSymlinks = SandboxedModule.require modulePath, requires: + "settings-sharelatex":@settings + "logger-sharelatex": + log:-> + warn:-> + "fs":@fs + + @dummyStatic = (rootDir, options) -> + return (req, res, next) -> + # console.log "dummyStatic serving file", rootDir, "called with", req.url + # serve it + next() + + @StaticServerForbidSymlinks = @ForbidSymlinks @dummyStatic, @settings.path.compilesDir + @req = + params: + project_id:"12345" + + @res = {} + @req.url = "/12345/output.pdf" + + + describe "sending a normal file through", -> + beforeEach -> + @fs.realpath = sinon.stub().callsArgWith(1, null, "#{@settings.path.compilesDir}/#{@req.params.project_id}/output.pdf") + + it "should call next", (done)-> + @res.sendStatus = (resCode)-> + resCode.should.equal 200 + done() + @StaticServerForbidSymlinks @req, @res, done + + + describe "with a missing file", -> + beforeEach -> + @fs.realpath = sinon.stub().callsArgWith(1, {code: 'ENOENT'}, "#{@settings.path.compilesDir}/#{@req.params.project_id}/unknown.pdf") + + it "should send a 404", (done)-> + @res.sendStatus = (resCode)-> + resCode.should.equal 404 + done() + @StaticServerForbidSymlinks @req, @res + + + describe "with a symlink file", -> + beforeEach -> + @fs.realpath = sinon.stub().callsArgWith(1, null, "/etc/#{@req.params.project_id}/output.pdf") + + it "should send a 404", (done)-> + @res.sendStatus = (resCode)-> + resCode.should.equal 404 + done() + @StaticServerForbidSymlinks @req, @res + + describe "with an error from fs.realpath", -> + + beforeEach -> + @fs.realpath = sinon.stub().callsArgWith(1, "error") + + it "should send a 500", (done)-> + @res.sendStatus = (resCode)-> + resCode.should.equal 500 + done() + @StaticServerForbidSymlinks @req, @res + diff --git a/test/unit/coffee/SymlinkCheckerMiddlewearTests.coffee b/test/unit/coffee/SymlinkCheckerMiddlewearTests.coffee deleted file mode 100644 index 50bd427..0000000 --- a/test/unit/coffee/SymlinkCheckerMiddlewearTests.coffee +++ /dev/null @@ -1,60 +0,0 @@ -should = require('chai').should() -SandboxedModule = require('sandboxed-module') -assert = require('assert') -path = require('path') -sinon = require('sinon') -modulePath = path.join __dirname, "../../../app/js/SymlinkCheckerMiddlewear" -expect = require("chai").expect - -describe "SymlinkCheckerMiddlewear", -> - - beforeEach -> - - @settings = - path: - compilesDir: "/compiles/here" - - @fs = {} - @SymlinkCheckerMiddlewear = SandboxedModule.require modulePath, requires: - "settings-sharelatex":@settings - "logger-sharelatex": - log:-> - warn:-> - "fs":@fs - @req = - params: - project_id:"12345" - - @res = {} - @req.params[0]= "output.pdf" - - - describe "sending a normal file through", -> - beforeEach -> - @fs.realpath = sinon.stub().callsArgWith(1, null, "#{@settings.path.compilesDir}/#{@req.params.project_id}/output.pdf") - - it "should call next", (done)-> - @SymlinkCheckerMiddlewear @req, @res, done - - - describe "with a symlink file", -> - beforeEach -> - @fs.realpath = sinon.stub().callsArgWith(1, null, "/etc/#{@req.params.project_id}/output.pdf") - - it "should send a 404", (done)-> - @res.send = (resCode)-> - resCode.should.equal 404 - done() - @SymlinkCheckerMiddlewear @req, @res - - describe "with an error from fs.realpath", -> - - beforeEach -> - @fs.realpath = sinon.stub().callsArgWith(1, "error") - - it "should send a 500", (done)-> - @res.send = (resCode)-> - resCode.should.equal 500 - done() - @SymlinkCheckerMiddlewear @req, @res -