diff --git a/index.ts b/index.ts index 017cd35..1a734a4 100644 --- a/index.ts +++ b/index.ts @@ -33,6 +33,9 @@ export { } from './src/app/models/area'; export { AreaService } from './src/app/areas/area.service'; +// Collaborators +export { CollaboratorService } from './src/app/collaborators/collaborator.service'; + // Generic classes export { GenericLinks, diff --git a/src/app/collaborators/collaborator.service.spec.ts b/src/app/collaborators/collaborator.service.spec.ts new file mode 100644 index 0000000..101d962 --- /dev/null +++ b/src/app/collaborators/collaborator.service.spec.ts @@ -0,0 +1,118 @@ +import { async, inject, TestBed } from '@angular/core/testing'; +import { BaseRequestOptions, Http, Response, ResponseOptions } from '@angular/http'; +import { MockBackend } from '@angular/http/testing'; + +import { cloneDeep } from 'lodash'; + +import { AuthenticationService, UserService, AUTH_API_URL, User } from 'ngx-login-client'; +import { Broadcaster, Logger } from 'ngx-base'; + +import { WIT_API_URL } from "../api/wit-api"; +import { CollaboratorService } from './collaborator.service'; + +describe('Service: CollaboratorService', () => { + + let collaboratorService: CollaboratorService; + let mockService: MockBackend; + let fakeAuthService: any; + + beforeEach(() => { + fakeAuthService = { + getToken: function () { + return ''; + }, + isLoggedIn: function () { + return true; + } + }; + TestBed.configureTestingModule({ + providers: [ + Logger, + BaseRequestOptions, + MockBackend, + { + provide: Http, + useFactory: (backend: MockBackend, + options: BaseRequestOptions) => new Http(backend, options), + deps: [MockBackend, BaseRequestOptions] + }, + { + provide: AuthenticationService, + useValue: fakeAuthService + }, + CollaboratorService, + { + provide: WIT_API_URL, + useValue: "http://example.com" + }, + { + provide: AUTH_API_URL, + useValue: 'http://example.com/auth' + }, + UserService, + Broadcaster + ] + }); + }); + + beforeEach(inject( + [CollaboratorService, MockBackend], + (service: CollaboratorService, mock: MockBackend) => { + collaboratorService = service; + mockService = mock; + } + )); + + let responseData: User[] = [ + { + "attributes": { + "bio": "", + "email": "user@gmail.com", + "fullName": "user name", + "imageURL": "https://www.gravatar.com/avatar/asdf.jpg", + "url": "https://user.github.io", + "username": "useruser" + }, + "id": "6abd2970-9407-469d-a8ad-9e18706d732c", + "links": { + "self": "https://api.openshift.io/api/users/6abd2970-9407-469d-a8ad-9e18706d732c" + }, + "type": "identities" + } + ]; + let response = { data: responseData, links: {} }; + let expectedResponse = cloneDeep(responseData); + + + it('Get collaborators', async(() => { + mockService.connections.subscribe((connection: any) => { + connection.mockRespond(new Response( + new ResponseOptions({ + body: JSON.stringify(response), + status: 200 + }) + )); + }); + + collaboratorService.getAllBySpaceId('1').subscribe((data: User[]) => { + expect(data[0].id).toEqual(expectedResponse[0].id); + expect(data[0].attributes.username).toEqual(expectedResponse[0].attributes.username); + }); + })); + + it('Add new collaborators', async(() => { + mockService.connections.subscribe((connection: any) => { + connection.mockRespond(new Response( + new ResponseOptions({ + body: JSON.stringify({data: ['id1']}), + status: 201 + }) + )); + }); + + collaboratorService.addCollaborators('1', responseData) + .subscribe(() => { + expect('1').toEqual('1'); + }); + })); +}); diff --git a/src/app/collaborators/collaborator.service.ts b/src/app/collaborators/collaborator.service.ts new file mode 100644 index 0000000..684d1a7 --- /dev/null +++ b/src/app/collaborators/collaborator.service.ts @@ -0,0 +1,64 @@ +import { Injectable, Inject } from '@angular/core'; +import { Headers, Http, URLSearchParams, Response } from '@angular/http'; +import { cloneDeep } from 'lodash'; +import { AuthenticationService, User } from 'ngx-login-client'; +import { Logger } from 'ngx-base'; +import { Observable } from 'rxjs'; + +import { WIT_API_URL } from '../api/wit-api'; + +@Injectable() +export class CollaboratorService { + + private headers = new Headers({ 'Content-Type': 'application/json' }); + private spacesUrl: string; + + constructor( + private http: Http, + private logger: Logger, + private auth: AuthenticationService, + @Inject(WIT_API_URL) apiUrl: string) { + if (this.auth.getToken() != null) { + this.headers.set('Authorization', 'Bearer ' + this.auth.getToken()); + } + this.spacesUrl = apiUrl + 'spaces'; + } + + getAllBySpaceId(spaceId: string): Observable { + let url = this.spacesUrl + '/' + spaceId + '/collaborators'; + return this.http + .get(url, { headers: this.headers }) + .map(response => { + let collaborators: User[] = response.json().data as User[]; + return collaborators; + }) + .catch((error) => { + return this.handleError(error); + }); + } + + addCollaborators(spaceId: string, users: User[]): Observable { + let url = this.spacesUrl + "/" + spaceId + '/collaborators'; + let payload = JSON.stringify({ data: users }); + return this.http + .post(url, payload, { headers: this.headers }) + .catch((error) => { + return this.handleError(error); + }); + } + + removeCollaborator(spaceId: string, collaboratorId: string): Observable { + let url = this.spacesUrl + "/" + spaceId + '/collaborators/' + collaboratorId; + return this.http + .delete(url, { headers: this.headers }) + .catch((error) => { + return this.handleError(error); + }); + } + + private handleError(error: any) { + this.logger.error(error); + return Observable.throw(error.message || error); + } + +}