Repository: couchdb-fauxton Updated Branches: refs/heads/master deaf53c9f -> 9ab1292d4
Detect if we run on a backdoor port This enables us to find out in Fauxton if we run on a backdoor port. COUCHDB-2601 COUCHDB-2599 COUCHDB-2390 COUCHDB-2390 PR: #400 PR-URL: https://github.com/apache/couchdb-fauxton/pull/400 Reviewed-By: garren smith <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/commit/9ab1292d Tree: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/tree/9ab1292d Diff: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/diff/9ab1292d Branch: refs/heads/master Commit: 9ab1292d4e926ecba7c15291a04feb0342b40b21 Parents: deaf53c Author: Robert Kowalski <[email protected]> Authored: Thu May 7 14:00:38 2015 +0200 Committer: Robert Kowalski <[email protected]> Committed: Wed May 13 11:25:30 2015 +0200 ---------------------------------------------------------------------- Gruntfile.js | 2 +- app/addons/auth/base.js | 12 +++-- app/addons/auth/test/baseSpec.js | 57 +++++++++++++++------ app/app.js | 24 +++++++++ app/tests/appSpec.js | 96 +++++++++++++++++++++++++++++++++++ 5 files changed, 172 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/9ab1292d/Gruntfile.js ---------------------------------------------------------------------- diff --git a/Gruntfile.js b/Gruntfile.js index b04993e..bd6b9d7 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -365,7 +365,7 @@ module.exports = function (grunt) { mochaSetup: { default: { files: { - src: initHelper.watchFiles(['[Ss]pec.js'], ['./app/addons/**/*[Ss]pec.js', './app/addons/**/*[Ss]pec.react.js', './app/core/**/*[Ss]pec.js']) + src: initHelper.watchFiles(['[Ss]pec.js'], ['./app/addons/**/*[Ss]pec.js', './app/addons/**/*[Ss]pec.react.js', './app/core/**/*[Ss]pec.js', './app/**/*[Ss]pec.js']) }, template: 'test/test.config.underscore', config: './app/config.js' http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/9ab1292d/app/addons/auth/base.js ---------------------------------------------------------------------- diff --git a/app/addons/auth/base.js b/app/addons/auth/base.js index 8f4b1b3..4d0bb0c 100644 --- a/app/addons/auth/base.js +++ b/app/addons/auth/base.js @@ -13,11 +13,13 @@ define([ "app", "api", - "addons/auth/routes" + 'addons/auth/routes' ], function (app, FauxtonAPI, Auth) { + var isRunningOnBackdoorPort = null; + Auth.session = new Auth.Session(); FauxtonAPI.setSession(Auth.session); app.session = Auth.session; @@ -37,6 +39,7 @@ function (app, FauxtonAPI, Auth) { var link = {}; if (session.isAdminParty()) { + link = { id: "auth", title: "Admin Party!", @@ -75,8 +78,11 @@ function (app, FauxtonAPI, Auth) { }); - Auth.session.fetchUser().then(function () { - Auth.session.trigger('change'); + FauxtonAPI.isRunningOnBackdoorPort().then(function (res) { + isRunningOnBackdoorPort = res.runsOnBackportPort; + Auth.session.fetchUser().then(function () { + Auth.session.trigger('change'); + }); }); var auth = function (session, roles) { http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/9ab1292d/app/addons/auth/test/baseSpec.js ---------------------------------------------------------------------- diff --git a/app/addons/auth/test/baseSpec.js b/app/addons/auth/test/baseSpec.js index cc5e83f..d22f710 100644 --- a/app/addons/auth/test/baseSpec.js +++ b/app/addons/auth/test/baseSpec.js @@ -21,12 +21,25 @@ define([ describe("failed login", function () { + after(function () { + FauxtonAPI.session.off('change'); + testUtils.restore(FauxtonAPI.session.isAdminParty); + testUtils.restore(FauxtonAPI.isRunningOnBackdoorPort); + testUtils.restore(FauxtonAPI.navigate); + }); + it("redirects with replace: true set", function () { var navigateSpy = sinon.spy(FauxtonAPI, 'navigate'); - FauxtonAPI.auth = new Auth(); - FauxtonAPI.session.isLoggedIn = function () { return false; }; + + var stub = sinon.stub(FauxtonAPI.session, 'isAdminParty').returns(true); + var deferred = FauxtonAPI.Deferred(); + sinon.stub(FauxtonAPI, 'isRunningOnBackdoorPort').returns(deferred); Base.initialize(); + + deferred.resolve({runsOnBackportPort: false}); + FauxtonAPI.auth.authDeniedCb(); + assert.ok(navigateSpy.withArgs('/login?urlback=', {replace: true}).calledOnce); }); }); @@ -35,20 +48,28 @@ define([ describe('auth session change', function () { afterEach(function () { - FauxtonAPI.updateHeaderLink.restore && FauxtonAPI.updateHeaderLink.restore(); - FauxtonAPI.session.isAdminParty.restore && FauxtonAPI.session.isAdminParty.restore(); + testUtils.restore(FauxtonAPI.updateHeaderLink); + testUtils.restore(FauxtonAPI.session.isAdminParty); + testUtils.restore(FauxtonAPI.isRunningOnBackdoorPort); + testUtils.restore(FauxtonAPI.session.isLoggedIn); + testUtils.restore(FauxtonAPI.session.user); + FauxtonAPI.session.off('change'); }); it('for admin party changes title to admin party', function () { var spy = sinon.spy(FauxtonAPI, 'updateHeaderLink'); var stub = sinon.stub(FauxtonAPI.session, 'isAdminParty').returns(true); + var deferred = FauxtonAPI.Deferred(); + sinon.stub(FauxtonAPI, 'isRunningOnBackdoorPort').returns(deferred); + Base.initialize(); + deferred.resolve({runsOnBackportPort: true}); + FauxtonAPI.session.trigger('change'); assert.ok(spy.calledOnce); var args = spy.getCall(0).args[0]; assert.ok(args.title.match(/Admin Party/)); - FauxtonAPI.session.isAdminParty.restore(); }); it('for login changes title to login', function () { @@ -56,47 +77,53 @@ define([ var stub = sinon.stub(FauxtonAPI.session, 'isAdminParty').returns(false); sinon.stub(FauxtonAPI.session, 'user').returns({name: 'test-user'}); sinon.stub(FauxtonAPI.session, 'isLoggedIn').returns(true); + var deferred = FauxtonAPI.Deferred(); + sinon.stub(FauxtonAPI, 'isRunningOnBackdoorPort').returns(deferred); + Base.initialize(); + deferred.resolve({runsOnBackportPort: true}); + FauxtonAPI.session.trigger('change'); assert.ok(spy.calledOnce); var args = spy.getCall(0).args[0]; assert.equal(args.title, 'test-user'); - FauxtonAPI.session.isLoggedIn.restore(); - FauxtonAPI.session.user.restore(); - FauxtonAPI.session.isAdminParty.restore(); + }); it('for login adds logout link', function () { - var spy = sinon.spy(FauxtonAPI, 'addHeaderLink'); var stub = sinon.stub(FauxtonAPI.session, 'isAdminParty').returns(false); sinon.stub(FauxtonAPI.session, 'user').returns({name: 'test-user'}); sinon.stub(FauxtonAPI.session, 'isLoggedIn').returns(true); + var deferred = FauxtonAPI.Deferred(); + sinon.stub(FauxtonAPI, 'isRunningOnBackdoorPort').returns(deferred); + Base.initialize(); + deferred.resolve({runsOnBackportPort: false}); + var spy = sinon.spy(FauxtonAPI, 'addHeaderLink'); FauxtonAPI.session.trigger('change'); assert.ok(spy.calledOnce); var args = spy.getCall(0).args[0]; assert.equal(args.title, 'Logout'); - FauxtonAPI.session.isLoggedIn.restore(); - FauxtonAPI.session.user.restore(); - FauxtonAPI.session.isAdminParty.restore(); }); it('for logout, removes logout link', function () { var spy = sinon.spy(FauxtonAPI, 'removeHeaderLink'); var stub = sinon.stub(FauxtonAPI.session, 'isAdminParty').returns(false); sinon.stub(FauxtonAPI.session, 'isLoggedIn').returns(false); + var deferred = FauxtonAPI.Deferred(); + sinon.stub(FauxtonAPI, 'isRunningOnBackdoorPort').returns(deferred); + Base.initialize(); + deferred.resolve({runsOnBackportPort: true}); + FauxtonAPI.session.trigger('change'); assert.ok(spy.calledOnce); var args = spy.getCall(0).args[0]; assert.equal(args.id, 'logout'); - FauxtonAPI.session.isLoggedIn.restore(); - FauxtonAPI.session.isAdminParty.restore(); }); - }); }); http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/9ab1292d/app/app.js ---------------------------------------------------------------------- diff --git a/app/app.js b/app/app.js index 6483e5a..a75949d 100644 --- a/app/app.js +++ b/app/app.js @@ -126,7 +126,31 @@ function (app, $, _, Backbone, Bootstrap, Helpers, Utils, FauxtonAPI, Couchdb) { type: 'REMOVE_NAVBAR_LINK', link: link }); + }, + + isRunningOnBackdoorPort: function () { + if (this._backdoorDeferred) { + return this._backdoorDeferred; + } + + this._backdoorDeferred = FauxtonAPI.Deferred(); + + $.ajax({ + type: 'GET', + url: app.host + '/_cluster_setup' + }) + .then(function () { + this._backdoorDeferred.resolve({runsOnBackportPort: false}); + }.bind(this)) + .fail(function (res) { + this._backdoorDeferred.resolve({ + runsOnBackportPort: res.status === 400 + }); + }.bind(this)); + + return this._backdoorDeferred; } + }); return app; http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/9ab1292d/app/tests/appSpec.js ---------------------------------------------------------------------- diff --git a/app/tests/appSpec.js b/app/tests/appSpec.js new file mode 100644 index 0000000..bf38f87 --- /dev/null +++ b/app/tests/appSpec.js @@ -0,0 +1,96 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. +define([ + 'app', + 'api', + 'testUtils', +], function (app, FauxtonAPI, testUtils) { + var assert = testUtils.assert; + + describe('app', function () { + + describe('isRunningOnBackdoorPort', function () { + var server, appHost; + beforeEach(function () { + appHost = app.host; + app.host = 'http://example.com'; + server = sinon.fakeServer.create(); + }); + afterEach(function () { + app.host = appHost; + testUtils.restore(server); + testUtils.restore($.ajax); + FauxtonAPI._backdoorDeferred = null; + }); + + it('caches content', function (done) { + server.respondWith('GET', 'http://example.com/_cluster_setup', + [200, { "Content-Type": "application/json" }, + '[{ "id": 12, "comment": "Hey there" }]']); + + var spyAjax = sinon.spy($, 'ajax'); + var promise = FauxtonAPI.isRunningOnBackdoorPort(); + server.respond(); + + promise.then(function (res) { + var promise2 = FauxtonAPI.isRunningOnBackdoorPort(); + promise2.then(function (res) { + + assert.ok(spyAjax.calledOnce); + done(); + }); + }); + }); + + it('returns false on a 401', function (done) { + server.respondWith('GET', 'http://example.com/_cluster_setup', + [401, { "Content-Type": "application/json" }, '']); + + var promise = FauxtonAPI.isRunningOnBackdoorPort(); + server.respond(); + + promise.then(function (res) { + assert.deepEqual({runsOnBackportPort: false}, res); + done(); + }); + }); + + it('returns false on a 200', function (done) { + server.respondWith('GET', 'http://example.com/_cluster_setup', + [200, { "Content-Type": "application/json" }, '']); + + var promise = FauxtonAPI.isRunningOnBackdoorPort(); + server.respond(); + + promise.then(function (res) { + assert.deepEqual({runsOnBackportPort: false}, res); + done(); + }); + }); + + + it('returns true on a 400', function (done) { + server.respondWith('GET', 'http://example.com/_cluster_setup', + [400, {'Content-Type': 'application/json' }, + '[{ "id": 12, "comment": "Hey there" }]']); + + var promise = FauxtonAPI.isRunningOnBackdoorPort(); + server.respond(); + + promise.then(function (res) { + assert.deepEqual({runsOnBackportPort: true}, res); + done(); + }); + }); + }); + }); +});
