Fix authentication unit tests

This commit is contained in:
Amedeo Lepore
2023-07-04 15:23:09 +02:00
committed by eromano
parent 4f74e570b3
commit f47d0d026c
5 changed files with 44 additions and 22 deletions

View File

@@ -54,14 +54,20 @@ export class BasicAlfrescoAuthService extends BaseAuthenticationService {
) {
super(appConfig, cookie, logService);
this.appConfig.onLoad.subscribe(() => {
if (this.isLoggedIn()) {
this.onLogin.next('logged-in');
}
});
this.contentAuth.onLogout.pipe(map((event) => {
this.onLogout.next(event);
}));
this.contentAuth.onLogin.pipe(map((event) => {
this.onLogout.next(event);
this.onLogin.next(event);
}));
this.contentAuth.onError.pipe(map((event) => {
this.onLogout.next(event);
this.onError.next(event);
}));
this.processAuth.onLogout.pipe(map((event) => {
this.onLogout.next(event);
@@ -185,8 +191,26 @@ export class BasicAlfrescoAuthService extends BaseAuthenticationService {
}
getToken(): string {
if (this.isBPMProvider()) {
return this.processAuth.getToken();
} else if (this.isECMProvider()) {
return this.contentAuth.getToken();
} else if (this.isALLProvider()) {
return this.contentAuth.getToken();
}else{
return '';
}
}
/** @deprecated */
getTicketEcm(): string{
return this.contentAuth.getToken();
}
/** @deprecated */
getTicketBpm(): string{
return this.processAuth.getToken();
}
isBpmLoggedIn(): boolean {
return this.processAuth.isLoggedIn();
@@ -213,17 +237,11 @@ export class BasicAlfrescoAuthService extends BaseAuthenticationService {
/**
* logout Alfresco API
* */
logout(): Promise<any> {
async logout(): Promise<any> {
if (this.isBPMProvider()) {
return this.processAuth.logout();
} else if (this.isECMProvider()) {
const contentPromise = this.contentAuth.logout();
contentPromise.then(
() => this.contentAuth.ticket = undefined,
() => {
}
);
return contentPromise;
return this.contentAuth.logout();
} else if (this.isALLProvider()) {
return this.logoutBPMECM();
}
@@ -321,9 +339,9 @@ export class BasicAlfrescoAuthService extends BaseAuthenticationService {
const contextRoot = this.appConfig.get<string>(AppConfigValues.CONTEXTROOTECM);
if (contextRoot && requestUrl.indexOf(contextRoot) !== -1) {
ticket = 'Basic ' + btoa(this.contentAuth.getTicket());
ticket = 'Basic ' + btoa(this.contentAuth.getToken());
} else if (contextRootBpm && requestUrl.indexOf(contextRootBpm) !== -1) {
ticket = 'Basic ' + this.processAuth.getTicket();
ticket = 'Basic ' + this.processAuth.getToken();
}
return ticket;

View File

@@ -133,9 +133,9 @@ export class ContentAuth {
return new Promise((resolve, reject) => {
this.deleteTicket().then(
() => {
this.invalidateSession();
this.adfHttpClient.emit('logout');
this.onLogout.next('logout');
this.invalidateSession();
resolve('logout');
},
(error) => {
@@ -164,7 +164,7 @@ export class ContentAuth {
/**
* Get the current Ticket
* */
getTicket(): string {
getToken(): string {
if(!this.ticket){
this.onError.next('error');
}

View File

@@ -91,11 +91,11 @@ export class ProcessAuth {
accept: 'application/json'
};
let promise: any = new Promise((resolve, reject) => {
const promise: any = new Promise((resolve, reject) => {
this.adfHttpClient.post(this.basePath + '/app/authentication', options).then(
() => {
this.saveUsername(username);
let ticket = this.basicAuth(this.authentications.basicAuth.username, this.authentications.basicAuth.password);
const ticket = this.basicAuth(this.authentications.basicAuth.username, this.authentications.basicAuth.password);
this.setTicket(ticket);
this.onLogin.next('success');
this.adfHttpClient.emit('success');
@@ -188,7 +188,7 @@ export class ProcessAuth {
/**
* Get the current Ticket
* */
getTicket(): string {
getToken(): string {
if(!this.ticket){
this.onError.next('error');
return null;

View File

@@ -94,10 +94,8 @@ describe('AuthenticationService', () => {
spyOn(basicAlfrescoAuthService, 'isRememberMeSet').and.returnValue(false);
spyOn(authService, 'isECMProvider').and.returnValue(true);
spyOn(authService, 'isOauth').and.returnValue(false);
spyOn(apiService, 'getInstance').and.callThrough();
expect(authService.isEcmLoggedIn()).toBeFalsy();
expect(apiService.getInstance).toHaveBeenCalled();
});
it('should require remember me set for ECM check', () => {
@@ -114,7 +112,7 @@ describe('AuthenticationService', () => {
it('[ECM] should return an ECM ticket after the login done', (done) => {
const disposableLogin = basicAlfrescoAuthService.login('fake-username', 'fake-password').subscribe(() => {
expect(authService.isLoggedIn()).toBe(true);
expect(basicAlfrescoAuthService.getToken()).toEqual('fake-post-ticket');
expect(authService.getToken()).toEqual('fake-post-ticket');
expect(authService.isEcmLoggedIn()).toBe(true);
disposableLogin.unsubscribe();
done();
@@ -140,7 +138,7 @@ describe('AuthenticationService', () => {
});
}));
it('[ECM] should return a ticket undefined after logout', fakeAsync(() => {
fit('[ECM] should return a ticket undefined after logout', fakeAsync(() => {
const disposableLogin = basicAlfrescoAuthService.login('fake-username', 'fake-password').subscribe(() => {
const disposableLogout = authService.logout().subscribe(() => {
expect(authService.isLoggedIn()).toBe(false);

View File

@@ -23,6 +23,7 @@ import { BaseAuthenticationService } from './base-authentication.service';
import { AppConfigService } from '../../app-config';
import { CookieService, LogService } from '../../common';
import { HttpHeaders } from '@angular/common/http';
import { tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
@@ -34,6 +35,11 @@ export class AuthenticationService extends BaseAuthenticationService {
logService: LogService,
private injector: Injector) {
super(appConfig, cookie, logService);
(this.isOauth() ? this.oidcAuthenticationService.onLogin : this.basicAlfrescoAuthService.onLogin)
.pipe(
tap(() => this.onLogin.next())
).subscribe();
}
private get oidcAuthenticationService(): OidcAuthenticationService {