mirror of https://github.com/Squidex/squidex.git
45 changed files with 873 additions and 3111 deletions
@ -0,0 +1,142 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import { HttpClient, HttpErrorResponse, HttpHeaders, HttpResponse } from '@angular/common/http'; |
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import { Version } from './../utils/version'; |
||||
|
|
||||
|
export class EntityCreatedDto { |
||||
|
constructor( |
||||
|
public readonly id: any |
||||
|
) { |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export class ErrorDto { |
||||
|
public get displayMessage(): string { |
||||
|
let result = this.message; |
||||
|
|
||||
|
if (this.details && this.details.length > 0) { |
||||
|
const detailMessage = this.details[0]; |
||||
|
|
||||
|
const lastChar = result[result.length - 1]; |
||||
|
|
||||
|
if (lastChar !== '.' && lastChar !== ',') { |
||||
|
result += '.'; |
||||
|
} |
||||
|
|
||||
|
result += ' '; |
||||
|
result += detailMessage; |
||||
|
} |
||||
|
|
||||
|
const lastChar = result[result.length - 1]; |
||||
|
|
||||
|
if (lastChar !== '.') { |
||||
|
result += '.'; |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
constructor( |
||||
|
public readonly statusCode: number, |
||||
|
public readonly message: string, |
||||
|
public readonly details: string[] = [] |
||||
|
) { |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export module HTTP { |
||||
|
export function getVersioned(http: HttpClient, url: string, version?: Version): Observable<any> { |
||||
|
if (version) { |
||||
|
return http.get(url, { observe: 'response', headers: new HttpHeaders().set('If-Match', version.value) }) |
||||
|
.do((response: HttpResponse<any>) => { |
||||
|
if (version && response.status.toString().indexOf('2') === 0 && response.headers) { |
||||
|
const etag = response.headers.get('etag'); |
||||
|
|
||||
|
if (etag) { |
||||
|
version.update(etag); |
||||
|
} |
||||
|
} |
||||
|
}).map((response: HttpResponse<any>) => response.body); |
||||
|
} else { |
||||
|
return http.get(url, { observe: 'response' }).map((response: HttpResponse<any>) => response.body); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export function postVersioned(http: HttpClient, url: string, body: any, version?: Version): Observable<any> { |
||||
|
if (version) { |
||||
|
return http.post(url, body, { observe: 'response', headers: new HttpHeaders().set('If-Match', version.value) }) |
||||
|
.do((response: HttpResponse<any>) => { |
||||
|
if (version && response.status.toString().indexOf('2') === 0 && response.headers) { |
||||
|
const etag = response.headers.get('etag'); |
||||
|
|
||||
|
if (etag) { |
||||
|
version.update(etag); |
||||
|
} |
||||
|
} |
||||
|
}).map((response: HttpResponse<any>) => response.body); |
||||
|
} else { |
||||
|
return http.post(url, body, { observe: 'response' }).map((response: HttpResponse<any>) => response.body); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export function putVersioned(http: HttpClient, url: string, body: any, version?: Version): Observable<any> { |
||||
|
if (version) { |
||||
|
return http.put(url, body, { observe: 'response', headers: new HttpHeaders().set('If-Match', version.value) }) |
||||
|
.do((response: HttpResponse<any>) => { |
||||
|
if (version && response.status.toString().indexOf('2') === 0 && response.headers) { |
||||
|
const etag = response.headers.get('etag'); |
||||
|
|
||||
|
if (etag) { |
||||
|
version.update(etag); |
||||
|
} |
||||
|
} |
||||
|
}).map((response: HttpResponse<any>) => response.body); |
||||
|
} else { |
||||
|
return http.put(url, body, { observe: 'response' }).map((response: HttpResponse<any>) => response.body); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export function deleteVersioned(http: HttpClient, url: string, version?: Version): Observable<any> { |
||||
|
if (version) { |
||||
|
return http.delete(url, { observe: 'response', headers: new HttpHeaders().set('If-Match', version.value) }) |
||||
|
.do((response: HttpResponse<any>) => { |
||||
|
if (version && response.status.toString().indexOf('2') === 0 && response.headers) { |
||||
|
const etag = response.headers.get('etag'); |
||||
|
|
||||
|
if (etag) { |
||||
|
version.update(etag); |
||||
|
} |
||||
|
} |
||||
|
}).map((response: HttpResponse<any>) => response.body); |
||||
|
} else { |
||||
|
return http.delete(url, { observe: 'response' }).map((response: HttpResponse<any>) => response.body); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export function pretifyError(message: string): Observable<any> { |
||||
|
return this.catch((response: HttpErrorResponse) => { |
||||
|
let result = new ErrorDto(500, message); |
||||
|
|
||||
|
if (!(response.error instanceof Error)) { |
||||
|
try { |
||||
|
if (response.status === 412) { |
||||
|
result = new ErrorDto(response.status, 'Failed to make the update. Another user has made a change. Please reload.'); |
||||
|
} else if (response.status !== 500) { |
||||
|
result = new ErrorDto(response.status, response.error.message, response.error.details); |
||||
|
} |
||||
|
} catch (e) { |
||||
|
result = result; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return Observable.throw(result); |
||||
|
}); |
||||
|
} |
||||
@ -1,72 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Response } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
|
|
||||
export class EntityCreatedDto { |
|
||||
constructor( |
|
||||
public readonly id: any |
|
||||
) { |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
export class ErrorDto { |
|
||||
public get displayMessage(): string { |
|
||||
let result = this.message; |
|
||||
|
|
||||
if (this.details && this.details.length > 0) { |
|
||||
const detailMessage = this.details[0]; |
|
||||
|
|
||||
const lastChar = result[result.length - 1]; |
|
||||
|
|
||||
if (lastChar !== '.' && lastChar !== ',') { |
|
||||
result += '.'; |
|
||||
} |
|
||||
|
|
||||
result += ' '; |
|
||||
result += detailMessage; |
|
||||
} |
|
||||
|
|
||||
const lastChar = result[result.length - 1]; |
|
||||
|
|
||||
if (lastChar !== '.') { |
|
||||
result += '.'; |
|
||||
} |
|
||||
|
|
||||
return result; |
|
||||
} |
|
||||
|
|
||||
constructor( |
|
||||
public readonly statusCode: number, |
|
||||
public readonly message: string, |
|
||||
public readonly details: string[] = [] |
|
||||
) { |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
export function catchError(message: string): Observable<any> { |
|
||||
return this.catch((error: any | Response) => { |
|
||||
let result = new ErrorDto(500, message); |
|
||||
|
|
||||
if (error instanceof Response) { |
|
||||
try { |
|
||||
const body = error.json(); |
|
||||
|
|
||||
if (error.status === 412) { |
|
||||
result = new ErrorDto(error.status, 'Failed to make the update. Another user has made a change. Please reload.'); |
|
||||
} else if (error.status !== 500) { |
|
||||
result = new ErrorDto(error.status, body.message, body.details); |
|
||||
} |
|
||||
} catch (e) { |
|
||||
result = result; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return Observable.throw(result); |
|
||||
}); |
|
||||
} |
|
||||
@ -0,0 +1,51 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpErrorResponse } from '@angular/common/http'; |
||||
|
import { Injectable} from '@angular/core'; |
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import { AuthService } from './../services/auth.service'; |
||||
|
import { ApiUrlConfig } from 'framework'; |
||||
|
|
||||
|
@Injectable() |
||||
|
export class AuthInterceptor implements HttpInterceptor { |
||||
|
private baseUrl: string; |
||||
|
|
||||
|
constructor(apiUrlConfig: ApiUrlConfig, |
||||
|
private readonly authService: AuthService |
||||
|
) { |
||||
|
this.baseUrl = apiUrlConfig.buildUrl(''); |
||||
|
} |
||||
|
|
||||
|
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { |
||||
|
if (req.url.indexOf(this.baseUrl) === 0 && !req.headers.has('NoAuth')) { |
||||
|
const authReq = req.clone({ |
||||
|
headers: req.headers |
||||
|
.set('Authorization', this.authService.user ? this.authService.user.authToken : '') |
||||
|
.set('Accept-Language', '*') |
||||
|
.set('Pragma', 'no-cache') |
||||
|
}); |
||||
|
|
||||
|
return next.handle(authReq) |
||||
|
.catch((error: HttpErrorResponse) => { |
||||
|
if (error.status === 404 && (!this.authService.user || this.authService.user.isExpired)) { |
||||
|
this.authService.logoutRedirect(); |
||||
|
|
||||
|
return Observable.empty<Response>(); |
||||
|
} else if (error.status === 401 || error.status === 403) { |
||||
|
this.authService.logoutRedirect(); |
||||
|
|
||||
|
return Observable.empty<Response>(); |
||||
|
} |
||||
|
return Observable.throw(error); |
||||
|
}); |
||||
|
} else { |
||||
|
return next.handle(req); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,354 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Response, ResponseOptions } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { ProgressHttp } from 'angular-progress-http'; |
|
||||
import { IMock, It, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { |
|
||||
ApiUrlConfig, |
|
||||
AssetsDto, |
|
||||
AssetDto, |
|
||||
AssetCreatedDto, |
|
||||
AssetReplacedDto, |
|
||||
AssetsService, |
|
||||
AuthService, |
|
||||
DateTime, |
|
||||
Profile, |
|
||||
UpdateAssetDto, |
|
||||
Version |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('AssetsService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let assetsService: AssetsService; |
|
||||
let progressHttp: IMock<ProgressHttp>; |
|
||||
let version = new Version('1'); |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
const factory = { |
|
||||
create: () => { |
|
||||
return <any>null; |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
progressHttp = Mock.ofInstance(new ProgressHttp(null!, null!, factory, null!)); |
|
||||
progressHttp.setup(x => x.withUploadProgressListener(It.isAny())).returns(() => <any> progressHttp.object); |
|
||||
|
|
||||
authService = Mock.ofType(AuthService); |
|
||||
authService.setup(x => x.user).returns(() => new Profile(<any>{})); |
|
||||
|
|
||||
assetsService = new AssetsService(authService.object, new ApiUrlConfig('http://service/p/'), progressHttp.object); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get assets', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/assets?take=17&skip=13')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
total: 10, |
|
||||
items: [ |
|
||||
{ |
|
||||
id: 'id1', |
|
||||
created: '2016-12-12T10:10', |
|
||||
createdBy: 'Created1', |
|
||||
lastModified: '2017-12-12T10:10', |
|
||||
lastModifiedBy: 'LastModifiedBy1', |
|
||||
fileName: 'my-asset1.png', |
|
||||
fileSize: 1024, |
|
||||
fileVersion: 2000, |
|
||||
mimeType: 'text/plain', |
|
||||
isImage: true, |
|
||||
pixelWidth: 1024, |
|
||||
pixelHeight: 2048, |
|
||||
version: 11 |
|
||||
}, |
|
||||
{ |
|
||||
id: 'id2', |
|
||||
created: '2016-10-12T10:10', |
|
||||
createdBy: 'Created2', |
|
||||
lastModified: '2017-10-12T10:10', |
|
||||
lastModifiedBy: 'LastModifiedBy2', |
|
||||
fileName: 'my-asset2.png', |
|
||||
fileSize: 1024, |
|
||||
fileVersion: 2000, |
|
||||
mimeType: 'text/plain', |
|
||||
isImage: true, |
|
||||
pixelWidth: 1024, |
|
||||
pixelHeight: 2048, |
|
||||
version: 22 |
|
||||
} |
|
||||
] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let assets: AssetsDto | null = null; |
|
||||
|
|
||||
assetsService.getAssets('my-app', 17, 13).subscribe(result => { |
|
||||
assets = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(assets).toEqual( |
|
||||
new AssetsDto(10, [ |
|
||||
new AssetDto( |
|
||||
'id1', 'Created1', 'LastModifiedBy1', |
|
||||
DateTime.parseISO_UTC('2016-12-12T10:10'), |
|
||||
DateTime.parseISO_UTC('2017-12-12T10:10'), |
|
||||
'my-asset1.png', |
|
||||
1024, |
|
||||
2000, |
|
||||
'text/plain', |
|
||||
true, |
|
||||
1024, |
|
||||
2048, |
|
||||
new Version('11')), |
|
||||
new AssetDto('id2', 'Created2', 'LastModifiedBy2', |
|
||||
DateTime.parseISO_UTC('2016-10-12T10:10'), |
|
||||
DateTime.parseISO_UTC('2017-10-12T10:10'), |
|
||||
'my-asset2.png', |
|
||||
1024, |
|
||||
2000, |
|
||||
'text/plain', |
|
||||
true, |
|
||||
1024, |
|
||||
2048, |
|
||||
new Version('22')) |
|
||||
])); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get asset', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/assets/123')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
id: 'id1', |
|
||||
created: '2016-12-12T10:10', |
|
||||
createdBy: 'Created1', |
|
||||
lastModified: '2017-12-12T10:10', |
|
||||
lastModifiedBy: 'LastModifiedBy1', |
|
||||
fileName: 'my-asset1.png', |
|
||||
fileSize: 1024, |
|
||||
fileVersion: 2000, |
|
||||
mimeType: 'text/plain', |
|
||||
isImage: true, |
|
||||
pixelWidth: 1024, |
|
||||
pixelHeight: 2048, |
|
||||
version: 11 |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let assets: AssetDto | null = null; |
|
||||
|
|
||||
assetsService.getAsset('my-app', '123').subscribe(result => { |
|
||||
assets = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(assets).toEqual( |
|
||||
new AssetDto( |
|
||||
'id1', 'Created1', 'LastModifiedBy1', |
|
||||
DateTime.parseISO_UTC('2016-12-12T10:10'), |
|
||||
DateTime.parseISO_UTC('2017-12-12T10:10'), |
|
||||
'my-asset1.png', |
|
||||
1024, |
|
||||
2000, |
|
||||
'text/plain', |
|
||||
true, |
|
||||
1024, |
|
||||
2048, |
|
||||
new Version('11'))); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should append query to find by name', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/assets?query=my-query&take=17&skip=13')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
total: 10, |
|
||||
items: [] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let assets: AssetsDto | null = null; |
|
||||
|
|
||||
assetsService.getAssets('my-app', 17, 13, 'my-query').subscribe(result => { |
|
||||
assets = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should append mime types to find by types', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/assets?mimeTypes=text/plain,image/png&take=17&skip=13')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
total: 10, |
|
||||
items: [] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let assets: AssetsDto | null = null; |
|
||||
|
|
||||
assetsService.getAssets('my-app', 17, 13, undefined, ['text/plain', 'image/png']).subscribe(result => { |
|
||||
assets = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should append mime types to find by ids', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/assets?ids=12,23&take=17&skip=13')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
total: 10, |
|
||||
items: [] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let assets: AssetsDto | null = null; |
|
||||
|
|
||||
assetsService.getAssets('my-app', 17, 13, undefined, undefined, ['12', '23']).subscribe(result => { |
|
||||
assets = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make post request to create asset', () => { |
|
||||
progressHttp.setup(x => x.post('http://service/p/api/apps/my-app/assets', It.isAny(), It.isAny())) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
id: 'id1', |
|
||||
fileName: 'my-asset1.png', |
|
||||
fileSize: 1024, |
|
||||
fileVersion: 2, |
|
||||
mimeType: 'text/plain', |
|
||||
isImage: true, |
|
||||
pixelWidth: 1024, |
|
||||
pixelHeight: 2048, |
|
||||
version: 11 |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let asset: AssetCreatedDto | null = null; |
|
||||
|
|
||||
assetsService.uploadFile('my-app', null!).subscribe(result => { |
|
||||
asset = <AssetCreatedDto>result; |
|
||||
}); |
|
||||
|
|
||||
expect(asset).toEqual( |
|
||||
new AssetCreatedDto( |
|
||||
'id1', |
|
||||
'my-asset1.png', |
|
||||
1024, 2, |
|
||||
'text/plain', |
|
||||
true, |
|
||||
1024, |
|
||||
2048, |
|
||||
new Version('11'))); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to replace asset content', () => { |
|
||||
progressHttp.setup(x => x.put('http://service/p/api/apps/my-app/assets/123/content', It.isAny(), It.isAny())) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
fileSize: 1024, |
|
||||
fileVersion: 2, |
|
||||
mimeType: 'text/plain', |
|
||||
isImage: true, |
|
||||
pixelWidth: 1024, |
|
||||
pixelHeight: 2048, |
|
||||
version: 11 |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let asset: AssetReplacedDto | null = null; |
|
||||
|
|
||||
assetsService.replaceFile('my-app', '123', null!, version).subscribe(result => { |
|
||||
asset = <AssetReplacedDto>result; |
|
||||
}); |
|
||||
|
|
||||
expect(asset).toEqual( |
|
||||
new AssetReplacedDto( |
|
||||
1024, 2, |
|
||||
'text/plain', |
|
||||
true, |
|
||||
1024, |
|
||||
2048, |
|
||||
new Version('11'))); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to update asset', () => { |
|
||||
const dto = new UpdateAssetDto('My-Asset.pdf'); |
|
||||
|
|
||||
authService.setup(x => x.authPut('http://service/p/api/apps/my-app/assets/123', dto, version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
assetsService.putAsset('my-app', '123', dto, version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make delete request to delete asset', () => { |
|
||||
authService.setup(x => x.authDelete('http://service/p/api/apps/my-app/assets/123', version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
assetsService.deleteAsset('my-app', '123', version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,290 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Response, ResponseOptions } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { IMock, It, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { |
|
||||
ApiUrlConfig, |
|
||||
AuthService, |
|
||||
ContentDto, |
|
||||
ContentsDto, |
|
||||
ContentsService, |
|
||||
DateTime, |
|
||||
Version |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('ContentsService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let contentsService: ContentsService; |
|
||||
let version = new Version('1'); |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
authService = Mock.ofType(AuthService); |
|
||||
contentsService = new ContentsService(authService.object, new ApiUrlConfig('http://service/p/')); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get contents', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/content/my-app/my-schema?nonPublished=true&hidden=true&$top=17&$skip=13')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
total: 10, |
|
||||
items: [ |
|
||||
{ |
|
||||
id: 'id1', |
|
||||
isPublished: true, |
|
||||
created: '2016-12-12T10:10', |
|
||||
createdBy: 'Created1', |
|
||||
lastModified: '2017-12-12T10:10', |
|
||||
lastModifiedBy: 'LastModifiedBy1', |
|
||||
version: 11, |
|
||||
data: {} |
|
||||
}, |
|
||||
{ |
|
||||
id: 'id2', |
|
||||
isPublished: true, |
|
||||
created: '2016-10-12T10:10', |
|
||||
createdBy: 'Created2', |
|
||||
lastModified: '2017-10-12T10:10', |
|
||||
lastModifiedBy: 'LastModifiedBy2', |
|
||||
version: 22, |
|
||||
data: {} |
|
||||
} |
|
||||
] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let contents: ContentsDto | null = null; |
|
||||
|
|
||||
contentsService.getContents('my-app', 'my-schema', 17, 13).subscribe(result => { |
|
||||
contents = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(contents).toEqual( |
|
||||
new ContentsDto(10, [ |
|
||||
new ContentDto('id1', true, 'Created1', 'LastModifiedBy1', |
|
||||
DateTime.parseISO_UTC('2016-12-12T10:10'), |
|
||||
DateTime.parseISO_UTC('2017-12-12T10:10'), |
|
||||
{}, |
|
||||
new Version('11')), |
|
||||
new ContentDto('id2', true, 'Created2', 'LastModifiedBy2', |
|
||||
DateTime.parseISO_UTC('2016-10-12T10:10'), |
|
||||
DateTime.parseISO_UTC('2017-10-12T10:10'), |
|
||||
{}, |
|
||||
new Version('22')) |
|
||||
])); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should append query to get request as search', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/content/my-app/my-schema?nonPublished=true&hidden=true&$search="my-query"&$top=17&$skip=13')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
total: 10, |
|
||||
items: [] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let contents: ContentsDto | null = null; |
|
||||
|
|
||||
contentsService.getContents('my-app', 'my-schema', 17, 13, 'my-query').subscribe(result => { |
|
||||
contents = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should append ids to get request with ids', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/content/my-app/my-schema?nonPublished=true&hidden=true&$top=17&$skip=13&ids=id1,id2')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
total: 10, |
|
||||
items: [] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let contents: ContentsDto | null = null; |
|
||||
|
|
||||
contentsService.getContents('my-app', 'my-schema', 17, 13, null, ['id1', 'id2']).subscribe(result => { |
|
||||
contents = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should append query to get request as plain query string', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/content/my-app/my-schema?nonPublished=true&hidden=true&$filter=my-filter&$top=17&$skip=13')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
total: 10, |
|
||||
items: [] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let contents: ContentsDto | null = null; |
|
||||
|
|
||||
contentsService.getContents('my-app', 'my-schema', 17, 13, '$filter=my-filter').subscribe(result => { |
|
||||
contents = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get content', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/content/my-app/my-schema/content1?hidden=true', version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
id: 'id1', |
|
||||
isPublished: true, |
|
||||
created: '2016-12-12T10:10', |
|
||||
createdBy: 'Created1', |
|
||||
lastModified: '2017-12-12T10:10', |
|
||||
lastModifiedBy: 'LastModifiedBy1', |
|
||||
version: 11, |
|
||||
data: {} |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let content: ContentDto | null = null; |
|
||||
|
|
||||
contentsService.getContent('my-app', 'my-schema', 'content1', version).subscribe(result => { |
|
||||
content = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(content).toEqual( |
|
||||
new ContentDto('id1', true, 'Created1', 'LastModifiedBy1', |
|
||||
DateTime.parseISO_UTC('2016-12-12T10:10'), |
|
||||
DateTime.parseISO_UTC('2017-12-12T10:10'), |
|
||||
{}, |
|
||||
new Version('11'))); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make post request to create content', () => { |
|
||||
const dto = {}; |
|
||||
|
|
||||
authService.setup(x => x.authPost('http://service/p/api/content/my-app/my-schema?publish=true', dto, version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
id: 'id1', |
|
||||
isPublished: true, |
|
||||
created: '2016-12-12T10:10', |
|
||||
createdBy: 'Created1', |
|
||||
lastModified: '2017-12-12T10:10', |
|
||||
lastModifiedBy: 'LastModifiedBy1', |
|
||||
version: 11, |
|
||||
data: {} |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let content: ContentDto | null = null; |
|
||||
|
|
||||
contentsService.postContent('my-app', 'my-schema', dto, true, version).subscribe(result => { |
|
||||
content = result; |
|
||||
}); |
|
||||
|
|
||||
expect(content).toEqual( |
|
||||
new ContentDto('id1', true, 'Created1', 'LastModifiedBy1', |
|
||||
DateTime.parseISO_UTC('2016-12-12T10:10'), |
|
||||
DateTime.parseISO_UTC('2017-12-12T10:10'), |
|
||||
{}, |
|
||||
new Version('11'))); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to update content', () => { |
|
||||
const dto = {}; |
|
||||
|
|
||||
authService.setup(x => x.authPut('http://service/p/api/content/my-app/my-schema/content1', dto, version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
contentsService.putContent('my-app', 'my-schema', 'content1', dto, version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to publish content', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/content/my-app/my-schema/content1/publish', It.isAny(), version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
contentsService.publishContent('my-app', 'my-schema', 'content1', version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to unpublish content', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/content/my-app/my-schema/content1/unpublish', It.isAny(), version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
contentsService.unpublishContent('my-app', 'my-schema', 'content1', version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make delete request to delete content', () => { |
|
||||
authService.setup(x => x.authDelete('http://service/p/api/content/my-app/my-schema/content1', version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
contentsService.deleteContent('my-app', 'my-schema', 'content1', version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,109 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Response, ResponseOptions } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { IMock, It, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { |
|
||||
ApiUrlConfig, |
|
||||
AuthService, |
|
||||
EventConsumerDto, |
|
||||
EventConsumersService |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('EventConsumersService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let eventConsumersService: EventConsumersService; |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
authService = Mock.ofType(AuthService); |
|
||||
eventConsumersService = new EventConsumersService(authService.object, new ApiUrlConfig('http://service/p/')); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get event consumers', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/event-consumers')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: [ |
|
||||
{ |
|
||||
name: 'event-consumer1', |
|
||||
position: '13', |
|
||||
isStopped: true, |
|
||||
isResetting: true, |
|
||||
error: 'an error 1' |
|
||||
}, |
|
||||
{ |
|
||||
name: 'event-consumer2', |
|
||||
position: '29', |
|
||||
isStopped: true, |
|
||||
isResetting: true, |
|
||||
error: 'an error 2' |
|
||||
} |
|
||||
] |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let eventConsumers: EventConsumerDto[] | null = null; |
|
||||
|
|
||||
eventConsumersService.getEventConsumers().subscribe(result => { |
|
||||
eventConsumers = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(eventConsumers).toEqual([ |
|
||||
new EventConsumerDto('event-consumer1', true, true, 'an error 1', '13'), |
|
||||
new EventConsumerDto('event-consumer2', true, true, 'an error 2', '29') |
|
||||
]); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to start event consumer', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/event-consumers/event-consumer1/start', It.isAny())) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
eventConsumersService.startEventConsumer('event-consumer1'); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to stop event consumer', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/event-consumers/event-consumer1/stop', It.isAny())) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
eventConsumersService.stopEventConsumer('event-consumer1'); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to reset event consumer', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/event-consumers/event-consumer1/reset', It.isAny())) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
eventConsumersService.resetEventConsumer('event-consumer1'); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,77 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Http, Response, ResponseOptions } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { IMock, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { HelpService } from './../'; |
|
||||
|
|
||||
describe('AppClientsService', () => { |
|
||||
let helpService: HelpService; |
|
||||
let http: IMock<Http>; |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
http = Mock.ofType(Http); |
|
||||
|
|
||||
helpService = new HelpService(http.object); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get help sections', () => { |
|
||||
http.setup(x => x.get('https://api.gitbook.com/book/squidex/squidex/contents/01-chapter/02-article.json')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
sections: [ |
|
||||
{ |
|
||||
content: 'A test content with' |
|
||||
}, |
|
||||
{ |
|
||||
content: 'A test content with a <a href="https://squidex.io">A Link</a>' |
|
||||
}, |
|
||||
{ |
|
||||
content: 'A test content with a <a href="../GLOSSARY.html#content">Glossary Link</a>' |
|
||||
} |
|
||||
] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let helpSections: string[] | null = null; |
|
||||
|
|
||||
helpService.getHelp('01-chapter/02-article').subscribe(result => { |
|
||||
helpSections = result; |
|
||||
}); |
|
||||
|
|
||||
expect(helpSections).toEqual([ |
|
||||
'A test content with', |
|
||||
'A test content with a <a href="https://squidex.io">A Link</a>', |
|
||||
'A test content with a <a target="_blank" href="https://docs.squidex.io/GLOSSARY.html#content">Glossary Link</a>' |
|
||||
]); |
|
||||
|
|
||||
http.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should return empty sections if get request fails', () => { |
|
||||
http.setup(x => x.get('https://api.gitbook.com/book/squidex/squidex/contents/01-chapter/02-article.json')) |
|
||||
.returns(() => Observable.throw('An error')) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let helpSections: string[] | null = null; |
|
||||
|
|
||||
helpService.getHelp('01-chapter/02-article').subscribe(result => { |
|
||||
helpSections = result; |
|
||||
}); |
|
||||
|
|
||||
expect(helpSections).toEqual([]); |
|
||||
|
|
||||
http.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,68 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Response, ResponseOptions } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { IMock, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { DateTime } from 'framework'; |
|
||||
|
|
||||
import { |
|
||||
ApiUrlConfig, |
|
||||
AuthService, |
|
||||
HistoryEventDto, |
|
||||
HistoryService |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('HistoryService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let languageService: HistoryService; |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
authService = Mock.ofType(AuthService); |
|
||||
languageService = new HistoryService(authService.object, new ApiUrlConfig('http://service/p/')); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get history events', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/history?channel=settings.contributors')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: [ |
|
||||
{ |
|
||||
actor: 'User1', |
|
||||
eventId: '1', |
|
||||
message: 'Message 1', |
|
||||
created: '2016-12-12T10:10' |
|
||||
}, |
|
||||
{ |
|
||||
actor: 'User2', |
|
||||
eventId: '2', |
|
||||
message: 'Message 2', |
|
||||
created: '2016-12-13T10:10' |
|
||||
} |
|
||||
] |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let events: HistoryEventDto[] | null = null; |
|
||||
|
|
||||
languageService.getHistory('my-app', 'settings.contributors').subscribe(result => { |
|
||||
events = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(events).toEqual( |
|
||||
[ |
|
||||
new HistoryEventDto('1', 'User1', 'Message 1', DateTime.parseISO_UTC('2016-12-12T10:10')), |
|
||||
new HistoryEventDto('2', 'User2', 'Message 2', DateTime.parseISO_UTC('2016-12-13T10:10')) |
|
||||
]); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,62 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Response, ResponseOptions } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { IMock, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { |
|
||||
ApiUrlConfig, |
|
||||
AuthService, |
|
||||
LanguageDto, |
|
||||
LanguageService |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('LanguageService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let languageService: LanguageService; |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
authService = Mock.ofType(AuthService); |
|
||||
languageService = new LanguageService(authService.object, new ApiUrlConfig('http://service/p/')); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get languages', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/languages')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: [ |
|
||||
{ |
|
||||
iso2Code: 'de', |
|
||||
englishName: 'German' |
|
||||
}, |
|
||||
{ |
|
||||
iso2Code: 'en', |
|
||||
englishName: 'English' |
|
||||
} |
|
||||
] |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let languages: LanguageDto[] | null = null; |
|
||||
|
|
||||
languageService.getLanguages().subscribe(result => { |
|
||||
languages = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(languages).toEqual( |
|
||||
[ |
|
||||
new LanguageDto('de', 'German'), |
|
||||
new LanguageDto('en', 'English') |
|
||||
]); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,99 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Response, ResponseOptions } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { IMock, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { |
|
||||
AppPlansDto, |
|
||||
ApiUrlConfig, |
|
||||
AuthService, |
|
||||
ChangePlanDto, |
|
||||
PlanDto, |
|
||||
PlansService, |
|
||||
Version |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('PlansService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let plansService: PlansService; |
|
||||
let version = new Version('1'); |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
authService = Mock.ofType(AuthService); |
|
||||
plansService = new PlansService(authService.object, new ApiUrlConfig('http://service/p/')); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get app plans', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/plans', version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
currentPlanId: '123', |
|
||||
hasConfigured: true, |
|
||||
hasPortal: true, |
|
||||
planOwner: '456', |
|
||||
plans: [{ |
|
||||
id: 'free', |
|
||||
name: 'Free', |
|
||||
costs: '14 €', |
|
||||
maxApiCalls: 1000, |
|
||||
maxAssetSize: 1500, |
|
||||
maxContributors: 2500 |
|
||||
}, { |
|
||||
id: 'prof', |
|
||||
name: 'Prof', |
|
||||
costs: '18 €', |
|
||||
maxApiCalls: 4000, |
|
||||
maxAssetSize: 5500, |
|
||||
maxContributors: 6500 |
|
||||
}] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let plans: AppPlansDto | null = null; |
|
||||
|
|
||||
plansService.getPlans('my-app', version).subscribe(result => { |
|
||||
plans = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(plans).toEqual( |
|
||||
new AppPlansDto( |
|
||||
'123', |
|
||||
'456', |
|
||||
true, |
|
||||
true, |
|
||||
[ |
|
||||
new PlanDto('free', 'Free', '14 €', 1000, 1500, 2500), |
|
||||
new PlanDto('prof', 'Prof', '18 €', 4000, 5500, 6500) |
|
||||
] |
|
||||
)); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to change plan', () => { |
|
||||
const dto = new ChangePlanDto('enterprise'); |
|
||||
|
|
||||
authService.setup(x => x.authPut('http://service/p/api/apps/my-app/plan', dto, version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
plansService.putPlan('my-app', dto, version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,175 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { |
|
||||
AssetsFieldPropertiesDto, |
|
||||
BooleanFieldPropertiesDto, |
|
||||
DateTimeFieldPropertiesDto, |
|
||||
FieldDto, |
|
||||
FieldPropertiesDto, |
|
||||
GeolocationFieldPropertiesDto, |
|
||||
JsonFieldPropertiesDto, |
|
||||
NumberFieldPropertiesDto, |
|
||||
ReferencesFieldPropertiesDto, |
|
||||
StringFieldPropertiesDto |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('AssetsField', () => { |
|
||||
const field = createField(new AssetsFieldPropertiesDto(null, null, null, true, false, 1, 1)); |
|
||||
|
|
||||
it('should create validators', () => { |
|
||||
expect(field.createValidators().length).toBe(3); |
|
||||
}); |
|
||||
|
|
||||
it('should format to empty string if null', () => { |
|
||||
expect(field.formatValue(null)).toBe(''); |
|
||||
}); |
|
||||
|
|
||||
it('should format to asset count', () => { |
|
||||
expect(field.formatValue([1, 2, 3])).toBe('3 Asset(s)'); |
|
||||
}); |
|
||||
|
|
||||
it('should return zero formatting if other type', () => { |
|
||||
expect(field.formatValue(1)).toBe('0 Assets'); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
describe('BooleanField', () => { |
|
||||
const field = createField(new BooleanFieldPropertiesDto(null, null, null, true, false, 'Checkbox')); |
|
||||
|
|
||||
it('should create validators', () => { |
|
||||
expect(field.createValidators().length).toBe(1); |
|
||||
}); |
|
||||
|
|
||||
it('should format to empty string if null', () => { |
|
||||
expect(field.formatValue(null)).toBe(''); |
|
||||
}); |
|
||||
|
|
||||
it('should format to checkmark if true', () => { |
|
||||
expect(field.formatValue(true)).toBe('✔'); |
|
||||
}); |
|
||||
|
|
||||
it('should format to minus if false', () => { |
|
||||
expect(field.formatValue(false)).toBe('-'); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
describe('DateTimeField', () => { |
|
||||
const field = createField(new DateTimeFieldPropertiesDto(null, null, null, true, false, 'Date')); |
|
||||
|
|
||||
it('should create validators', () => { |
|
||||
expect(field.createValidators().length).toBe(1); |
|
||||
}); |
|
||||
|
|
||||
it('should format to empty string if null', () => { |
|
||||
expect(field.formatValue(null)).toBe(''); |
|
||||
}); |
|
||||
|
|
||||
it('should format to input if parsing failed', () => { |
|
||||
expect(field.formatValue(true)).toBe(true); |
|
||||
}); |
|
||||
|
|
||||
it('should format to date', () => { |
|
||||
const dateField = createField(new DateTimeFieldPropertiesDto(null, null, null, true, false, 'Date')); |
|
||||
|
|
||||
expect(dateField.formatValue('2017-12-12T16:00:00Z')).toBe('2017-12-12'); |
|
||||
}); |
|
||||
|
|
||||
it('should format to date', () => { |
|
||||
const dateTimeField = createField(new DateTimeFieldPropertiesDto(null, null, null, true, false, 'DateTime')); |
|
||||
|
|
||||
expect(dateTimeField.formatValue('2017-12-12T16:00:00Z').substr(0, 10)).toBe('2017-12-12'); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
describe('GeolocationField', () => { |
|
||||
const field = createField(new GeolocationFieldPropertiesDto(null, null, null, true, false, 'Default')); |
|
||||
|
|
||||
it('should create validators', () => { |
|
||||
expect(field.createValidators().length).toBe(1); |
|
||||
}); |
|
||||
|
|
||||
it('should format to empty string if null', () => { |
|
||||
expect(field.formatValue(null)).toBe(''); |
|
||||
}); |
|
||||
|
|
||||
it('should format to latitude and longitude', () => { |
|
||||
expect(field.formatValue({ latitude: 42, longitude: 3.14 })).toBe('3.14, 42'); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
describe('JsonField', () => { |
|
||||
const field = createField(new JsonFieldPropertiesDto(null, null, null, true, false)); |
|
||||
|
|
||||
it('should create validators', () => { |
|
||||
expect(field.createValidators().length).toBe(1); |
|
||||
}); |
|
||||
|
|
||||
it('should format to empty string if null', () => { |
|
||||
expect(field.formatValue(null)).toBe(''); |
|
||||
}); |
|
||||
|
|
||||
it('should format to constant', () => { |
|
||||
expect(field.formatValue({})).toBe('<Json />'); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
describe('NumberField', () => { |
|
||||
const field = createField(new NumberFieldPropertiesDto(null, null, null, true, false, 'Input', undefined, 3, 1, [1, 2, 3])); |
|
||||
|
|
||||
it('should create validators', () => { |
|
||||
expect(field.createValidators().length).toBe(4); |
|
||||
}); |
|
||||
|
|
||||
it('should format to empty string if null', () => { |
|
||||
expect(field.formatValue(null)).toBe(''); |
|
||||
}); |
|
||||
|
|
||||
it('should format to number', () => { |
|
||||
expect(field.formatValue(42)).toBe(42); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
describe('ReferencesField', () => { |
|
||||
const field = createField(new ReferencesFieldPropertiesDto(null, null, null, true, false, 1, 1)); |
|
||||
|
|
||||
it('should create validators', () => { |
|
||||
expect(field.createValidators().length).toBe(3); |
|
||||
}); |
|
||||
|
|
||||
it('should format to empty string if null', () => { |
|
||||
expect(field.formatValue(null)).toBe(''); |
|
||||
}); |
|
||||
|
|
||||
it('should format to asset count', () => { |
|
||||
expect(field.formatValue([1, 2, 3])).toBe('3 Reference(s)'); |
|
||||
}); |
|
||||
|
|
||||
it('should return zero formatting if other type', () => { |
|
||||
expect(field.formatValue(1)).toBe('0 References'); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
describe('NumberField', () => { |
|
||||
const field = createField(new StringFieldPropertiesDto(null, null, null, true, false, 'Input', undefined, 'pattern', undefined, 3, 1, ['1', '2'])); |
|
||||
|
|
||||
it('should create validators', () => { |
|
||||
expect(field.createValidators().length).toBe(5); |
|
||||
}); |
|
||||
|
|
||||
it('should format to empty string if null', () => { |
|
||||
expect(field.formatValue(null)).toBe(''); |
|
||||
}); |
|
||||
|
|
||||
it('should format to string', () => { |
|
||||
expect(field.formatValue('hello')).toBe('hello'); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
function createField(properties: FieldPropertiesDto) { |
|
||||
return new FieldDto(1, 'field1', false, false, 'languages', properties); |
|
||||
} |
|
||||
@ -1,450 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Response, ResponseOptions } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { It, IMock, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { |
|
||||
AddFieldDto, |
|
||||
ApiUrlConfig, |
|
||||
AuthService, |
|
||||
CreateSchemaDto, |
|
||||
createProperties, |
|
||||
DateTime, |
|
||||
EntityCreatedDto, |
|
||||
FieldDto, |
|
||||
SchemaDetailsDto, |
|
||||
SchemaDto, |
|
||||
SchemaPropertiesDto, |
|
||||
SchemasService, |
|
||||
UpdateFieldDto, |
|
||||
UpdateSchemaDto, |
|
||||
Version |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('SchemasService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let schemasService: SchemasService; |
|
||||
let version = new Version('1'); |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
authService = Mock.ofType(AuthService); |
|
||||
schemasService = new SchemasService(authService.object, new ApiUrlConfig('http://service/p/')); |
|
||||
}); |
|
||||
|
|
||||
it('should throw if creating invalid property type', () => { |
|
||||
expect(() => createProperties('invalid')).toThrow('Invalid properties type'); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get schemas', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/schemas')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: [ |
|
||||
{ |
|
||||
id: 'id1', |
|
||||
name: 'name1', |
|
||||
properties: { |
|
||||
label: 'label1', |
|
||||
hints: 'hints1' |
|
||||
}, |
|
||||
isPublished: true, |
|
||||
created: '2016-12-12T10:10', |
|
||||
createdBy: 'Created1', |
|
||||
lastModified: '2017-12-12T10:10', |
|
||||
lastModifiedBy: 'LastModifiedBy1', |
|
||||
version: 11, |
|
||||
data: {} |
|
||||
}, |
|
||||
{ |
|
||||
id: 'id2', |
|
||||
name: 'name2', |
|
||||
properties: { |
|
||||
label: 'label2', |
|
||||
hints: 'hints2' |
|
||||
}, |
|
||||
isPublished: true, |
|
||||
created: '2016-10-12T10:10', |
|
||||
createdBy: 'Created2', |
|
||||
lastModified: '2017-10-12T10:10', |
|
||||
lastModifiedBy: 'LastModifiedBy2', |
|
||||
version: 22, |
|
||||
data: {} |
|
||||
} |
|
||||
] |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let schemas: SchemaDto[] | null = null; |
|
||||
|
|
||||
schemasService.getSchemas('my-app').subscribe(result => { |
|
||||
schemas = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(schemas).toEqual([ |
|
||||
new SchemaDto('id1', 'name1', new SchemaPropertiesDto('label1', 'hints1'), true, 'Created1', 'LastModifiedBy1', |
|
||||
DateTime.parseISO_UTC('2016-12-12T10:10'), |
|
||||
DateTime.parseISO_UTC('2017-12-12T10:10'), |
|
||||
new Version('11')), |
|
||||
new SchemaDto('id2', 'name2', new SchemaPropertiesDto('label2', 'hints2'), true, 'Created2', 'LastModifiedBy2', |
|
||||
DateTime.parseISO_UTC('2016-10-12T10:10'), |
|
||||
DateTime.parseISO_UTC('2017-10-12T10:10'), |
|
||||
new Version('22')) |
|
||||
]); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get schema', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/schemas/my-schema')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
id: 'id1', |
|
||||
name: 'name1', |
|
||||
properties: { |
|
||||
label: 'label1', |
|
||||
hints: 'hints1' |
|
||||
}, |
|
||||
isPublished: true, |
|
||||
created: '2016-12-12T10:10', |
|
||||
createdBy: 'Created1', |
|
||||
lastModified: '2017-12-12T10:10', |
|
||||
lastModifiedBy: 'LastModifiedBy1', |
|
||||
version: 11, |
|
||||
fields: [ |
|
||||
{ |
|
||||
fieldId: 1, |
|
||||
name: 'field1', |
|
||||
isHidden: true, |
|
||||
isDisabled: true, |
|
||||
partitioning: 'language', |
|
||||
properties: { |
|
||||
fieldType: 'Number' |
|
||||
} |
|
||||
}, |
|
||||
{ |
|
||||
fieldId: 2, |
|
||||
name: 'field2', |
|
||||
isHidden: true, |
|
||||
isDisabled: true, |
|
||||
partitioning: 'language', |
|
||||
properties: { |
|
||||
fieldType: 'String' |
|
||||
} |
|
||||
}, |
|
||||
{ |
|
||||
fieldId: 3, |
|
||||
name: 'field3', |
|
||||
isHidden: true, |
|
||||
isDisabled: true, |
|
||||
partitioning: 'language', |
|
||||
properties: { |
|
||||
fieldType: 'Boolean' |
|
||||
} |
|
||||
}, |
|
||||
{ |
|
||||
fieldId: 4, |
|
||||
name: 'field4', |
|
||||
isHidden: true, |
|
||||
isDisabled: true, |
|
||||
partitioning: 'language', |
|
||||
properties: { |
|
||||
fieldType: 'DateTime' |
|
||||
} |
|
||||
}, |
|
||||
{ |
|
||||
fieldId: 5, |
|
||||
name: 'field5', |
|
||||
isHidden: true, |
|
||||
isDisabled: true, |
|
||||
partitioning: 'language', |
|
||||
properties: { |
|
||||
fieldType: 'Json' |
|
||||
} |
|
||||
}, |
|
||||
{ |
|
||||
fieldId: 6, |
|
||||
name: 'field6', |
|
||||
isHidden: true, |
|
||||
isDisabled: true, |
|
||||
partitioning: 'language', |
|
||||
properties: { |
|
||||
fieldType: 'Geolocation' |
|
||||
} |
|
||||
}, |
|
||||
{ |
|
||||
fieldId: 7, |
|
||||
name: 'field7', |
|
||||
isHidden: true, |
|
||||
isDisabled: true, |
|
||||
partitioning: 'language', |
|
||||
properties: { |
|
||||
fieldType: 'Assets' |
|
||||
} |
|
||||
}, |
|
||||
{ |
|
||||
fieldId: 8, |
|
||||
name: 'field8', |
|
||||
isHidden: true, |
|
||||
isDisabled: true, |
|
||||
partitioning: 'language', |
|
||||
properties: { |
|
||||
fieldType: 'References' |
|
||||
} |
|
||||
} |
|
||||
] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let schema: SchemaDetailsDto | null = null; |
|
||||
|
|
||||
schemasService.getSchema('my-app', 'my-schema', version).subscribe(result => { |
|
||||
schema = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(schema).toEqual( |
|
||||
new SchemaDetailsDto('id1', 'name1', new SchemaPropertiesDto('label1', 'hints1'), true, 'Created1', 'LastModifiedBy1', |
|
||||
DateTime.parseISO_UTC('2016-12-12T10:10'), |
|
||||
DateTime.parseISO_UTC('2017-12-12T10:10'), |
|
||||
new Version('11'), |
|
||||
[ |
|
||||
new FieldDto(1, 'field1', true, true, 'language', createProperties('Number')), |
|
||||
new FieldDto(2, 'field2', true, true, 'language', createProperties('String')), |
|
||||
new FieldDto(3, 'field3', true, true, 'language', createProperties('Boolean')), |
|
||||
new FieldDto(4, 'field4', true, true, 'language', createProperties('DateTime')), |
|
||||
new FieldDto(5, 'field5', true, true, 'language', createProperties('Json')), |
|
||||
new FieldDto(6, 'field6', true, true, 'language', createProperties('Geolocation')), |
|
||||
new FieldDto(7, 'field7', true, true, 'language', createProperties('Assets')), |
|
||||
new FieldDto(8, 'field8', true, true, 'language', createProperties('References')) |
|
||||
])); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make post request to create schema', () => { |
|
||||
const dto = new CreateSchemaDto('name'); |
|
||||
|
|
||||
authService.setup(x => x.authPost('http://service/p/api/apps/my-app/schemas', dto, version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
id: 'my-schema' |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let created: EntityCreatedDto | null = null; |
|
||||
|
|
||||
schemasService.postSchema('my-app', dto, version).subscribe(result => { |
|
||||
created = result; |
|
||||
}); |
|
||||
|
|
||||
expect(created).toEqual( |
|
||||
new EntityCreatedDto('my-schema')); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make post request to add field', () => { |
|
||||
const dto = new AddFieldDto('name', 'invariant', createProperties('Number')); |
|
||||
|
|
||||
authService.setup(x => x.authPost('http://service/p/api/apps/my-app/schemas/my-schema/fields', dto, version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
id: 123 |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let created: EntityCreatedDto | null = null; |
|
||||
|
|
||||
schemasService.postField('my-app', 'my-schema', dto, version).subscribe(result => { |
|
||||
created = result; |
|
||||
}); |
|
||||
|
|
||||
expect(created).toEqual( |
|
||||
new EntityCreatedDto(123)); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to update schema', () => { |
|
||||
const dto = new UpdateSchemaDto('label', 'hints'); |
|
||||
|
|
||||
authService.setup(x => x.authPut('http://service/p/api/apps/my-app/schemas/my-schema', dto, version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
schemasService.putSchema('my-app', 'my-schema', dto, version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to update field', () => { |
|
||||
const dto = new UpdateFieldDto(createProperties('Number')); |
|
||||
|
|
||||
authService.setup(x => x.authPut('http://service/p/api/apps/my-app/schemas/my-schema/fields/1', dto, version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
schemasService.putField('my-app', 'my-schema', 1, dto, version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to update field ordering', () => { |
|
||||
const dto = [1, 2, 3]; |
|
||||
|
|
||||
authService.setup(x => x.authPut('http://service/p/api/apps/my-app/schemas/my-schema/fields/ordering', It.isAny(), version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
schemasService.putFieldOrdering('my-app', 'my-schema', dto, version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to publish schema', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/apps/my-app/schemas/my-schema/publish', It.isAny(), version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
schemasService.publishSchema('my-app', 'my-schema', version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to unpublish schema', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/apps/my-app/schemas/my-schema/unpublish', It.isAny(), version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
schemasService.unpublishSchema('my-app', 'my-schema', version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to enable field', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/apps/my-app/schemas/my-schema/fields/1/enable', It.isAny(), version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
schemasService.enableField('my-app', 'my-schema', 1, version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to disable field', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/apps/my-app/schemas/my-schema/fields/1/disable', It.isAny(), version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
schemasService.disableField('my-app', 'my-schema', 1, version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to show field', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/apps/my-app/schemas/my-schema/fields/1/show', It.isAny(), version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
schemasService.showField('my-app', 'my-schema', 1, version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to hide field', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/apps/my-app/schemas/my-schema/fields/1/hide', It.isAny(), version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
schemasService.hideField('my-app', 'my-schema', 1, version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make delete request to delete field', () => { |
|
||||
authService.setup(x => x.authDelete('http://service/p/api/apps/my-app/schemas/my-schema/fields/1', version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
schemasService.deleteField('my-app', 'my-schema', 1, version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make delete request to delete schema', () => { |
|
||||
authService.setup(x => x.authDelete('http://service/p/api/apps/my-app/schemas/my-schema', version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
schemasService.deleteSchema('my-app', 'my-schema', version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,149 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Response, ResponseOptions } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { IMock, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { |
|
||||
ApiUrlConfig, |
|
||||
AuthService, |
|
||||
CallsUsageDto, |
|
||||
CurrentCallsDto, |
|
||||
CurrentStorageDto, |
|
||||
DateTime, |
|
||||
StorageUsageDto, |
|
||||
UsagesService |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('UsagesService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let usagesService: UsagesService; |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
authService = Mock.ofType(AuthService); |
|
||||
usagesService = new UsagesService(authService.object, new ApiUrlConfig('http://service/p/')); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get calls usages', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/usages/calls/2017-10-12/2017-10-13')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: [ |
|
||||
{ |
|
||||
date: '2017-10-12', |
|
||||
count: 10, |
|
||||
averageMs: 130 |
|
||||
}, |
|
||||
{ |
|
||||
date: '2017-10-13', |
|
||||
count: 13, |
|
||||
averageMs: 170 |
|
||||
} |
|
||||
] |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let usages: CallsUsageDto[] | null = null; |
|
||||
|
|
||||
usagesService.getCallsUsages('my-app', DateTime.parseISO_UTC('2017-10-12'), DateTime.parseISO_UTC('2017-10-13')).subscribe(result => { |
|
||||
usages = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(usages).toEqual( |
|
||||
[ |
|
||||
new CallsUsageDto(DateTime.parseISO_UTC('2017-10-12'), 10, 130), |
|
||||
new CallsUsageDto(DateTime.parseISO_UTC('2017-10-13'), 13, 170) |
|
||||
]); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get month calls', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/usages/calls/month')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { count: 130, maxAllowed: 150 } |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let usages: CurrentCallsDto | null = null; |
|
||||
|
|
||||
usagesService.getMonthCalls('my-app').subscribe(result => { |
|
||||
usages = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(usages).toEqual(new CurrentCallsDto(130, 150)); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get storage usages', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/usages/storage/2017-10-12/2017-10-13')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: [ |
|
||||
{ |
|
||||
date: '2017-10-12', |
|
||||
count: 10, |
|
||||
size: 130 |
|
||||
}, |
|
||||
{ |
|
||||
date: '2017-10-13', |
|
||||
count: 13, |
|
||||
size: 170 |
|
||||
} |
|
||||
] |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let usages: StorageUsageDto[] | null = null; |
|
||||
|
|
||||
usagesService.getStorageUsages('my-app', DateTime.parseISO_UTC('2017-10-12'), DateTime.parseISO_UTC('2017-10-13')).subscribe(result => { |
|
||||
usages = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(usages).toEqual( |
|
||||
[ |
|
||||
new StorageUsageDto(DateTime.parseISO_UTC('2017-10-12'), 10, 130), |
|
||||
new StorageUsageDto(DateTime.parseISO_UTC('2017-10-13'), 13, 170) |
|
||||
]); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get today storage', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/usages/storage/today')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { size: 130, maxAllowed: 150 } |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let usages: CurrentStorageDto | null = null; |
|
||||
|
|
||||
usagesService.getTodayStorage('my-app').subscribe(result => { |
|
||||
usages = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(usages).toEqual(new CurrentStorageDto(130, 150)); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,103 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { IMock, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { |
|
||||
AuthService, |
|
||||
Profile, |
|
||||
UserDto, |
|
||||
UsersProviderService, |
|
||||
UsersService |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('UsersProviderService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let usersService: IMock<UsersService>; |
|
||||
let usersProviderService: UsersProviderService; |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
authService = Mock.ofType(AuthService); |
|
||||
usersService = Mock.ofType(UsersService); |
|
||||
usersProviderService = new UsersProviderService(usersService.object, authService.object); |
|
||||
}); |
|
||||
|
|
||||
it('Should return users service when user not cached', () => { |
|
||||
const user = new UserDto('123', 'mail@domain.com', 'User1', 'path/to/image', true); |
|
||||
|
|
||||
usersService.setup(x => x.getUser('123')) |
|
||||
.returns(() => Observable.of(user)).verifiable(Times.once()); |
|
||||
|
|
||||
let resultingUser: UserDto | null = null; |
|
||||
|
|
||||
usersProviderService.getUser('123').subscribe(result => { |
|
||||
resultingUser = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(resultingUser).toBe(user); |
|
||||
|
|
||||
usersService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('Should return provide user from cache', () => { |
|
||||
const user = new UserDto('123', 'mail@domain.com', 'User1', 'path/to/image', true); |
|
||||
|
|
||||
usersService.setup(x => x.getUser('123')) |
|
||||
.returns(() => Observable.of(user)).verifiable(Times.once()); |
|
||||
|
|
||||
usersProviderService.getUser('123'); |
|
||||
|
|
||||
let resultingUser: UserDto | null = null; |
|
||||
|
|
||||
usersProviderService.getUser('123').subscribe(result => { |
|
||||
resultingUser = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(resultingUser).toBe(user); |
|
||||
|
|
||||
usersService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('Should return me when user is current user', () => { |
|
||||
const user = new UserDto('123', 'mail@domain.com', 'User1', 'path/to/image', true); |
|
||||
|
|
||||
authService.setup(x => x.user) |
|
||||
.returns(() => new Profile(<any>{ profile: { sub: '123'}})); |
|
||||
|
|
||||
usersService.setup(x => x.getUser('123')) |
|
||||
.returns(() => Observable.of(user)).verifiable(Times.once()); |
|
||||
|
|
||||
let resultingUser: UserDto | null = null; |
|
||||
|
|
||||
usersProviderService.getUser('123').subscribe(result => { |
|
||||
resultingUser = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(resultingUser).toEqual(new UserDto('123', 'mail@domain.com', 'Me', 'path/to/image', true)); |
|
||||
|
|
||||
usersService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('Should return invalid user when not found', () => { |
|
||||
authService.setup(x => x.user) |
|
||||
.returns(() => new Profile(<any>{ profile: { sub: '123'}})); |
|
||||
|
|
||||
usersService.setup(x => x.getUser('123')) |
|
||||
.returns(() => Observable.throw('NOT FOUND')).verifiable(Times.once()); |
|
||||
|
|
||||
let resultingUser: UserDto | null = null; |
|
||||
|
|
||||
usersProviderService.getUser('123').subscribe(result => { |
|
||||
resultingUser = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(resultingUser).toEqual(new UserDto('NOT FOUND', 'NOT FOUND', 'NOT FOUND', null, false)); |
|
||||
|
|
||||
usersService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,339 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Response, ResponseOptions } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { It, IMock, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { |
|
||||
ApiUrlConfig, |
|
||||
AuthService, |
|
||||
CreateUserDto, |
|
||||
UpdateUserDto, |
|
||||
UserCreatedDto, |
|
||||
UserDto, |
|
||||
UserManagementService, |
|
||||
UsersDto, |
|
||||
UsersService |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('UsersService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let usersService: UsersService; |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
authService = Mock.ofType(AuthService); |
|
||||
usersService = new UsersService(authService.object, new ApiUrlConfig('http://service/p/')); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get many users', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/users?query=')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: [ |
|
||||
{ |
|
||||
id: '123', |
|
||||
email: 'mail1@domain.com', |
|
||||
displayName: 'User1', |
|
||||
pictureUrl: 'path/to/image1', |
|
||||
isLocked: true |
|
||||
}, |
|
||||
{ |
|
||||
id: '456', |
|
||||
email: 'mail2@domain.com', |
|
||||
displayName: 'User2', |
|
||||
pictureUrl: 'path/to/image2', |
|
||||
isLocked: true |
|
||||
} |
|
||||
] |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let user: UserDto[] | null = null; |
|
||||
|
|
||||
usersService.getUsers().subscribe(result => { |
|
||||
user = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(user).toEqual( |
|
||||
[ |
|
||||
new UserDto('123', 'mail1@domain.com', 'User1', 'path/to/image1', true), |
|
||||
new UserDto('456', 'mail2@domain.com', 'User2', 'path/to/image2', true) |
|
||||
]); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request with query to get many users', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/users?query=my-query')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: [ |
|
||||
{ |
|
||||
id: '123', |
|
||||
email: 'mail1@domain.com', |
|
||||
displayName: 'User1', |
|
||||
pictureUrl: 'path/to/image1', |
|
||||
isLocked: true |
|
||||
}, |
|
||||
{ |
|
||||
id: '456', |
|
||||
email: 'mail2@domain.com', |
|
||||
displayName: 'User2', |
|
||||
pictureUrl: 'path/to/image2', |
|
||||
isLocked: true |
|
||||
} |
|
||||
] |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let user: UserDto[] | null = null; |
|
||||
|
|
||||
usersService.getUsers('my-query').subscribe(result => { |
|
||||
user = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(user).toEqual( |
|
||||
[ |
|
||||
new UserDto('123', 'mail1@domain.com', 'User1', 'path/to/image1', true), |
|
||||
new UserDto('456', 'mail2@domain.com', 'User2', 'path/to/image2', true) |
|
||||
]); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get single user', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/users/123')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
id: '123', |
|
||||
email: 'mail1@domain.com', |
|
||||
displayName: 'User1', |
|
||||
pictureUrl: 'path/to/image1', |
|
||||
isLocked: true |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let user: UserDto | null = null; |
|
||||
|
|
||||
usersService.getUser('123').subscribe(result => { |
|
||||
user = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(user).toEqual(new UserDto('123', 'mail1@domain.com', 'User1', 'path/to/image1', true)); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
describe('UserManagementService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let userManagementService: UserManagementService; |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
authService = Mock.ofType(AuthService); |
|
||||
userManagementService = new UserManagementService(authService.object, new ApiUrlConfig('http://service/p/')); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get many users', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/user-management?take=20&skip=30&query=')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
total: 100, |
|
||||
items: [ |
|
||||
{ |
|
||||
id: '123', |
|
||||
email: 'mail1@domain.com', |
|
||||
displayName: 'User1', |
|
||||
pictureUrl: 'path/to/image1', |
|
||||
isLocked: true |
|
||||
}, |
|
||||
{ |
|
||||
id: '456', |
|
||||
email: 'mail2@domain.com', |
|
||||
displayName: 'User2', |
|
||||
pictureUrl: 'path/to/image2', |
|
||||
isLocked: true |
|
||||
} |
|
||||
] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let user: UsersDto | null = null; |
|
||||
|
|
||||
userManagementService.getUsers(20, 30).subscribe(result => { |
|
||||
user = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(user).toEqual( |
|
||||
new UsersDto(100, [ |
|
||||
new UserDto('123', 'mail1@domain.com', 'User1', 'path/to/image1', true), |
|
||||
new UserDto('456', 'mail2@domain.com', 'User2', 'path/to/image2', true) |
|
||||
])); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request with query to get many users', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/user-management?take=20&skip=30&query=my-query')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
total: 100, |
|
||||
items: [ |
|
||||
{ |
|
||||
id: '123', |
|
||||
email: 'mail1@domain.com', |
|
||||
displayName: 'User1', |
|
||||
pictureUrl: 'path/to/image1', |
|
||||
isLocked: true |
|
||||
}, |
|
||||
{ |
|
||||
id: '456', |
|
||||
email: 'mail2@domain.com', |
|
||||
displayName: 'User2', |
|
||||
pictureUrl: 'path/to/image2', |
|
||||
isLocked: true |
|
||||
} |
|
||||
] |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let user: UsersDto | null = null; |
|
||||
|
|
||||
userManagementService.getUsers(20, 30, 'my-query').subscribe(result => { |
|
||||
user = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(user).toEqual( |
|
||||
new UsersDto(100, [ |
|
||||
new UserDto('123', 'mail1@domain.com', 'User1', 'path/to/image1', true), |
|
||||
new UserDto('456', 'mail2@domain.com', 'User2', 'path/to/image2', true) |
|
||||
])); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get single user', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/user-management/123')) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
id: '123', |
|
||||
email: 'mail1@domain.com', |
|
||||
displayName: 'User1', |
|
||||
pictureUrl: 'path/to/image1', |
|
||||
isLocked: true |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let user: UserDto | null = null; |
|
||||
|
|
||||
userManagementService.getUser('123').subscribe(result => { |
|
||||
user = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(user).toEqual(new UserDto('123', 'mail1@domain.com', 'User1', 'path/to/image1', true)); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make post request to create user', () => { |
|
||||
const dto = new CreateUserDto('mail@squidex.io', 'Squidex User', 'password'); |
|
||||
|
|
||||
authService.setup(x => x.authPost('http://service/p/api/user-management', It.isAny())) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { |
|
||||
id: '123', |
|
||||
pictureUrl: 'path/to/image1' |
|
||||
} |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let user: UserDto | null = null; |
|
||||
|
|
||||
userManagementService.postUser(dto).subscribe(result => { |
|
||||
user = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(user).toEqual(new UserCreatedDto('123', 'path/to/image1')); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to update user', () => { |
|
||||
const dto = new UpdateUserDto('mail@squidex.io', 'Squidex User', 'password'); |
|
||||
|
|
||||
authService.setup(x => x.authPut('http://service/p/api/user-management/123', It.isAny())) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
userManagementService.putUser('123', dto); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to lock user', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/user-management/123/lock', It.isAny())) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
userManagementService.lockUser('123'); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make put request to unlock user', () => { |
|
||||
authService.setup(x => x.authPut('http://service/p/api/user-management/123/unlock', It.isAny())) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
userManagementService.unlockUser('123'); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
@ -0,0 +1,99 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import { HttpClient } from '@angular/common/http'; |
||||
|
import { Injectable } from '@angular/core'; |
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import 'framework/angular/http-extensions'; |
||||
|
|
||||
|
import { |
||||
|
ApiUrlConfig, |
||||
|
HTTP, |
||||
|
Version |
||||
|
} from 'framework'; |
||||
|
|
||||
|
export class WebhookDto { |
||||
|
constructor( |
||||
|
public readonly id: string, |
||||
|
public readonly schemaId: string, |
||||
|
public readonly sharedSecret: string, |
||||
|
public readonly url: string, |
||||
|
public readonly totalSucceeded: number, |
||||
|
public readonly totalFailed: number, |
||||
|
public readonly totalTimedout: number, |
||||
|
public readonly averageRequestTimeMs: number, |
||||
|
public readonly lastDumps: string[] |
||||
|
) { |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export class WebhookCreatedDto { |
||||
|
constructor( |
||||
|
public readonly id: string, |
||||
|
public readonly sharedSecret: string |
||||
|
) { |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export class CreateWebhookDto { |
||||
|
constructor( |
||||
|
public readonly url: string |
||||
|
) { |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Injectable() |
||||
|
export class WebhooksService { |
||||
|
constructor( |
||||
|
private readonly http: HttpClient, |
||||
|
private readonly apiUrl: ApiUrlConfig |
||||
|
) { |
||||
|
} |
||||
|
|
||||
|
public getWebhooks(appName: string, version?: Version): Observable<WebhookDto[]> { |
||||
|
const url = this.apiUrl.buildUrl(`api/apps/${appName}/webhooks`); |
||||
|
|
||||
|
return HTTP.getVersioned(this.http, url, version) |
||||
|
.map(response => { |
||||
|
const items: any[] = response; |
||||
|
|
||||
|
return items.map(item => { |
||||
|
return new WebhookDto( |
||||
|
item.id, |
||||
|
item.schemaId, |
||||
|
item.sharedSecret, |
||||
|
item.url, |
||||
|
item.totalSucceeded, |
||||
|
item.totalFailed, |
||||
|
item.totalTimedout, |
||||
|
item.averageRequestTimeMs, |
||||
|
item.lastDumps); |
||||
|
}); |
||||
|
}) |
||||
|
.pretifyError('Failed to load webhooks. Please reload.'); |
||||
|
} |
||||
|
|
||||
|
public postWebhook(appName: string, schemaName: string, dto: CreateWebhookDto, version?: Version): Observable<WebhookCreatedDto> { |
||||
|
const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/webhooks`); |
||||
|
|
||||
|
return HTTP.postVersioned(this.http, url, dto, version) |
||||
|
.map(response => { |
||||
|
return new WebhookCreatedDto( |
||||
|
response.id, |
||||
|
response.sharedSecret); |
||||
|
}) |
||||
|
.pretifyError('Failed to create webhook. Please reload.'); |
||||
|
} |
||||
|
|
||||
|
public deleteWebhook(appName: string, schemaName: string, id: string, version?: Version): Observable<any> { |
||||
|
const url = this.apiUrl.buildUrl(`api/apps/${appName}/schemas/${schemaName}/webhooks/${id}`); |
||||
|
|
||||
|
return HTTP.deleteVersioned(this.http, url, version) |
||||
|
.pretifyError('Failed to delete webhook. Please reload.'); |
||||
|
} |
||||
|
} |
||||
@ -1,114 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Sebastian Stehle. All rights reserved |
|
||||
*/ |
|
||||
|
|
||||
import { Response, ResponseOptions } from '@angular/http'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
import { IMock, Mock, Times } from 'typemoq'; |
|
||||
|
|
||||
import { |
|
||||
ApiUrlConfig, |
|
||||
AuthService, |
|
||||
CreateWebhookDto, |
|
||||
Version, |
|
||||
WebhookCreatedDto, |
|
||||
WebhookDto, |
|
||||
WebhooksService |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('WebhooksService', () => { |
|
||||
let authService: IMock<AuthService>; |
|
||||
let webhooksService: WebhooksService; |
|
||||
let version = new Version('1'); |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
authService = Mock.ofType(AuthService); |
|
||||
webhooksService = new WebhooksService(authService.object, new ApiUrlConfig('http://service/p/')); |
|
||||
}); |
|
||||
|
|
||||
it('should make get request to get app webhooks', () => { |
|
||||
authService.setup(x => x.authGet('http://service/p/api/apps/my-app/webhooks', version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: [{ |
|
||||
id: 'id1', |
|
||||
schemaId: 'schemaId1', |
|
||||
sharedSecret: 'token1', |
|
||||
url: 'http://squidex.io/1', |
|
||||
totalSucceeded: 1, |
|
||||
totalFailed: 2, |
|
||||
totalTimedout: 3, |
|
||||
averageRequestTimeMs: 4, |
|
||||
lastDumps: ['dump1'] |
|
||||
}, { |
|
||||
id: 'id2', |
|
||||
schemaId: 'schemaId2', |
|
||||
sharedSecret: 'token2', |
|
||||
url: 'http://squidex.io/2', |
|
||||
totalSucceeded: 5, |
|
||||
totalFailed: 6, |
|
||||
totalTimedout: 7, |
|
||||
averageRequestTimeMs: 8, |
|
||||
lastDumps: ['dump2'] |
|
||||
}] |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let webhooks: WebhookDto[] | null = null; |
|
||||
|
|
||||
webhooksService.getWebhooks('my-app', version).subscribe(result => { |
|
||||
webhooks = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(webhooks).toEqual([ |
|
||||
new WebhookDto('id1', 'schemaId1', 'token1', 'http://squidex.io/1', 1, 2, 3, 4, ['dump1']), |
|
||||
new WebhookDto('id2', 'schemaId2', 'token2', 'http://squidex.io/2', 5, 6, 7, 8, ['dump2']) |
|
||||
]); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make post request to create webhook', () => { |
|
||||
const dto = new CreateWebhookDto('http://squidex.io/hook'); |
|
||||
|
|
||||
authService.setup(x => x.authPost('http://service/p/api/apps/my-app/schemas/my-schema/webhooks', dto, version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions({ |
|
||||
body: { id: 'id1', sharedSecret: 'token1' } |
|
||||
}) |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
let webhook: WebhookCreatedDto | null = null; |
|
||||
|
|
||||
webhooksService.postWebhook('my-app', 'my-schema', dto, version).subscribe(result => { |
|
||||
webhook = result; |
|
||||
}).unsubscribe(); |
|
||||
|
|
||||
expect(webhook).toEqual(new WebhookCreatedDto('id1', 'token1')); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
|
|
||||
it('should make delete request to delete webhook', () => { |
|
||||
authService.setup(x => x.authDelete('http://service/p/api/apps/my-app/schemas/my-schema/webhooks/123', version)) |
|
||||
.returns(() => Observable.of( |
|
||||
new Response( |
|
||||
new ResponseOptions() |
|
||||
) |
|
||||
)) |
|
||||
.verifiable(Times.once()); |
|
||||
|
|
||||
webhooksService.deleteWebhook('my-app', 'my-schema', '123', version); |
|
||||
|
|
||||
authService.verifyAll(); |
|
||||
}); |
|
||||
}); |
|
||||
Loading…
Reference in new issue