# HG changeset patch # User Andrew Azores # Date 1509029254 14400 # Node ID bdbc30b9f43c3b26f1378a44ed442c16632a9395 # Parent 84d4e3e9d77aa008366ce88a4d18914a9fc640d1 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 diff -r 84d4e3e9d77a -r bdbc30b9f43c src/app/app-root.controller.spec.js --- 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'); }); }); diff -r 84d4e3e9d77a -r bdbc30b9f43c src/app/components/auth/auth.module.js --- 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(); } } diff -r 84d4e3e9d77a -r bdbc30b9f43c src/app/components/auth/basic-auth.service.ts --- 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;