diff --git a/ng2-components/ng2-alfresco-core/src/assets/AlfrescoApi.mock.ts b/ng2-components/ng2-alfresco-core/src/assets/AlfrescoApi.mock.ts new file mode 100644 index 0000000000..5324f862b3 --- /dev/null +++ b/ng2-components/ng2-alfresco-core/src/assets/AlfrescoApi.mock.ts @@ -0,0 +1,62 @@ +/*! + * @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. + */ + +export class AlfrescoApiMock { + + static getClientWithTicket(basePath: string, ticket: string) { + return {}; + } + +} +export module AlfrescoApiMock { + + export module Auth { + + export class AuthenticationApi { + + constructor(alfrescoClient: any) { + } + + createTicket(loginRequest: any) { + if (loginRequest.userId === 'fake-username' && loginRequest.password === 'fake-password') { + return new Promise(function (resolve, reject) { + resolve({ + userId: 'fake-username', + ticket: 'TICKET_blah' + }); + }); + } else { + new Promise(function (resolve, reject) { + reject(); + }); + } + } + + deleteTicket(loginRequest: any) { + return new Promise(function (resolve, reject) { + resolve(); + }); + } + + } + + export class LoginRequest { + } + } +} + + diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.spec.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.spec.ts index d912d42258..c110a1205e 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.spec.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.spec.ts @@ -16,25 +16,20 @@ */ import { it, describe, beforeEach } from 'angular2/testing'; -import { provide, Injector } from 'angular2/core'; -import { Http, HTTP_PROVIDERS, XHRBackend, Response, ResponseOptions } from 'angular2/http'; -import { MockBackend } from 'angular2/http/testing'; -import { AlfrescoAuthenticationService } from './AlfrescoAuthenticationService.service'; +import { Injector } from 'angular2/core'; import { AlfrescoSettingsService } from './AlfrescoSettingsService.service'; +import { AlfrescoAuthenticationService } from './AlfrescoAuthenticationService.service'; +import { AlfrescoApiMock } from '../assets/AlfrescoApi.mock'; + +declare var AlfrescoApi: any; describe('AlfrescoAuthentication', () => { let injector, - backend, - mockBackend, - httpService, service; beforeEach(() => { injector = Injector.resolveAndCreate([ - HTTP_PROVIDERS, - MockBackend, - provide(XHRBackend, {useClass: MockBackend}), AlfrescoAuthenticationService, AlfrescoSettingsService ]); @@ -58,14 +53,12 @@ describe('AlfrescoAuthentication', () => { return keys[i] || null; }); - mockBackend = injector.get(MockBackend); - backend = injector.get(XHRBackend); - httpService = injector.get(Http); + window['AlfrescoApi'] = AlfrescoApiMock; service = injector.get(AlfrescoAuthenticationService); }); it('should return true and token if the user is logged in', () => { - service.saveJwt('fake-local-token'); + service.saveToken('fake-local-token'); expect(service.isLoggedIn()).toBe(true); expect(localStorage.getItem('token')).toBeDefined(); expect(localStorage.getItem('token')).toEqual('fake-local-token'); @@ -78,11 +71,8 @@ describe('AlfrescoAuthentication', () => { }); it('should return true and token on sign in', () => { - backend.connections.subscribe(connection => { - connection.mockRespond(new Response(new ResponseOptions({body: {data: {ticket: 'fake-post-token'}}}))); - }); service.token = ''; - service.login('POST', 'fakeUser', 'fakePassword') + service.login('fake-username', 'fake-password') .subscribe(() => { expect(service.isLoggedIn()).toBe(true); expect(service.token).toEqual('fake-post-token'); @@ -104,16 +94,4 @@ describe('AlfrescoAuthentication', () => { ); }); - it('should return no error if method value is GET', () => { - expect(service.login('GET', 'fakeUser', 'fakePassword').hasErrored).toBe(false); - }); - - it('should return no error if method value is POST', () => { - expect(service.login('POST', 'fakeUser', 'fakePassword').hasErrored).toBe(false); - }); - - it('should throw an exception if method value is different from GET or POST', () => { - expect(service.login('PUT', 'fakeUser', 'fakePassword').error).toEqual('Invalid method name the value should be GET or POST'); - }); - }); diff --git a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.ts b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.ts index 41ce9215e3..55bc905fd6 100644 --- a/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.ts +++ b/ng2-components/ng2-alfresco-core/src/services/AlfrescoAuthenticationService.service.ts @@ -20,6 +20,8 @@ import { Observable } from 'rxjs/Rx'; import { Http, Headers, Response } from 'angular2/http'; import { AlfrescoSettingsService } from './AlfrescoSettingsService.service'; +declare let AlfrescoApi: any; + /** * The AlfrescoAuthenticationService provide the login service and store the token in the localStorage */ @@ -30,15 +32,19 @@ export class AlfrescoAuthenticationService { /** * Constructor - * @param http + * @param alfrescoSettingsService */ - constructor(public http: Http, private alfrescoSettingsService: AlfrescoSettingsService) { + constructor(private alfrescoSettingsService: AlfrescoSettingsService) { } getBaseUrl(): string { return this.alfrescoSettingsService.host + this._authUrl; } + private getAlfrescoClient() { + return AlfrescoApi.getClientWithTicket(this.getBaseUrl(), this.getToken()); + } + /** * The method return tru if the user is logged in * @returns {boolean} @@ -58,49 +64,50 @@ export class AlfrescoAuthenticationService { } /** - * The method provide the login with POST Request + * Perform a login on behalf of the user and store the ticket returned + * * @param username * @param password * @returns {Observable|Observable} */ loginPost(username: string, password: string) { - let credentials = '{ "userId": "' + username + '", "password": "' + password + '" }'; - - let headers = new Headers(); - headers.append('Content-Type', 'application/json'); - headers.append('Accept', 'application/json'); - - return this.http.post(this.getBaseUrl() + '/tickets', credentials, { - headers: headers - }) - .map((res: any) => { - let response = res.json(); + return Observable.fromPromise(this.getCreateTicketPromise(username, password)) + .map(res => res) + .do(response => { this.saveToken(response.entry.id); return this.getToken(); }) .catch(this.handleError); } + getCreateTicketPromise(username: string, password: string) { + let apiInstance = new AlfrescoApi.Auth.AuthenticationApi(this.getAlfrescoClient()); + let loginRequest = new AlfrescoApi.Auth.LoginRequest(); + loginRequest.userId = username; + loginRequest.password = password; + return apiInstance.createTicket(loginRequest); + } + /** * Delete the current login ticket from the server * * @returns {Observable|Observable} */ loginDelete() { - let headers = new Headers(); - headers.append('Content-Type', 'application/json'); - headers.append('Authorization', 'Basic ' + btoa(this.getToken())); - - return this.http.delete(this.getBaseUrl() + '/tickets/-me-', { - headers: headers - }) - .map((res: any) => { + return Observable.fromPromise(this.getDeleteTicketPromise()) + .map(res => res) + .do(response => { this.removeToken(); this.saveToken(''); }) .catch(this.handleError); } + getDeleteTicketPromise() { + let apiInstance = new AlfrescoApi.Auth.AuthenticationApi(this.getAlfrescoClient()); + return apiInstance.deleteTicket(); + } + /** * Return the token stored in the localStorage * @param token @@ -141,6 +148,6 @@ export class AlfrescoAuthenticationService { */ private handleError(error: Response) { console.error('Error when logging in', error); - return Observable.throw(error.json().message || 'Server error'); + return Observable.throw(error || 'Server error'); } }