@@ -11,12 +11,17 @@ import { authkitLoader, encryptSession, terminateSession, refreshSession } from
11
11
import { assertIsResponse } from './test-utils/test-helpers.js' ;
12
12
import { getWorkOS } from './workos.js' ;
13
13
import { getConfig } from './config.js' ;
14
+ import { getAuthorizationUrl } from './get-authorization-url.js' ;
14
15
15
16
jest . mock ( './sessionStorage.js' , ( ) => ( {
16
17
configureSessionStorage : jest . fn ( ) ,
17
18
getSessionStorage : jest . fn ( ) ,
18
19
} ) ) ;
19
20
21
+ jest . mock ( './get-authorization-url.js' , ( ) => ( {
22
+ getAuthorizationUrl : jest . fn ( ) ,
23
+ } ) ) ;
24
+
20
25
// Mock dependencies
21
26
const fakeWorkosInstance = {
22
27
userManagement : {
@@ -39,6 +44,7 @@ const authenticateWithRefreshToken = jest.mocked(workos.userManagement.authentic
39
44
const getSessionStorage = jest . mocked ( getSessionStorageMock ) ;
40
45
const configureSessionStorage = jest . mocked ( configureSessionStorageMock ) ;
41
46
const jwtVerify = jest . mocked ( jose . jwtVerify ) ;
47
+ const getAuthorizationUrlMock = jest . mocked ( getAuthorizationUrl ) ;
42
48
43
49
function getHeaderValue ( headers : HeadersInit | undefined , name : string ) : string | null {
44
50
if ( ! headers ) {
@@ -113,6 +119,10 @@ describe('session', () => {
113
119
destroySession,
114
120
commitSession,
115
121
} ) ;
122
+
123
+ // Reset getAuthorizationUrl mock
124
+ getAuthorizationUrlMock . mockReset ( ) ;
125
+ getAuthorizationUrlMock . mockResolvedValue ( 'https://auth.workos.com/oauth/authorize' ) ;
116
126
} ) ;
117
127
118
128
describe ( 'encryptSession' , ( ) => {
@@ -594,17 +604,26 @@ describe('session', () => {
594
604
expect ( getHeaderValue ( init ?. headers , 'Set-Cookie' ) ) . toBe ( 'new-session-cookie' ) ;
595
605
} ) ;
596
606
597
- it ( 'should redirect to root when refresh fails' , async ( ) => {
607
+ it ( 'should redirect to authorization URL preserving returnPathname when refresh fails' , async ( ) => {
598
608
authenticateWithRefreshToken . mockRejectedValue ( new Error ( 'Refresh token invalid' ) ) ;
599
609
610
+ // Setup the mock to return a URL with state parameter
611
+ getAuthorizationUrlMock . mockResolvedValue ( 'https://auth.workos.com/oauth/authorize?state=abc123' ) ;
612
+
600
613
try {
601
- await authkitLoader ( createLoaderArgs ( createMockRequest ( ) ) ) ;
614
+ const mockRequest = createMockRequest ( 'test-cookie' , 'https://app.example.com/dashboard/settings' ) ;
615
+ await authkitLoader ( createLoaderArgs ( mockRequest ) ) ;
602
616
fail ( 'Expected redirect response to be thrown' ) ;
603
617
} catch ( response : unknown ) {
604
618
assertIsResponse ( response ) ;
605
619
expect ( response . status ) . toBe ( 302 ) ;
606
- expect ( response . headers . get ( 'Location' ) ) . toBe ( '/ ' ) ;
620
+ expect ( response . headers . get ( 'Location' ) ) . toBe ( 'https://auth.workos.com/oauth/authorize?state=abc123 ' ) ;
607
621
expect ( response . headers . get ( 'Set-Cookie' ) ) . toBe ( 'destroyed-session-cookie' ) ;
622
+
623
+ // Verify getAuthorizationUrl was called with the correct returnPathname
624
+ expect ( getAuthorizationUrlMock ) . toHaveBeenCalledWith ( {
625
+ returnPathname : '/dashboard/settings' ,
626
+ } ) ;
608
627
}
609
628
} ) ;
610
629
0 commit comments