Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(dashboard): pin the template row delete and its permission gate

Yudhi Armyndharisf07c1b959ff1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 43c3a7a84..4aabe7c17 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Webhook and automation filters accept a `chatId` condition, so a webhook can be scoped to specific groups or chats instead of only to a sender ([#1634](https://github.com/rmyndharis/OpenWA/issues/1634)). Thanks @krishshah9944.
-- The dashboard Templates list has a delete button on each row, so a template can be deleted without opening it in the editor first. Like the editor's delete button, it shows only for keys that can write templates.
+- The dashboard Templates list has a delete button on each row, so a template can be deleted without opening it in the editor first. Like the editor's delete button, it shows only for keys that can write templates. Thanks @C24212.
### Changed
diff --git a/dashboard/src/pages/Templates.test.ts b/dashboard/src/pages/Templates.test.ts
index c5c7272a3..ecad26fb2 100644
--- a/dashboard/src/pages/Templates.test.ts
+++ b/dashboard/src/pages/Templates.test.ts
@@ -8,6 +8,8 @@ import { createElement } from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
let templatesStatus = 200;
+let templates: Array<{ id: string; name: string; body: string }> = [];
+const deleted: string[] = [];
function jsonResponse(data: unknown, status = 200): Response {
return new Response(JSON.stringify(data), { status, headers: { 'Content-Type': 'application/json' } });
@@ -36,7 +38,13 @@ function installFetchStub(): void {
}
if (templatesStatus !== 200)
return Promise.resolve(jsonResponse({ message: 'database offline' }, templatesStatus));
- return Promise.resolve(jsonResponse([]));
+ return Promise.resolve(jsonResponse(templates));
+ }
+ const rowMatch = /^\/api\/sessions\/sess-1\/templates\/(.+)$/.exec(path);
+ if (rowMatch) {
+ deleted.push(rowMatch[1]);
+ templates = templates.filter(t => t.id !== rowMatch[1]);
+ return Promise.resolve(jsonResponse({ success: true }));
}
return Promise.resolve(jsonResponse({ message: `unstubbed ${path}` }, 404));
}) as typeof fetch;
@@ -78,6 +86,39 @@ function renderTemplates(): void {
);
}
+// The row button exists so a template can be deleted without opening it in the editor first, and it
+// is gated on the same write permission as the editor's own delete. Both halves are pinned here.
+test('a write key can delete a template from its row, and a read-only key cannot', async () => {
+ const { screen, fireEvent, within, waitFor } = rtl;
+ templatesStatus = 200;
+ templates = [{ id: 'tpl-1', name: 'invoice-reminder', body: 'Hi {{name}}' }];
+ deleted.length = 0;
+
+ window.localStorage.setItem('openwa_user_role', 'operator');
+ renderTemplates();
+
+ const row = (await screen.findByText('invoice-reminder')).closest('.template-list-row') as HTMLElement;
+ fireEvent.click(within(row).getByRole('button', { name: 'Delete' }));
+
+ // The confirm names the template, so a mis-click on a crowded list is recoverable.
+ const dialog = await screen.findByRole('dialog');
+ assert.ok(within(dialog).getByText(/invoice-reminder/), 'the confirm did not name the template');
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Delete' }));
+ await waitFor(() => assert.deepEqual(deleted, ['tpl-1'], 'the delete never reached the API'));
+
+ rtl.cleanup();
+ window.localStorage.setItem('openwa_user_role', 'viewer');
+ templates = [{ id: 'tpl-1', name: 'invoice-reminder', body: 'Hi {{name}}' }];
+ renderTemplates();
+
+ const readOnlyRow = (await screen.findByText('invoice-reminder')).closest('.template-list-row') as HTMLElement;
+ assert.equal(
+ within(readOnlyRow).queryByRole('button', { name: 'Delete' }),
+ null,
+ 'a read-only key was offered the row delete',
+ );
+});
+
test('a 403 on the template list shows a permission state, not an empty library', async () => {
templatesStatus = 403;
renderTemplates();