|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import { usePermissions } from '../hooks/use-permissions'; |
| 3 | + |
| 4 | +// Mock @inertiajs/react |
| 5 | +jest.mock('@inertiajs/react', () => ({ |
| 6 | + usePage: jest.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +import { usePage } from '@inertiajs/react'; |
| 10 | +const mockUsePage = usePage as jest.MockedFunction<typeof usePage>; |
| 11 | + |
| 12 | +describe('Verify User Case: properties.view-all||properties.view-own', () => { |
| 13 | + beforeEach(() => { |
| 14 | + jest.clearAllMocks(); |
| 15 | + }); |
| 16 | + |
| 17 | + const mockPageProps = (userPermissions: string[] = []) => { |
| 18 | + mockUsePage.mockReturnValue({ |
| 19 | + props: { |
| 20 | + auth: { |
| 21 | + user: { |
| 22 | + permissions: userPermissions, |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 27 | + } as any); |
| 28 | + }; |
| 29 | + |
| 30 | + it('should work with the exact user case: properties.view-all||properties.view-own', () => { |
| 31 | + // Test case 1: User has both permissions |
| 32 | + mockPageProps(['properties.view-all', 'properties.view-own']); |
| 33 | + let { result } = renderHook(() => usePermissions()); |
| 34 | + |
| 35 | + console.log('\n=== Test Case 1: User has BOTH permissions ==='); |
| 36 | + console.log('User permissions:', ['properties.view-all', 'properties.view-own']); |
| 37 | + console.log('Expression: "properties.view-all||properties.view-own"'); |
| 38 | + |
| 39 | + const result1 = result.current.hasPermission('properties.view-all||properties.view-own'); |
| 40 | + console.log('Result:', result1); |
| 41 | + expect(result1).toBe(true); |
| 42 | + console.log('✅ PASS: Should return true when user has both permissions'); |
| 43 | + |
| 44 | + // Test case 2: User has only first permission |
| 45 | + mockPageProps(['properties.view-all']); |
| 46 | + result = renderHook(() => usePermissions()).result; |
| 47 | + |
| 48 | + console.log('\n=== Test Case 2: User has FIRST permission only ==='); |
| 49 | + console.log('User permissions:', ['properties.view-all']); |
| 50 | + console.log('Expression: "properties.view-all||properties.view-own"'); |
| 51 | + |
| 52 | + const result2 = result.current.hasPermission('properties.view-all||properties.view-own'); |
| 53 | + console.log('Result:', result2); |
| 54 | + expect(result2).toBe(true); |
| 55 | + console.log('✅ PASS: Should return true when user has first permission'); |
| 56 | + |
| 57 | + // Test case 3: User has only second permission |
| 58 | + mockPageProps(['properties.view-own']); |
| 59 | + result = renderHook(() => usePermissions()).result; |
| 60 | + |
| 61 | + console.log('\n=== Test Case 3: User has SECOND permission only ==='); |
| 62 | + console.log('User permissions:', ['properties.view-own']); |
| 63 | + console.log('Expression: "properties.view-all||properties.view-own"'); |
| 64 | + |
| 65 | + const result3 = result.current.hasPermission('properties.view-all||properties.view-own'); |
| 66 | + console.log('Result:', result3); |
| 67 | + expect(result3).toBe(true); |
| 68 | + console.log('✅ PASS: Should return true when user has second permission'); |
| 69 | + |
| 70 | + // Test case 4: User has neither permission |
| 71 | + mockPageProps([]); |
| 72 | + result = renderHook(() => usePermissions()).result; |
| 73 | + |
| 74 | + console.log('\n=== Test Case 4: User has NO permissions ==='); |
| 75 | + console.log('User permissions:', []); |
| 76 | + console.log('Expression: "properties.view-all||properties.view-own"'); |
| 77 | + |
| 78 | + const result4 = result.current.hasPermission('properties.view-all||properties.view-own'); |
| 79 | + console.log('Result:', result4); |
| 80 | + expect(result4).toBe(false); |
| 81 | + console.log('✅ PASS: Should return false when user has no permissions'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should work with different permission combinations', () => { |
| 85 | + // Test with different permission names to ensure the fix is general |
| 86 | + mockPageProps(['users.create', 'posts.view']); |
| 87 | + |
| 88 | + const { result } = renderHook(() => usePermissions()); |
| 89 | + |
| 90 | + console.log('\n=== Test with different permissions ==='); |
| 91 | + console.log('User permissions:', ['users.create', 'posts.view']); |
| 92 | + |
| 93 | + // Test OR expression |
| 94 | + const orResult = result.current.hasPermission('users.create||posts.view'); |
| 95 | + console.log('users.create||posts.view:', orResult); |
| 96 | + expect(orResult).toBe(true); |
| 97 | + |
| 98 | + // Test AND expression |
| 99 | + const andResult = result.current.hasPermission('users.create&&posts.view'); |
| 100 | + console.log('users.create&&posts.view:', andResult); |
| 101 | + expect(andResult).toBe(true); |
| 102 | + |
| 103 | + // Test with one missing permission |
| 104 | + const missingResult = result.current.hasPermission('users.create||posts.delete'); |
| 105 | + console.log('users.create||posts.delete:', missingResult); |
| 106 | + expect(missingResult).toBe(true); |
| 107 | + |
| 108 | + // Test with both missing permissions |
| 109 | + const bothMissingResult = result.current.hasPermission('users.delete||posts.delete'); |
| 110 | + console.log('users.delete||posts.delete:', bothMissingResult); |
| 111 | + expect(bothMissingResult).toBe(false); |
| 112 | + |
| 113 | + console.log('✅ PASS: All different permission combinations work correctly'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should work with hyphenated permission names', () => { |
| 117 | + // Test specifically with hyphenated permissions like the user's case |
| 118 | + mockPageProps(['user-profile.edit', 'api-access.read', 'system-config.update']); |
| 119 | + |
| 120 | + const { result } = renderHook(() => usePermissions()); |
| 121 | + |
| 122 | + console.log('\n=== Test with hyphenated permissions ==='); |
| 123 | + console.log('User permissions:', ['user-profile.edit', 'api-access.read', 'system-config.update']); |
| 124 | + |
| 125 | + // Test OR with hyphens |
| 126 | + const orResult = result.current.hasPermission('user-profile.edit||api-access.read'); |
| 127 | + console.log('user-profile.edit||api-access.read:', orResult); |
| 128 | + expect(orResult).toBe(true); |
| 129 | + |
| 130 | + // Test AND with hyphens |
| 131 | + const andResult = result.current.hasPermission('user-profile.edit&&api-access.read'); |
| 132 | + console.log('user-profile.edit&&api-access.read:', andResult); |
| 133 | + expect(andResult).toBe(true); |
| 134 | + |
| 135 | + // Test complex expression with hyphens |
| 136 | + const complexResult = result.current.hasPermission('(user-profile.edit||api-access.read)&&system-config.update'); |
| 137 | + console.log('(user-profile.edit||api-access.read)&&system-config.update:', complexResult); |
| 138 | + expect(complexResult).toBe(true); |
| 139 | + |
| 140 | + console.log('✅ PASS: Hyphenated permission names work correctly'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should debug the evaluation process step by step', () => { |
| 144 | + mockPageProps(['properties.view-all', 'properties.view-own']); |
| 145 | + |
| 146 | + const { result } = renderHook(() => usePermissions()); |
| 147 | + |
| 148 | + console.log('\n=== Step-by-step debugging ==='); |
| 149 | + console.log('User permissions:', ['properties.view-all', 'properties.view-own']); |
| 150 | + |
| 151 | + const expression = 'properties.view-all||properties.view-own'; |
| 152 | + console.log('Original expression:', expression); |
| 153 | + |
| 154 | + // Let's manually trace through the normalization process |
| 155 | + let jsExpression = expression; |
| 156 | + |
| 157 | + // Step 1: Replace double operators with markers |
| 158 | + jsExpression = jsExpression |
| 159 | + .replace(/\|\|/g, ' DOUBLE_PIPE ') |
| 160 | + .replace(/&&/g, ' DOUBLE_AMP '); |
| 161 | + console.log('After double operator replacement:', jsExpression); |
| 162 | + |
| 163 | + // Step 2: Replace single operators |
| 164 | + jsExpression = jsExpression |
| 165 | + .replace(/\|/g, ' || ') |
| 166 | + .replace(/&/g, ' && '); |
| 167 | + console.log('After single operator replacement:', jsExpression); |
| 168 | + |
| 169 | + // Step 3: Replace markers with proper operators |
| 170 | + jsExpression = jsExpression |
| 171 | + .replace(/DOUBLE_PIPE/g, ' || ') |
| 172 | + .replace(/DOUBLE_AMP/g, ' && '); |
| 173 | + console.log('After marker replacement:', jsExpression); |
| 174 | + |
| 175 | + // Step 4: Clean up spaces |
| 176 | + jsExpression = jsExpression.replace(/\s+/g, ' ').trim(); |
| 177 | + console.log('Final normalized expression:', jsExpression); |
| 178 | + |
| 179 | + // Test the actual function |
| 180 | + const actualResult = result.current.hasPermission(expression); |
| 181 | + console.log('Actual function result:', actualResult); |
| 182 | + |
| 183 | + expect(actualResult).toBe(true); |
| 184 | + console.log('✅ PASS: Step-by-step debugging shows correct evaluation'); |
| 185 | + }); |
| 186 | +}); |
0 commit comments