@@ -3,6 +3,8 @@ import { ProxyOAuthServerProvider, ProxyOptions } from "./proxyProvider.js";
33import { AuthInfo } from "./types.js" ;
44import { OAuthClientInformationFull , OAuthTokens } from "../../shared/auth.js" ;
55import { ServerError } from "./errors.js" ;
6+ import { InvalidTokenError } from "./errors.js" ;
7+ import { InsufficientScopeError } from "./errors.js" ;
68
79describe ( "Proxy OAuth Server Provider" , ( ) => {
810 // Mock client data
@@ -17,6 +19,10 @@ describe("Proxy OAuth Server Provider", () => {
1719 redirect : jest . fn ( ) ,
1820 } as unknown as Response ;
1921
22+ // Mock provider functions
23+ const mockVerifyToken = jest . fn ( ) ;
24+ const mockGetClient = jest . fn ( ) ;
25+
2026 // Base provider options
2127 const baseOptions : ProxyOptions = {
2228 endpoints : {
@@ -25,7 +31,20 @@ describe("Proxy OAuth Server Provider", () => {
2531 revocationUrl : "https://auth.example.com/revoke" ,
2632 registrationUrl : "https://auth.example.com/register" ,
2733 } ,
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 ) => {
2948 if ( token === "valid-token" ) {
3049 return {
3150 token,
@@ -34,23 +53,15 @@ describe("Proxy OAuth Server Provider", () => {
3453 expiresAt : Date . now ( ) / 1000 + 3600 ,
3554 } as AuthInfo ;
3655 }
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 ) => {
4060 if ( clientId === "test-client" ) {
4161 return validClient ;
4262 }
4363 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+ } ) ;
5465 } ) ;
5566
5667 afterEach ( ( ) => {
@@ -271,15 +282,44 @@ describe("Proxy OAuth Server Provider", () => {
271282
272283 describe ( "token verification" , ( ) => {
273284 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+
274293 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" ) ;
277296 } ) ;
278297
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" ) ;
283323 } ) ;
284324 } ) ;
285325} ) ;
0 commit comments