mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-3162] Setting component - Should be able to render the providers passed as input (#3464)
* Be able to change the available providers values * Add deprecated tag * add default providers * Fix empty OAuth and selected providers Improve the documentation * Fix BPM guard to check SSO condition * Add the oauthConfig again * SSO or Basic otpion auth setting change * fix host settings sso/basic add login documentation * remove test string * fix auth guard test * dispose upload emitter test events * remove space * exclude failing test
This commit is contained in:
committed by
Eugenio Romano
parent
9221d1d0d0
commit
476b5864bf
@@ -16,310 +16,109 @@
|
||||
*/
|
||||
|
||||
import { async, TestBed } from '@angular/core/testing';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
import { Router } from '@angular/router';
|
||||
import { AlfrescoApiService } from './alfresco-api.service';
|
||||
import { AppConfigService } from '../app-config/app-config.service';
|
||||
import { AuthGuardEcm } from './auth-guard-ecm.service';
|
||||
import { AuthenticationService } from './authentication.service';
|
||||
import { AppConfigService } from '../app-config/app-config.service';
|
||||
import { HttpClientModule } from '@angular/common/http';
|
||||
|
||||
class AlfrescoApiServiceProvider {
|
||||
private settings: any = {
|
||||
validateTicket: true,
|
||||
isLoggedIn: true
|
||||
};
|
||||
|
||||
constructor(settings: any = {}) {
|
||||
Object.assign(this.settings, settings);
|
||||
}
|
||||
|
||||
getInstance() {
|
||||
return {
|
||||
ecmAuth: this.ecmAuth
|
||||
};
|
||||
}
|
||||
|
||||
private get ecmAuth() {
|
||||
return {
|
||||
validateTicket: this.validateTicket.bind(this),
|
||||
isLoggedIn: this.isLoggedIn.bind(this)
|
||||
};
|
||||
}
|
||||
|
||||
private validateTicket() {
|
||||
const { validateTicket } = this.settings;
|
||||
|
||||
return validateTicket
|
||||
? Promise.resolve('Valid!')
|
||||
: Promise.reject('Invalid');
|
||||
}
|
||||
|
||||
private isLoggedIn() {
|
||||
return this.settings.isLoggedIn;
|
||||
}
|
||||
}
|
||||
|
||||
class AuthenticationServiceProvider {
|
||||
setRedirect: Function = jasmine.createSpy('setRedirect');
|
||||
}
|
||||
|
||||
class TestConfig {
|
||||
router: any;
|
||||
guard: any;
|
||||
auth: any;
|
||||
|
||||
private settings: any = {
|
||||
validateTicket: true,
|
||||
isLoggedIn: true
|
||||
};
|
||||
|
||||
constructor(settings: any = {}) {
|
||||
Object.assign(this.settings, settings);
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
HttpClientModule,
|
||||
RouterTestingModule
|
||||
],
|
||||
providers: [
|
||||
AppConfigService,
|
||||
this.alfrescoApiServiceProvider,
|
||||
this.authenticationProvider,
|
||||
AuthGuardEcm
|
||||
]
|
||||
});
|
||||
|
||||
this.guard = TestBed.get(AuthGuardEcm);
|
||||
this.router = TestBed.get(Router);
|
||||
this.auth = TestBed.get(AuthenticationService);
|
||||
|
||||
}
|
||||
|
||||
private get authenticationProvider() {
|
||||
return {
|
||||
provide: AuthenticationService,
|
||||
useValue: new AuthenticationServiceProvider()
|
||||
};
|
||||
}
|
||||
|
||||
private get alfrescoApiServiceProvider () {
|
||||
const { validateTicket, isLoggedIn } = this.settings;
|
||||
|
||||
return {
|
||||
provide: AlfrescoApiService,
|
||||
useValue: new AlfrescoApiServiceProvider({
|
||||
validateTicket,
|
||||
isLoggedIn
|
||||
})
|
||||
};
|
||||
}
|
||||
}
|
||||
import { RouterStateSnapshot, Router } from '@angular/router';
|
||||
import { setupTestBed } from '../testing/setupTestBed';
|
||||
import { CoreTestingModule } from '../testing/core.testing.module';
|
||||
|
||||
describe('AuthGuardService ECM', () => {
|
||||
describe('user is not logged in', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: false
|
||||
});
|
||||
|
||||
const { guard, router } = this.test;
|
||||
let authGuard: AuthGuardEcm;
|
||||
let authService: AuthenticationService;
|
||||
let routerService: Router;
|
||||
let appConfigService: AppConfigService;
|
||||
|
||||
guard.canActivate(null, { url: 'some-url' }).then((activate) => {
|
||||
this.activate = activate;
|
||||
});
|
||||
|
||||
this.navigateSpy = spyOn(router, 'navigate');
|
||||
}));
|
||||
|
||||
it('does not allow route to activate', () => {
|
||||
expect(this.activate).toBe(false);
|
||||
});
|
||||
|
||||
it('redirects to /login', () => {
|
||||
expect(this.navigateSpy).toHaveBeenCalledWith([ '/login' ]);
|
||||
});
|
||||
setupTestBed({
|
||||
imports: [CoreTestingModule]
|
||||
});
|
||||
|
||||
describe('user is logged in but ticket is invalid', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: true,
|
||||
validateTicket: false
|
||||
});
|
||||
|
||||
const { guard, router } = this.test;
|
||||
|
||||
guard.canActivate(null, { url: 'some-url' }).then((activate) => {
|
||||
this.activate = activate;
|
||||
});
|
||||
|
||||
this.navigateSpy = spyOn(router, 'navigate');
|
||||
}));
|
||||
|
||||
it('does not allow route to activate', () => {
|
||||
expect(this.activate).toBe(false);
|
||||
});
|
||||
|
||||
it('redirects to /login', () => {
|
||||
expect(this.navigateSpy).toHaveBeenCalledWith([ '/login' ]);
|
||||
});
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
authService = TestBed.get(AuthenticationService);
|
||||
authGuard = TestBed.get(AuthGuardEcm);
|
||||
routerService = TestBed.get(Router);
|
||||
appConfigService = TestBed.get(AppConfigService);
|
||||
});
|
||||
|
||||
describe('user is logged in and ticket is valid', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: true,
|
||||
validateTicket: true
|
||||
});
|
||||
it('if the alfresco js api is logged in should canActivate be true', async(() => {
|
||||
spyOn(authService, 'isEcmLoggedIn').and.returnValue(true);
|
||||
const router: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};
|
||||
|
||||
const { guard, router } = this.test;
|
||||
expect(authGuard.canActivate(null, router)).toBeTruthy();
|
||||
}));
|
||||
|
||||
guard.canActivate(null, { url: 'some-url' }).then((activate) => {
|
||||
this.activate = activate;
|
||||
});
|
||||
it('if the alfresco js api is NOT logged in should canActivate be false', async(() => {
|
||||
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
|
||||
spyOn(routerService, 'navigate').and.stub();
|
||||
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
|
||||
|
||||
this.navigateSpy = spyOn(router, 'navigate');
|
||||
}));
|
||||
expect(authGuard.canActivate(null, router)).toBeFalsy();
|
||||
}));
|
||||
|
||||
it('allows route to activate', () => {
|
||||
expect(this.activate).toBe(true);
|
||||
it('if the alfresco js api is NOT logged in should trigger a redirect event', async(() => {
|
||||
appConfigService.config.loginRoute = 'login';
|
||||
|
||||
spyOn(routerService, 'navigate');
|
||||
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
|
||||
const router: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};
|
||||
|
||||
expect(authGuard.canActivate(null, router)).toBeFalsy();
|
||||
expect(routerService.navigate).toHaveBeenCalledWith(['/login']);
|
||||
}));
|
||||
|
||||
it('should set redirect navigation commands', async(() => {
|
||||
spyOn(authService, 'setRedirect').and.callThrough();
|
||||
spyOn(routerService, 'navigate').and.stub();
|
||||
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
|
||||
|
||||
authGuard.canActivate(null, router);
|
||||
|
||||
expect(authService.setRedirect).toHaveBeenCalledWith({
|
||||
provider: 'ECM', navigation: ['some-url', {}]
|
||||
});
|
||||
expect(authService.getRedirect('ECM')).toEqual(['some-url', {}]);
|
||||
}));
|
||||
|
||||
it('does not redirect', () => {
|
||||
expect(this.navigateSpy).not.toHaveBeenCalled();
|
||||
it('should set redirect navigation commands with query params', async(() => {
|
||||
spyOn(authService, 'setRedirect').and.callThrough();
|
||||
spyOn(routerService, 'navigate').and.stub();
|
||||
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url;q=123' };
|
||||
|
||||
authGuard.canActivate(null, router);
|
||||
|
||||
expect(authService.setRedirect).toHaveBeenCalledWith({
|
||||
provider: 'ECM', navigation: ['some-url', {q: '123'}]
|
||||
});
|
||||
});
|
||||
expect(authService.getRedirect('ECM')).toEqual(['some-url', { q: '123' }]);
|
||||
}));
|
||||
|
||||
describe('redirect', () => {
|
||||
describe('path', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: false
|
||||
});
|
||||
it('should set redirect navigation commands with query params', async(() => {
|
||||
spyOn(authService, 'setRedirect').and.callThrough();
|
||||
spyOn(routerService, 'navigate').and.stub();
|
||||
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: '/' };
|
||||
|
||||
const { guard, auth, router } = this.test;
|
||||
authGuard.canActivate(null, router);
|
||||
|
||||
guard.canActivate(null, { url: 'some-url/123' }).then((activate) => {
|
||||
this.auth = auth;
|
||||
});
|
||||
|
||||
this.navigateSpy = spyOn(router, 'navigate');
|
||||
}));
|
||||
|
||||
it('should set redirect navigation commands', () => {
|
||||
expect(this.auth.setRedirect).toHaveBeenCalledWith({
|
||||
provider: 'ECM', navigation: ['some-url', {}, '123', {}]
|
||||
});
|
||||
});
|
||||
expect(authService.setRedirect).toHaveBeenCalledWith({
|
||||
provider: 'ECM', navigation: ['/']
|
||||
});
|
||||
expect(authService.getRedirect('ECM')).toEqual(['/']);
|
||||
}));
|
||||
|
||||
describe('with query params', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: false
|
||||
});
|
||||
it('should get redirect url from config if there is one configured', async(() => {
|
||||
appConfigService.config.loginRoute = 'fakeLoginRoute';
|
||||
spyOn(authService, 'setRedirect').and.callThrough();
|
||||
spyOn(routerService, 'navigate').and.stub();
|
||||
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
|
||||
|
||||
const { guard, auth, router } = this.test;
|
||||
authGuard.canActivate(null, router);
|
||||
|
||||
guard.canActivate(null, { url: 'some-url;q=123' }).then((activate) => {
|
||||
this.auth = auth;
|
||||
});
|
||||
|
||||
this.navigateSpy = spyOn(router, 'navigate');
|
||||
}));
|
||||
|
||||
it('should set redirect navigation commands with query params', () => {
|
||||
expect(this.auth.setRedirect).toHaveBeenCalledWith({
|
||||
provider: 'ECM', navigation: ['some-url', { q: '123' }]
|
||||
});
|
||||
});
|
||||
expect(authService.setRedirect).toHaveBeenCalledWith({
|
||||
provider: 'ECM', navigation: ['some-url', {}]
|
||||
});
|
||||
expect(routerService.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']);
|
||||
}));
|
||||
|
||||
describe('with no route state', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: false
|
||||
});
|
||||
|
||||
const { guard, auth, router } = this.test;
|
||||
|
||||
guard.canActivate(null, { url: '/' }).then((activate) => {
|
||||
this.auth = auth;
|
||||
});
|
||||
|
||||
this.navigateSpy = spyOn(router, 'navigate');
|
||||
}));
|
||||
|
||||
it('should set redirect navigation commands with query params', () => {
|
||||
expect(this.auth.setRedirect).toHaveBeenCalledWith({
|
||||
provider: 'ECM', navigation: ['/']
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('canActivateChild', () => {
|
||||
describe('user is not logged in', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: false
|
||||
});
|
||||
|
||||
const { guard, router } = this.test;
|
||||
|
||||
guard.canActivateChild(null, { url: 'some-url' }).then((activate) => {
|
||||
this.activate = activate;
|
||||
});
|
||||
|
||||
this.navigateSpy = spyOn(router, 'navigate');
|
||||
}));
|
||||
|
||||
it('should not allow route to activate', () => {
|
||||
expect(this.activate).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('user is logged in but ticket is invalid', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: true,
|
||||
validateTicket: false
|
||||
});
|
||||
|
||||
const { guard, router } = this.test;
|
||||
|
||||
guard.canActivateChild(null, { url: 'some-url' }).then((activate) => {
|
||||
this.activate = activate;
|
||||
});
|
||||
|
||||
this.navigateSpy = spyOn(router, 'navigate');
|
||||
}));
|
||||
|
||||
it('should not allow route to activate', () => {
|
||||
expect(this.activate).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('user is logged in and ticket is valid', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: true,
|
||||
validateTicket: true
|
||||
});
|
||||
|
||||
const { guard, router } = this.test;
|
||||
|
||||
guard.canActivateChild(null, { url: '' }).then((activate) => {
|
||||
this.activate = activate;
|
||||
});
|
||||
|
||||
this.navigateSpy = spyOn(router, 'navigate');
|
||||
}));
|
||||
|
||||
it('should allow route to activate', () => {
|
||||
expect(this.activate).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user