AddNotificationModalSecrets.test.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Saved provider secrets come back as a placeholder. The edit dialog keeps it
  3. * (sent back unchanged it keeps the secret), says so, lets typing replace it,
  4. * and tests an edited provider by its id so the backend fills in the secret.
  5. */
  6. import { describe, it, expect, afterEach } from 'vitest';
  7. import { screen, waitFor } from '@testing-library/react';
  8. import userEvent from '@testing-library/user-event';
  9. import { http, HttpResponse } from 'msw';
  10. import { render } from '../utils';
  11. import { server } from '../mocks/server';
  12. import { AddNotificationModal } from '../../components/AddNotificationModal';
  13. import type { NotificationProvider } from '../../api/client';
  14. const PLACEHOLDER = '********';
  15. afterEach(() => {
  16. server.resetHandlers();
  17. });
  18. function provider(overrides: Partial<NotificationProvider>): NotificationProvider {
  19. return {
  20. id: 7,
  21. name: 'Saved',
  22. provider_type: 'telegram',
  23. enabled: true,
  24. config: { bot_token: PLACEHOLDER, chat_id: '42' },
  25. on_print_start: false,
  26. on_print_complete: true,
  27. on_print_failed: true,
  28. on_print_stopped: true,
  29. on_print_progress: false,
  30. on_print_missing_spool_assignment: false,
  31. on_printer_offline: false,
  32. on_printer_error: false,
  33. on_ai_failure_detection: false,
  34. on_filament_low: false,
  35. on_maintenance_due: false,
  36. on_ams_humidity_high: false,
  37. on_ams_temperature_high: false,
  38. on_ams_ht_humidity_high: false,
  39. on_ams_ht_temperature_high: false,
  40. on_plate_not_empty: true,
  41. on_plate_clear_required: false,
  42. on_bed_cooled: false,
  43. on_first_layer_complete: false,
  44. on_queue_job_added: false,
  45. on_queue_job_assigned: false,
  46. on_queue_job_started: false,
  47. on_queue_job_waiting: true,
  48. on_queue_job_skipped: true,
  49. on_queue_job_failed: true,
  50. on_queue_completed: false,
  51. on_stock_reorder_alert: false,
  52. on_stock_break_alert: false,
  53. quiet_hours_enabled: false,
  54. quiet_hours_start: null,
  55. quiet_hours_end: null,
  56. daily_digest_enabled: false,
  57. daily_digest_time: null,
  58. printer_id: null,
  59. last_success: null,
  60. last_error: null,
  61. last_error_at: null,
  62. created_at: '2026-10-13T00:00:00Z',
  63. updated_at: '2026-10-13T00:00:00Z',
  64. ...overrides,
  65. };
  66. }
  67. describe('AddNotificationModal saved secrets', () => {
  68. it('tests an edited provider by its id, with the placeholder as it came', async () => {
  69. let body: Record<string, unknown> | null = null;
  70. server.use(
  71. http.post('*/api/v1/notifications/test-config', async ({ request }) => {
  72. body = (await request.json()) as Record<string, unknown>;
  73. return HttpResponse.json({ success: true, message: 'ok' });
  74. }),
  75. );
  76. const user = userEvent.setup();
  77. render(<AddNotificationModal provider={provider({})} onClose={() => undefined} />);
  78. await user.click(await screen.findByRole('button', { name: /test configuration/i }));
  79. await waitFor(() => expect(body).not.toBeNull());
  80. expect(body).toMatchObject({ provider_id: 7, config: { bot_token: PLACEHOLDER, chat_id: '42' } });
  81. });
  82. it('tests a new provider without an id', async () => {
  83. let body: Record<string, unknown> | null = null;
  84. server.use(
  85. http.post('*/api/v1/notifications/test-config', async ({ request }) => {
  86. body = (await request.json()) as Record<string, unknown>;
  87. return HttpResponse.json({ success: true, message: 'ok' });
  88. }),
  89. );
  90. const user = userEvent.setup();
  91. render(<AddNotificationModal onClose={() => undefined} />);
  92. // A new provider starts as email: fill its required fields.
  93. await user.type(await screen.findByLabelText(/smtp server/i), 'smtp.example.com');
  94. await user.type(screen.getByLabelText(/from email/i), 'a@example.com');
  95. await user.type(screen.getByLabelText(/to email/i), 'b@example.com');
  96. await user.click(screen.getByRole('button', { name: /test configuration/i }));
  97. await waitFor(() => expect(body).not.toBeNull());
  98. expect(body).not.toHaveProperty('provider_id');
  99. });
  100. it('says a saved secret is kept, and not for a value being entered', async () => {
  101. const user = userEvent.setup();
  102. render(<AddNotificationModal provider={provider({})} onClose={() => undefined} />);
  103. expect(await screen.findByText(/leave it as is to keep it/i)).toBeInTheDocument();
  104. const token = screen.getByLabelText(/bot token/i);
  105. await user.clear(token);
  106. await user.type(token, 'new-token');
  107. expect(screen.queryByText(/leave it as is to keep it/i)).not.toBeInTheDocument();
  108. });
  109. it('replaces the placeholder with what is typed instead of adding to it', async () => {
  110. const user = userEvent.setup();
  111. render(<AddNotificationModal provider={provider({})} onClose={() => undefined} />);
  112. const token = await screen.findByLabelText(/bot token/i);
  113. await user.click(token);
  114. await user.keyboard('fresh');
  115. expect(token).toHaveValue('fresh');
  116. });
  117. it('sends an empty saved value back empty, not as the text null', async () => {
  118. let body: Record<string, unknown> | null = null;
  119. server.use(
  120. http.post('*/api/v1/notifications/test-config', async ({ request }) => {
  121. body = (await request.json()) as Record<string, unknown>;
  122. return HttpResponse.json({ success: true, message: 'ok' });
  123. }),
  124. );
  125. const user = userEvent.setup();
  126. render(
  127. <AddNotificationModal
  128. provider={provider({ provider_type: 'ntfy', config: { server: null, topic: 't', auth_token: PLACEHOLDER } })}
  129. onClose={() => undefined}
  130. />,
  131. );
  132. await user.click(await screen.findByRole('button', { name: /test configuration/i }));
  133. await waitFor(() => expect(body).not.toBeNull());
  134. expect((body as unknown as { config: Record<string, unknown> }).config.server).toBe('');
  135. });
  136. it.each([
  137. ['discord', { webhook_url: PLACEHOLDER }, /webhook url/i],
  138. ['webhook', { webhook_url: PLACEHOLDER, auth_header: '' }, /webhook url/i],
  139. ['pushover', { user_key: PLACEHOLDER, app_token: PLACEHOLDER }, /user key/i],
  140. ['pushover', { user_key: PLACEHOLDER, app_token: PLACEHOLDER }, /app token/i],
  141. ['callmebot', { phone: '+1', apikey: PLACEHOLDER }, /api key/i],
  142. ['bark', { device_key: PLACEHOLDER }, /device key/i],
  143. ] as const)('hides the %s secret as a password field', async (type, config, label) => {
  144. render(
  145. <AddNotificationModal
  146. provider={provider({ provider_type: type, config: { ...config } })}
  147. onClose={() => undefined}
  148. />,
  149. );
  150. expect(await screen.findByLabelText(label)).toHaveAttribute('type', 'password');
  151. });
  152. });