mirror of https://github.com/Squidex/squidex.git
4 changed files with 141 additions and 118 deletions
@ -1,99 +0,0 @@ |
|||||
/* |
|
||||
* 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.'); |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,117 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; |
||||
|
import { inject, TestBed } from '@angular/core/testing'; |
||||
|
|
||||
|
import { |
||||
|
ApiUrlConfig, |
||||
|
CreateWebhookDto, |
||||
|
Version, |
||||
|
WebhookCreatedDto, |
||||
|
WebhookDto, |
||||
|
WebhooksService |
||||
|
} from './../'; |
||||
|
|
||||
|
describe('WebhooksService', () => { |
||||
|
let version = new Version('1'); |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
TestBed.configureTestingModule({ |
||||
|
imports: [ |
||||
|
HttpClientTestingModule |
||||
|
], |
||||
|
providers: [ |
||||
|
WebhooksService, |
||||
|
{ provide: ApiUrlConfig, useValue: new ApiUrlConfig('http://service/p/') } |
||||
|
] |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
afterEach(inject([HttpTestingController], (httpMock: HttpTestingController) => { |
||||
|
httpMock.verify(); |
||||
|
})); |
||||
|
|
||||
|
it('should make get request to get app webhooks', |
||||
|
inject([WebhooksService, HttpTestingController], (webhooksService: WebhooksService, httpMock: HttpTestingController) => { |
||||
|
|
||||
|
let webhooks: WebhookDto[] | null = null; |
||||
|
|
||||
|
webhooksService.getWebhooks('my-app', version).subscribe(result => { |
||||
|
webhooks = result; |
||||
|
}); |
||||
|
|
||||
|
const req = httpMock.expectOne('http://service/p/api/apps/my-app/webhooks'); |
||||
|
|
||||
|
expect(req.request.method).toEqual('GET'); |
||||
|
expect(req.request.headers.get('If-Match')).toBe(version.value); |
||||
|
|
||||
|
req.flush([ |
||||
|
{ |
||||
|
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'] |
||||
|
} |
||||
|
]); |
||||
|
|
||||
|
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']) |
||||
|
]); |
||||
|
})); |
||||
|
|
||||
|
it('should make post request to create webhook', |
||||
|
inject([WebhooksService, HttpTestingController], (webhooksService: WebhooksService, httpMock: HttpTestingController) => { |
||||
|
|
||||
|
const dto = new CreateWebhookDto('http://squidex.io/hook'); |
||||
|
|
||||
|
let webhook: WebhookCreatedDto | null = null; |
||||
|
|
||||
|
webhooksService.postWebhook('my-app', 'my-schema', dto, version).subscribe(result => { |
||||
|
webhook = result; |
||||
|
}); |
||||
|
|
||||
|
const req = httpMock.expectOne('http://service/p/api/apps/my-app/schemas/my-schema/webhooks'); |
||||
|
|
||||
|
expect(req.request.method).toEqual('POST'); |
||||
|
expect(req.request.headers.get('If-Match')).toBe(version.value); |
||||
|
|
||||
|
req.flush({ id: 'id1', sharedSecret: 'token1' }); |
||||
|
|
||||
|
expect(webhook).toEqual(new WebhookCreatedDto('id1', 'token1')); |
||||
|
})); |
||||
|
|
||||
|
it('should make delete request to delete webhook', |
||||
|
inject([WebhooksService, HttpTestingController], (webhooksService: WebhooksService, httpMock: HttpTestingController) => { |
||||
|
|
||||
|
webhooksService.deleteWebhook('my-app', 'my-schema', '123', version).subscribe(); |
||||
|
|
||||
|
const req = httpMock.expectOne('http://service/p/api/apps/my-app/schemas/my-schema/webhooks/123'); |
||||
|
|
||||
|
expect(req.request.method).toEqual('DELETE'); |
||||
|
expect(req.request.headers.get('If-Match')).toBe(version.value); |
||||
|
|
||||
|
req.flush({ id: 'id1', sharedSecret: 'token1' }); |
||||
|
})); |
||||
|
}); |
||||
Loading…
Reference in new issue