Quellcode durchsuchen

AMBARI-7526. Unable to logout from Admin View, when ambari-server is stopped. (jaimin)

Jaimin Jetly vor 10 Jahren
Ursprung
Commit
3843209c12

+ 16 - 1
ambari-admin/pom.xml

@@ -15,7 +15,7 @@
    limitations under the License.
 -->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <parent>
     <groupId>org.apache.ambari</groupId>
     <artifactId>ambari-project</artifactId>
@@ -122,6 +122,21 @@
               </arguments>
             </configuration>
           </execution>
+          <execution>
+            <id>unit test</id>
+            <phase>test</phase>
+            <goals>
+              <goal>exec</goal>
+            </goals>
+            <configuration>
+              <workingDirectory>${basedir}/src/main/resources/ui/admin-web</workingDirectory>
+              <executable>npm</executable>
+              <arguments>
+                <argument>run</argument>
+                <argument>test-single-run</argument>
+              </arguments>
+            </configuration>
+          </execution>
         </executions>
       </plugin>
       <plugin>

+ 1 - 1
ambari-admin/src/main/resources/ui/admin-web/app/index.html

@@ -52,7 +52,7 @@
             <ul class="nav navbar-nav navbar-right">
               <li>
                 <div class="btn-group" dropdown is-open="status.isopen">
-                <button type="button" class="btn btn-default dropdown-toggle navbar-btn" ng-disabled="disabled">
+                  <button type="button" class="btn btn-default dropdown-toggle navbar-btn" ng-disabled="disabled">
                     <i class="fa fa-user"></i> {{currentUser}} <span class="caret"></span>
                   </button>
                   <ul class="dropdown-menu" role="menu">

+ 9 - 6
ambari-admin/src/main/resources/ui/admin-web/app/scripts/controllers/mainCtrl.js

@@ -18,13 +18,16 @@
 'use strict';
 
 angular.module('ambariAdminConsole')
