|
| 1 | +import { act, renderHook, waitFor } from '@testing-library/react'; |
| 2 | +import { beforeEach, describe, expect, it, jest } from '@jest/globals'; |
| 3 | +import { useEmbeddedLinkInterceptor } from './useEmbeddedLinkInterceptor'; |
| 4 | +import { isIntegrated } from '../utils/embedUtils'; |
| 5 | + |
| 6 | +jest.mock('../utils/embedUtils', () => ({ |
| 7 | + isIntegrated: jest.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +const GUARDED_LINK_ATTR = 'data-mlflow-embedded-link-guard'; |
| 11 | + |
| 12 | +const createAnchor = (href: string) => { |
| 13 | + const anchor = document.createElement('a'); |
| 14 | + anchor.href = href; |
| 15 | + anchor.textContent = href; |
| 16 | + return anchor; |
| 17 | +}; |
| 18 | + |
| 19 | +const createFederatedWrapper = () => { |
| 20 | + const wrapper = document.createElement('div'); |
| 21 | + wrapper.className = 'mlflow-federated'; |
| 22 | + document.body.appendChild(wrapper); |
| 23 | + return wrapper; |
| 24 | +}; |
| 25 | + |
| 26 | +const createFederatedPortalRoot = () => { |
| 27 | + const portalRoot = document.createElement('div'); |
| 28 | + portalRoot.setAttribute('data-radix-popper-content-wrapper', ''); |
| 29 | + document.body.appendChild(portalRoot); |
| 30 | + return portalRoot; |
| 31 | +}; |
| 32 | + |
| 33 | +const createFederatedPortalContainer = () => { |
| 34 | + const portalContainer = document.createElement('div'); |
| 35 | + portalContainer.setAttribute('data-mlflow-federated-portal-container', 'true'); |
| 36 | + document.body.appendChild(portalContainer); |
| 37 | + return portalContainer; |
| 38 | +}; |
| 39 | + |
| 40 | +const createMlflowOwnedMarker = () => { |
| 41 | + const marker = document.createElement('div'); |
| 42 | + marker.setAttribute('data-component-id', 'mlflow.test.portal'); |
| 43 | + return marker; |
| 44 | +}; |
| 45 | + |
| 46 | +describe('useEmbeddedLinkInterceptor', () => { |
| 47 | + beforeEach(() => { |
| 48 | + jest.clearAllMocks(); |
| 49 | + (isIntegrated as jest.Mock).mockReturnValue(true); |
| 50 | + document.body.innerHTML = ''; |
| 51 | + document.body.setAttribute('data-mlflow-federated', 'true'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('guards restricted links inside the federated wrapper on mount', async () => { |
| 55 | + const wrapper = createFederatedWrapper(); |
| 56 | + const restrictedLink = createAnchor('/models/test-model'); |
| 57 | + const allowedLink = createAnchor('/experiments/23/models/test-model'); |
| 58 | + |
| 59 | + wrapper.append(restrictedLink, allowedLink); |
| 60 | + |
| 61 | + renderHook(() => useEmbeddedLinkInterceptor()); |
| 62 | + |
| 63 | + await waitFor(() => { |
| 64 | + expect(restrictedLink).toHaveAttribute('tabindex', '-1'); |
| 65 | + }); |
| 66 | + |
| 67 | + expect(restrictedLink).toHaveAttribute(GUARDED_LINK_ATTR, 'true'); |
| 68 | + expect(allowedLink).not.toHaveAttribute(GUARDED_LINK_ATTR); |
| 69 | + }); |
| 70 | + |
| 71 | + it('guards restricted links added after mount', async () => { |
| 72 | + const wrapper = createFederatedWrapper(); |
| 73 | + renderHook(() => useEmbeddedLinkInterceptor()); |
| 74 | + |
| 75 | + const restrictedLink = createAnchor('/models/test-model'); |
| 76 | + act(() => { |
| 77 | + wrapper.appendChild(restrictedLink); |
| 78 | + }); |
| 79 | + |
| 80 | + await waitFor(() => { |
| 81 | + expect(restrictedLink).toHaveAttribute(GUARDED_LINK_ATTR, 'true'); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('restores guarded links when their href becomes allowed', async () => { |
| 86 | + const wrapper = createFederatedWrapper(); |
| 87 | + const restrictedLink = createAnchor('/models/test-model'); |
| 88 | + wrapper.appendChild(restrictedLink); |
| 89 | + |
| 90 | + renderHook(() => useEmbeddedLinkInterceptor()); |
| 91 | + |
| 92 | + await waitFor(() => { |
| 93 | + expect(restrictedLink).toHaveAttribute(GUARDED_LINK_ATTR, 'true'); |
| 94 | + }); |
| 95 | + |
| 96 | + act(() => { |
| 97 | + restrictedLink.setAttribute('href', '/experiments/23/models/test-model'); |
| 98 | + }); |
| 99 | + |
| 100 | + await waitFor(() => { |
| 101 | + expect(restrictedLink).not.toHaveAttribute(GUARDED_LINK_ATTR); |
| 102 | + }); |
| 103 | + |
| 104 | + expect(restrictedLink).not.toHaveAttribute('tabindex'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('restores guarded links when the federated wrapper loses scope', async () => { |
| 108 | + const wrapper = createFederatedWrapper(); |
| 109 | + const restrictedLink = createAnchor('/models/test-model'); |
| 110 | + wrapper.appendChild(restrictedLink); |
| 111 | + |
| 112 | + renderHook(() => useEmbeddedLinkInterceptor()); |
| 113 | + |
| 114 | + await waitFor(() => { |
| 115 | + expect(restrictedLink).toHaveAttribute(GUARDED_LINK_ATTR, 'true'); |
| 116 | + }); |
| 117 | + |
| 118 | + act(() => { |
| 119 | + wrapper.className = ''; |
| 120 | + }); |
| 121 | + |
| 122 | + await waitFor(() => { |
| 123 | + expect(restrictedLink).not.toHaveAttribute(GUARDED_LINK_ATTR); |
| 124 | + }); |
| 125 | + |
| 126 | + expect(restrictedLink).not.toHaveAttribute('tabindex'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('guards restricted links inside MLflow-owned body portals', async () => { |
| 130 | + const portalRoot = createFederatedPortalRoot(); |
| 131 | + const restrictedLink = createAnchor('/models/test-model'); |
| 132 | + |
| 133 | + portalRoot.append(createMlflowOwnedMarker(), restrictedLink); |
| 134 | + |
| 135 | + renderHook(() => useEmbeddedLinkInterceptor()); |
| 136 | + |
| 137 | + await waitFor(() => { |
| 138 | + expect(restrictedLink).toHaveAttribute(GUARDED_LINK_ATTR, 'true'); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it('guards restricted links inside the dedicated federated portal container', async () => { |
| 143 | + const portalContainer = createFederatedPortalContainer(); |
| 144 | + const portalRoot = document.createElement('div'); |
| 145 | + portalRoot.setAttribute('data-radix-popper-content-wrapper', ''); |
| 146 | + const restrictedLink = createAnchor('/models/test-model'); |
| 147 | + |
| 148 | + portalRoot.appendChild(restrictedLink); |
| 149 | + portalContainer.appendChild(portalRoot); |
| 150 | + |
| 151 | + renderHook(() => useEmbeddedLinkInterceptor()); |
| 152 | + |
| 153 | + await waitFor(() => { |
| 154 | + expect(restrictedLink).toHaveAttribute(GUARDED_LINK_ATTR, 'true'); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + it('ignores generic body portals until they become MLflow-owned', async () => { |
| 159 | + const portalRoot = createFederatedPortalRoot(); |
| 160 | + const restrictedLink = createAnchor('/models/test-model'); |
| 161 | + |
| 162 | + portalRoot.appendChild(restrictedLink); |
| 163 | + |
| 164 | + renderHook(() => useEmbeddedLinkInterceptor()); |
| 165 | + |
| 166 | + expect(restrictedLink).not.toHaveAttribute(GUARDED_LINK_ATTR); |
| 167 | + |
| 168 | + act(() => { |
| 169 | + portalRoot.appendChild(createMlflowOwnedMarker()); |
| 170 | + }); |
| 171 | + |
| 172 | + await waitFor(() => { |
| 173 | + expect(restrictedLink).toHaveAttribute(GUARDED_LINK_ATTR, 'true'); |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments