changeset 174:62bcc6165e77

Broadcast userLoginChanged events when user login status changes Listen for event and update user menu dropdown label. Reviewed-by: jkang Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-August/024742.html
author Andrew Azores <aazores@redhat.com>
date Fri, 25 Aug 2017 10:10:30 -0400
parents 8fe5fed67703
children e10e61e34baf
files src/app/app.controller.js src/app/app.controller.spec.js src/app/components/auth/auth.module.js src/app/components/auth/basic-auth.service.js src/app/components/auth/basic-auth.service.spec.js src/app/components/auth/keycloak-auth.service.js src/app/components/auth/keycloak-auth.service.spec.js
diffstat 7 files changed, 76 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/app.controller.js	Tue Aug 29 14:36:04 2017 -0400
+++ b/src/app/app.controller.js	Fri Aug 25 10:10:30 2017 -0400
@@ -39,13 +39,11 @@
 
     $scope.loginStatus = () => authService.status();
 
-    $scope.logout = () => {
-      authService.logout();
-    };
+    $scope.logout = () => authService.logout();
 
-    $scope.username = authService.username;
+    $scope.$on('userLoginChanged', () => $scope.username = authService.username);
+  }
 
-  }
 }
 
 let name = 'AppController';
--- a/src/app/app.controller.spec.js	Tue Aug 29 14:36:04 2017 -0400
+++ b/src/app/app.controller.spec.js	Fri Aug 25 10:10:30 2017 -0400
@@ -30,6 +30,13 @@
   beforeEach(angular.mock.module($provide => {
     'ngInject';
     $provide.value('$transitions', { onBefore: angular.noop });
+
+    let cookies = {
+      put: sinon.spy(),
+      get: sinon.stub().returns('fake-username'),
+      remove: sinon.spy()
+    };
+    $provide.value('$cookies', cookies);
   }));
 
   beforeEach(angular.mock.module('AppController'));
@@ -107,10 +114,11 @@
   });
 
   describe('$scope.username', () => {
-    let scope, authService;
+    let rootScope, scope, authService;
     beforeEach(inject(($controller, $rootScope) => {
       'ngInject';
 
+      rootScope = $rootScope;
       scope = $rootScope.$new();
       authService = {
         status: sinon.stub().returns(true),
@@ -126,7 +134,9 @@
       });
     }));
 
-    it('should be set according to authService username', () => {
+    it('should be set on userLoginChanged according to authService username', () => {
+      scope.should.not.have.ownProperty('username');
+      rootScope.$broadcast('userLoginChanged');
       scope.should.have.ownProperty('username');
       scope.username.should.equal(authService.username);
     });
--- a/src/app/components/auth/auth.module.js	Tue Aug 29 14:36:04 2017 -0400
+++ b/src/app/components/auth/auth.module.js	Fri Aug 25 10:10:30 2017 -0400
@@ -51,6 +51,10 @@
 
   mod.constant('AUTH_MODULE', MOD_NAME);
   mod.controller('LoginController', LoginController);
+  mod.run((authService, $rootScope) => {
+    'ngInject';
+    authService.rootScope = $rootScope;
+  });
 
   if (env === 'production') {
     let cloak = keycloakProvider();
--- a/src/app/components/auth/basic-auth.service.js	Tue Aug 29 14:36:04 2017 -0400
+++ b/src/app/components/auth/basic-auth.service.js	Fri Aug 25 10:10:30 2017 -0400
@@ -40,6 +40,10 @@
     this._pass = null;
   }
 
+  set rootScope (rootScope) {
+    this._rootScope = rootScope;
+  }
+
   status () {
     return this.state;
   }
@@ -51,6 +55,7 @@
     if (this._rememberUser) {
       this.cookies.put('username', user);
     }
+    this._rootScope.$broadcast('userLoginChanged');
     success();
   }
 
@@ -63,6 +68,7 @@
     this._pass = null;
     this.state = false;
     this.$state.go('login');
+    this._rootScope.$broadcast('userLoginChanged');
     callback();
   }
 
--- a/src/app/components/auth/basic-auth.service.spec.js	Tue Aug 29 14:36:04 2017 -0400
+++ b/src/app/components/auth/basic-auth.service.spec.js	Fri Aug 25 10:10:30 2017 -0400
@@ -30,7 +30,7 @@
 import BasicAuthService from './basic-auth.service.js';
 
 describe('BasicAuthService', () => {
-  let basicAuthService, q, qPromise, state, cookies;
+  let basicAuthService, q, qPromise, state, cookies, rootScope;
   beforeEach(() => {
     qPromise = sinon.stub().yields();
     q = {
@@ -51,7 +51,9 @@
       get: sinon.stub(),
       remove: sinon.spy()
     };
+    rootScope = { $broadcast: sinon.spy() };
     basicAuthService = new BasicAuthService(q, state, cookies);
+    basicAuthService.rootScope = rootScope;
   });
 
   it('should be initially logged out', () => {
@@ -89,6 +91,15 @@
         done();
       });
     });
+
+    it('should broadcast userLoginChanged event', done => {
+      rootScope.$broadcast.should.not.be.called();
+      basicAuthService.login('client', 'client-pwd', () => {
+        rootScope.$broadcast.should.be.calledOnce();
+        rootScope.$broadcast.should.be.calledWith('userLoginChanged');
+        done();
+      });
+    });
   });
 
   describe('#goToLogin()', () => {
@@ -125,6 +136,15 @@
         done();
       });
     });
