|
| 1 | +// Lock the action-redirect routing (#620). `navigate` for SPA-internal |
| 2 | +// paths, `window.location.assign` (injected as `assignLocation` for |
| 3 | +// testability) for everything else. The legacy behaviour piped every |
| 4 | +// redirect through `navigate` — which silently no-op'd for any URL |
| 5 | +// outside the SPA mount. |
| 6 | +import { describe, expect, it, vi } from 'vitest'; |
| 7 | + |
| 8 | +import { followActionRedirect } from './action-redirect'; |
| 9 | + |
| 10 | +const MOUNT = '/admin-react/'; |
| 11 | +const ORIGIN = 'http://localhost:3000'; |
| 12 | + |
| 13 | +function makeArgs(redirect: string) { |
| 14 | + const navigate = vi.fn(); |
| 15 | + const assignLocation = vi.fn(); |
| 16 | + return { |
| 17 | + args: { redirect, mount: MOUNT, navigate, currentOrigin: ORIGIN, assignLocation }, |
| 18 | + navigate, |
| 19 | + assignLocation, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +describe('followActionRedirect', () => { |
| 24 | + it('uses navigate for a same-origin path inside the SPA mount', () => { |
| 25 | + const { args, navigate, assignLocation } = makeArgs('/admin-react/auth/user/42/'); |
| 26 | + followActionRedirect(args); |
| 27 | + // The mount prefix is stripped so BrowserRouter's basename |
| 28 | + // doesn't double up. Trailing slash before the relative path is |
| 29 | + // preserved. |
| 30 | + expect(navigate).toHaveBeenCalledWith('/auth/user/42/'); |
| 31 | + expect(assignLocation).not.toHaveBeenCalled(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('preserves search + hash when navigating client-side', () => { |
| 35 | + const { args, navigate } = makeArgs('/admin-react/auth/user/42/?tab=audit#log'); |
| 36 | + followActionRedirect(args); |
| 37 | + expect(navigate).toHaveBeenCalledWith('/auth/user/42/?tab=audit#log'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('falls back to assignLocation for a same-origin path OUTSIDE the mount', () => { |
| 41 | + // Legacy admin path — must be a full browser navigation since |
| 42 | + // React Router only routes within the SPA mount. |
| 43 | + const { args, navigate, assignLocation } = makeArgs('/admin/auth/user/42/change/'); |
| 44 | + followActionRedirect(args); |
| 45 | + expect(assignLocation).toHaveBeenCalledWith('/admin/auth/user/42/change/'); |
| 46 | + expect(navigate).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('falls back to assignLocation for cross-origin URLs', () => { |
| 50 | + // The signed-S3-download case — must be a real browser navigation |
| 51 | + // so the download starts on the operator's machine. |
| 52 | + const { args, navigate, assignLocation } = makeArgs('https://s3.example.com/signed/file.pdf'); |
| 53 | + followActionRedirect(args); |
| 54 | + expect(assignLocation).toHaveBeenCalledWith('https://s3.example.com/signed/file.pdf'); |
| 55 | + expect(navigate).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('falls back to assignLocation for a hijack-style /hijack/... URL', () => { |
| 59 | + // Common third-party pattern (`django-hijack`) — action returns |
| 60 | + // `HttpResponseRedirect("/hijack/release-user/?next=...")`. |
| 61 | + const { args, navigate, assignLocation } = makeArgs('/hijack/release-user/?next=/admin/foo/'); |
| 62 | + followActionRedirect(args); |
| 63 | + expect(assignLocation).toHaveBeenCalledWith('/hijack/release-user/?next=/admin/foo/'); |
| 64 | + expect(navigate).not.toHaveBeenCalled(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('falls back to assignLocation on an unparseable URL', () => { |
| 68 | + // A malformed input shouldn't make the operator's click disappear |
| 69 | + // into the void — surface it as a real navigation and let the |
| 70 | + // browser report the failure to the user. |
| 71 | + // |
| 72 | + // Using "http://" alone — bare scheme with no authority — produces |
| 73 | + // a TypeError in the URL constructor on all majors. |
| 74 | + const { args, navigate, assignLocation } = makeArgs('http://'); |
| 75 | + followActionRedirect(args); |
| 76 | + expect(assignLocation).toHaveBeenCalledWith('http://'); |
| 77 | + expect(navigate).not.toHaveBeenCalled(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments