decaffeinate: Convert CompileControllerTests.coffee and 17 other files to JS

This commit is contained in:
decaffeinate
2020-02-19 12:15:08 +01:00
committed by mserranom
parent 18e6b4715d
commit 79a0891fee
18 changed files with 3291 additions and 2401 deletions

View File

@@ -1,158 +1,219 @@
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
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const should = require('chai').should();
const SandboxedModule = require('sandboxed-module');
const assert = require('assert');
const path = require('path');
const sinon = require('sinon');
const modulePath = path.join(__dirname, "../../../app/js/StaticServerForbidSymlinks");
const { expect } = require("chai");
describe "StaticServerForbidSymlinks", ->
describe("StaticServerForbidSymlinks", function() {
beforeEach ->
beforeEach(function() {
@settings =
path:
this.settings = {
path: {
compilesDir: "/compiles/here"
}
};
@fs = {}
@ForbidSymlinks = SandboxedModule.require modulePath, requires:
"settings-sharelatex":@settings
"logger-sharelatex":
log:->
warn:->
error:->
"fs":@fs
this.fs = {};
this.ForbidSymlinks = SandboxedModule.require(modulePath, { requires: {
"settings-sharelatex":this.settings,
"logger-sharelatex": {
log() {},
warn() {},
error() {}
},
"fs":this.fs
}
}
);
@dummyStatic = (rootDir, options) ->
return (req, res, next) ->
# console.log "dummyStatic serving file", rootDir, "called with", req.url
# serve it
this.dummyStatic = (rootDir, options) =>
(req, res, next) =>
// console.log "dummyStatic serving file", rootDir, "called with", req.url
// serve it
next()
;
@StaticServerForbidSymlinks = @ForbidSymlinks @dummyStatic, @settings.path.compilesDir
@req =
params:
this.StaticServerForbidSymlinks = this.ForbidSymlinks(this.dummyStatic, this.settings.path.compilesDir);
this.req = {
params: {
project_id:"12345"
}
};
@res = {}
@req.url = "/12345/output.pdf"
this.res = {};
return this.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")
describe("sending a normal file through", function() {
beforeEach(function() {
return this.fs.realpath = sinon.stub().callsArgWith(1, null, `${this.settings.path.compilesDir}/${this.req.params.project_id}/output.pdf`);
});
it "should call next", (done)->
@res.sendStatus = (resCode)->
resCode.should.equal 200
done()
@StaticServerForbidSymlinks @req, @res, done
return it("should call next", function(done){
this.res.sendStatus = function(resCode){
resCode.should.equal(200);
return done();
};
return this.StaticServerForbidSymlinks(this.req, this.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")
describe("with a missing file", function() {
beforeEach(function() {
return this.fs.realpath = sinon.stub().callsArgWith(1, {code: 'ENOENT'}, `${this.settings.path.compilesDir}/${this.req.params.project_id}/unknown.pdf`);
});
it "should send a 404", (done)->
@res.sendStatus = (resCode)->
resCode.should.equal 404
done()
@StaticServerForbidSymlinks @req, @res
return it("should send a 404", function(done){
this.res.sendStatus = function(resCode){
resCode.should.equal(404);
return done();
};
return this.StaticServerForbidSymlinks(this.req, this.res);
});
});
describe "with a symlink file", ->
beforeEach ->
@fs.realpath = sinon.stub().callsArgWith(1, null, "/etc/#{@req.params.project_id}/output.pdf")
describe("with a symlink file", function() {
beforeEach(function() {
return this.fs.realpath = sinon.stub().callsArgWith(1, null, `/etc/${this.req.params.project_id}/output.pdf`);
});
it "should send a 404", (done)->
@res.sendStatus = (resCode)->
resCode.should.equal 404
done()
@StaticServerForbidSymlinks @req, @res
return it("should send a 404", function(done){
this.res.sendStatus = function(resCode){
resCode.should.equal(404);
return done();
};
return this.StaticServerForbidSymlinks(this.req, this.res);
});
});
describe "with a relative file", ->
beforeEach ->
@req.url = "/12345/../67890/output.pdf"
describe("with a relative file", function() {
beforeEach(function() {
return this.req.url = "/12345/../67890/output.pdf";
});
it "should send a 404", (done)->
@res.sendStatus = (resCode)->
resCode.should.equal 404
done()
@StaticServerForbidSymlinks @req, @res
return it("should send a 404", function(done){
this.res.sendStatus = function(resCode){
resCode.should.equal(404);
return done();
};
return this.StaticServerForbidSymlinks(this.req, this.res);
});
});
describe "with a unnormalized file containing .", ->
beforeEach ->
@req.url = "/12345/foo/./output.pdf"
describe("with a unnormalized file containing .", function() {
beforeEach(function() {
return this.req.url = "/12345/foo/./output.pdf";
});
it "should send a 404", (done)->
@res.sendStatus = (resCode)->
resCode.should.equal 404
done()
@StaticServerForbidSymlinks @req, @res
return it("should send a 404", function(done){
this.res.sendStatus = function(resCode){
resCode.should.equal(404);
return done();
};
return this.StaticServerForbidSymlinks(this.req, this.res);
});
});
describe "with a file containing an empty path", ->
beforeEach ->
@req.url = "/12345/foo//output.pdf"
describe("with a file containing an empty path", function() {
beforeEach(function() {
return this.req.url = "/12345/foo//output.pdf";
});
it "should send a 404", (done)->
@res.sendStatus = (resCode)->
resCode.should.equal 404
done()
@StaticServerForbidSymlinks @req, @res
return it("should send a 404", function(done){
this.res.sendStatus = function(resCode){
resCode.should.equal(404);
return done();
};
return this.StaticServerForbidSymlinks(this.req, this.res);
});
});
describe "with a non-project file", ->
beforeEach ->
@req.url = "/.foo/output.pdf"
describe("with a non-project file", function() {
beforeEach(function() {
return this.req.url = "/.foo/output.pdf";
});
it "should send a 404", (done)->
@res.sendStatus = (resCode)->
resCode.should.equal 404
done()
@StaticServerForbidSymlinks @req, @res
return it("should send a 404", function(done){
this.res.sendStatus = function(resCode){
resCode.should.equal(404);
return done();
};
return this.StaticServerForbidSymlinks(this.req, this.res);
});
});
describe "with a file outside the compiledir", ->
beforeEach ->
@req.url = "/../bar/output.pdf"
describe("with a file outside the compiledir", function() {
beforeEach(function() {
return this.req.url = "/../bar/output.pdf";
});
it "should send a 404", (done)->
@res.sendStatus = (resCode)->
resCode.should.equal 404
done()
@StaticServerForbidSymlinks @req, @res
return it("should send a 404", function(done){
this.res.sendStatus = function(resCode){
resCode.should.equal(404);
return done();
};
return this.StaticServerForbidSymlinks(this.req, this.res);
});
});
describe "with a file with no leading /", ->
beforeEach ->
@req.url = "./../bar/output.pdf"
describe("with a file with no leading /", function() {
beforeEach(function() {
return this.req.url = "./../bar/output.pdf";
});
it "should send a 404", (done)->
@res.sendStatus = (resCode)->
resCode.should.equal 404
done()
@StaticServerForbidSymlinks @req, @res
return it("should send a 404", function(done){
this.res.sendStatus = function(resCode){
resCode.should.equal(404);
return done();
};
return this.StaticServerForbidSymlinks(this.req, this.res);
});
});
describe "with a github style path", ->
beforeEach ->
@req.url = "/henryoswald-latex_example/output/output.log"
@fs.realpath = sinon.stub().callsArgWith(1, null, "#{@settings.path.compilesDir}/henryoswald-latex_example/output/output.log")
describe("with a github style path", function() {
beforeEach(function() {
this.req.url = "/henryoswald-latex_example/output/output.log";
return this.fs.realpath = sinon.stub().callsArgWith(1, null, `${this.settings.path.compilesDir}/henryoswald-latex_example/output/output.log`);
});
it "should call next", (done)->
@res.sendStatus = (resCode)->
resCode.should.equal 200
done()
@StaticServerForbidSymlinks @req, @res, done
return it("should call next", function(done){
this.res.sendStatus = function(resCode){
resCode.should.equal(200);
return done();
};
return this.StaticServerForbidSymlinks(this.req, this.res, done);
});
});
describe "with an error from fs.realpath", ->
return describe("with an error from fs.realpath", function() {
beforeEach ->
@fs.realpath = sinon.stub().callsArgWith(1, "error")
beforeEach(function() {
return this.fs.realpath = sinon.stub().callsArgWith(1, "error");
});
it "should send a 500", (done)->
@res.sendStatus = (resCode)->
resCode.should.equal 500
done()
@StaticServerForbidSymlinks @req, @res
return it("should send a 500", function(done){
this.res.sendStatus = function(resCode){
resCode.should.equal(500);
return done();
};
return this.StaticServerForbidSymlinks(this.req, this.res);
});
});
});