mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-3224] move ticket store logic in js-api (#3505)
* move ticket store logic in js-api * remove space * fix test * fix tests
This commit is contained in:
@@ -125,8 +125,6 @@ export class AlfrescoApiService {
|
||||
|
||||
const config = {
|
||||
provider: this.appConfig.get<string>(AppConfigValues.PROVIDERS),
|
||||
ticketEcm: this.storage.getItem('ticket-ECM'),
|
||||
ticketBpm: this.storage.getItem('ticket-BPM'),
|
||||
hostEcm: this.appConfig.get<string>(AppConfigValues.ECMHOST),
|
||||
hostBpm: this.appConfig.get<string>(AppConfigValues.BPMHOST),
|
||||
authType: this.appConfig.get<string>(AppConfigValues.AUTHTYPE, 'BASIC'),
|
||||
|
@@ -19,7 +19,6 @@ import { TestBed } from '@angular/core/testing';
|
||||
import { AlfrescoApiService } from './alfresco-api.service';
|
||||
import { AuthenticationService } from './authentication.service';
|
||||
import { CookieService } from './cookie.service';
|
||||
import { StorageService } from './storage.service';
|
||||
import { AppConfigService } from '../app-config/app-config.service';
|
||||
import { setupTestBed } from '../testing/setupTestBed';
|
||||
import { CoreTestingModule } from '../testing/core.testing.module';
|
||||
@@ -30,7 +29,6 @@ describe('AuthenticationService', () => {
|
||||
let apiService: AlfrescoApiService;
|
||||
let authService: AuthenticationService;
|
||||
let appConfigService: AppConfigService;
|
||||
let storage: StorageService;
|
||||
let cookie: CookieService;
|
||||
|
||||
setupTestBed({
|
||||
@@ -38,28 +36,25 @@ describe('AuthenticationService', () => {
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
sessionStorage.clear();
|
||||
apiService = TestBed.get(AlfrescoApiService);
|
||||
authService = TestBed.get(AuthenticationService);
|
||||
|
||||
cookie = TestBed.get(CookieService);
|
||||
cookie.clear();
|
||||
|
||||
storage = TestBed.get(StorageService);
|
||||
storage.clear();
|
||||
|
||||
jasmine.Ajax.install();
|
||||
appConfigService = TestBed.get(AppConfigService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cookie.clear();
|
||||
storage.clear();
|
||||
jasmine.Ajax.uninstall();
|
||||
});
|
||||
|
||||
describe('remember me', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
appConfigService = TestBed.get(AppConfigService);
|
||||
appConfigService.config.providers = 'ECM';
|
||||
appConfigService.load();
|
||||
apiService.reset();
|
||||
@@ -164,49 +159,6 @@ describe('AuthenticationService', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('[ECM] should save only ECM ticket on localStorage', (done) => {
|
||||
let disposableLogin = authService.login('fake-username', 'fake-password').subscribe(() => {
|
||||
expect(authService.isLoggedIn()).toBe(true);
|
||||
expect(authService.getTicketBpm()).toBeNull();
|
||||
expect(apiService.getInstance().bpmAuth.isLoggedIn()).toBeFalsy();
|
||||
disposableLogin.unsubscribe();
|
||||
done();
|
||||
});
|
||||
|
||||
jasmine.Ajax.requests.mostRecent().respondWith({
|
||||
'status': 201,
|
||||
contentType: 'application/json',
|
||||
responseText: JSON.stringify({ 'entry': { 'id': 'fake-post-ticket', 'userId': 'admin' } })
|
||||
});
|
||||
});
|
||||
|
||||
it('[ECM] should return ticket undefined when the credentials are wrong', (done) => {
|
||||
let disposableLogin = authService.login('fake-wrong-username', 'fake-wrong-password').subscribe(
|
||||
(res) => {
|
||||
},
|
||||
(err: any) => {
|
||||
expect(authService.isLoggedIn()).toBe(false);
|
||||
expect(authService.getTicketEcm()).toBe(null);
|
||||
expect(authService.isEcmLoggedIn()).toBe(false);
|
||||
disposableLogin.unsubscribe();
|
||||
done();
|
||||
});
|
||||
|
||||
jasmine.Ajax.requests.mostRecent().respondWith({
|
||||
'status': 403,
|
||||
contentType: 'application/json',
|
||||
responseText: JSON.stringify({
|
||||
'error': {
|
||||
'errorKey': 'Login failed',
|
||||
'statusCode': 403,
|
||||
'briefSummary': '05150009 Login failed',
|
||||
'stackTrace': 'For security reasons the stack trace is no longer displayed, but the property is kept for previous versions.',
|
||||
'descriptionURL': 'https://api-explorer.alfresco.com'
|
||||
}
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it('[ECM] should login in the ECM if no provider are defined calling the login', (done) => {
|
||||
let disposableLogin = authService.login('fake-username', 'fake-password').subscribe(() => {
|
||||
disposableLogin.unsubscribe();
|
||||
@@ -308,39 +260,6 @@ describe('AuthenticationService', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('[BPM] should save only BPM ticket on localStorage', (done) => {
|
||||
let disposableLogin = authService.login('fake-username', 'fake-password').subscribe(() => {
|
||||
expect(authService.isLoggedIn()).toBe(true);
|
||||
expect(authService.getTicketEcm()).toBeNull();
|
||||
expect(apiService.getInstance().ecmAuth.isLoggedIn()).toBeFalsy();
|
||||
disposableLogin.unsubscribe();
|
||||
done();
|
||||
});
|
||||
|
||||
jasmine.Ajax.requests.mostRecent().respondWith({
|
||||
'status': 201,
|
||||
contentType: 'application/json',
|
||||
responseText: JSON.stringify({ 'entry': { 'id': 'fake-post-ticket', 'userId': 'admin' } })
|
||||
});
|
||||
});
|
||||
|
||||
it('[BPM] should return ticket undefined when the credentials are wrong', (done) => {
|
||||
let disposableLogin = authService.login('fake-wrong-username', 'fake-wrong-password').subscribe(
|
||||
(res) => {
|
||||
},
|
||||
(err: any) => {
|
||||
expect(authService.isLoggedIn()).toBe(false, 'isLoggedIn');
|
||||
expect(authService.getTicketBpm()).toBe(null, 'getTicketBpm');
|
||||
expect(authService.isBpmLoggedIn()).toBe(false, 'isBpmLoggedIn');
|
||||
disposableLogin.unsubscribe();
|
||||
done();
|
||||
});
|
||||
|
||||
jasmine.Ajax.requests.mostRecent().respondWith({
|
||||
'status': 403
|
||||
});
|
||||
});
|
||||
|
||||
it('[BPM] should return a ticket undefined after logout', (done) => {
|
||||
let disposableLogin = authService.login('fake-username', 'fake-password').subscribe(() => {
|
||||
let disposableLogout = authService.logout().subscribe(() => {
|
||||
@@ -432,8 +351,8 @@ describe('AuthenticationService', () => {
|
||||
},
|
||||
(err: any) => {
|
||||
expect(authService.isLoggedIn()).toBe(false, 'isLoggedIn');
|
||||
expect(authService.getTicketEcm()).toBe(null, 'getTicketEcm');
|
||||
expect(authService.getTicketBpm()).toBe(null, 'getTicketBpm');
|
||||
expect(authService.getTicketEcm()).toBe(undefined, 'getTicketEcm');
|
||||
expect(authService.getTicketBpm()).toBe('Basic ZmFrZS11c2VybmFtZTpmYWtlLXBhc3N3b3Jk', 'getTicketBpm');
|
||||
expect(authService.isEcmLoggedIn()).toBe(false, 'isEcmLoggedIn');
|
||||
disposableLogin.unsubscribe();
|
||||
done();
|
||||
@@ -454,8 +373,8 @@ describe('AuthenticationService', () => {
|
||||
},
|
||||
(err: any) => {
|
||||
expect(authService.isLoggedIn()).toBe(false);
|
||||
expect(authService.getTicketEcm()).toBe(null);
|
||||
expect(authService.getTicketBpm()).toBe(null);
|
||||
expect(authService.getTicketEcm()).toBe('fake-post-ticket');
|
||||
expect(authService.getTicketBpm()).toBe(undefined);
|
||||
expect(authService.isBpmLoggedIn()).toBe(false);
|
||||
disposableLogin.unsubscribe();
|
||||
done();
|
||||
@@ -478,8 +397,8 @@ describe('AuthenticationService', () => {
|
||||
},
|
||||
(err: any) => {
|
||||
expect(authService.isLoggedIn()).toBe(false);
|
||||
expect(authService.getTicketEcm()).toBe(null);
|
||||
expect(authService.getTicketBpm()).toBe(null);
|
||||
expect(authService.getTicketEcm()).toBe(undefined);
|
||||
expect(authService.getTicketBpm()).toBe(undefined);
|
||||
expect(authService.isBpmLoggedIn()).toBe(false);
|
||||
expect(authService.isEcmLoggedIn()).toBe(false);
|
||||
disposableLogin.unsubscribe();
|
||||
|
@@ -21,7 +21,6 @@ import { Subject } from 'rxjs/Subject';
|
||||
import { AlfrescoApiService } from './alfresco-api.service';
|
||||
import { CookieService } from './cookie.service';
|
||||
import { LogService } from './log.service';
|
||||
import { StorageService } from './storage.service';
|
||||
import { RedirectionModel } from '../models/redirection.model';
|
||||
import { AppConfigService, AppConfigValues } from '../app-config/app-config.service';
|
||||
import 'rxjs/add/observable/fromPromise';
|
||||
@@ -41,7 +40,6 @@ export class AuthenticationService {
|
||||
constructor(
|
||||
private appConfig: AppConfigService,
|
||||
private alfrescoApi: AlfrescoApiService,
|
||||
private storage: StorageService,
|
||||
private cookie: CookieService,
|
||||
private logService: LogService) {
|
||||
}
|
||||
@@ -66,11 +64,9 @@ export class AuthenticationService {
|
||||
* @returns Object with auth type ("ECM", "BPM" or "ALL") and auth ticket
|
||||
*/
|
||||
login(username: string, password: string, rememberMe: boolean = false): Observable<{ type: string, ticket: any }> {
|
||||
this.removeTicket();
|
||||
return Observable.fromPromise(this.alfrescoApi.getInstance().login(username, password))
|
||||
.map((response: any) => {
|
||||
this.saveRememberMeCookie(rememberMe);
|
||||
this.saveTickets();
|
||||
this.onLogin.next(response);
|
||||
return {
|
||||
type: this.appConfig.get(AppConfigValues.PROVIDERS),
|
||||
@@ -116,7 +112,7 @@ export class AuthenticationService {
|
||||
* @returns Response event called when logout is complete
|
||||
*/
|
||||
logout() {
|
||||
this.removeTicket();
|
||||
this.alfrescoApi.getInstance().invalidateSession();
|
||||
|
||||
return Observable.fromPromise(this.callApiLogout())
|
||||
.do(response => {
|
||||
@@ -135,21 +131,12 @@ export class AuthenticationService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the login ticket from Storage.
|
||||
*/
|
||||
removeTicket(): void {
|
||||
this.storage.removeItem('ticket-ECM');
|
||||
this.storage.removeItem('ticket-BPM');
|
||||
this.alfrescoApi.getInstance().setTicket(undefined, undefined);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the ECM ticket stored in the Storage.
|
||||
* @returns The ticket or `null` if none was found
|
||||
*/
|
||||
getTicketEcm(): string | null {
|
||||
return this.storage.getItem('ticket-ECM');
|
||||
return this.alfrescoApi.getInstance().getTicketEcm();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,7 +144,7 @@ export class AuthenticationService {
|
||||
* @returns The ticket or `null` if none was found
|
||||
*/
|
||||
getTicketBpm(): string | null {
|
||||
return this.storage.getItem('ticket-BPM');
|
||||
return this.alfrescoApi.getInstance().getTicketBpm();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,49 +152,13 @@ export class AuthenticationService {
|
||||
* @returns The ticket or `null` if none was found
|
||||
*/
|
||||
getTicketEcmBase64(): string | null {
|
||||
let ticket = this.storage.getItem('ticket-ECM');
|
||||
let ticket = this.alfrescoApi.getInstance().getTicketEcm();
|
||||
if (ticket) {
|
||||
return 'Basic ' + btoa(ticket);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the ECM and BPM ticket in the Storage.
|
||||
*/
|
||||
saveTickets(): void {
|
||||
this.saveTicketEcm();
|
||||
this.saveTicketBpm();
|
||||
this.saveTicketAuth();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the ECM ticket in the Storage.
|
||||
*/
|
||||
saveTicketEcm(): void {
|
||||
if (this.alfrescoApi.getInstance() && this.alfrescoApi.getInstance().getTicketEcm()) {
|
||||
this.storage.setItem('ticket-ECM', this.alfrescoApi.getInstance().getTicketEcm());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the BPM ticket in the Storage.
|
||||
*/
|
||||
saveTicketBpm(): void {
|
||||
if (this.alfrescoApi.getInstance() && this.alfrescoApi.getInstance().getTicketBpm()) {
|
||||
this.storage.setItem('ticket-BPM', this.alfrescoApi.getInstance().getTicketBpm());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the AUTH ticket in the Storage.
|
||||
*/
|
||||
saveTicketAuth(): void {
|
||||
if (this.alfrescoApi.getInstance() && (<any> this.alfrescoApi.getInstance()).getTicketAuth()) {
|
||||
this.storage.setItem('ticket-AUTH', (<any> this.alfrescoApi.getInstance()).getTicketAuth());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user is logged in on an ECM provider.
|
||||
* @returns True if logged in, false otherwise
|
||||
|
@@ -32,12 +32,12 @@ const allowAngularToReset = () => (TestBed.resetTestingModule = resetTestingModu
|
||||
export const setupTestBed = (moduleDef: TestModuleMetadata) => {
|
||||
beforeAll(done =>
|
||||
(async () => {
|
||||
localStorage.clear();
|
||||
sessionStorage.clear();
|
||||
resetTestingModule();
|
||||
preventAngularFromResetting();
|
||||
TestBed.configureTestingModule(moduleDef);
|
||||
await TestBed.compileComponents();
|
||||
localStorage.clear();
|
||||
sessionStorage.clear();
|
||||
|
||||
// prevent Angular from resetting testing module
|
||||
TestBed.resetTestingModule = () => TestBed;
|
||||
|
Reference in New Issue
Block a user