ConnectSlicerPage.test.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Connect slicer (#3602): what a user without Settings access enters in their
  3. * slicer, the account menu that leads there and the admin's instructions.
  4. */
  5. import { describe, it, expect, afterEach, vi } from 'vitest';
  6. import { screen, waitFor } from '@testing-library/react';
  7. import userEvent from '@testing-library/user-event';
  8. import { http, HttpResponse } from 'msw';
  9. import { render } from '../utils';
  10. import { server } from '../mocks/server';
  11. import { ConnectSlicerPage } from '../../pages/ConnectSlicerPage';
  12. import { AccountMenu } from '../../components/AccountMenu';
  13. import { ConnectSlicerInstructionsCard } from '../../components/ConnectSlicerInstructionsCard';
  14. import { setAuthToken, type VPConnectInfo } from '../../api/client';
  15. function signIn(permissions: string[] = ['library:upload'], authEnabled = true) {
  16. setAuthToken('test-token-1', 'session');
  17. server.use(
  18. http.get('*/api/v1/auth/status', () => HttpResponse.json({ auth_enabled: authEnabled, requires_setup: false })),
  19. http.get('*/api/v1/auth/me', () =>
  20. HttpResponse.json({
  21. id: 9,
  22. username: 'student',
  23. is_active: true,
  24. is_admin: false,
  25. groups: [],
  26. permissions,
  27. created_at: '2026-01-01T00:00:00Z',
  28. }),
  29. ),
  30. http.get('*/api/v1/virtual-printers/my-access-code', () => HttpResponse.json({ code: '12345678' })),
  31. http.get('*/api/v1/virtual-printers/ca-certificate', () =>
  32. HttpResponse.json({ pem: '-----BEGIN CERTIFICATE-----', fingerprint_sha256: 'AA:BB', not_valid_after: '2036-01-01' }),
  33. ),
  34. );
  35. }
  36. function connectInfo(info: Partial<VPConnectInfo>) {
  37. server.use(
  38. http.get('*/api/v1/virtual-printers/connect-info', () =>
  39. HttpResponse.json({ auth_enabled: true, instructions: '', printers: [], ...info }),
  40. ),
  41. );
  42. }
  43. const PRINTER = {
  44. model: 'BL-P001',
  45. model_name: 'X1C',
  46. serial: '00M09A391800001',
  47. target_printer_name: null,
  48. running: true,
  49. };
  50. describe('ConnectSlicerPage', () => {
  51. afterEach(() => setAuthToken(null));
  52. it('shows what to enter for each virtual printer the user may send to', async () => {
  53. signIn();
  54. connectInfo({
  55. printers: [
  56. { ...PRINTER, id: 1, name: 'Files', mode: 'library', address: '10.0.0.5' },
  57. { ...PRINTER, id: 2, name: 'Lab queue', mode: 'queue', address: '10.0.0.6', target_printer_name: 'Lab X1C', running: false },
  58. { ...PRINTER, id: 3, name: 'Any X1C', mode: 'queue', address: '10.0.0.7' },
  59. ],
  60. });
  61. render(<ConnectSlicerPage />);
  62. expect(await screen.findByText('Files')).toBeInTheDocument();
  63. expect(screen.getByText('10.0.0.5')).toBeInTheDocument();
  64. expect(screen.getAllByText('00M09A391800001')).toHaveLength(3);
  65. expect(screen.getByText('Files you send are saved to the File Manager.')).toBeInTheDocument();
  66. expect(screen.getByText('Files you send are queued for Lab X1C.')).toBeInTheDocument();
  67. expect(screen.getByText('Files you send are queued for the first free X1C.')).toBeInTheDocument();
  68. expect(screen.getByText('Your slicer shows the live status of Lab X1C.')).toBeInTheDocument();
  69. expect(screen.getAllByText('Not running')).toHaveLength(1);
  70. // The user's own code is on the page.
  71. expect(await screen.findByTestId('slicer-access-code')).toBeInTheDocument();
  72. });
  73. it('says so when there is nothing to send to', async () => {
  74. signIn();
  75. connectInfo({ printers: [] });
  76. render(<ConnectSlicerPage />);
  77. expect(await screen.findByText('There is no virtual printer you can send to. Ask your administrator.')).toBeInTheDocument();
  78. });
  79. it("renders the admin's instructions as markdown, without raw HTML", async () => {
  80. signIn();
  81. connectInfo({ instructions: '**Connect to the VPN** first <img src="x" alt="injected">' });
  82. render(<ConnectSlicerPage />);
  83. const instructions = await screen.findByTestId('connect-slicer-instructions');
  84. expect(instructions.querySelector('strong')?.textContent).toBe('Connect to the VPN');
  85. expect(instructions.querySelector('img')).toBeNull();
  86. });
  87. it('with auth off, points to the virtual printer settings instead of a personal code', async () => {
  88. signIn(['settings:read'], false);
  89. connectInfo({ auth_enabled: false });
  90. render(<ConnectSlicerPage />);
  91. expect(await screen.findByText(/Personal access codes need user authentication/)).toBeInTheDocument();
  92. expect(screen.getByRole('link', { name: 'Open Virtual Printer settings' })).toHaveAttribute(
  93. 'href',
  94. '/settings?tab=virtual-printer',
  95. );
  96. expect(screen.queryByTestId('slicer-access-code')).toBeNull();
  97. });
  98. });
  99. describe('AccountMenu', () => {
  100. it('leads to the password change and to the Connect slicer page', async () => {
  101. const onChangePassword = vi.fn();
  102. const user = userEvent.setup();
  103. render(<AccountMenu username="student" onChangePassword={onChangePassword} />);
  104. await user.click(screen.getByRole('button', { name: 'Account' }));
  105. expect(screen.getByRole('menu')).toHaveTextContent('student');
  106. await user.click(screen.getByRole('menuitem', { name: 'Change Password' }));
  107. expect(onChangePassword).toHaveBeenCalledOnce();
  108. expect(screen.queryByRole('menu')).toBeNull();
  109. await user.click(screen.getByRole('button', { name: 'Account' }));
  110. await user.click(screen.getByRole('menuitem', { name: 'Connect slicer' }));
  111. expect(window.location.pathname).toBe('/connect-slicer');
  112. });
  113. it('closes on Escape', async () => {
  114. const user = userEvent.setup();
  115. render(<AccountMenu username="student" onChangePassword={() => {}} />);
  116. await user.click(screen.getByRole('button', { name: 'Account' }));
  117. await user.keyboard('{Escape}');
  118. expect(screen.queryByRole('menu')).toBeNull();
  119. });
  120. });
  121. describe('ConnectSlicerInstructionsCard', () => {
  122. afterEach(() => setAuthToken(null));
  123. it('saves the instructions as a setting', async () => {
  124. signIn(['settings:read', 'settings:update']);
  125. let saved: unknown = null;
  126. server.use(
  127. http.put('*/api/v1/settings/', async ({ request }) => {
  128. saved = await request.json();
  129. return HttpResponse.json({});
  130. }),
  131. );
  132. const user = userEvent.setup();
  133. render(<ConnectSlicerInstructionsCard />);
  134. const box = await screen.findByLabelText('Instructions for users');
  135. await waitFor(() => expect(box).not.toBeDisabled());
  136. await user.type(box, 'VPN first');
  137. await user.click(screen.getByRole('button', { name: 'Save' }));
  138. await waitFor(() => expect(saved).toEqual({ virtual_printer_connect_instructions: 'VPN first' }));
  139. });
  140. it('is read-only without settings:update', async () => {
  141. signIn(['settings:read']);
  142. render(<ConnectSlicerInstructionsCard />);
  143. await waitFor(() => expect(screen.getByLabelText('Instructions for users')).toBeDisabled());
  144. expect(screen.queryByRole('button', { name: 'Save' })).toBeNull();
  145. });
  146. });