Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
feat(collabs): add collaborators service
Browse files Browse the repository at this point in the history
  • Loading branch information
dgutride authored and joshuawilson committed Apr 25, 2017
1 parent 2f575bc commit 5687e27
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
118 changes: 118 additions & 0 deletions src/app/collaborators/collaborator.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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');
});
}));
});
64 changes: 64 additions & 0 deletions src/app/collaborators/collaborator.service.ts
Original file line number Diff line number Diff line change
@@ -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<User[]> {
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<Response> {
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<void> {
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);
}

}

0 comments on commit 5687e27

Please sign in to comment.