c4c48d4104
refs https://github.com/TryGhost/Admin/pull/2209 - `miragejs` has been extracted to a framework-independent library, the re-exports of `miragejs` elements in `ember-cli-mirage` have been deprecated making our test logs very noisy - added `miragejs` as a top-level dependency - updated all relevant imports to pull from `miragejs` instead of `ember-cli-mirage`
63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
import {Response} from 'miragejs';
|
|
import {isEmpty} from '@ember/utils';
|
|
import {paginatedResponse} from '../utils';
|
|
|
|
export default function mockWebhooks(server) {
|
|
server.get('/webhooks/', paginatedResponse('webhooks'));
|
|
|
|
server.post('/webhooks/', function ({webhooks}) {
|
|
let attrs = this.normalizedRequestAttrs();
|
|
let errors = [];
|
|
|
|
if (!attrs.name) {
|
|
errors.push({
|
|
type: 'ValidationError',
|
|
message: 'Name is required',
|
|
property: 'name'
|
|
});
|
|
}
|
|
|
|
if (!attrs.event) {
|
|
errors.push({
|
|
type: 'ValidationError',
|
|
message: 'Event is required',
|
|
property: 'event'
|
|
});
|
|
}
|
|
|
|
if (!attrs.targetUrl) {
|
|
errors.push({
|
|
type: 'ValidationError',
|
|
message: 'Target URL is required',
|
|
property: 'target_url'
|
|
});
|
|
}
|
|
|
|
if (attrs.name && (webhooks.findBy({name: attrs.name, integrationId: attrs.integrationId}) || attrs.name.match(/Duplicate/i))) {
|
|
errors.push({
|
|
type: 'ValidationError',
|
|
message: 'Name has already been used',
|
|
property: 'name'
|
|
});
|
|
}
|
|
|
|
// TODO: check server-side validation
|
|
if (webhooks.findBy({targetUrl: attrs.targetUrl, event: attrs.event})) {
|
|
errors.push({
|
|
type: 'ValidationError',
|
|
message: 'Target URL has already been used for this event',
|
|
property: 'target_url'
|
|
});
|
|
}
|
|
|
|
if (!isEmpty(errors)) {
|
|
return new Response(422, {}, {errors});
|
|
}
|
|
|
|
return webhooks.create(attrs);
|
|
});
|
|
|
|
server.put('/webhooks/:id/');
|
|
server.del('/webhooks/:id/');
|
|
}
|