FailureDetectionSettingsSecrets.test.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * The saved Obico token comes back as a placeholder. Kept, it is not sent to a
  3. * new URL: changing the URL holds the auto-save until the token is entered
  4. * again, and the field says so.
  5. */
  6. import { describe, it, expect, beforeEach } 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 { FailureDetectionSettings } from '../../components/FailureDetectionSettings';
  13. const M = '********';
  14. const settings = {
  15. obico_enabled: true,
  16. obico_ml_url: 'http://obico.example:3333',
  17. bambuddy_internal_url: '',
  18. obico_ml_token: M,
  19. obico_sensitivity: 'medium',
  20. obico_action: 'notify',
  21. obico_poll_interval: 10,
  22. obico_enabled_printers: '',
  23. };
  24. describe('FailureDetectionSettings saved token', () => {
  25. let saves: Record<string, unknown>[];
  26. beforeEach(() => {
  27. saves = [];
  28. server.use(
  29. http.get('/api/v1/settings/', () => HttpResponse.json(settings)),
  30. http.put('/api/v1/settings/', async ({ request }) => {
  31. const body = (await request.json()) as Record<string, unknown>;
  32. saves.push(body);
  33. return HttpResponse.json({ ...settings, ...body });
  34. }),
  35. http.get('/api/v1/obico/status', () =>
  36. HttpResponse.json({ is_running: true, last_error: null, per_printer: {}, thresholds: { low: 0.38, high: 0.78 }, history: [], enabled: true, ml_url: '', sensitivity: 'medium', action: 'notify', poll_interval: 10 }),
  37. ),
  38. http.get('/api/v1/printers', () => HttpResponse.json([])),
  39. );
  40. });
  41. it('holds the save and asks for the token again when the URL changes', async () => {
  42. const user = userEvent.setup();
  43. render(<FailureDetectionSettings />);
  44. expect(await screen.findByText(/leave it as is to keep it/i)).toBeInTheDocument();
  45. const url = screen.getByDisplayValue('http://obico.example:3333');
  46. await user.clear(url);
  47. await user.type(url, 'http://obico.elsewhere.example:3333');
  48. expect(await screen.findByText(/the address changed: enter it again/i)).toBeInTheDocument();
  49. await new Promise((resolve) => setTimeout(resolve, 1000));
  50. expect(saves).toEqual([]);
  51. const token = screen.getByPlaceholderText('Leave empty if the server runs without ML_API_TOKEN');
  52. await user.click(token);
  53. await user.keyboard('new-token');
  54. await waitFor(() => expect(saves.length).toBeGreaterThan(0), { timeout: 3000 });
  55. expect(saves.at(-1)).toMatchObject({ obico_ml_url: 'http://obico.elsewhere.example:3333', obico_ml_token: 'new-token' });
  56. });
  57. });