[ADF-2200] fixed problem with redirection url (#2877)

* [ADF-2200] fixed wrong handling of redirectUrl

* [ADF-2200] fixed problem with redirection url

* [ADF-2200] fixed redirection config path

* [ADF-2200] fixed wrong fdescribe test

* [ADF-2200] removed authserviceMock and fixed some tests
This commit is contained in:
Vito 2018-01-25 12:48:47 +01:00 committed by Eugenio Romano
parent 9f88c02ef3
commit 77f6c51dc2
14 changed files with 255 additions and 100 deletions

View File

@ -2,7 +2,7 @@
"ecmHost": "http://{hostname}:{port}",
"bpmHost": "http://{hostname}:{port}",
"application": {
"name": "Alfresco ADF Appplication"
"name": "Alfresco ADF Application"
},
"languages": [
{

View File

@ -16,7 +16,7 @@
*/
import { Component, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
import { SettingsService, PageTitleService, StorageService, TranslationService } from '@alfresco/adf-core';
@Component({
@ -26,14 +26,12 @@ import { SettingsService, PageTitleService, StorageService, TranslationService }
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
searchTerm = '';
constructor(private settingsService: SettingsService,
private storage: StorageService,
translationService: TranslationService,
pageTitleService: PageTitleService,
route: ActivatedRoute,
router: Router) {
route: ActivatedRoute) {
this.setProvider();
pageTitleService.setTitle();
}

View File

@ -26,8 +26,8 @@ import { AuthenticationService } from '../../services/authentication.service';
import { MaterialModule } from '../../material.module';
import { LoginErrorEvent } from '../models/login-error.event';
import { LoginSuccessEvent } from '../models/login-success.event';
import { AuthenticationMock } from './../../mock/authentication.service.mock';
import { LoginComponent } from './login.component';
import { Observable } from 'rxjs/Observable';
describe('LoginComponent', () => {
let component: LoginComponent;
@ -64,7 +64,7 @@ describe('LoginComponent', () => {
LoginComponent
],
providers: [
{provide: AuthenticationService, useClass: AuthenticationMock}
AuthenticationService
]
}).compileComponents();
}));
@ -101,6 +101,7 @@ describe('LoginComponent', () => {
}
it('should redirect to route on successful login', () => {
spyOn(authService, 'login').and.returnValue(Observable.of({ type: 'type', ticket: 'ticket'}));
const redirect = '/home';
component.successRoute = redirect;
spyOn(router, 'navigate');
@ -109,9 +110,10 @@ describe('LoginComponent', () => {
});
it('should redirect to previous route state on successful login', () => {
spyOn(authService, 'login').and.returnValue(Observable.of({ type: 'type', ticket: 'ticket'}));
const redirect = '/home';
component.successRoute = redirect;
authService.setRedirectUrl('redirect-url');
authService.setRedirectUrl({ provider: 'ECM', url: 'redirect-url' } );
spyOn(router, 'navigate');
@ -152,12 +154,14 @@ describe('LoginComponent', () => {
});
it('should be changed back to the default after a failed login attempt', () => {
spyOn(authService, 'login').and.returnValue(Observable.throw('Fake server error'));
loginWithCredentials('fake-wrong-username', 'fake-wrong-password');
expect(getLoginButtonText()).toEqual('LOGIN.BUTTON.LOGIN');
});
it('should be changed to the "welcome key" after a successful login attempt', () => {
spyOn(authService, 'login').and.returnValue(Observable.of({ type: 'type', ticket: 'ticket'}));
loginWithCredentials('fake-username', 'fake-password');
expect(getLoginButtonText()).toEqual('LOGIN.BUTTON.WELCOME');
@ -436,6 +440,12 @@ describe('LoginComponent', () => {
}));
it('should return CORS error when server CORS error occurs', async(() => {
spyOn(authService, 'login').and.returnValue(Observable.throw({
error: {
crossDomain: true,
message: 'ERROR: the network is offline, Origin is not allowed by Access-Control-Allow-Origin'
}
}));
component.providers = 'ECM';
component.error.subscribe(() => {
@ -450,6 +460,8 @@ describe('LoginComponent', () => {
}));
it('should return CSRF error when server CSRF error occurs', async(() => {
spyOn(authService, 'login')
.and.returnValue(Observable.throw({ message: 'ERROR: Invalid CSRF-token', status: 403 }));
component.providers = 'ECM';
component.error.subscribe(() => {
@ -464,6 +476,14 @@ describe('LoginComponent', () => {
}));
it('should return ECOM read-oly error when error occurs', async(() => {
spyOn(authService, 'login')
.and.returnValue(
Observable.throw(
{
message: 'ERROR: 00170728 Access Denied. The system is currently in read-only mode',
status: 403
}
));
component.providers = 'ECM';
component.error.subscribe(() => {
@ -493,6 +513,7 @@ describe('LoginComponent', () => {
}));
it('should emit error event after the login has failed', async(() => {
spyOn(authService, 'login').and.returnValue(Observable.throw('Fake server error'));
component.providers = 'ECM';
component.error.subscribe((error) => {

View File

@ -213,7 +213,7 @@ export class LoginComponent implements OnInit {
this.authService.login(values.username, values.password, this.rememberMe)
.subscribe(
(token: any) => {
const redirectUrl = this.authService.getRedirectUrl();
const redirectUrl = this.authService.getRedirectUrl(this.providers);
this.actualLoginStep = LoginSteps.Welcome;
this.userPreferences.setStoragePrefix(values.username);

View File

@ -21,3 +21,4 @@ export * from './product-version.model';
export * from './user-process.model';
export * from './comment-process.model';
export * from './ecm-company.model';
export * from './redirection.model';

View File

@ -0,0 +1,33 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed 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.
*/
/**
* This object represent the user redirection guard.*
*/
export class RedirectionModel {
provider: string;
url?: string;
constructor(obj?: any) {
if (obj) {
this.provider = obj.provider;
this.url = obj.url || null;
}
}
}

View File

@ -15,14 +15,14 @@
* limitations under the License.
*/
import { async, inject, TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';
import { async, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { CookieServiceMock } from './../mock/cookie.service.mock';
import { AlfrescoApiService } from './alfresco-api.service';
import { SettingsService } from './settings.service';
import { AppConfigModule } from '../app-config/app-config.module';
import { AppConfigService } from '../app-config/app-config.service';
import { AuthGuardBpm } from './auth-guard-bpm.service';
import { AuthenticationService } from './authentication.service';
import { CookieService } from './cookie.service';
@ -30,9 +30,15 @@ import { LogService } from './log.service';
import { StorageService } from './storage.service';
import { TranslateLoaderService } from './translate-loader.service';
import { UserPreferencesService } from './user-preferences.service';
import { RouterStateSnapshot, Router } from '@angular/router';
describe('AuthGuardService BPM', () => {
let authGuard: AuthGuardBpm;
let authService: AuthenticationService;
let routerService: Router;
let appConfigService: AppConfigService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
@ -58,43 +64,58 @@ describe('AuthGuardService BPM', () => {
}).compileComponents();
}));
it('if the alfresco js api is logged in should canActivate be true',
async(inject([AuthGuardBpm, Router, SettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => {
spyOn(router, 'navigate');
beforeEach(() => {
authService = TestBed.get(AuthenticationService);
authGuard = TestBed.get(AuthGuardBpm);
routerService = TestBed.get(Router);
appConfigService = TestBed.get(AppConfigService);
});
authService.isBpmLoggedIn = () => {
return true;
};
it('if the alfresco js api is logged in should canActivate be true', async(() => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(true);
const router: RouterStateSnapshot = <RouterStateSnapshot> {url : ''};
expect(auth.canActivate(null, { url: '' })).toBeTruthy();
expect(router.navigate).not.toHaveBeenCalled();
}))
);
expect(authGuard.canActivate(null, router)).toBeTruthy();
}));
it('if the alfresco js api is NOT logged in should canActivate be false',
async(inject([AuthGuardBpm, Router, SettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => {
it('if the alfresco js api is NOT logged in should canActivate be false', async(() => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(false);
spyOn(routerService, 'navigate').and.stub();
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: '' };
spyOn(router, 'navigate');
expect(authGuard.canActivate(null, router)).toBeFalsy();
}));
authService.isBpmLoggedIn = () => {
return false;
};
it('if the alfresco js api is NOT logged in should trigger a redirect event', async(() => {
spyOn(routerService, 'navigate');
spyOn(authService, 'isBpmLoggedIn').and.returnValue(false);
const router: RouterStateSnapshot = <RouterStateSnapshot> {url : ''};
expect(auth.canActivate(null, { url: '' })).toBeFalsy();
expect(router.navigate).toHaveBeenCalled();
}))
);
expect(authGuard.canActivate(null, router)).toBeFalsy();
expect(routerService.navigate).toHaveBeenCalledWith(['/login']);
}));
it('should set redirect url',
async(inject([AuthGuardBpm, Router, AuthenticationService], (auth, router, authService) => {
const state = { url: 'some-url' };
it('should set redirect url', async(() => {
spyOn(authService, 'setRedirectUrl').and.callThrough();
spyOn(routerService, 'navigate').and.stub();
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
spyOn(router, 'navigate');
spyOn(authService, 'setRedirectUrl');
authGuard.canActivate(null, router);
auth.canActivate(null , state);
expect(authService.setRedirectUrl).toHaveBeenCalledWith({provider: 'BPM', url: 'some-url' } );
expect(authService.getRedirectUrl('BPM')).toBe('some-url');
}));
it('should get redirect url from config if there is one configured', async(() => {
appConfigService.config.loginRoute = 'fakeLoginRoute';
spyOn(authService, 'setRedirectUrl').and.callThrough();
spyOn(routerService, 'navigate').and.stub();
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
authGuard.canActivate(null, router);
expect(authService.setRedirectUrl).toHaveBeenCalledWith({provider: 'BPM', url: 'some-url' } );
expect(routerService.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']);
}));
expect(authService.setRedirectUrl).toHaveBeenCalledWith(state.url);
}))
);
});

View File

@ -18,15 +18,16 @@
import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot, CanActivate, CanActivateChild,
Router,
RouterStateSnapshot
RouterStateSnapshot, Router
} from '@angular/router';
import { AppConfigService } from '../app-config/app-config.service';
import { AuthenticationService } from './authentication.service';
@Injectable()
export class AuthGuardBpm implements CanActivate, CanActivateChild {
constructor(private authService: AuthenticationService, private router: Router) {}
constructor(private authService: AuthenticationService,
private router: Router,
private appConfig: AppConfigService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.checkLogin(state.url);
@ -41,9 +42,16 @@ export class AuthGuardBpm implements CanActivate, CanActivateChild {
return true;
}
this.authService.setRedirectUrl(redirectUrl);
this.authService.setRedirectUrl({ provider: 'BPM', url: redirectUrl });
const pathToLogin = this.getRouteDestinationForLogin();
this.router.navigate(['/' + pathToLogin]);
this.router.navigate(['/login']);
return false;
}
private getRouteDestinationForLogin(): string {
return this.appConfig &&
this.appConfig.get<string>('loginRoute') ?
this.appConfig.get<string>('loginRoute') : 'login';
}
}

View File

@ -125,7 +125,7 @@ class TestConfig {
}
}
describe('CanActivateLoggedIn', () => {
describe('AuthGuardService ECM', () => {
describe('user is not logged in', () => {
beforeEach(async(() => {
this.test = new TestConfig({
@ -211,7 +211,7 @@ describe('CanActivateLoggedIn', () => {
}));
it('should set redirect url', () => {
expect(this.auth.setRedirectUrl).toHaveBeenCalledWith('some-url');
expect(this.auth.setRedirectUrl).toHaveBeenCalledWith({ provider: 'ECM', url: 'some-url' });
});
});

View File

@ -16,16 +16,18 @@
*/
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, Router } from '@angular/router';
import { AlfrescoApiService } from './alfresco-api.service';
import { AuthenticationService } from './authentication.service';
import { AppConfigService } from '../app-config/app-config.service';
@Injectable()
export class AuthGuardEcm implements CanActivate {
constructor(
private authService: AuthenticationService,
private apiService: AlfrescoApiService,
private router: Router) {
private router: Router,
private appConfig: AppConfigService) {
}
private get authApi() {
@ -51,11 +53,18 @@ export class AuthGuardEcm implements CanActivate {
return this.isLoggedIn().then(isLoggedIn => {
if (!isLoggedIn) {
this.authService.setRedirectUrl(state.url);
this.router.navigate([ '/login' ]);
this.authService.setRedirectUrl({ provider: 'ECM', url: state.url });
const pathToLogin = this.getRouteDestinationForLogin();
this.router.navigate(['/' + pathToLogin]);
}
return isLoggedIn;
});
}
private getRouteDestinationForLogin(): string {
return this.appConfig &&
this.appConfig.get<string>('loginRoute') ?
this.appConfig.get<string>('loginRoute') : 'login';
}
}

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import { async, inject, TestBed } from '@angular/core/testing';
import { async, TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
@ -24,6 +24,7 @@ import { CookieServiceMock } from './../mock/cookie.service.mock';
import { AlfrescoApiService } from './alfresco-api.service';
import { SettingsService } from './settings.service';
import { AppConfigModule } from '../app-config/app-config.module';
import { AppConfigService } from '../app-config/app-config.service';
import { AuthGuard } from './auth-guard.service';
import { AuthenticationService } from './authentication.service';
import { CookieService } from './cookie.service';
@ -34,6 +35,10 @@ import { UserPreferencesService } from './user-preferences.service';
describe('AuthGuardService', () => {
let state;
let authService: AuthenticationService;
let router: Router;
let service: AuthGuard;
let appConfigService: AppConfigService;
beforeEach(async(() => {
TestBed.configureTestingModule({
@ -62,45 +67,50 @@ describe('AuthGuardService', () => {
beforeEach(() => {
state = { url: '' };
authService = TestBed.get(AuthenticationService);
router = TestBed.get(Router);
service = TestBed.get(AuthGuard);
appConfigService = TestBed.get(AppConfigService);
});
it('if the alfresco js api is logged in should canActivate be true',
async(inject([AuthGuard, Router, SettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => {
spyOn(router, 'navigate');
it('if the alfresco js api is logged in should canActivate be true', async(() => {
spyOn(router, 'navigate');
spyOn(authService, 'isLoggedIn').and.returnValue(true);
authService.isLoggedIn = () => {
return true;
};
expect(service.canActivate(null, state)).toBeTruthy();
expect(router.navigate).not.toHaveBeenCalled();
}));
expect(auth.canActivate(null, state)).toBeTruthy();
expect(router.navigate).not.toHaveBeenCalled();
}))
);
it('if the alfresco js api is NOT logged in should canActivate be false', async(() => {
spyOn(router, 'navigate');
spyOn(authService, 'isLoggedIn').and.returnValue(false);
it('if the alfresco js api is NOT logged in should canActivate be false',
async(inject([AuthGuard, Router, SettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => {
expect(service.canActivate(null, state)).toBeFalsy();
expect(router.navigate).toHaveBeenCalled();
}));
spyOn(router, 'navigate');
it('should set redirect url', async(() => {
state.url = 'some-url';
authService.isLoggedIn = () => {
return false;
};
spyOn(router, 'navigate');
spyOn(authService, 'setRedirectUrl');
expect(auth.canActivate(null, state)).toBeFalsy();
expect(router.navigate).toHaveBeenCalled();
}))
);
service.canActivate(null, state);
it('should set redirect url',
async(inject([AuthGuard, Router, AuthenticationService], (auth, router, authService) => {
state.url = 'some-url';
expect(authService.setRedirectUrl).toHaveBeenCalledWith({ provider: 'ALL', url: 'some-url' });
expect(router.navigate).toHaveBeenCalledWith(['/login']);
}));
spyOn(router, 'navigate');
spyOn(authService, 'setRedirectUrl');
it('should get redirect url from config if there is one configured', async(() => {
state.url = 'some-url';
appConfigService.config.loginRoute = 'fakeLoginRoute';
auth.canActivate(null , state);
spyOn(router, 'navigate');
spyOn(authService, 'setRedirectUrl');
expect(authService.setRedirectUrl).toHaveBeenCalledWith(state.url);
}))
);
service.canActivate(null, state);
expect(authService.setRedirectUrl).toHaveBeenCalledWith({ provider: 'ALL', url: 'some-url' });
expect(router.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']);
}));
});

View File

@ -17,16 +17,17 @@
import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot, CanActivate, CanActivateChild,
Router,
RouterStateSnapshot
ActivatedRouteSnapshot, CanActivate,
CanActivateChild, RouterStateSnapshot, Router
} from '@angular/router';
import { AppConfigService } from '../app-config/app-config.service';
import { AuthenticationService } from './authentication.service';
@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(private authService: AuthenticationService, private router: Router) {}
constructor(private authService: AuthenticationService,
private router: Router,
private appConfig: AppConfigService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
const redirectUrl = state.url;
@ -43,9 +44,16 @@ export class AuthGuard implements CanActivate, CanActivateChild {
return true;
}
this.authService.setRedirectUrl(redirectUrl);
this.authService.setRedirectUrl({ provider: 'ALL', url: redirectUrl } );
const pathToLogin = this.getRouteDestinationForLogin();
this.router.navigate(['/' + pathToLogin]);
this.router.navigate(['/login']);
return false;
}
private getRouteDestinationForLogin(): string {
return this.appConfig &&
this.appConfig.get<string>('loginRoute') ?
this.appConfig.get<string>('loginRoute') : 'login';
}
}

View File

@ -258,6 +258,18 @@ describe('AuthenticationService', () => {
expect(authService.isLoggedIn()).toBe(false);
expect(authService.isEcmLoggedIn()).toBe(false);
});
it('[ECM] should set/get redirectUrl when provider is ECM', () => {
authService.setRedirectUrl({provider: 'ECM', url: 'some-url' } );
expect(authService.getRedirectUrl(preferences.authType)).toBe('some-url');
});
it('[ECM] should set/get redirectUrl when provider is BPM', () => {
authService.setRedirectUrl({provider: 'BPM', url: 'some-url' } );
expect(authService.getRedirectUrl(preferences.authType)).toBeNull();
});
});
describe('when the setting is BPM', () => {
@ -367,6 +379,18 @@ describe('AuthenticationService', () => {
'status': 403
});
});
it('[BPM] should set/get redirectUrl when provider is BPM', () => {
authService.setRedirectUrl({provider: 'BPM', url: 'some-url' } );
expect(authService.getRedirectUrl(preferences.authType)).toBe('some-url');
});
it('[BPM] should set/get redirectUrl when provider is ECM', () => {
authService.setRedirectUrl({provider: 'ECM', url: 'some-url' } );
expect(authService.getRedirectUrl(preferences.authType)).toBeNull();
});
});
describe('when the setting is both ECM and BPM ', () => {
@ -396,7 +420,7 @@ describe('AuthenticationService', () => {
});
});
xit('[ALL] should return login fail if only ECM call fail', (done) => {
it('[ALL] should return login fail if only ECM call fail', (done) => {
authService.login('fake-username', 'fake-password').subscribe(
(res) => {
},
@ -417,7 +441,7 @@ describe('AuthenticationService', () => {
});
});
xit('[ALL] should return login fail if only BPM call fail', (done) => {
it('[ALL] should return login fail if only BPM call fail', (done) => {
authService.login('fake-username', 'fake-password').subscribe(
(res) => {
},
@ -461,11 +485,24 @@ describe('AuthenticationService', () => {
'status': 403
});
});
it('[ALL] should set/get redirectUrl when provider is ALL', () => {
authService.setRedirectUrl({provider: 'ALL', url: 'some-url' } );
expect(authService.getRedirectUrl(preferences.authType)).toBe('some-url');
});
it('[ALL] should set/get redirectUrl when provider is BPM', () => {
authService.setRedirectUrl({provider: 'BPM', url: 'some-url' } );
expect(authService.getRedirectUrl(preferences.authType)).toBe('some-url');
});
it('[ALL] should set/get redirectUrl when provider is ECM', () => {
authService.setRedirectUrl({provider: 'ECM', url: 'some-url' } );
expect(authService.getRedirectUrl(preferences.authType)).toBe('some-url');
});
});
it('should set/get redirectUrl', () => {
authService.setRedirectUrl('some-url');
expect(authService.getRedirectUrl()).toBe('some-url');
});
});

View File

@ -26,13 +26,14 @@ import { UserPreferencesService } from './user-preferences.service';
import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { RedirectionModel } from '../models/redirection.model';
const REMEMBER_ME_COOKIE_KEY = 'ALFRESCO_REMEMBER_ME';
const REMEMBER_ME_UNTIL = 1000 * 60 * 60 * 24 * 30 ;
@Injectable()
export class AuthenticationService {
private redirectUrl: string = '';
private redirectUrl: RedirectionModel = null;
onLogin: Subject<any> = new Subject<any>();
onLogout: Subject<any> = new Subject<any>();
@ -233,12 +234,20 @@ export class AuthenticationService {
return this.alfrescoApi.getInstance().bpmAuth.username;
}
setRedirectUrl(url: string) {
setRedirectUrl(url: RedirectionModel) {
this.redirectUrl = url;
}
getRedirectUrl(): string {
return this.redirectUrl;
getRedirectUrl(provider: string): string {
return this.hasValidRedirection(provider) ? this.redirectUrl.url : null;
}
private hasValidRedirection(provider: string): boolean {
return this.redirectUrl && this.redirectUrl.provider === provider || this.hasSelectedProviderAll(provider);
}
private hasSelectedProviderAll(provider: string): boolean {
return this.redirectUrl && this.redirectUrl.provider === 'ALL' || provider === 'ALL';
}
/**