mirror of https://github.com/Squidex/squidex.git
8 changed files with 264 additions and 18 deletions
@ -0,0 +1,88 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import * as TypeMoq from 'typemoq'; |
||||
|
import * as Ng2Http from '@angular/http'; |
||||
|
|
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import { |
||||
|
ApiUrlConfig, |
||||
|
AppContributor, |
||||
|
AppContributorsService, |
||||
|
AuthService |
||||
|
} from './../'; |
||||
|
|
||||
|
describe('AppContributorsService', () => { |
||||
|
let authService: TypeMoq.Mock<AuthService>; |
||||
|
let appContributorsService: AppContributorsService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
authService = TypeMoq.Mock.ofType(AuthService); |
||||
|
appContributorsService = new AppContributorsService(authService.object, new ApiUrlConfig('http://service/p/')); |
||||
|
}); |
||||
|
|
||||
|
it('should make get request with auth service', () => { |
||||
|
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/contributors')) |
||||
|
.returns(() => Observable.of( |
||||
|
new Ng2Http.Response( |
||||
|
new Ng2Http.ResponseOptions({ |
||||
|
body: [{ |
||||
|
contributorId: '123', |
||||
|
permission: 'Owner' |
||||
|
}, { |
||||
|
contributorId: '456', |
||||
|
permission: 'Editor' |
||||
|
}] |
||||
|
}) |
||||
|
) |
||||
|
)) |
||||
|
.verifiable(TypeMoq.Times.once()); |
||||
|
|
||||
|
let contributors: AppContributor[] = null; |
||||
|
|
||||
|
appContributorsService.getContributors('my-app').subscribe(result => { |
||||
|
contributors = result; |
||||
|
}).unsubscribe(); |
||||
|
|
||||
|
expect(contributors).toEqual( |
||||
|
[ |
||||
|
new AppContributor('123', 'Owner'), |
||||
|
new AppContributor('456', 'Editor'), |
||||
|
]); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
|
||||
|
it('should make post assign contributor', () => { |
||||
|
const contributor = new AppContributor('123', 'Owner'); |
||||
|
|
||||
|
authService.setup(x => x.authPost('http://service/p/api/apps/my-app/contributors', TypeMoq.It.is(c => c === contributor))) |
||||
|
.returns(() => Observable.of( |
||||
|
new Ng2Http.Response( |
||||
|
new Ng2Http.ResponseOptions() |
||||
|
) |
||||
|
)); |
||||
|
|
||||
|
appContributorsService.postContributor('my-app', contributor); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
|
||||
|
it('should make delete request to remove contributor', () => { |
||||
|
authService.setup(x => x.authDelete('http://service/p/api/apps/my-app/contributors/123')) |
||||
|
.returns(() => Observable.of( |
||||
|
new Ng2Http.Response( |
||||
|
new Ng2Http.ResponseOptions() |
||||
|
) |
||||
|
)); |
||||
|
|
||||
|
appContributorsService.deleteContributor('my-app', '123'); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,53 @@ |
|||||
|
|
||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import * as Ng2 from '@angular/core'; |
||||
|
|
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import { ApiUrlConfig } from 'framework'; |
||||
|
|
||||
|
import { AuthService } from './auth.service'; |
||||
|
|
||||
|
export class AppContributor { |
||||
|
constructor( |
||||
|
public readonly contributorId: string, |
||||
|
public readonly permission: string |
||||
|
) { |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Ng2.Injectable() |
||||
|
export class AppContributorsService { |
||||
|
constructor( |
||||
|
private readonly authService: AuthService, |
||||
|
private readonly apiUrl: ApiUrlConfig |
||||
|
) { |
||||
|
} |
||||
|
|
||||
|
public getContributors(appName: string): Observable<AppContributor[]> { |
||||
|
return this.authService.authGet(this.apiUrl.buildUrl(`api/apps/${appName}/contributors`)) |
||||
|
.map(response => { |
||||
|
const body: any[] = response.json(); |
||||
|
|
||||
|
return body.map(item => { |
||||
|
return new AppContributor( |
||||
|
item.contributorId, |
||||
|
item.permission); |
||||
|
}); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public postContributor(appName: string, contributor: AppContributor): Observable<any> { |
||||
|
return this.authService.authPost(this.apiUrl.buildUrl(`api/apps/${appName}/contributors`), contributor); |
||||
|
} |
||||
|
|
||||
|
public deleteContributor(appName: string, contributorId: string): Observable<any> { |
||||
|
return this.authService.authDelete(this.apiUrl.buildUrl(`api/apps/${appName}/contributors/{contributorId}`)); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,60 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import * as TypeMoq from 'typemoq'; |
||||
|
import * as Ng2Http from '@angular/http'; |
||||
|
|
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import { |
||||
|
ApiUrlConfig, |
||||
|
AuthService, |
||||
|
Language, |
||||
|
LanguageService |
||||
|
} from './../'; |
||||
|
|
||||
|
describe('LanguageService', () => { |
||||
|
let authService: TypeMoq.Mock<AuthService>; |
||||
|
let languageService: LanguageService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
authService = TypeMoq.Mock.ofType(AuthService); |
||||
|
languageService = new LanguageService(authService.object, new ApiUrlConfig('http://service/p/')); |
||||
|
}); |
||||
|
|
||||
|
it('should make get request with auth service to get languages', () => { |
||||
|
authService.setup(x => x.authGet('http://service/p/api/languages')) |
||||
|
.returns(() => Observable.of( |
||||
|
new Ng2Http.Response( |
||||
|
new Ng2Http.ResponseOptions({ |
||||
|
body: [{ |
||||
|
iso2Code: 'de', |
||||
|
englishName: 'German' |
||||
|
}, { |
||||
|
iso2Code: 'en', |
||||
|
englishName: 'English' |
||||
|
}] |
||||
|
}) |
||||
|
) |
||||
|
)) |
||||
|
.verifiable(TypeMoq.Times.once()); |
||||
|
|
||||
|
let languages: Language[] = null; |
||||
|
|
||||
|
languageService.getLanguages().subscribe(result => { |
||||
|
languages = result; |
||||
|
}).unsubscribe(); |
||||
|
|
||||
|
expect(languages).toEqual( |
||||
|
[ |
||||
|
new Language('de', 'German'), |
||||
|
new Language('en', 'English'), |
||||
|
]); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,43 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import * as Ng2 from '@angular/core'; |
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import { ApiUrlConfig } from 'framework'; |
||||
|
|
||||
|
import { AuthService } from './auth.service'; |
||||
|
|
||||
|
export class Language { |
||||
|
constructor( |
||||
|
public readonly iso2Code: string, |
||||
|
public readonly englishName: string |
||||
|
) { |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Ng2.Injectable() |
||||
|
export class LanguageService { |
||||
|
constructor( |
||||
|
private readonly authService: AuthService, |
||||
|
private readonly apiUrl: ApiUrlConfig |
||||
|
) { |
||||
|
} |
||||
|
|
||||
|
public getLanguages(): Observable<Language[]> { |
||||
|
return this.authService.authGet(this.apiUrl.buildUrl('api/languages')) |
||||
|
.map(response => { |
||||
|
const body: any[] = response.json(); |
||||
|
|
||||
|
return body.map(item => { |
||||
|
return new Language( |
||||
|
item.iso2Code, |
||||
|
item.englishName); |
||||
|
}); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue