mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
change ticket storing and remove is login check in the login
This commit is contained in:
@@ -23,22 +23,11 @@ import {AlfrescoAuthenticationService} from './AlfrescoAuthentication.service';
|
|||||||
declare var AlfrescoApi: any;
|
declare var AlfrescoApi: any;
|
||||||
|
|
||||||
describe('AlfrescoAuthentication', () => {
|
describe('AlfrescoAuthentication', () => {
|
||||||
let injector, fakePromiseECM, fakePromiseBPM, authService, fakePromiseBPMECM;
|
let injector, fakePromise, fakePromiseBPMECM, authService;
|
||||||
|
|
||||||
fakePromiseECM = new Promise(function (resolve, reject) {
|
fakePromise = new Promise(function (resolve, reject) {
|
||||||
resolve(
|
resolve(
|
||||||
'fake-post-ticket-ECM'
|
'fake-post-ticket'
|
||||||
);
|
|
||||||
reject({
|
|
||||||
response: {
|
|
||||||
error: 'fake-error'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
fakePromiseBPM = new Promise(function (resolve, reject) {
|
|
||||||
resolve(
|
|
||||||
'fake-post-ticket-BPM'
|
|
||||||
);
|
);
|
||||||
reject({
|
reject({
|
||||||
response: {
|
response: {
|
||||||
@@ -91,11 +80,11 @@ describe('AlfrescoAuthentication', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return an ECM ticket after the login done', (done) => {
|
it('should return an ECM ticket after the login done', (done) => {
|
||||||
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromise);
|
||||||
|
|
||||||
authService.login('fake-username', 'fake-password').subscribe(() => {
|
authService.login('fake-username', 'fake-password').subscribe(() => {
|
||||||
expect(authService.isLoggedIn()).toBe(true);
|
expect(authService.isLoggedIn()).toBe(true);
|
||||||
expect(authService.getTicket()).toEqual('fake-post-ticket-ECM');
|
expect(authService.getTicket()).toEqual('fake-post-ticket');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -115,7 +104,7 @@ describe('AlfrescoAuthentication', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should login in the ECM if no provider are defined calling the login', (done) => {
|
it('should login in the ECM if no provider are defined calling the login', (done) => {
|
||||||
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseECM);
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromise);
|
||||||
|
|
||||||
authService.login('fake-username', 'fake-password').subscribe(() => {
|
authService.login('fake-username', 'fake-password').subscribe(() => {
|
||||||
done();
|
done();
|
||||||
@@ -123,28 +112,28 @@ describe('AlfrescoAuthentication', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return a ticket undefined after logout', (done) => {
|
it('should return a ticket undefined after logout', (done) => {
|
||||||
localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM');
|
localStorage.setItem('ticket', 'fake-post-ticket');
|
||||||
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseECM);
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromise);
|
||||||
|
|
||||||
authService.logout().subscribe(() => {
|
authService.logout().subscribe(() => {
|
||||||
expect(authService.isLoggedIn()).toBe(false);
|
expect(authService.isLoggedIn()).toBe(false);
|
||||||
expect(authService.getTicket()).toBeUndefined();
|
expect(authService.getTicket()).toBeUndefined();
|
||||||
expect(localStorage.getItem('ticket-ECM')).toBeUndefined();
|
expect(localStorage.getItem('ticket')).toBeUndefined();
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should logout only if the provider is already logged in', (done) => {
|
it('should logout only if the provider is already logged in', (done) => {
|
||||||
localStorage.setItem('ticket-ECM', 'fake-post-ticket-ECM');
|
localStorage.setItem('ticket', 'fake-post-ticket');
|
||||||
|
|
||||||
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseECM);
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromise);
|
||||||
|
|
||||||
authService.saveTicket('fake-ticket-ECM');
|
authService.saveTicket('fake-ticket');
|
||||||
|
|
||||||
authService.logout().subscribe(() => {
|
authService.logout().subscribe(() => {
|
||||||
expect(authService.isLoggedIn()).toBe(false);
|
expect(authService.isLoggedIn()).toBe(false);
|
||||||
expect(authService.getTicket()).toBeUndefined();
|
expect(authService.getTicket()).toBeUndefined();
|
||||||
expect(localStorage.getItem('ticket-ECM')).toBeUndefined();
|
expect(localStorage.getItem('ticket')).toBeUndefined();
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -163,11 +152,11 @@ describe('AlfrescoAuthentication', () => {
|
|||||||
|
|
||||||
|
|
||||||
it('should return an BPM ticket after the login done', (done) => {
|
it('should return an BPM ticket after the login done', (done) => {
|
||||||
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPM);
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromise);
|
||||||
|
|
||||||
authService.login('fake-username', 'fake-password').subscribe(() => {
|
authService.login('fake-username', 'fake-password').subscribe(() => {
|
||||||
expect(authService.isLoggedIn()).toBe(true);
|
expect(authService.isLoggedIn()).toBe(true);
|
||||||
expect(authService.getTicket()).toEqual('fake-post-ticket-BPM');
|
expect(authService.getTicket()).toEqual('fake-post-ticket');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -186,22 +175,22 @@ describe('AlfrescoAuthentication', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return a ticket undefined after logout', (done) => {
|
it('should return a ticket undefined after logout', (done) => {
|
||||||
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPM);
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromise);
|
||||||
|
|
||||||
authService.login('fake-username', 'fake-password').subscribe(() => {
|
authService.login('fake-username', 'fake-password').subscribe(() => {
|
||||||
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromiseBPM);
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(fakePromise);
|
||||||
|
|
||||||
authService.logout().subscribe(() => {
|
authService.logout().subscribe(() => {
|
||||||
expect(authService.isLoggedIn()).toBe(false);
|
expect(authService.isLoggedIn()).toBe(false);
|
||||||
expect(authService.getTicket()).toBeUndefined();
|
expect(authService.getTicket()).toBeUndefined();
|
||||||
expect(localStorage.getItem('ticket-BPM')).toBeUndefined();
|
expect(localStorage.getItem('ticket')).toBeUndefined();
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an error when the logout return error', (done) => {
|
it('should return an error when the logout return error', (done) => {
|
||||||
localStorage.setItem('ticket-BPM', 'fake-post-ticket-BPM');
|
localStorage.setItem('ticket', 'fake-post-ticket');
|
||||||
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(Promise.reject('fake logout error'));
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogout').and.returnValue(Promise.reject('fake logout error'));
|
||||||
|
|
||||||
authService.logout().subscribe(
|
authService.logout().subscribe(
|
||||||
@@ -209,7 +198,7 @@ describe('AlfrescoAuthentication', () => {
|
|||||||
},
|
},
|
||||||
(err: any) => {
|
(err: any) => {
|
||||||
expect(err).toBeDefined();
|
expect(err).toBeDefined();
|
||||||
expect(localStorage.getItem('ticket-BPM')).toEqual('fake-post-ticket-BPM');
|
expect(localStorage.getItem('ticket')).toEqual('fake-post-ticket');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -220,7 +209,7 @@ describe('AlfrescoAuthentication', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
authService = injector.get(AlfrescoAuthenticationService);
|
authService = injector.get(AlfrescoAuthenticationService);
|
||||||
authService.alfrescoSetting.setProviders('ALL');
|
authService.alfrescoSetting.setProviders('ALL');
|
||||||
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromiseBPMECM);
|
spyOn(AlfrescoAuthenticationService.prototype, 'callApiLogin').and.returnValue(fakePromise);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should host ecm url change be reflected in the api configuration', (done) => {
|
it('should host ecm url change be reflected in the api configuration', (done) => {
|
||||||
|
@@ -69,13 +69,6 @@ export class AlfrescoAuthenticationService {
|
|||||||
* @returns {Observable<R>|Observable<T>}
|
* @returns {Observable<R>|Observable<T>}
|
||||||
*/
|
*/
|
||||||
login(username: string, password: string) {
|
login(username: string, password: string) {
|
||||||
|
|
||||||
if (this.isLoggedIn()) {
|
|
||||||
return Observable.create((observer) => {
|
|
||||||
observer.next({type: this.alfrescoSetting.getProviders(), ticket: this.getTicket()});
|
|
||||||
observer.complete();
|
|
||||||
}).catch(this.handleError);
|
|
||||||
} else {
|
|
||||||
return Observable.fromPromise(this.callApiLogin(username, password))
|
return Observable.fromPromise(this.callApiLogin(username, password))
|
||||||
.map((response: any) => {
|
.map((response: any) => {
|
||||||
this.saveTicket(response);
|
this.saveTicket(response);
|
||||||
@@ -83,7 +76,6 @@ export class AlfrescoAuthenticationService {
|
|||||||
})
|
})
|
||||||
.catch(this.handleError);
|
.catch(this.handleError);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the alfresco Api with user and password end call the login method
|
* Initialize the alfresco Api with user and password end call the login method
|
||||||
@@ -110,13 +102,6 @@ export class AlfrescoAuthenticationService {
|
|||||||
.catch(this.handleError);
|
.catch(this.handleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove the login ticket from localStorage
|
|
||||||
*/
|
|
||||||
public removeTicket(): void {
|
|
||||||
localStorage.removeItem(`ticket-${this.alfrescoSetting.getProviders()}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @returns {*|Observable<string>|Observable<any>|Promise<T>}
|
* @returns {*|Observable<string>|Observable<any>|Promise<T>}
|
||||||
@@ -127,12 +112,19 @@ export class AlfrescoAuthenticationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the login ticket from localStorage
|
||||||
|
*/
|
||||||
|
public removeTicket(): void {
|
||||||
|
localStorage.removeItem('ticket');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The method return the ticket stored in the localStorage
|
* The method return the ticket stored in the localStorage
|
||||||
* @returns ticket
|
* @returns ticket
|
||||||
*/
|
*/
|
||||||
public getTicket(): string {
|
public getTicket(): string {
|
||||||
return localStorage.getItem(`ticket-${this.alfrescoSetting.getProviders()}`);
|
return localStorage.getItem('ticket');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -141,7 +133,7 @@ export class AlfrescoAuthenticationService {
|
|||||||
*/
|
*/
|
||||||
public saveTicket(ticket): void {
|
public saveTicket(ticket): void {
|
||||||
if (ticket) {
|
if (ticket) {
|
||||||
localStorage.setItem(`ticket-${this.alfrescoSetting.getProviders()}`, ticket);
|
localStorage.setItem('ticket', ticket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -49,13 +49,13 @@ describe('AlfrescoContentService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return a valid content URL', () => {
|
it('should return a valid content URL', () => {
|
||||||
expect(contentService.getContentUrl(node)).toBe('http://127.0.0.1:8080/alfresco/api/' +
|
expect(contentService.getContentUrl(node)).toBe('http://localhost:8080/alfresco/api/' +
|
||||||
'-default-/public/alfresco/versions/1/nodes/fake-node-id/content?attachment=false&alf_ticket=myTicket');
|
'-default-/public/alfresco/versions/1/nodes/fake-node-id/content?attachment=false&alf_ticket=myTicket');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a valid thumbnail URL', () => {
|
it('should return a valid thumbnail URL', () => {
|
||||||
expect(contentService.getDocumentThumbnailUrl(node))
|
expect(contentService.getDocumentThumbnailUrl(node))
|
||||||
.toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco' +
|
.toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco' +
|
||||||
'/versions/1/nodes/fake-node-id/renditions/doclib/content?attachment=false&alf_ticket=myTicket');
|
'/versions/1/nodes/fake-node-id/renditions/doclib/content?attachment=false&alf_ticket=myTicket');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user