Skip to content

Commit

Permalink
Tests for the proxy path
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio Cicali committed Dec 25, 2015
1 parent 3605b82 commit 8dffba2
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ globals:
process: true
require: true
module: true
sinon: true
console: true
expect: true
describe: true
it: true
afterEach: true
beforeEach: true
__dirname: true
Buffer: true
Git: true
Expand Down
6 changes: 4 additions & 2 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Released under the MIT license
*/

var express = require("express"),
var express = require("express"),
path = require("path"),
components = require("./components"),
wikiStatic = require("./wikistatic"),
Expand Down Expand Up @@ -135,7 +135,9 @@ module.exports.initialize = function (config) {

app.set("views", __dirname + "/../views");
app.set("view engine", "jade");
app.set("x-powered-by", false);

// Read this before disabling it https://github.com/strongloop/express/pull/2813#issuecomment-159270428
app.set("x-powered-by", true);

app.use(function (req, res, next) {

Expand Down
12 changes: 8 additions & 4 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,26 @@ module.exports = (function () {
return true;
},

get: function (key) {
get: function (key, useDefaults) {

if (!config) {
if (!config && !useDefaults) {
throw new Error("The configuration has not been read and cannot be `get`");
}

if (!config && useDefaults) {
return this.defaults[key];
}

if (typeof key == "undefined") {
return config;
}

return config[key];
},

getProxyPath: function () {
getProxyPath: function (override) {

var path = config.application.proxyPath.trim();
var path = (override || this.get("application", true).proxyPath).trim();

// @TODO make sure the path is something that makes sense (?)
if (path.length == 0 || path == "/") {
Expand Down
15 changes: 11 additions & 4 deletions lib/configurable.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
var _ = require("lodash");
var config = require("./config");
var configObject = require("./config");

var Configurable = function () {
this.config = {};
this.overrides = {};
this.configObject = configObject;
};

Configurable.prototype.configOverride = function (snippet) {
Expand All @@ -14,13 +15,19 @@ Configurable.prototype.getConfig = function () {

try {
// The config has been already loaded somewhere else
this.config = _.clone(config.get(), true);
this.config = _.clone(configObject.get(), true);
}
catch (e) {
this.config = _.clone(config.defaults, true);
this.config = _.clone(configObject.defaults, true);
}

return _.merge(this.config, this.overrides);
};

module.exports = Configurable;
// Provides a way to override the proxyPath
Configurable.prototype.getProxyPath = function () {

return configObject.getProxyPath(this.overrides.application && this.overrides.application.proxyPath);
};

module.exports = Configurable;
6 changes: 3 additions & 3 deletions lib/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ Page.prototype.save = function (message) {

Page.prototype.urlFor = function (action) {

return Page.urlFor(this.wikiname, action);
return Page.urlFor(this.wikiname, action, this.getProxyPath());
};

Page.urlFor = function (name, action) {
Page.urlFor = function (name, action, proxyPath) {

var wname = encodeURIComponent(name);
var proxyPath = app.getInstance().locals.config.getProxyPath();
proxyPath = proxyPath || "";

var url = "";

Expand Down
2 changes: 1 addition & 1 deletion lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function evalTags(text) {
if (parts[1]) {
pageName = parts[1];
}
url = Page.urlFor(namer.wikify(pageName), "show");
url = Page.urlFor(namer.wikify(pageName), "show", configuration.configObject.getProxyPath());

tagmap[k] = "<a class=\"internal\" href=\"" + url + "\">" + name + "</a>";
}
Expand Down
4 changes: 2 additions & 2 deletions routes/wiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var router = require("express").Router(),
models = require("../lib/models"),
app = require("../lib/app").getInstance();

var mountPath = app.locals.config.get("application").mountPath;
var proxyPath = app.locals.config.getProxyPath();

models.use(Git);

Expand Down Expand Up @@ -232,7 +232,7 @@ function _getCompare(req, res) {
}

function _getIndex(req, res) {
res.redirect(mountPath + "/wiki/" + app.locals.config.get("pages").index);
res.redirect(proxyPath + "/wiki/" + app.locals.config.get("pages").index);
}

module.exports = router;
72 changes: 54 additions & 18 deletions test/spec/configSpec.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
var yaml = require('js-yaml');
var yaml = require("js-yaml");

var configKeys = ['application', 'authentication', 'features', 'server', 'authorization', 'pages', 'customizations'];
var configKeys = ["application", "authentication", "features", "server", "authorization", "pages", "customizations"];
var Config = require("../../lib/config");

describe ("Config", function() {

Config.setup();

afterEach(function() {
afterEach(function () {
// Unset the config
Config.setup();
});

it ("should render a valid config in YAML", function() {
it ("should render a valid config in YAML", function () {

var def = yaml.load(Config.sample());

expect(Object.keys(def).join('')).to.equal(configKeys.join(''));
expect(Object.keys(def).join(")).to.equal(configKeys.join("));

expect(def.application.title).to.equal('Jingo');
expect(def.application.repository).to.equal('');
expect(def.application.docSubdir).to.equal('');
expect(def.application.remote).to.equal('');
expect(def.application.title).to.equal("Jingo");
expect(def.application.repository).to.equal("");
expect(def.application.docSubdir).to.equal("");
expect(def.application.remote).to.equal("");
expect(def.application.pushInterval).to.equal(30);
expect(def.application.secret).to.equal('change me');
expect(def.application.secret).to.equal("change me");
expect(def.application.git).to.equal("git");
expect(def.application.skipGitCheck).to.be.false;
expect(def.application.loggingMode).to.equal(1);
expect(def.application.pedanticMarkdown).to.be.true;
expect(def.application.gfmBreaks).to.be.true;
expect(def.application.staticWhitelist).to.equal("/\\.png$/i, /\\.jpg$/i, /\\.gif$/i"),
expect(def.application.proxyPath).to.equal("")

expect(def.features.codemirror).to.be.true;
expect(def.features.markitup).to.be.false;

expect(def.server.hostname).to.equal('localhost');
expect(def.server.hostname).to.equal("localhost");
expect(def.server.port).to.equal(6067);
expect(def.server.localOnly).to.be.false;
expect(def.server.baseUrl).to.equal("");

expect(def.authorization.anonRead).to.be.true;
expect(def.authorization.validMatches).to.equal('.+');
expect(def.authorization.validMatches).to.equal(".+");
expect(def.authorization.emptyEmailMatches).to.be.false;

expect(def.authentication.google.enabled).to.be.true;
expect(def.authentication.local.enabled).to.be.false;
expect(def.authentication.github.enabled).to.be.false;
});

it ("should get the config as a whole", function() {
it ("should get the config as a whole", function () {

var c;
try {
var c = Config.get();
} catch(e) {
c = 'boom';
}
catch (e) {
c = "boom";
}

expect(c).to.equal('boom');
expect(c).to.equal("boom");

Config.setup({
test: 23
Expand All @@ -58,7 +68,7 @@ describe ("Config", function() {
expect(Config.get()).to.be.an("object");
});

it ("should get a variable from the config", function() {
it ("should get a variable from the config", function () {

Config.setup({
test: 23,
Expand All @@ -71,4 +81,30 @@ describe ("Config", function() {
expect(Config.get("test1").test2).to.equal(44);
});

});
it ("should get the proxyPath when not set", function () {

expect(Config.getProxyPath()).to.equal("");
});

it ("should get the proxyPath when set", function () {

Config.setup({
application: {
proxyPath: "foobar"
}
});

expect(Config.getProxyPath()).to.equal("/foobar");
});

it ("should not get a variable from the ungettable config", function () {

expect(function () {
Config.get("test");
}).to.throw();
});

it ("should get a variable from the default if the config is not set up", function () {
expect(Config.get("application", true).title).to.equal("Jingo");
});
});
82 changes: 66 additions & 16 deletions test/spec/modelsSpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var fs = require('fs');
var fs = require("fs");
var models = require("../../lib/models");
var Configurable = require("../../lib/configurable");

var m;

Expand Down Expand Up @@ -45,18 +46,18 @@ describe ("Models", function () {
describe("Rename method", function () {

it ("should not rename if the destination exists", function (done) {
var stub0 = sinon.stub(fs, 'existsSync').returns(true);
var stub0 = sinon.stub(fs, "existsSync").returns(true);
m = getModel("verguenza");
m.renameTo('vergogna').catch(function() {
m.renameTo("vergogna").catch(function() {
stub0.restore();
done();
});
});

it ("should rename if the destination does not exist", function (done) {
var stub0 = sinon.stub(fs, 'existsSync').returns(false);
var stub0 = sinon.stub(fs, "existsSync").returns(false);
m = getModel("verguenza");
m.renameTo('vergogna').then(function () {
m.renameTo("vergogna").then(function () {
expect(m.name).to.equal("vergogna");
expect(m.wikiname).to.equal("vergogna");
expect(m.filename).to.equal("vergogna.md");
Expand All @@ -73,7 +74,7 @@ describe ("Models", function () {
m = getModel("verguenza");
m.title = "The huge";
m.content = "The verge";
var stub0 = sinon.stub(fs, 'writeFile').callsArgOn(2, m);
var stub0 = sinon.stub(fs, "writeFile").callsArgOn(2, m);
m.save().then(function (content) {
expect(content).to.equal("The verge");
stub0.restore();
Expand All @@ -96,7 +97,7 @@ describe ("Models", function () {

m.title = "The huge";
m.content = "The verge";
var stub0 = sinon.stub(fs, 'writeFile').callsArgOn(2, m);
var stub0 = sinon.stub(fs, "writeFile").callsArgOn(2, m);
m.save().then(function (content) {
expect(content).to.equal("# The huge\nThe verge");
stub0.restore();
Expand All @@ -107,20 +108,69 @@ describe ("Models", function () {

describe("UrlFor method", function () {

it ("should generate the correct url for page actions when a proxypath is not set", function () {

var Page = models.Page,
pname = "verguenza";

expect(Page.urlFor(pname, "show")).to.equal("/wiki/verguenza");
expect(Page.urlFor(pname, "edit")).to.equal("/pages/verguenza/edit");
expect(Page.urlFor(pname, "edit error")).to.equal("/pages/verguenza/edit?e=1");
expect(Page.urlFor(pname, "edit put")).to.equal("/pages/verguenza");
expect(Page.urlFor(pname, "history")).to.equal("/wiki/verguenza/history");
expect(Page.urlFor(pname, "compare")).to.equal("/wiki/verguenza/compare");
expect(Page.urlFor(pname, "new")).to.equal("/pages/new/verguenza");
expect(Page.urlFor(pname, "new error")).to.equal("/pages/new/verguenza?e=1");
});

it ("should generate the correct url for page actions when a proxypath is set", function () {

var Page = models.Page,
pname = "verguenza";

expect(Page.urlFor(pname, "show", "/bazinga")).to.equal("/bazinga/wiki/verguenza");
expect(Page.urlFor(pname, "edit", "/bazinga")).to.equal("/bazinga/pages/verguenza/edit");
expect(Page.urlFor(pname, "edit error", "/bazinga")).to.equal("/bazinga/pages/verguenza/edit?e=1");
expect(Page.urlFor(pname, "edit put", "/bazinga")).to.equal("/bazinga/pages/verguenza");
expect(Page.urlFor(pname, "history", "/bazinga")).to.equal("/bazinga/wiki/verguenza/history");
expect(Page.urlFor(pname, "compare", "/bazinga")).to.equal("/bazinga/wiki/verguenza/compare");
expect(Page.urlFor(pname, "new", "/bazinga")).to.equal("/bazinga/pages/new/verguenza");
expect(Page.urlFor(pname, "new error", "/bazinga")).to.equal("/bazinga/pages/new/verguenza?e=1");
});

it ("should generate the correct url for page actions", function () {

m = getModel("verguenza");
m = getModel("chupito");

expect(m.urlFor('show')).to.equal('/wiki/verguenza');
expect(m.urlFor('edit')).to.equal('/pages/verguenza/edit');
expect(m.urlFor('edit error')).to.equal('/pages/verguenza/edit?e=1');
expect(m.urlFor('edit put')).to.equal('/pages/verguenza');
expect(m.urlFor('history')).to.equal('/wiki/verguenza/history');
expect(m.urlFor('compare')).to.equal('/wiki/verguenza/compare');
expect(m.urlFor('new')).to.equal('/pages/new/verguenza');
expect(m.urlFor('new error')).to.equal('/pages/new/verguenza?e=1');
expect(m.urlFor("show")).to.equal("/wiki/chupito");
expect(m.urlFor("edit")).to.equal("/pages/chupito/edit");
expect(m.urlFor("edit error")).to.equal("/pages/chupito/edit?e=1");
expect(m.urlFor("edit put")).to.equal("/pages/chupito");
expect(m.urlFor("history")).to.equal("/wiki/chupito/history");
expect(m.urlFor("compare")).to.equal("/wiki/chupito/compare");
expect(m.urlFor("new")).to.equal("/pages/new/chupito");
expect(m.urlFor("new error")).to.equal("/pages/new/chupito?e=1");
});

it ("should generate the correct url for page actions with a set proxypath", function () {

m = getModel("chupito");

m.configOverride({
application: {
proxyPath: "lovely"
}
});

expect(m.urlFor("show")).to.equal("/lovely/wiki/chupito");
expect(m.urlFor("edit")).to.equal("/lovely/pages/chupito/edit");
expect(m.urlFor("edit error")).to.equal("/lovely/pages/chupito/edit?e=1");
expect(m.urlFor("edit put")).to.equal("/lovely/pages/chupito");
expect(m.urlFor("history")).to.equal("/lovely/wiki/chupito/history");
expect(m.urlFor("compare")).to.equal("/lovely/wiki/chupito/compare");
expect(m.urlFor("new")).to.equal("/lovely/pages/new/chupito");
expect(m.urlFor("new error")).to.equal("/lovely/pages/new/chupito?e=1");
});
});

describe("isIndex method", function () {
Expand Down

0 comments on commit 8dffba2

Please sign in to comment.