changeset 274:bdbc30b9f43c default tip master

Alternative authService migration approach Allows one service instance to be shared between Angular and AngularJS. Reviewed-by: jkang Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025509.html
author Andrew Azores <aazores@redhat.com>
date Thu, 26 Oct 2017 10:47:34 -0400
parents 84d4e3e9d77a
children
files src/app/app-root.controller.spec.js src/app/components/auth/auth.module.js src/app/components/auth/basic-auth.service.ts
diffstat 3 files changed, 55 insertions(+), 94 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/app-root.controller.spec.js	Thu Oct 26 10:41:29 2017 -0400
+++ b/src/app/app-root.controller.spec.js	Thu Oct 26 10:47:34 2017 -0400
@@ -30,50 +30,62 @@
 
 describe('AppRootController', () => {
 
-  beforeEach(angular.mock.module($provide => {
-    'ngInject';
-    $provide.value('$transitions', { onBefore: angular.noop });
-
-    let localStorage = {
-      getItem: sinon.stub(),
-      hasItem: sinon.stub(),
-      removeItem: sinon.spy(),
-      setItem: sinon.spy(),
-      clear: sinon.spy()
-    };
-    $provide.value('localStorage', localStorage);
-  }));
-
+  let rootScope, authService, translate, ctrl;
   beforeEach(() => {
     angular.mock.module(authModule);
     config();
     angular.mock.module(controllerModule);
+
+    angular.mock.module($provide => {
+      'ngInject';
+      $provide.value('$transitions', { onBefore: angular.noop });
+
+      let localStorage = {
+        getItem: sinon.stub(),
+        hasItem: sinon.stub(),
+        removeItem: sinon.spy(),
+        setItem: sinon.spy(),
+        clear: sinon.spy()
+      };
+      $provide.value('localStorage', localStorage);
+
+      authService = {
+        logout: sinon.spy(),
+        status: sinon.stub().returns(true),
+        username: 'foo-user',
+      };
+      $provide.value('authService', authService);
+
+      translate = sinon.stub().returns({
+        then: sinon.stub().yields({
+          'navbar.states.JVM_LISTING': 'JVM Listing',
+          'navbar.states.MULTICHARTS': 'Multicharts'
+        })
+      });
+    });
+
+    angular.mock.inject(($controller, $rootScope) => {
+      'ngInject';
+
+      rootScope = $rootScope;
+
+      ctrl = $controller('AppRootController', {
+        environment: 'testing',
+        $scope: rootScope.$new(),
+        authService: authService,
+        $translate: translate
+      });
+      ctrl.$onInit();
+    });
   });
 
   ['testing', 'development', 'production'].forEach(env => {
     describe(env, () => {
-      let ctrl, scope, authService, translate;
-      beforeEach(inject($controller => {
+      beforeEach(angular.mock.inject($controller => {
         'ngInject';
-
-        scope = { $on: sinon.spy() };
-        authService = {
-          status: sinon.stub().returns(true),
-          login: sinon.spy(),
-          logout: sinon.spy()
-        };
-        translate = sinon.stub().returns({
-          then: sinon.stub().yields(
-            {
-              'navbar.states.JVM_LISTING': 'JVM Listing',
-              'navbar.states.MULTICHARTS': 'Multicharts'
-            }
-          )
-        });
-
         ctrl = $controller('AppRootController', {
           environment: env,
-          $scope: scope,
+          $scope: rootScope.$new(),
           authService: authService,
           $translate: translate
         });
@@ -88,34 +100,6 @@
   });
 
   describe('logout()', () => {
-    let ctrl, scope, authService, translate;
-    beforeEach(inject($controller => {
-      'ngInject';
-
-      scope = { $on: sinon.spy() };
-      authService = {
-        status: sinon.stub().returns(true),
-        login: sinon.spy(),
-        logout: sinon.spy()
-      };
-      translate = sinon.stub().returns({
-        then: sinon.stub().yields(
-          {
-            'navbar.states.JVM_LISTING': 'JVM Listing',
-            'navbar.states.MULTICHARTS': 'Multicharts'
-          }
-        )
-      });
-
-      ctrl = $controller('AppRootController', {
-        environment: 'testing',
-        $scope: scope,
-        authService: authService,
-        $translate: translate
-      });
-      ctrl.$onInit();
-    }));
-
     it('should delegate to AuthService', () => {
       authService.logout.should.not.be.called();
       ctrl.logout();
@@ -124,36 +108,6 @@
   });
 
   describe('username', () => {
-    let rootScope, scope, ctrl, authService, translate;
-    beforeEach(inject(($controller, $rootScope) => {
-      'ngInject';
-
-      rootScope = $rootScope;
-      scope = $rootScope.$new();
-      authService = {
-        status: sinon.stub().returns(true),
-        login: sinon.spy(),
-        logout: sinon.spy(),
-        username: 'fake-username'
-      };
-      translate = sinon.stub().returns({
-        then: sinon.stub().yields(
-          {
-            'navbar.states.JVM_LISTING': 'JVM Listing',
-            'navbar.states.MULTICHARTS': 'Multicharts'
-          }
-        )
-      });
-
-      ctrl = $controller('AppRootController', {
-        $scope: scope,
-        environment: 'testing',
-        authService: authService,
-        $translate: translate
-      });
-      ctrl.$onInit();
-    }));
-
     it('should be set on init', () => {
       ctrl.should.have.ownProperty('username');
       ctrl.username.should.equal(authService.username);
@@ -163,7 +117,7 @@
       authService.username = 'new-username';
       rootScope.$broadcast('userLoginChanged');
       ctrl.should.have.ownProperty('username');
-      ctrl.username.should.equal(authService.username);
+      ctrl.username.should.equal('new-username');
     });
   });
 
--- a/src/app/components/auth/auth.module.js	Thu Oct 26 10:41:29 2017 -0400
+++ b/src/app/components/auth/auth.module.js	Thu Oct 26 10:47:34 2017 -0400
@@ -25,10 +25,12 @@
  * exception statement from your version.
  */
 
+import { downgradeInjectable } from '@angular/upgrade/static';
+
 import Keycloak from 'keycloak-js/dist/keycloak.js';
 
+import { AuthServiceToken } from './auth-service.interface.ts';
 import { KeycloakAuthService } from './keycloak-auth.service';
-import { BasicAuthService } from './basic-auth.service';
 
 let MOD_NAME = 'authModule';
 export default MOD_NAME;
@@ -62,7 +64,7 @@
       .success(done)
       .error(() => window.location.reload());
   } else {
-    mod.service('authService', ['$injector', BasicAuthService]);
+    mod.factory('authService', downgradeInjectable(AuthServiceToken));
     done();
   }
 }
--- a/src/app/components/auth/basic-auth.service.ts	Thu Oct 26 10:41:29 2017 -0400
+++ b/src/app/components/auth/basic-auth.service.ts	Thu Oct 26 10:47:34 2017 -0400
@@ -26,7 +26,11 @@
  */
 
 import * as angular from "angular";
+
+import { Injectable } from "@angular/core";
+
 import * as url from "url";
+
 import { IAuthService } from "./auth-service.interface";
 
 import { StateService } from "@uirouter/angularjs";
@@ -35,6 +39,7 @@
 
 const SESSION_EXPIRY_MINUTES = 15;
 
+@Injectable()
 export class BasicAuthService implements IAuthService {
 
   private q: ng.IQService;