|
| 1 | +import type { SanitizedConfig } from 'payload' |
| 2 | + |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | + |
| 5 | +import { isCustomAdminView } from './isCustomAdminView.js' |
| 6 | + |
| 7 | +describe('isCustomAdminView', () => { |
| 8 | + const adminRoute = '/admin' |
| 9 | + |
| 10 | + const configWithCustomDashboard = { |
| 11 | + admin: { |
| 12 | + components: { |
| 13 | + views: { |
| 14 | + dashboard: { |
| 15 | + path: '/', |
| 16 | + }, |
| 17 | + }, |
| 18 | + }, |
| 19 | + }, |
| 20 | + } as unknown as SanitizedConfig |
| 21 | + |
| 22 | + it('should not bypass admin access for the root admin route even with a custom dashboard view', () => { |
| 23 | + // The root path '/' maps to the dashboard — it is not a public custom view. |
| 24 | + // canAccessAdmin must still be enforced for the admin root. |
| 25 | + const result = isCustomAdminView({ |
| 26 | + adminRoute, |
| 27 | + config: configWithCustomDashboard, |
| 28 | + route: '/admin', |
| 29 | + }) |
| 30 | + |
| 31 | + expect(result).toBe(false) |
| 32 | + }) |
| 33 | + |
| 34 | + it('should not match collection routes as custom dashboard views when dashboard path is "/"', () => { |
| 35 | + const result = isCustomAdminView({ |
| 36 | + adminRoute, |
| 37 | + config: configWithCustomDashboard, |
| 38 | + route: '/admin/collections/tickets', |
| 39 | + }) |
| 40 | + |
| 41 | + expect(result).toBe(false) |
| 42 | + }) |
| 43 | + |
| 44 | + it('should not match document routes as custom dashboard views when dashboard path is "/"', () => { |
| 45 | + const result = isCustomAdminView({ |
| 46 | + adminRoute, |
| 47 | + config: configWithCustomDashboard, |
| 48 | + route: '/admin/collections/tickets/123', |
| 49 | + }) |
| 50 | + |
| 51 | + expect(result).toBe(false) |
| 52 | + }) |
| 53 | + |
| 54 | + it('should return false when no custom views are configured', () => { |
| 55 | + const config = {} as SanitizedConfig |
| 56 | + |
| 57 | + const result = isCustomAdminView({ |
| 58 | + adminRoute, |
| 59 | + config, |
| 60 | + route: '/admin/collections/tickets', |
| 61 | + }) |
| 62 | + |
| 63 | + expect(result).toBe(false) |
| 64 | + }) |
| 65 | + |
| 66 | + it('should return true for a custom view with a non-root path', () => { |
| 67 | + const config = { |
| 68 | + admin: { |
| 69 | + components: { |
| 70 | + views: { |
| 71 | + myCustomView: { |
| 72 | + path: '/my-custom-view', |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } as unknown as SanitizedConfig |
| 78 | + |
| 79 | + const result = isCustomAdminView({ |
| 80 | + adminRoute, |
| 81 | + config, |
| 82 | + route: '/admin/my-custom-view', |
| 83 | + }) |
| 84 | + |
| 85 | + expect(result).toBe(true) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should not match collection routes when a non-root custom view path is set', () => { |
| 89 | + const config = { |
| 90 | + admin: { |
| 91 | + components: { |
| 92 | + views: { |
| 93 | + myCustomView: { |
| 94 | + path: '/my-custom-view', |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + } as unknown as SanitizedConfig |
| 100 | + |
| 101 | + const result = isCustomAdminView({ |
| 102 | + adminRoute, |
| 103 | + config, |
| 104 | + route: '/admin/collections/tickets', |
| 105 | + }) |
| 106 | + |
| 107 | + expect(result).toBe(false) |
| 108 | + }) |
| 109 | +}) |
0 commit comments