This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(collabs): add collaborators service
- Loading branch information
1 parent
2f575bc
commit 5687e27
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |