Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(dashboard): cover the plugin config editor save bridge
Yudhi Armyndharisddf864d348cd
diff --git a/dashboard/src/pages/PluginConfigUi.test.ts b/dashboard/src/pages/PluginConfigUi.test.tsindex b0b8afc84..fb50c8e83 100644--- a/dashboard/src/pages/PluginConfigUi.test.ts+++ b/dashboard/src/pages/PluginConfigUi.test.ts@@ -14,11 +14,18 @@ import type { Plugin } from '../services/api';const pluginHtml = '<p>Editor</p>';let pluginsList: Plugin[] = [];+// What the config route answered, and every body it was sent.+let putReply: { success: boolean; message?: string } = { success: true };+let putBodies: unknown[] = [];function installFetchStub(): void {- globalThis.fetch = ((input: RequestInfo | URL): Promise<Response> => {+ globalThis.fetch = ((input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;const path = url.replace(/^https?:\/\/[^/]+/, '');+ if (init?.method === 'PUT' && path === '/api/plugins/p1/config') {+ putBodies.push(JSON.parse(String(init.body)));+ return Promise.resolve(jsonResponse(putReply));+ }if (path === '/api/plugins') return Promise.resolve(jsonResponse(pluginsList));if (path.startsWith('/api/plugins/p1/config-ui')) return Promise.resolve(new Response(pluginHtml, { status: 200 }));if (path === '/api/plugins/catalog') return Promise.resolve(jsonResponse([]));@@ -62,6 +69,8 @@ afterEach(() => {queryClient?.clear();queryClient = undefined;pluginsList = [];+ putReply = { success: true };+ putBodies = [];});function renderPlugins(): void {@@ -148,3 +157,54 @@ test('PluginConfigUi localizes its frame title and replies to config:get with loassert.equal(msg.locale, 'es', 'locale was not sent in config:value');assert.equal(msg.schema?.properties.field1.title, 'Spanish Title', 'schema was not localized in config:value');});++// The editor runs in a sandboxed frame; the bridge answers only that frame, because a save carries the operator's key.+async function openConfigUi(): Promise<{ iframe: HTMLIFrameElement; posted: Record<string, unknown>[] }> {+ await i18next.changeLanguage('en');+ pluginsList = [+ {+ id: 'p1',+ name: 'Plugin 1',+ type: 'extension',+ config: {},+ configUi: { entry: 'ui.html' },+ configSchema: { type: 'object', properties: { field1: { type: 'string', title: 'Field' } } },+ } as unknown as Plugin,+ ];+ renderPlugins();+ rtl.fireEvent.click(await rtl.screen.findByTitle('Configure'));+ const iframe = await rtl.waitFor(() => {+ const el = document.querySelector('iframe');+ assert.ok(el, 'ConfigUi iframe not found in DOM');+ return el;+ });+ const posted: Record<string, unknown>[] = [];+ iframe.contentWindow!.postMessage = (message: unknown) => {+ posted.push(message as Record<string, unknown>);+ };+ return { iframe, posted };+}++const messageFrom = (source: Window, data: unknown) =>+ window.dispatchEvent(new window.MessageEvent('message', { source, data }));++test('PluginConfigUi ignores a save that does not come from its own frame', async () => {+ const { iframe, posted } = await openConfigUi();++ messageFrom(window, { type: 'config:save', config: { field1: 'foreign' } });+ messageFrom(iframe.contentWindow as Window, { type: 'config:save', config: { field1: 'own' } });++ await rtl.waitFor(() => assert.equal(putBodies.length, 1));+ assert.deepEqual(putBodies, [{ config: { field1: 'own' } }]);+ await rtl.waitFor(() => assert.deepEqual(posted, [{ type: 'config:saved' }]));+});++test('PluginConfigUi answers config:error with the gateway message when a save is rejected', async () => {+ putReply = { success: false, message: 'Rejected by the plugin' };+ const { iframe, posted } = await openConfigUi();++ messageFrom(iframe.contentWindow as Window, { type: 'config:save', config: { field1: 'x' } });++ await rtl.waitFor(() => assert.deepEqual(posted, [{ type: 'config:error', message: 'Rejected by the plugin' }]));+ assert.deepEqual(putBodies, [{ config: { field1: 'x' } }]);+});