Files
directus/api/src/services/webhooks.test.ts
Daniel Biegler 7f14e387ed Fix 15553 remove webhooks (1/2) (#21808)
* add deprecation notice to webhooks

I dont like how the "Flows"-Link is not a real router-link so it reloads the page but we could sort this out later in the PR

* add feature flag store

* make links reactive

* add depreaction to webhook item route too

* better progressive deprecation, update translation

* fix batch deletion of webhooks

* remove batch editing (it doesnt work), remove irrelevant now code

* add changeset

* add up migration

* improve upon migration

* disallow creation of new webhooks

* remove create action

* remove isvisible field

no longer used

* remove feature flags store

no longer needed

* rm featureflag store remnants

* hide webhook fields from migration

* update comment

* change width of notice

* rm button for creating webhooks

* add changeset

* Update api/src/database/migrations/20240311A-deprecate-webhooks.ts

Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>

* Update api/src/controllers/webhooks.ts

Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>

* prettier

* add changeset for system data

* update changesets

* remove top padding

* update deprecation notice

Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>

* remove ability to patch webhooks and relevant snippets from its item route

* rm bottom padding since v-form has its own

* remove Flows created by the up-migration

* only add run-script if needed

* filter whitespace values out

* deprecate webhooks on service level

* fix tests for webhooksservice

* reuse deprecation error

* add deprecation and update test for updateBatch

* update changeset

* remove webhook initialization and handling

* remove check for POST

people could use raw values for PATCH, SEARCH, etc.

* Filter empty collections in migration

* skip run script for GET requests

---------

Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>
Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
Co-authored-by: Brainslug <tim@brainslug.nl>
2024-03-28 17:10:06 +01:00

130 lines
3.3 KiB
TypeScript

import { createError, ErrorCode } from '@directus/errors';
import type { Knex } from 'knex';
import knex from 'knex';
import { createTracker, MockClient, Tracker } from 'knex-mock-client';
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi, type MockInstance } from 'vitest';
import { useBus } from '../bus/index.js';
import { WebhooksService } from './index.js';
vi.mock('../../src/database/index', () => {
return { __esModule: true, default: vi.fn(), getDatabaseClient: vi.fn().mockReturnValue('postgres') };
});
vi.mock('../messenger', () => {
return { getMessenger: vi.fn().mockReturnValue({ publish: vi.fn(), subscribe: vi.fn() }) };
});
describe('Integration Tests', () => {
let db: Knex;
let tracker: Tracker;
beforeAll(async () => {
db = knex.default({ client: MockClient });
tracker = createTracker(db);
});
beforeEach(() => {
tracker.on.any('directus_webhooks').response({});
});
afterEach(() => {
tracker.reset();
});
describe('Services / Webhooks', () => {
let service: WebhooksService;
let messengerPublishSpy: MockInstance;
const errorDeprecation = new (createError(
ErrorCode.MethodNotAllowed,
'Webhooks are deprecated, use Flows instead',
405,
))();
beforeEach(() => {
service = new WebhooksService({
knex: db,
schema: {
collections: {
directus_webhooks: {
collection: 'directus_webhooks',
primary: 'id',
singleton: false,
sortField: null,
note: null,
accountability: null,
fields: {
id: {
field: 'id',
defaultValue: null,
nullable: false,
generated: true,
type: 'integer',
dbType: 'integer',
precision: null,
scale: null,
special: [],
note: null,
validation: null,
alias: false,
},
},
},
},
relations: [],
},
});
messengerPublishSpy = vi.spyOn(useBus(), 'publish');
});
afterEach(() => {
messengerPublishSpy.mockClear();
});
describe('createOne', () => {
it('should error because of deprecation', async () => {
return expect(service.createOne({})).rejects.toEqual(errorDeprecation);
});
});
describe('createMany', () => {
it('should error because of deprecation', async () => {
return expect(service.createMany([{}])).rejects.toEqual(errorDeprecation);
});
});
describe('updateOne', () => {
it('should error because of deprecation', async () => {
return expect(service.updateOne(1, {})).rejects.toEqual(errorDeprecation);
});
});
describe('updateMany', () => {
it('should error because of deprecation', async () => {
return expect(service.updateMany([1], {})).rejects.toEqual(errorDeprecation);
});
});
describe('updateBatch', () => {
it('should error because of deprecation', async () => {
return expect(service.updateBatch()).rejects.toEqual(errorDeprecation);
});
});
describe('deleteOne', () => {
it('should publish webhooks reload message once', async () => {
await service.deleteOne(1);
expect(messengerPublishSpy).toBeCalledTimes(1);
});
});
describe('deleteMany', () => {
it('should publish webhooks reload message once', async () => {
await service.deleteMany([1]);
expect(messengerPublishSpy).toBeCalledTimes(1);
});
});
});
});