|
| 1 | +import { snakeToCamel } from '@clerk/shared/underscore'; |
| 2 | +import type { RedirectOptions } from '@clerk/types'; |
| 3 | + |
| 4 | +import { RedirectUrls } from '../redirectUrls'; |
| 5 | + |
| 6 | +const oldWindowLocation = window.location; |
| 7 | + |
| 8 | +describe('redirectUrls', () => { |
| 9 | + let mockWindowLocation: Window['location']; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + mockWindowLocation = new URL('https://www.clerk.com') as any as Window['location']; |
| 13 | + Object.defineProperty(global.window, 'location', { value: mockWindowLocation }); |
| 14 | + }); |
| 15 | + |
| 16 | + afterAll(() => { |
| 17 | + Object.defineProperty(global.window, 'location', { |
| 18 | + value: oldWindowLocation, |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('parse input values', () => { |
| 23 | + it('parses options or props', () => { |
| 24 | + const options: [keyof RedirectOptions, string][] = [ |
| 25 | + ['signInForceRedirectUrl', 'sign-in-force-redirect-url'], |
| 26 | + ['signInFallbackRedirectUrl', 'sign-in-fallback-redirect'], |
| 27 | + ['signUpForceRedirectUrl', 'sign-up-force-redirect'], |
| 28 | + ['signUpFallbackRedirectUrl', 'sign-up-fallback-redirect'], |
| 29 | + ]; |
| 30 | + |
| 31 | + // test options (first param) |
| 32 | + for (const [k, v] of options) { |
| 33 | + const redirectUrls = new RedirectUrls({ [k]: v }); |
| 34 | + // @ts-expect-error ignoring private modifier |
| 35 | + expect(redirectUrls.fromOptions[k]).toBe(`${mockWindowLocation.href}${v}`); |
| 36 | + } |
| 37 | + |
| 38 | + // test props (second param) |
| 39 | + for (const [k, v] of options) { |
| 40 | + const redirectUrls = new RedirectUrls({}, { [k]: v }); |
| 41 | + // @ts-expect-error ignoring private modifier |
| 42 | + expect(redirectUrls.fromProps[k]).toBe(`${mockWindowLocation.href}${v}`); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + it('parses search params', () => { |
| 47 | + const params = [ |
| 48 | + ['sign_in_force_redirect_url', 'sign-in-force-redirect'], |
| 49 | + ['sign_in_fallback_redirect_url', 'sign-in-fallback-redirect'], |
| 50 | + ['sign_up_force_redirect_url', 'sign-up-force-redirect'], |
| 51 | + ['sign_up_fallback_redirect_url', 'sign-up-fallback-redirect'], |
| 52 | + ]; |
| 53 | + |
| 54 | + for (const [key, val] of params) { |
| 55 | + const redirectUrls = new RedirectUrls({}, {}, { [key]: val }); |
| 56 | + // @ts-expect-error ignoring private modifier |
| 57 | + expect(redirectUrls.fromSearchParams[snakeToCamel(key)]).toBe(`${mockWindowLocation.href}${val}`); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + it('filters origins that are not allowed', () => { |
| 62 | + const redirectUrls = new RedirectUrls( |
| 63 | + { |
| 64 | + allowedRedirectOrigins: ['https://www.clerk.com'], |
| 65 | + // This would take priority but its not allowed |
| 66 | + signInForceRedirectUrl: 'https://www.other.com/sign-in-force-redirect-url', |
| 67 | + }, |
| 68 | + { |
| 69 | + // This will be used instead |
| 70 | + signInForceRedirectUrl: 'https://www.clerk.com/sign-in-force-redirect-url', |
| 71 | + }, |
| 72 | + ); |
| 73 | + |
| 74 | + expect(redirectUrls.getAfterSignInUrl()).toBe('https://www.clerk.com/sign-in-force-redirect-url'); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('get redirect urls', () => { |
| 79 | + it('prioritizes force urls among other urls in the same group', () => { |
| 80 | + const redirectUrls = new RedirectUrls({ |
| 81 | + signInForceRedirectUrl: 'sign-in-force-redirect-url', |
| 82 | + signInFallbackRedirectUrl: 'sign-in-fallback-redirect-url', |
| 83 | + signUpForceRedirectUrl: 'sign-up-force-redirect-url', |
| 84 | + signUpFallbackRedirectUrl: 'sign-up-fallback-redirect-url', |
| 85 | + }); |
| 86 | + |
| 87 | + expect(redirectUrls.getAfterSignInUrl()).toBe(`${mockWindowLocation.href}sign-in-force-redirect-url`); |
| 88 | + expect(redirectUrls.getAfterSignUpUrl()).toBe(`${mockWindowLocation.href}sign-up-force-redirect-url`); |
| 89 | + }); |
| 90 | + |
| 91 | + it('uses fallback urls if force do not exist', () => { |
| 92 | + const redirectUrls = new RedirectUrls({ |
| 93 | + signInFallbackRedirectUrl: 'sign-in-fallback-redirect-url', |
| 94 | + signUpFallbackRedirectUrl: 'sign-up-fallback-redirect-url', |
| 95 | + }); |
| 96 | + expect(redirectUrls.getAfterSignInUrl()).toBe(`${mockWindowLocation.href}sign-in-fallback-redirect-url`); |
| 97 | + expect(redirectUrls.getAfterSignUpUrl()).toBe(`${mockWindowLocation.href}sign-up-fallback-redirect-url`); |
| 98 | + }); |
| 99 | + |
| 100 | + it('prioritizes props over options', () => { |
| 101 | + const redirectUrls = new RedirectUrls( |
| 102 | + { |
| 103 | + signInFallbackRedirectUrl: 'sign-in-fallback-redirect-url', |
| 104 | + signUpFallbackRedirectUrl: 'sign-up-fallback-redirect-url', |
| 105 | + }, |
| 106 | + { |
| 107 | + signInFallbackRedirectUrl: 'prop-sign-in-fallback-redirect-url', |
| 108 | + signUpFallbackRedirectUrl: 'prop-sign-up-fallback-redirect-url', |
| 109 | + }, |
| 110 | + ); |
| 111 | + expect(redirectUrls.getAfterSignInUrl()).toBe(`${mockWindowLocation.href}prop-sign-in-fallback-redirect-url`); |
| 112 | + expect(redirectUrls.getAfterSignUpUrl()).toBe(`${mockWindowLocation.href}prop-sign-up-fallback-redirect-url`); |
| 113 | + }); |
| 114 | + |
| 115 | + it('prioritizes force even if props take priority over options', () => { |
| 116 | + const redirectUrls = new RedirectUrls( |
| 117 | + { |
| 118 | + signInForceRedirectUrl: 'sign-in-fallback-redirect-url', |
| 119 | + signUpForceRedirectUrl: 'sign-up-fallback-redirect-url', |
| 120 | + }, |
| 121 | + { |
| 122 | + signInFallbackRedirectUrl: 'prop-sign-in-fallback-redirect-url', |
| 123 | + signUpFallbackRedirectUrl: 'prop-sign-up-fallback-redirect-url', |
| 124 | + }, |
| 125 | + ); |
| 126 | + expect(redirectUrls.getAfterSignInUrl()).toBe(`${mockWindowLocation.href}sign-in-fallback-redirect-url`); |
| 127 | + expect(redirectUrls.getAfterSignUpUrl()).toBe(`${mockWindowLocation.href}sign-up-fallback-redirect-url`); |
| 128 | + }); |
| 129 | + |
| 130 | + it('prioritizes searchParams over all else', () => { |
| 131 | + const redirectUrls = new RedirectUrls( |
| 132 | + { |
| 133 | + signInFallbackRedirectUrl: 'sign-in-fallback-redirect-url', |
| 134 | + signUpFallbackRedirectUrl: 'sign-up-fallback-redirect-url', |
| 135 | + }, |
| 136 | + { |
| 137 | + signInFallbackRedirectUrl: 'prop-sign-in-fallback-redirect-url', |
| 138 | + signUpFallbackRedirectUrl: 'prop-sign-up-fallback-redirect-url', |
| 139 | + }, |
| 140 | + { |
| 141 | + sign_in_fallback_redirect_url: 'search-param-sign-in-fallback-redirect-url', |
| 142 | + sign_up_fallback_redirect_url: 'search-param-sign-up-fallback-redirect-url', |
| 143 | + }, |
| 144 | + ); |
| 145 | + expect(redirectUrls.getAfterSignInUrl()).toBe( |
| 146 | + `${mockWindowLocation.href}search-param-sign-in-fallback-redirect-url`, |
| 147 | + ); |
| 148 | + expect(redirectUrls.getAfterSignUpUrl()).toBe( |
| 149 | + `${mockWindowLocation.href}search-param-sign-up-fallback-redirect-url`, |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + it('prioritizes force even if searchParams exist', () => { |
| 154 | + const redirectUrls = new RedirectUrls( |
| 155 | + { |
| 156 | + signInForceRedirectUrl: 'sign-in-force-redirect-url', |
| 157 | + signUpForceRedirectUrl: 'sign-up-force-redirect-url', |
| 158 | + }, |
| 159 | + { |
| 160 | + signInFallbackRedirectUrl: 'prop-sign-in-fallback-redirect-url', |
| 161 | + signUpFallbackRedirectUrl: 'prop-sign-up-fallback-redirect-url', |
| 162 | + }, |
| 163 | + { |
| 164 | + sign_in_fallback_redirect_url: 'search-param-sign-in-fallback-redirect-url', |
| 165 | + sign_up_fallback_redirect_url: 'search-param-sign-up-fallback-redirect-url', |
| 166 | + }, |
| 167 | + ); |
| 168 | + expect(redirectUrls.getAfterSignInUrl()).toBe(`${mockWindowLocation.href}sign-in-force-redirect-url`); |
| 169 | + expect(redirectUrls.getAfterSignUpUrl()).toBe(`${mockWindowLocation.href}sign-up-force-redirect-url`); |
| 170 | + }); |
| 171 | + |
| 172 | + it('prioritizes redirect_url from searchParamsover fallback urls', () => { |
| 173 | + const redirectUrls = new RedirectUrls( |
| 174 | + { |
| 175 | + signInFallbackRedirectUrl: 'sign-in-fallback-redirect-url', |
| 176 | + signUpFallbackRedirectUrl: 'sign-up-fallback-redirect-url', |
| 177 | + }, |
| 178 | + { |
| 179 | + signInFallbackRedirectUrl: 'prop-sign-in-fallback-redirect-url', |
| 180 | + signUpFallbackRedirectUrl: 'prop-sign-up-fallback-redirect-url', |
| 181 | + }, |
| 182 | + { |
| 183 | + redirect_url: 'search-param-redirect-url', |
| 184 | + }, |
| 185 | + ); |
| 186 | + expect(redirectUrls.getAfterSignInUrl()).toBe(`${mockWindowLocation.href}search-param-redirect-url`); |
| 187 | + expect(redirectUrls.getAfterSignUpUrl()).toBe(`${mockWindowLocation.href}search-param-redirect-url`); |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + describe('search params', () => { |
| 192 | + it('appends only the preserved props', () => { |
| 193 | + const redirectUrls = new RedirectUrls( |
| 194 | + { |
| 195 | + signInFallbackRedirectUrl: 'sign-in-fallback-redirect-url', |
| 196 | + }, |
| 197 | + { |
| 198 | + signInFallbackRedirectUrl: 'props-sign-in-fallback-redirect-url', |
| 199 | + signInForceRedirectUrl: 'props-sign-in-force-redirect-url', |
| 200 | + }, |
| 201 | + { |
| 202 | + sign_up_fallback_redirect_url: 'search-param-sign-up-fallback-redirect-url', |
| 203 | + redirect_url: 'search-param-redirect-url', |
| 204 | + }, |
| 205 | + ); |
| 206 | + |
| 207 | + const params = redirectUrls.toSearchParams(); |
| 208 | + expect([...params.keys()].length).toBe(1); |
| 209 | + expect(params.get('redirect_url')).toBe(`${mockWindowLocation.href}search-param-redirect-url`); |
| 210 | + }); |
| 211 | + }); |
| 212 | + |
| 213 | + describe('append to url', () => { |
| 214 | + it('does not append redirect urls from options to the url if the url is same origin', () => { |
| 215 | + const redirectUrls = new RedirectUrls({ |
| 216 | + signInFallbackRedirectUrl: 'sign-in-fallback-redirect-url', |
| 217 | + signUpFallbackRedirectUrl: 'sign-up-fallback-redirect-url', |
| 218 | + }); |
| 219 | + |
| 220 | + const url = redirectUrls.appendPreservedPropsToUrl('https://www.clerk.com'); |
| 221 | + expect(url).toBe('https://www.clerk.com/'); |
| 222 | + }); |
| 223 | + |
| 224 | + it('appends redirect urls from options to the url if the url is cross origin', () => { |
| 225 | + const redirectUrls = new RedirectUrls({}, {}, { redirect_url: '/search-param-redirect-url' }); |
| 226 | + |
| 227 | + const url = redirectUrls.appendPreservedPropsToUrl('https://www.example.com'); |
| 228 | + expect(url).toContain('search-param-redirect-url'); |
| 229 | + }); |
| 230 | + |
| 231 | + it('overrides the existing search params', () => { |
| 232 | + const redirectUrls = new RedirectUrls( |
| 233 | + { |
| 234 | + signInFallbackRedirectUrl: 'sign-in-fallback-redirect-url', |
| 235 | + signUpFallbackRedirectUrl: 'sign-up-fallback-redirect-url', |
| 236 | + }, |
| 237 | + {}, |
| 238 | + { redirect_url: '/search-param-redirect-url' }, |
| 239 | + ); |
| 240 | + |
| 241 | + const url = redirectUrls.appendPreservedPropsToUrl('https://www.example.com?redirect_url=existing'); |
| 242 | + expect(url).toBe( |
| 243 | + 'https://www.example.com/?redirect_url=existing#/?redirect_url=https%3A%2F%2Fwww.clerk.com%2Fsearch-param-redirect-url', |
| 244 | + ); |
| 245 | + }); |
| 246 | + |
| 247 | + it('appends redirect urls from props to the url even if the url is same origin', () => { |
| 248 | + const redirectUrls = new RedirectUrls({}, {}, { redirect_url: '/search-param-redirect-url' }); |
| 249 | + |
| 250 | + const url = redirectUrls.appendPreservedPropsToUrl('https://www.clerk.com'); |
| 251 | + expect(url).toContain('search-param-redirect-url'); |
| 252 | + }); |
| 253 | + |
| 254 | + it('does not append redirect urls from props to the url if the url is same origin if they match the options urls', () => { |
| 255 | + const redirectUrls = new RedirectUrls( |
| 256 | + { |
| 257 | + signInFallbackRedirectUrl: 'sign-in-fallback-redirect-url', |
| 258 | + signUpFallbackRedirectUrl: 'sign-up-fallback-redirect-url', |
| 259 | + }, |
| 260 | + { |
| 261 | + signInFallbackRedirectUrl: 'sign-in-fallback-redirect-url', |
| 262 | + signUpFallbackRedirectUrl: 'sign-up-fallback-redirect-url', |
| 263 | + }, |
| 264 | + ); |
| 265 | + |
| 266 | + const url = redirectUrls.appendPreservedPropsToUrl('https://www.clerk.com'); |
| 267 | + expect(url).toBe('https://www.clerk.com/'); |
| 268 | + }); |
| 269 | + }); |
| 270 | +}); |
0 commit comments