+
+    it('should broadcast userLoginChanged event', done => {
+      rootScope.$broadcast.should.not.be.called();
+      basicAuthService.logout(() => {
+        rootScope.$broadcast.should.be.calledOnce();
+        rootScope.$broadcast.should.be.calledWith('userLoginChanged');
+        done();
+      });
+    });
   });
 
   describe('#refresh()', () => {
--- a/src/app/components/auth/keycloak-auth.service.js	Tue Aug 29 14:36:04 2017 -0400
+++ b/src/app/components/auth/keycloak-auth.service.js	Fri Aug 25 10:10:30 2017 -0400
@@ -33,17 +33,24 @@
     this.keycloak = keycloak;
   }
 
+  set rootScope (rootScope) {
+    this._rootScope = rootScope;
+  }
+
   login () {
     // no-op
   }
 
   goToLogin (promise) {
     this.keycloak.login();
+    this._rootScope.$broadcast('userLoginChanged');
     promise.resolve();
   }
 
-  logout () {
+  logout (callback = angular.noop) {
+    this._rootScope.$broadcast('userLoginChanged');
     this.keycloak.logout();
+    callback();
   }
 
   status () {
--- a/src/app/components/auth/keycloak-auth.service.spec.js	Tue Aug 29 14:36:04 2017 -0400
+++ b/src/app/components/auth/keycloak-auth.service.spec.js	Fri Aug 25 10:10:30 2017 -0400
@@ -31,7 +31,7 @@
 
 describe('KeycloakAuthService', () => {
 
-  let keycloakAuthService, mockCloak;
+  let keycloakAuthService, mockCloak, rootScope;
   beforeEach(() => {
     let login = sinon.spy();
     let logout = sinon.spy();
@@ -49,6 +49,9 @@
       }
     };
     keycloakAuthService = new KeycloakAuthService(mockCloak);
+
+    rootScope = { $broadcast: sinon.spy() };
+    keycloakAuthService.rootScope = rootScope;
   });
 
   describe('#login()', () => {
@@ -67,6 +70,15 @@
       mockCloak.login.should.be.calledOnce();
       promise.resolve.should.be.calledOnce();
     });
+
+    it('should broadcast userLoginChanged event', () => {
+      rootScope.$broadcast.should.not.be.called();
+      let promise = { resolve: sinon.spy() };
+      keycloakAuthService.goToLogin(promise);
+      rootScope.$broadcast.should.be.calledOnce();
+      rootScope.$broadcast.should.be.calledWith('userLoginChanged');
+      promise.resolve.should.be.calledOnce();
+    });
   });
 
   describe('#logout()', () => {
@@ -74,6 +86,15 @@
       keycloakAuthService.logout();
       mockCloak.logout.should.be.calledOnce();
     });
+
+    it('should broadcast userLoginChanged event', done => {
+      rootScope.$broadcast.should.not.be.called();
+      keycloakAuthService.logout(() => {
+        rootScope.$broadcast.should.be.calledOnce();
+        rootScope.$broadcast.should.be.calledWith('userLoginChanged');
+        done();
+      });
+    });
   });
 
   describe('#status()', () => {