Repository: ambari Updated Branches: refs/heads/branch-2.1 86431d521 -> b404e04c8
AMBARI-13447 Redirect to Admin View after navigating to Install Wizard. (atkach) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/b404e04c Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/b404e04c Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/b404e04c Branch: refs/heads/branch-2.1 Commit: b404e04c89b45c31644786b34ec5924fe4401c14 Parents: 86431d5 Author: Andrii Tkach <atk...@hortonworks.com> Authored: Fri Oct 16 12:49:09 2015 +0300 Committer: Andrii Tkach <atk...@hortonworks.com> Committed: Fri Oct 16 13:38:38 2015 +0300 ---------------------------------------------------------------------- ambari-web/app/router.js | 116 +++++++++++------- ambari-web/test/router_test.js | 238 ++++++++++++++++++++++++++++++++++++ 2 files changed, 310 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/b404e04c/ambari-web/app/router.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/router.js b/ambari-web/app/router.js index f6ffc41..f3bcbfe 100644 --- a/ambari-web/app/router.js +++ b/ambari-web/app/router.js @@ -313,57 +313,81 @@ App.Router = Em.Router.extend({ }, + /** + * success callback of login request + * @param {object} clustersData + * @param {object} opt + * @param {object} params + */ loginGetClustersSuccessCallback: function (clustersData, opt, params) { - var loginController = this.get('loginController'); - var loginData = params.loginData; - var privileges = loginData.privileges || []; + var privileges = params.loginData.privileges || []; var router = this; - var permissionList = privileges.mapProperty('PrivilegeInfo.permission_name'); - var isAdmin = permissionList.contains('AMBARI.ADMIN'); - var transitionToApp = false; - if (isAdmin) { - App.set('isAdmin', true); - if (clustersData.items.length) { - router.setClusterInstalled(clustersData); - transitionToApp = true; - } else { - App.ajax.send({ - name: 'ambari.service.load_server_version', - sender: this, - success: 'adminViewInfoSuccessCallback' - }); - } + var isAdmin = privileges.mapProperty('PrivilegeInfo.permission_name').contains('AMBARI.ADMIN'); + + App.set('isAdmin', isAdmin); + + if (clustersData.items.length) { + var clusterPermissions = privileges. + filterProperty('PrivilegeInfo.cluster_name', clustersData.items[0].Clusters.cluster_name). + mapProperty('PrivilegeInfo.permission_name'); + + //cluster installed + router.setClusterInstalled(clustersData); + if (clusterPermissions.contains('CLUSTER.OPERATE')) { + App.setProperties({ + isAdmin: true, + isOperator: true + }); + } + if (isAdmin || clusterPermissions.contains('CLUSTER.READ') || clusterPermissions.contains('CLUSTER.OPERATE')) { + router.transitionToApp(); } else { - if (clustersData.items.length) { - router.setClusterInstalled(clustersData); - //TODO: Iterate over clusters - var clusterName = clustersData.items[0].Clusters.cluster_name; - var clusterPermissions = privileges.filterProperty('PrivilegeInfo.cluster_name', clusterName).mapProperty('PrivilegeInfo.permission_name'); - if (clusterPermissions.contains('CLUSTER.OPERATE')) { - App.setProperties({ - isAdmin: true, - isOperator: true - }); - transitionToApp = true; - } else if (clusterPermissions.contains('CLUSTER.READ')) { - transitionToApp = true; - } - } + router.transitionToViews(); } - App.set('isPermissionDataLoaded', true); - if (transitionToApp) { - if (!router.restorePreferedPath()) { - router.getSection(function (route) { - router.transitionTo(route); - loginController.postLogin(true, true); - }); - } + } else { + if (isAdmin) { + router.transitionToAdminView(); } else { - App.router.get('mainViewsController').loadAmbariViews(); - router.transitionTo('main.views.index'); - loginController.postLogin(true,true); + router.transitionToViews(); } + } + App.set('isPermissionDataLoaded', true); + App.router.get('userSettingsController').dataLoading(); }, + + /** + * redirect user to Admin View + * @returns {$.ajax} + */ + transitionToAdminView: function() { + return App.ajax.send({ + name: 'ambari.service.load_server_version', + sender: this, + success: 'adminViewInfoSuccessCallback', + error: 'adminViewInfoErrorCallback' + }); + }, + + /** + * redirect user to application Dashboard + */ + transitionToApp: function () { + var router = this; + if (!router.restorePreferedPath()) { + router.getSection(function (route) { + router.transitionTo(route); + }); + } + }, + + /** + * redirect user to application Views + */ + transitionToViews: function() { + App.router.get('mainViewsController').loadAmbariViews(); + this.transitionTo('main.views.index'); + }, + adminViewInfoSuccessCallback: function(data) { var components = Em.get(data,'components'); if (Em.isArray(components)) { @@ -378,6 +402,10 @@ App.Router = Em.Router.extend({ } }, + adminViewInfoErrorCallback: function() { + this.transitionToViews(); + }, + getSection: function (callback) { if (App.get('testMode')) { if (App.alwaysGoToInstaller) { http://git-wip-us.apache.org/repos/asf/ambari/blob/b404e04c/ambari-web/test/router_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/router_test.js b/ambari-web/test/router_test.js index 9122b54..057a13f 100644 --- a/ambari-web/test/router_test.js +++ b/ambari-web/test/router_test.js @@ -208,4 +208,242 @@ describe('App.Router', function () { expect(router.get('preferedPath')).to.be.null; }); }); + + describe("#loginGetClustersSuccessCallback()", function () { + var mock = {dataLoading: Em.K}; + beforeEach(function () { + sinon.stub(router, 'setClusterInstalled'); + sinon.stub(router, 'transitionToApp'); + sinon.stub(router, 'transitionToViews'); + sinon.stub(router, 'transitionToAdminView'); + sinon.stub(App.router, 'get').returns(mock); + sinon.spy(mock, 'dataLoading'); + App.setProperties({ + isAdmin: false, + isOperator: false, + isPermissionDataLoaded: false + }); + }); + afterEach(function () { + router.setClusterInstalled.restore(); + router.transitionToApp.restore(); + router.transitionToViews.restore(); + router.transitionToAdminView.restore(); + App.router.get.restore(); + mock.dataLoading.restore(); + }); + it("cluster exists, OPERATOR privileges", function () { + var clusterData = { + items: [{ + Clusters: { + cluster_name: 'c1' + } + }] + }; + var params = { + loginData: { + privileges: [{ + PrivilegeInfo: { + cluster_name: 'c1', + permission_name: 'CLUSTER.OPERATE' + } + }] + } + }; + router.loginGetClustersSuccessCallback(clusterData, {}, params); + expect(router.setClusterInstalled.calledWith(clusterData)).to.be.true; + expect(router.transitionToApp.calledOnce).to.be.true; + expect(App.get('isAdmin')).to.be.true; + expect(App.get('isOperator')).to.be.true; + expect(App.get('isPermissionDataLoaded')).to.be.true; + expect(mock.dataLoading.calledOnce).to.be.true; + }); + it("cluster exists, READ privileges", function () { + var clusterData = { + items: [{ + Clusters: { + cluster_name: 'c1' + } + }] + }; + var params = { + loginData: { + privileges: [{ + PrivilegeInfo: { + cluster_name: 'c1', + permission_name: 'CLUSTER.READ' + } + }] + } + }; + router.loginGetClustersSuccessCallback(clusterData, {}, params); + expect(router.setClusterInstalled.calledWith(clusterData)).to.be.true; + expect(router.transitionToApp.calledOnce).to.be.true; + expect(App.get('isAdmin')).to.be.false; + expect(App.get('isOperator')).to.be.false; + expect(App.get('isPermissionDataLoaded')).to.be.true; + expect(mock.dataLoading.calledOnce).to.be.true; + }); + it("cluster exists, ADMIN privileges", function () { + var clusterData = { + items: [{ + Clusters: { + cluster_name: 'c1' + } + }] + }; + var params = { + loginData: { + privileges: [{ + PrivilegeInfo: { + cluster_name: 'c1', + permission_name: 'AMBARI.ADMIN' + } + }] + } + }; + router.loginGetClustersSuccessCallback(clusterData, {}, params); + expect(router.setClusterInstalled.calledWith(clusterData)).to.be.true; + expect(router.transitionToApp.calledOnce).to.be.true; + expect(App.get('isAdmin')).to.be.true; + expect(App.get('isOperator')).to.be.false; + expect(App.get('isPermissionDataLoaded')).to.be.true; + expect(mock.dataLoading.calledOnce).to.be.true; + }); + it("cluster exists, no privileges", function () { + var clusterData = { + items: [{ + Clusters: { + cluster_name: 'c1' + } + }] + }; + var params = { + loginData: { + privileges: [] + } + }; + router.loginGetClustersSuccessCallback(clusterData, {}, params); + expect(router.setClusterInstalled.calledWith(clusterData)).to.be.true; + expect(router.transitionToViews.calledOnce).to.be.true; + expect(App.get('isAdmin')).to.be.false; + expect(App.get('isOperator')).to.be.false; + expect(App.get('isPermissionDataLoaded')).to.be.true; + expect(mock.dataLoading.calledOnce).to.be.true; + }); + it("cluster not installed, ADMIN privileges", function () { + var clusterData = { + items: [] + }; + var params = { + loginData: { + privileges: [{ + PrivilegeInfo: { + cluster_name: 'c1', + permission_name: 'AMBARI.ADMIN' + } + }] + } + }; + router.loginGetClustersSuccessCallback(clusterData, {}, params); + expect(router.transitionToAdminView.calledOnce).to.be.true; + expect(App.get('isAdmin')).to.be.true; + expect(App.get('isOperator')).to.be.false; + expect(App.get('isPermissionDataLoaded')).to.be.true; + expect(mock.dataLoading.calledOnce).to.be.true; + }); + it("cluster not installed, non-admin privileges", function () { + var clusterData = { + items: [] + }; + var params = { + loginData: { + privileges: [] + } + }; + router.loginGetClustersSuccessCallback(clusterData, {}, params); + expect(router.transitionToViews.calledOnce).to.be.true; + expect(App.get('isAdmin')).to.be.false; + expect(App.get('isOperator')).to.be.false; + expect(App.get('isPermissionDataLoaded')).to.be.true; + expect(mock.dataLoading.calledOnce).to.be.true; + }); + }); + + describe("#transitionToAdminView()", function () { + beforeEach(function () { + sinon.stub(App.ajax, 'send'); + }); + afterEach(function () { + App.ajax.send.restore(); + }); + it("", function () { + router.transitionToAdminView(); + expect(App.ajax.send.calledWith({ + name: 'ambari.service.load_server_version', + sender: router, + success: 'adminViewInfoSuccessCallback', + error: 'adminViewInfoErrorCallback' + })).to.be.true; + }); + }); + + describe("#transitionToApp()", function () { + beforeEach(function () { + this.mock = sinon.stub(router, 'restorePreferedPath'); + sinon.stub(router, 'getSection', function (callback) { + callback('route'); + }); + sinon.stub(router, 'transitionTo'); + }); + afterEach(function () { + this.mock.restore(); + router.getSection.restore(); + router.transitionTo.restore(); + }); + it("has restore path", function () { + this.mock.returns(true); + router.transitionToApp(); + expect(router.getSection.called).to.be.false; + expect(router.transitionTo.called).to.be.false; + }); + it("does not have restore path", function () { + this.mock.returns(false); + router.transitionToApp(); + expect(router.getSection.calledOnce).to.be.true; + expect(router.transitionTo.calledWith('route')).to.be.true; + }); + }); + + describe("#transitionToViews()", function () { + var mock = {loadAmbariViews: Em.K}; + beforeEach(function () { + sinon.stub(App.router, 'get').returns(mock); + sinon.stub(router, 'transitionTo'); + sinon.spy(mock, 'loadAmbariViews'); + }); + afterEach(function () { + App.router.get.restore(); + router.transitionTo.restore(); + mock.loadAmbariViews.restore(); + }); + it("", function () { + router.transitionToViews(); + expect(mock.loadAmbariViews.calledOnce).to.be.true; + expect(router.transitionTo.calledWith('main.views.index')).to.be.true; + }); + }); + + describe("#adminViewInfoErrorCallback()", function () { + beforeEach(function () { + sinon.stub(router, 'transitionToViews'); + }); + afterEach(function () { + router.transitionToViews.restore(); + }); + it("", function () { + router.adminViewInfoErrorCallback(); + expect(router.transitionToViews.calledOnce).to.be.true; + }); + }); });