@@ -3,9 +3,31 @@ import { vi } from 'vitest';
3
3
4
4
import { clerkMiddleware } from '../clerkMiddleware' ;
5
5
6
- const AUTH_RESPONSE = {
6
+ const SESSION_AUTH_RESPONSE = {
7
7
userId : 'user_2jZSstSbxtTndD9P7q4kDl0VVZa' ,
8
8
sessionId : 'sess_2jZSstSbxtTndD9P7q4kDl0VVZa' ,
9
+ tokenType : 'session_token' ,
10
+ isAuthenticated : true ,
11
+ sessionStatus : 'active' ,
12
+ sessionClaims : { } ,
13
+ actor : null ,
14
+ factorVerificationAge : null ,
15
+ orgId : null ,
16
+ orgRole : null ,
17
+ orgSlug : null ,
18
+ orgPermissions : null ,
19
+ } ;
20
+
21
+ const MACHINE_AUTH_RESPONSE = {
22
+ id : 'ak_123456789' ,
23
+ subject : 'user_2jZSstSbxtTndD9P7q4kDl0VVZa' ,
24
+ scopes : [ 'read:users' , 'write:users' ] ,
25
+ tokenType : 'api_key' ,
26
+ isAuthenticated : true ,
27
+ name : 'Test API Key' ,
28
+ claims : { custom : 'claim' } ,
29
+ userId : 'user_2jZSstSbxtTndD9P7q4kDl0VVZa' ,
30
+ orgId : null ,
9
31
} ;
10
32
11
33
const MOCK_OPTIONS = {
@@ -22,7 +44,7 @@ vi.mock('#imports', () => {
22
44
} ) ;
23
45
24
46
const authenticateRequestMock = vi . fn ( ) . mockResolvedValue ( {
25
- toAuth : ( ) => AUTH_RESPONSE ,
47
+ toAuth : ( ) => SESSION_AUTH_RESPONSE ,
26
48
headers : new Headers ( ) ,
27
49
} ) ;
28
50
@@ -47,7 +69,7 @@ describe('clerkMiddleware(params)', () => {
47
69
const response = await handler ( new Request ( new URL ( '/' , 'http://localhost' ) ) ) ;
48
70
49
71
expect ( response . status ) . toBe ( 200 ) ;
50
- expect ( await response . json ( ) ) . toEqual ( AUTH_RESPONSE ) ;
72
+ expect ( await response . json ( ) ) . toEqual ( SESSION_AUTH_RESPONSE ) ;
51
73
} ) ;
52
74
53
75
test ( 'renders route as normally when used with options param' , async ( ) => {
@@ -62,7 +84,7 @@ describe('clerkMiddleware(params)', () => {
62
84
63
85
expect ( response . status ) . toBe ( 200 ) ;
64
86
expect ( authenticateRequestMock ) . toHaveBeenCalledWith ( expect . any ( Request ) , expect . objectContaining ( MOCK_OPTIONS ) ) ;
65
- expect ( await response . json ( ) ) . toEqual ( AUTH_RESPONSE ) ;
87
+ expect ( await response . json ( ) ) . toEqual ( SESSION_AUTH_RESPONSE ) ;
66
88
} ) ;
67
89
68
90
test ( 'executes handler and renders route when used with a custom handler' , async ( ) => {
@@ -81,7 +103,7 @@ describe('clerkMiddleware(params)', () => {
81
103
82
104
expect ( response . status ) . toBe ( 200 ) ;
83
105
expect ( response . headers . get ( 'a-custom-header' ) ) . toBe ( '1' ) ;
84
- expect ( await response . json ( ) ) . toEqual ( AUTH_RESPONSE ) ;
106
+ expect ( await response . json ( ) ) . toEqual ( SESSION_AUTH_RESPONSE ) ;
85
107
} ) ;
86
108
87
109
test ( 'executes handler and renders route when used with a custom handler and options' , async ( ) => {
@@ -101,6 +123,108 @@ describe('clerkMiddleware(params)', () => {
101
123
expect ( response . status ) . toBe ( 200 ) ;
102
124
expect ( response . headers . get ( 'a-custom-header' ) ) . toBe ( '1' ) ;
103
125
expect ( authenticateRequestMock ) . toHaveBeenCalledWith ( expect . any ( Request ) , expect . objectContaining ( MOCK_OPTIONS ) ) ;
104
- expect ( await response . json ( ) ) . toEqual ( AUTH_RESPONSE ) ;
126
+ expect ( await response . json ( ) ) . toEqual ( SESSION_AUTH_RESPONSE ) ;
127
+ } ) ;
128
+
129
+ describe ( 'machine authentication' , ( ) => {
130
+ test ( 'returns machine auth object when acceptsToken is machine token type' , async ( ) => {
131
+ authenticateRequestMock . mockResolvedValueOnce ( {
132
+ toAuth : ( ) => MACHINE_AUTH_RESPONSE ,
133
+ headers : new Headers ( ) ,
134
+ } ) ;
135
+
136
+ const app = createApp ( ) ;
137
+ const handler = toWebHandler ( app ) ;
138
+ app . use ( clerkMiddleware ( ) ) ;
139
+ app . use (
140
+ '/' ,
141
+ eventHandler ( event => event . context . auth ( { acceptsToken : 'api_key' } ) ) ,
142
+ ) ;
143
+ const response = await handler ( new Request ( new URL ( '/' , 'http://localhost' ) ) ) ;
144
+
145
+ expect ( response . status ) . toBe ( 200 ) ;
146
+ expect ( await response . json ( ) ) . toEqual ( MACHINE_AUTH_RESPONSE ) ;
147
+ } ) ;
148
+
149
+ test ( 'returns machine auth object when acceptsToken array includes machine token type' , async ( ) => {
150
+ authenticateRequestMock . mockResolvedValueOnce ( {
151
+ toAuth : ( ) => MACHINE_AUTH_RESPONSE ,
152
+ headers : new Headers ( ) ,
153
+ } ) ;
154
+
155
+ const app = createApp ( ) ;
156
+ const handler = toWebHandler ( app ) ;
157
+ app . use ( clerkMiddleware ( ) ) ;
158
+ app . use (
159
+ '/' ,
160
+ eventHandler ( event => event . context . auth ( { acceptsToken : [ 'session_token' , 'api_key' ] } ) ) ,
161
+ ) ;
162
+ const response = await handler ( new Request ( new URL ( '/' , 'http://localhost' ) ) ) ;
163
+
164
+ expect ( response . status ) . toBe ( 200 ) ;
165
+ expect ( await response . json ( ) ) . toEqual ( MACHINE_AUTH_RESPONSE ) ;
166
+ } ) ;
167
+
168
+ test ( 'returns any auth object when acceptsToken is any' , async ( ) => {
169
+ authenticateRequestMock . mockResolvedValueOnce ( {
170
+ toAuth : ( ) => MACHINE_AUTH_RESPONSE ,
171
+ headers : new Headers ( ) ,
172
+ } ) ;
173
+
174
+ const app = createApp ( ) ;
175
+ const handler = toWebHandler ( app ) ;
176
+ app . use ( clerkMiddleware ( ) ) ;
177
+ app . use (
178
+ '/' ,
179
+ eventHandler ( event => event . context . auth ( { acceptsToken : 'any' } ) ) ,
180
+ ) ;
181
+ const response = await handler ( new Request ( new URL ( '/' , 'http://localhost' ) ) ) ;
182
+
183
+ expect ( response . status ) . toBe ( 200 ) ;
184
+ expect ( await response . json ( ) ) . toEqual ( MACHINE_AUTH_RESPONSE ) ;
185
+ } ) ;
186
+
187
+ test ( 'returns unauthenticated machine object when token type does not match acceptsToken' , async ( ) => {
188
+ authenticateRequestMock . mockResolvedValueOnce ( {
189
+ toAuth : ( ) => MACHINE_AUTH_RESPONSE ,
190
+ headers : new Headers ( ) ,
191
+ } ) ;
192
+
193
+ const app = createApp ( ) ;
194
+ const handler = toWebHandler ( app ) ;
195
+ app . use ( clerkMiddleware ( ) ) ;
196
+ app . use (
197
+ '/' ,
198
+ eventHandler ( event => event . context . auth ( { acceptsToken : 'machine_token' } ) ) ,
199
+ ) ;
200
+ const response = await handler ( new Request ( new URL ( '/' , 'http://localhost' ) ) ) ;
201
+
202
+ expect ( response . status ) . toBe ( 200 ) ;
203
+ const result = await response . json ( ) ;
204
+ expect ( result . tokenType ) . toBe ( 'machine_token' ) ;
205
+ expect ( result . isAuthenticated ) . toBe ( false ) ;
206
+ expect ( result . id ) . toBe ( null ) ;
207
+ } ) ;
208
+
209
+ test ( 'returns invalid token object when token type is not in acceptsToken array' , async ( ) => {
210
+ authenticateRequestMock . mockResolvedValueOnce ( {
211
+ toAuth : ( ) => MACHINE_AUTH_RESPONSE ,
212
+ headers : new Headers ( ) ,
213
+ } ) ;
214
+
215
+ const app = createApp ( ) ;
216
+ const handler = toWebHandler ( app ) ;
217
+ app . use ( clerkMiddleware ( ) ) ;
218
+ app . use (
219
+ '/' ,
220
+ eventHandler ( event => event . context . auth ( { acceptsToken : [ 'session_token' , 'machine_token' ] } ) ) ,
221
+ ) ;
222
+ const response = await handler ( new Request ( new URL ( '/' , 'http://localhost' ) ) ) ;
223
+
224
+ expect ( response . status ) . toBe ( 200 ) ;
225
+ const result = await response . json ( ) ;
226
+ expect ( result . tokenType ) . toBe ( null ) ;
227
+ expect ( result . isAuthenticated ) . toBe ( false ) ;
228
+ } ) ;
105
229
} ) ;
106
230
} ) ;
0 commit comments