@@ -3,6 +3,8 @@ import { ProxyOAuthServerProvider, ProxyOptions } from "./proxyProvider.js";
3
3
import { AuthInfo } from "./types.js" ;
4
4
import { OAuthClientInformationFull , OAuthTokens } from "../../shared/auth.js" ;
5
5
import { ServerError } from "./errors.js" ;
6
+ import { InvalidTokenError } from "./errors.js" ;
7
+ import { InsufficientScopeError } from "./errors.js" ;
6
8
7
9
describe ( "Proxy OAuth Server Provider" , ( ) => {
8
10
// Mock client data
@@ -17,6 +19,10 @@ describe("Proxy OAuth Server Provider", () => {
17
19
redirect : jest . fn ( ) ,
18
20
} as unknown as Response ;
19
21
22
+ // Mock provider functions
23
+ const mockVerifyToken = jest . fn ( ) ;
24
+ const mockGetClient = jest . fn ( ) ;
25
+
20
26
// Base provider options
21
27
const baseOptions : ProxyOptions = {
22
28
endpoints : {
@@ -25,7 +31,20 @@ describe("Proxy OAuth Server Provider", () => {
25
31
revocationUrl : "https://auth.example.com/revoke" ,
26
32
registrationUrl : "https://auth.example.com/register" ,
27
33
} ,
28
- verifyToken : jest . fn ( ) . mockImplementation ( async ( token : string ) => {
34
+ verifyAccessToken : mockVerifyToken ,
35
+ getClient : mockGetClient ,
36
+ } ;
37
+
38
+ let provider : ProxyOAuthServerProvider ;
39
+ let originalFetch : typeof global . fetch ;
40
+
41
+ beforeEach ( ( ) => {
42
+ provider = new ProxyOAuthServerProvider ( baseOptions ) ;
43
+ originalFetch = global . fetch ;
44
+ global . fetch = jest . fn ( ) ;
45
+
46
+ // Setup mock implementations
47
+ mockVerifyToken . mockImplementation ( async ( token : string ) => {
29
48
if ( token === "valid-token" ) {
30
49
return {
31
50
token,
@@ -34,23 +53,15 @@ describe("Proxy OAuth Server Provider", () => {
34
53
expiresAt : Date . now ( ) / 1000 + 3600 ,
35
54
} as AuthInfo ;
36
55
}
37
- throw new Error ( "Invalid token" ) ;
38
- } ) ,
39
- getClient : jest . fn ( ) . mockImplementation ( async ( clientId : string ) => {
56
+ throw new InvalidTokenError ( "Invalid token" ) ;
57
+ } ) ;
58
+
59
+ mockGetClient . mockImplementation ( async ( clientId : string ) => {
40
60
if ( clientId === "test-client" ) {
41
61
return validClient ;
42
62
}
43
63
return undefined ;
44
- } ) ,
45
- } ;
46
-
47
- let provider : ProxyOAuthServerProvider ;
48
- let originalFetch : typeof global . fetch ;
49
-
50
- beforeEach ( ( ) => {
51
- provider = new ProxyOAuthServerProvider ( baseOptions ) ;
52
- originalFetch = global . fetch ;
53
- global . fetch = jest . fn ( ) ;
64
+ } ) ;
54
65
} ) ;
55
66
56
67
afterEach ( ( ) => {
@@ -271,15 +282,44 @@ describe("Proxy OAuth Server Provider", () => {
271
282
272
283
describe ( "token verification" , ( ) => {
273
284
it ( "verifies valid token" , async ( ) => {
285
+ const validAuthInfo : AuthInfo = {
286
+ token : "valid-token" ,
287
+ clientId : "test-client" ,
288
+ scopes : [ "read" , "write" ] ,
289
+ expiresAt : Date . now ( ) / 1000 + 3600 ,
290
+ } ;
291
+ mockVerifyToken . mockResolvedValue ( validAuthInfo ) ;
292
+
274
293
const authInfo = await provider . verifyAccessToken ( "valid-token" ) ;
275
- expect ( authInfo . token ) . toBe ( "valid-token" ) ;
276
- expect ( baseOptions . verifyToken ) . toHaveBeenCalledWith ( "valid-token" ) ;
294
+ expect ( authInfo ) . toEqual ( validAuthInfo ) ;
295
+ expect ( mockVerifyToken ) . toHaveBeenCalledWith ( "valid-token" ) ;
277
296
} ) ;
278
297
279
- it ( "rejects invalid token" , async ( ) => {
280
- await expect (
281
- provider . verifyAccessToken ( "invalid-token" )
282
- ) . rejects . toThrow ( "Invalid token" ) ;
298
+ it ( "passes through InvalidTokenError" , async ( ) => {
299
+ const error = new InvalidTokenError ( "Token expired" ) ;
300
+ mockVerifyToken . mockRejectedValue ( error ) ;
301
+
302
+ await expect ( provider . verifyAccessToken ( "invalid-token" ) )
303
+ . rejects . toBe ( error ) ;
304
+ expect ( mockVerifyToken ) . toHaveBeenCalledWith ( "invalid-token" ) ;
305
+ } ) ;
306
+
307
+ it ( "passes through InsufficientScopeError" , async ( ) => {
308
+ const error = new InsufficientScopeError ( "Required scopes: read, write" ) ;
309
+ mockVerifyToken . mockRejectedValue ( error ) ;
310
+
311
+ await expect ( provider . verifyAccessToken ( "token-with-insufficient-scope" ) )
312
+ . rejects . toBe ( error ) ;
313
+ expect ( mockVerifyToken ) . toHaveBeenCalledWith ( "token-with-insufficient-scope" ) ;
314
+ } ) ;
315
+
316
+ it ( "passes through unexpected errors" , async ( ) => {
317
+ const error = new Error ( "Unexpected error" ) ;
318
+ mockVerifyToken . mockRejectedValue ( error ) ;
319
+
320
+ await expect ( provider . verifyAccessToken ( "valid-token" ) )
321
+ . rejects . toBe ( error ) ;
322
+ expect ( mockVerifyToken ) . toHaveBeenCalledWith ( "valid-token" ) ;
283
323
} ) ;
284
324
} ) ;
285
325
} ) ;
0 commit comments