AuthContext.test.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * Tests for the AuthContext permission helpers.
  3. */
  4. import { describe, it, expect, beforeEach } from 'vitest';
  5. import { renderHook, waitFor } from '@testing-library/react';
  6. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  7. import { BrowserRouter } from 'react-router-dom';
  8. import { http, HttpResponse } from 'msw';
  9. import { server } from '../mocks/server';
  10. import { AuthProvider, useAuth } from '../../contexts/AuthContext';
  11. import { ThemeProvider } from '../../contexts/ThemeContext';
  12. import { ToastProvider } from '../../contexts/ToastContext';
  13. import type { Permission } from '../../api/client';
  14. function createWrapper() {
  15. const queryClient = new QueryClient({
  16. defaultOptions: {
  17. queries: { retry: false },
  18. },
  19. });
  20. return function Wrapper({ children }: { children: React.ReactNode }) {
  21. return (
  22. <QueryClientProvider client={queryClient}>
  23. <BrowserRouter>
  24. <ThemeProvider>
  25. <ToastProvider>
  26. <AuthProvider>{children}</AuthProvider>
  27. </ToastProvider>
  28. </ThemeProvider>
  29. </BrowserRouter>
  30. </QueryClientProvider>
  31. );
  32. };
  33. }
  34. describe('AuthContext', () => {
  35. describe('when auth is disabled', () => {
  36. beforeEach(() => {
  37. server.use(
  38. http.get('/api/v1/auth/status', () => {
  39. return HttpResponse.json({
  40. auth_enabled: false,
  41. requires_setup: false,
  42. });
  43. })
  44. );
  45. });
  46. it('authEnabled is false', async () => {
  47. const { result } = renderHook(() => useAuth(), {
  48. wrapper: createWrapper(),
  49. });
  50. await waitFor(() => {
  51. expect(result.current.authEnabled).toBe(false);
  52. });
  53. });
  54. it('hasPermission returns true for any permission when auth disabled', async () => {
  55. const { result } = renderHook(() => useAuth(), {
  56. wrapper: createWrapper(),
  57. });
  58. await waitFor(() => {
  59. expect(result.current.authEnabled).toBe(false);
  60. });
  61. // When auth is disabled, all permissions should be granted
  62. expect(result.current.hasPermission('printers:read' as Permission)).toBe(true);
  63. expect(result.current.hasPermission('settings:update' as Permission)).toBe(true);
  64. expect(result.current.hasPermission('users:delete' as Permission)).toBe(true);
  65. });
  66. it('hasAnyPermission returns true for any permissions when auth disabled', async () => {
  67. const { result } = renderHook(() => useAuth(), {
  68. wrapper: createWrapper(),
  69. });
  70. await waitFor(() => {
  71. expect(result.current.authEnabled).toBe(false);
  72. });
  73. expect(
  74. result.current.hasAnyPermission('printers:read' as Permission, 'settings:update' as Permission)
  75. ).toBe(true);
  76. });
  77. it('hasAllPermissions returns true for any permissions when auth disabled', async () => {
  78. const { result } = renderHook(() => useAuth(), {
  79. wrapper: createWrapper(),
  80. });
  81. await waitFor(() => {
  82. expect(result.current.authEnabled).toBe(false);
  83. });
  84. expect(
  85. result.current.hasAllPermissions('printers:read' as Permission, 'settings:update' as Permission)
  86. ).toBe(true);
  87. });
  88. });
  89. describe('when auth requires setup', () => {
  90. beforeEach(() => {
  91. server.use(
  92. http.get('/api/v1/auth/status', () => {
  93. return HttpResponse.json({
  94. auth_enabled: false,
  95. requires_setup: true,
  96. });
  97. })
  98. );
  99. });
  100. it('requiresSetup is true', async () => {
  101. const { result } = renderHook(() => useAuth(), {
  102. wrapper: createWrapper(),
  103. });
  104. await waitFor(() => {
  105. expect(result.current.requiresSetup).toBe(true);
  106. });
  107. });
  108. });
  109. describe('when auth is enabled but not logged in', () => {
  110. beforeEach(() => {
  111. // Clear any stored token
  112. localStorage.removeItem('auth_token');
  113. server.use(
  114. http.get('/api/v1/auth/status', () => {
  115. return HttpResponse.json({
  116. auth_enabled: true,
  117. requires_setup: false,
  118. });
  119. })
  120. );
  121. });
  122. it('user is null when not logged in', async () => {
  123. const { result } = renderHook(() => useAuth(), {
  124. wrapper: createWrapper(),
  125. });
  126. await waitFor(() => {
  127. expect(result.current.authEnabled).toBe(true);
  128. });
  129. // User should be null when not logged in
  130. expect(result.current.user).toBeNull();
  131. });
  132. it('hasPermission returns false when not logged in', async () => {
  133. const { result } = renderHook(() => useAuth(), {
  134. wrapper: createWrapper(),
  135. });
  136. await waitFor(() => {
  137. expect(result.current.authEnabled).toBe(true);
  138. });
  139. // Without a user, permissions should be denied
  140. expect(result.current.hasPermission('printers:read' as Permission)).toBe(false);
  141. });
  142. });
  143. describe('CVE-2026-25505 fix: auth disabled grants all access', () => {
  144. beforeEach(() => {
  145. server.use(
  146. http.get('/api/v1/auth/status', () => {
  147. return HttpResponse.json({
  148. auth_enabled: false,
  149. requires_setup: false,
  150. });
  151. })
  152. );
  153. });
  154. it('isAdmin is true when auth is disabled', async () => {
  155. const { result } = renderHook(() => useAuth(), {
  156. wrapper: createWrapper(),
  157. });
  158. await waitFor(() => {
  159. expect(result.current.loading).toBe(false);
  160. });
  161. // When auth disabled, user is treated as admin
  162. expect(result.current.isAdmin).toBe(true);
  163. });
  164. it('canModify allows all modifications when auth disabled', async () => {
  165. const { result } = renderHook(() => useAuth(), {
  166. wrapper: createWrapper(),
  167. });
  168. await waitFor(() => {
  169. expect(result.current.loading).toBe(false);
  170. });
  171. // All canModify checks should pass when auth is disabled
  172. expect(result.current.canModify('queue', 'update', 1)).toBe(true);
  173. expect(result.current.canModify('queue', 'update', 999)).toBe(true);
  174. expect(result.current.canModify('queue', 'update', null)).toBe(true);
  175. expect(result.current.canModify('archives', 'delete', 1)).toBe(true);
  176. expect(result.current.canModify('library', 'update', null)).toBe(true);
  177. });
  178. it('all permissions are granted when auth is disabled', async () => {
  179. const { result } = renderHook(() => useAuth(), {
  180. wrapper: createWrapper(),
  181. });
  182. await waitFor(() => {
  183. expect(result.current.loading).toBe(false);
  184. });
  185. // All permission checks should pass
  186. expect(result.current.hasPermission('archives:read' as Permission)).toBe(true);
  187. expect(result.current.hasPermission('archives:delete_all' as Permission)).toBe(true);
  188. expect(result.current.hasPermission('settings:update' as Permission)).toBe(true);
  189. expect(result.current.hasPermission('api_keys:create' as Permission)).toBe(true);
  190. expect(result.current.hasPermission('groups:delete' as Permission)).toBe(true);
  191. });
  192. it('hasAnyPermission returns true for protected permissions', async () => {
  193. const { result } = renderHook(() => useAuth(), {
  194. wrapper: createWrapper(),
  195. });
  196. await waitFor(() => {
  197. expect(result.current.loading).toBe(false);
  198. });
  199. expect(
  200. result.current.hasAnyPermission(
  201. 'api_keys:create' as Permission,
  202. 'groups:delete' as Permission
  203. )
  204. ).toBe(true);
  205. });
  206. it('hasAllPermissions returns true for any combination', async () => {
  207. const { result } = renderHook(() => useAuth(), {
  208. wrapper: createWrapper(),
  209. });
  210. await waitFor(() => {
  211. expect(result.current.loading).toBe(false);
  212. });
  213. expect(
  214. result.current.hasAllPermissions(
  215. 'settings:update' as Permission,
  216. 'api_keys:create' as Permission,
  217. 'groups:delete' as Permission
  218. )
  219. ).toBe(true);
  220. });
  221. });
  222. });