-.controller('MainCtrl',['$scope', 'Auth', 'uiAlert', '$modal', 'Cluster', function($scope, Auth, uiAlert, $modal, Cluster) {
+.controller('MainCtrl',['$scope', '$window','Auth', 'uiAlert', '$modal', 'Cluster', function($scope, $window, Auth, uiAlert, $modal, Cluster) {
   $scope.signOut = function() {
-    Auth.signout().then(function() {
-     window.location.pathname = ''; // Change location hard, because Angular works only with relative urls
-    }).catch(function(data) {
-      uiAlert.danger(data.data.status, data.data.message);
-    });
+    var data = JSON.parse(localStorage.ambari);
+    delete data.app.authenticated;
+    delete data.app.loginName;
+    delete data.app.user;
+    localStorage.ambari = JSON.stringify(data);
+    $window.location.pathname = '';
+    $scope.hello = "hello";
+    Auth.signout();
   };
 
   $scope.about = function() {

+ 8 - 1
ambari-admin/src/main/resources/ui/admin-web/app/scripts/services/Auth.js

@@ -19,7 +19,14 @@
 
 angular.module('ambariAdminConsole')
 .factory('Auth',['$http', 'Settings', function($http, Settings) {
-	var currentUserName = JSON.parse(localStorage.ambari).app.loginName;
+  var ambari;
+  var currentUserName;
+  if (localStorage.ambari) {
+    ambari = JSON.parse(localStorage.ambari);
+    if (ambari && ambari.app && ambari.app.loginName) {
+      currentUserName = ambari.app.loginName;
+    }
+  }
   return {
     signout: function() {
       return $http({

+ 11 - 4
ambari-admin/src/main/resources/ui/admin-web/bower.json

@@ -3,14 +3,21 @@
   "private": true,
   "dependencies": {
     "bootstrap": "~3.1.1",
-    "angular": "~1.2.18",
-    "angular-route": "~1.2.18",
+    "angular": "~1.2.25",
+    "angular-route": "~1.2.25",
     "angular-bootstrap": "~0.11.0",
+    "underscore": "~1.7.0",
     "restangular": "~1.4.0",
     "angular-bootstrap-toggle-switch": "~0.5.1",
-    "angular-animate": "~1.2.23",
+    "angular-animate": "~1.2.25",
     "angular-translate": "~2.2.0",
     "font-awesome": "~4.2.0"
   },
-  "devDependencies": {}
+  "devDependencies": {
+    "angular-mocks": "~1.2.25",
+    "commonjs": "~0.2.0",
+    "chai": "~1.8.0",
+    "mocha": "~1.14.0",
+    "sinon": "~1.10.3"
+  }
 }

+ 19 - 0
ambari-admin/src/main/resources/ui/admin-web/package.json

@@ -4,6 +4,12 @@
   "dependencies": {
   },
   "devDependencies": {
+    "karma": "^0.12.16",
+    "karma-phantomjs-launcher": "~0.1",
+    "karma-chrome-launcher": "^0.1.4",
+    "karma-jasmine": "^0.1.5",
+    "protractor": "~1.0.0",
+    "http-server": "^0.6.1",
     "bower": "1.3.8",
     "gulp": "^3.6.0",
     "gulp-autoprefixer": "^0.0.7",
@@ -18,6 +24,19 @@
     "gulp-useref": "^0.4.2",
     "gulp-plumber": "*"
   },
+  "scripts": {
+    "postinstall": "bower install",
+    "prestart": "npm install",
+    "start": "http-server -a 0.0.0.0 -p 8000",
+    "pretest": "npm install",
+    "test": "node node_modules/karma/bin/karma start test/karma.conf.js",
+    "test-single-run": "node node_modules/karma/bin/karma start test/karma.conf.js  --single-run",
+    "preupdate-webdriver": "npm install",
+    "update-webdriver": "webdriver-manager update",
+    "preprotractor": "npm run update-webdriver",
+    "protractor": "protractor test/protractor-conf.js",
+    "update-index-async": "node -e \"require('shelljs/global'); sed('-i', /\\/\\/@@NG_LOADER_START@@[\\s\\S]*\\/\\/@@NG_LOADER_END@@/, '//@@NG_LOADER_START@@\\n' + cat('bower_components/angular-loader/angular-loader.min.js') + '\\n//@@NG_LOADER_END@@', 'app/index-async.html');\""
+  },
   "engines": {
     "node": ">=0.10.0"
   }

+ 43 - 0
ambari-admin/src/main/resources/ui/admin-web/test/e2e/signout.js

@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+'use strict';
+describe('Ambari sign out from Admin view', function () {
+  describe('Admin view', function () {
+    var ptor = protractor.getInstance();
+    beforeEach(function () {
+      ptor.get('app/index.html');
+      ptor.waitForAngular();
+    });
+    it('should navigate to login page on clicking "Sign out" action', function () {
+      var userDropdownBtn = element(by.binding('currentUser'));
+      var signOutAction = element(by.css('[ng-click="signOut()"]'));
+      //Action-1: Click on user dropdown menu and
+      //Action-2: Click on SignOut action link
+      userDropdownBtn.click().then(function () {
+        signOutAction.click().then(function () {
+          //Validation
+          setTimeout(function () {
+            expect(ptor.getCurrentUrl()).toContain('#/login');
+          }, 3000);
+        });
+      });
+    });
+  });
+});
+
+

+ 58 - 0
ambari-admin/src/main/resources/ui/admin-web/test/karma.conf.js

@@ -0,0 +1,58 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+module.exports = function(config){
+  config.set({
+
+    basePath : '../',
+
+    files : [
+      'app/bower_components/angular/angular.js',
+      'app/bower_components/angular-animate/angular-animate.js',
+      'app/bower_components/angular-bootstrap/ui-bootstrap.js',
+      'app/bower_components/angular-bootstrap-toggle-switch/angular-toggle-switch.js',
+      'app/bower_components/angular-route/angular-route.js',
+      'app/bower_components/angular-translate/angular-translate.js',
+      'app/bower_components/underscore/underscore.js',
+      'app/bower_components/restangular/dist/restangular.js',
+      'app/bower_components/mocha/mocha.js',
+      'app/bower_components/chai/chai.js',
+      'app/bower_components/sinon/lib/sinon.js',
+      'app/bower_components/angular-mocks/angular-mocks.js',
+      'app/scripts/**/*.js',
+      'test/unit/**/*.js'
+    ],
+
+    autoWatch : true,
+
+    frameworks: ['jasmine'],
+
+    browsers: ['PhantomJS'],
+
+    plugins : [
+            'karma-jasmine',
+            'karma-phantomjs-launcher'
+            ],
+
+    junitReporter : {
+      outputFile: 'test_out/unit.xml',
+      suite: 'unit'
+    }
+
+  });
+};

+ 31 - 10
ambari-admin/src/main/resources/ui/admin-web/test/spec/test.js → ambari-admin/src/main/resources/ui/admin-web/test/protractor-conf.js

@@ -15,16 +15,37 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/* global describe, it */
 
-(function () {
-    'use strict';
+exports.config = {
+  allScriptsTimeout: 11000,
 
-    describe('Give it some context', function () {
-        describe('maybe a bit more context here', function () {
-            it('should run here few assertions', function () {
+  specs: [
+    'e2e/*.js'
+  ],
+
+  capabilities: {
+    'browserName': 'chrome'
+  },
+
+  chromeOnly: true,
+
+  baseUrl: 'http://localhost:8000',
+
+  rootElement: 'body',
+
+  onPrepare: function() {
+
+  },
+
+
+  framework: 'jasmine',
+
+  jasmineNodeOpts: {
+    onComplete: null,
+    isVerbose: true,
+    showColors: true,
+    includeStackTrace: true,
+    defaultTimeoutInterval: 30000
+  }
+};
 
-            });
-        });
-    });
-})();

+ 54 - 0
ambari-admin/src/main/resources/ui/admin-web/test/unit/controllers/mainCtrl_test.js

@@ -0,0 +1,54 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+describe('#Auth', function () {
+
+  describe('signout', function () {
+    var scope, ctrl, $httpBackend, $window, clusterService,deferred;
+    beforeEach(module('ambariAdminConsole', function($provide){
+      $provide.value('$window', {location: {pathname: 'http://c6401.ambari.apache.org:8080/views/ADMIN_VIEW/1.0.0/INSTANCE/#/'}});
+      localStorage.ambari = JSON.stringify({app: {authenticated: true, loginName: 'admin', user: 'user'}});
+    }));
+    afterEach(function() {
+      $httpBackend.verifyNoOutstandingExpectation();
+      $httpBackend.verifyNoOutstandingRequest();
+    });
+    beforeEach(inject(function (_$httpBackend_, $rootScope, $controller, _$window_, _Cluster_,_$q_) {
+      clusterService =  _Cluster_;
+      deferred = _$q_.defer();
+      spyOn(clusterService, 'getStatus').andReturn(deferred.promise);
+      deferred.resolve('c1');
+      $window = _$window_;
+      $httpBackend = _$httpBackend_;
+      $httpBackend.whenGET('/api/v1/logout').respond(200,{message: "successfully logged out"});
+      scope = $rootScope.$new();
+      scope.$apply();
+      ctrl = $controller('MainCtrl', {$scope: scope});
+    }));
+
+    it('should reset window.location and ambari localstorage', function () {
+      scope.signOut();
+      $httpBackend.flush();
+      chai.expect($window.location.pathname).to.be.empty;
+      var data = JSON.parse(localStorage.ambari);
+      chai.expect(data.app.authenticated).to.equal(undefined);
+      chai.expect(data.app.loginName).to.equal(undefined);
+      chai.expect(data.app.user).to.equal(undefined);
+    });
+  });
+});