|
4 | 4 | exchangeAuthorization,
|
5 | 5 | refreshAuthorization,
|
6 | 6 | registerClient,
|
| 7 | + auth, |
| 8 | + OAuthClientProvider |
7 | 9 | } from "./auth.js";
|
8 | 10 |
|
9 | 11 | // Mock fetch globally
|
@@ -503,4 +505,126 @@ describe("OAuth Authorization", () => {
|
503 | 505 | ).rejects.toThrow("Dynamic client registration failed");
|
504 | 506 | });
|
505 | 507 | });
|
| 508 | + |
| 509 | + describe("auth function", () => { |
| 510 | + const validMetadata = { |
| 511 | + issuer: "https://auth.example.com", |
| 512 | + authorization_endpoint: "https://auth.example.com/authorize", |
| 513 | + token_endpoint: "https://auth.example.com/token", |
| 514 | + registration_endpoint: "https://auth.example.com/register", |
| 515 | + response_types_supported: ["code"], |
| 516 | + code_challenge_methods_supported: ["S256"], |
| 517 | + }; |
| 518 | + |
| 519 | + const validClientInfo = { |
| 520 | + client_id: "client123", |
| 521 | + client_secret: "secret123", |
| 522 | + redirect_uris: ["http://localhost:3000/callback"], |
| 523 | + client_name: "Test Client", |
| 524 | + }; |
| 525 | + |
| 526 | + const validTokens = { |
| 527 | + access_token: "access123", |
| 528 | + token_type: "Bearer", |
| 529 | + expires_in: 3600, |
| 530 | + refresh_token: "refresh123", |
| 531 | + }; |
| 532 | + |
| 533 | + beforeEach(() => { |
| 534 | + // Mock discoverOAuthMetadata to return valid metadata |
| 535 | + mockFetch.mockResolvedValueOnce({ |
| 536 | + ok: true, |
| 537 | + status: 200, |
| 538 | + json: async () => validMetadata, |
| 539 | + }); |
| 540 | + }); |
| 541 | + |
| 542 | + it("should use delegateAuthorization when implemented and return AUTHORIZED", async () => { |
| 543 | + const mockProvider: OAuthClientProvider = { |
| 544 | + redirectUrl: "http://localhost:3000/callback", |
| 545 | + clientMetadata: { |
| 546 | + redirect_uris: ["http://localhost:3000/callback"], |
| 547 | + client_name: "Test Client" |
| 548 | + }, |
| 549 | + clientInformation: () => validClientInfo, |
| 550 | + tokens: () => validTokens, |
| 551 | + saveTokens: jest.fn(), |
| 552 | + redirectToAuthorization: jest.fn(), |
| 553 | + saveCodeVerifier: jest.fn(), |
| 554 | + codeVerifier: () => "test_verifier", |
| 555 | + delegateAuthorization: jest.fn().mockResolvedValue("AUTHORIZED") |
| 556 | + }; |
| 557 | + |
| 558 | + const result = await auth(mockProvider, { serverUrl: "https://auth.example.com" }); |
| 559 | + |
| 560 | + expect(result).toBe("AUTHORIZED"); |
| 561 | + expect(mockProvider.delegateAuthorization).toHaveBeenCalledWith( |
| 562 | + "https://auth.example.com", |
| 563 | + expect.objectContaining(validMetadata) |
| 564 | + ); |
| 565 | + expect(mockProvider.redirectToAuthorization).not.toHaveBeenCalled(); |
| 566 | + }); |
| 567 | + |
| 568 | + it("should fall back to standard flow when delegateAuthorization returns undefined", async () => { |
| 569 | + // Mock refresh token endpoint |
| 570 | + mockFetch.mockResolvedValueOnce({ |
| 571 | + ok: true, |
| 572 | + status: 200, |
| 573 | + json: async () => validTokens, |
| 574 | + }); |
| 575 | + |
| 576 | + const mockProvider: OAuthClientProvider = { |
| 577 | + redirectUrl: "http://localhost:3000/callback", |
| 578 | + clientMetadata: { |
| 579 | + redirect_uris: ["http://localhost:3000/callback"], |
| 580 | + client_name: "Test Client" |
| 581 | + }, |
| 582 | + clientInformation: () => validClientInfo, |
| 583 | + tokens: () => validTokens, |
| 584 | + saveTokens: jest.fn(), |
| 585 | + redirectToAuthorization: jest.fn(), |
| 586 | + saveCodeVerifier: jest.fn(), |
| 587 | + codeVerifier: () => "test_verifier", |
| 588 | + delegateAuthorization: jest.fn().mockResolvedValue(undefined) |
| 589 | + }; |
| 590 | + |
| 591 | + const result = await auth(mockProvider, { serverUrl: "https://auth.example.com" }); |
| 592 | + |
| 593 | + expect(result).toBe("AUTHORIZED"); |
| 594 | + expect(mockProvider.delegateAuthorization).toHaveBeenCalled(); |
| 595 | + expect(mockProvider.saveTokens).toHaveBeenCalled(); |
| 596 | + }); |
| 597 | + |
| 598 | + it("should not call delegateAuthorization when processing authorizationCode", async () => { |
| 599 | + // Mock token exchange endpoint |
| 600 | + mockFetch.mockResolvedValueOnce({ |
| 601 | + ok: true, |
| 602 | + status: 200, |
| 603 | + json: async () => validTokens, |
| 604 | + }); |
| 605 | + |
| 606 | + const mockProvider: OAuthClientProvider = { |
| 607 | + redirectUrl: "http://localhost:3000/callback", |
| 608 | + clientMetadata: { |
| 609 | + redirect_uris: ["http://localhost:3000/callback"], |
| 610 | + client_name: "Test Client" |
| 611 | + }, |
| 612 | + clientInformation: () => validClientInfo, |
| 613 | + tokens: jest.fn(), |
| 614 | + saveTokens: jest.fn(), |
| 615 | + redirectToAuthorization: jest.fn(), |
| 616 | + saveCodeVerifier: jest.fn(), |
| 617 | + codeVerifier: () => "test_verifier", |
| 618 | + delegateAuthorization: jest.fn() |
| 619 | + }; |
| 620 | + |
| 621 | + await auth(mockProvider, { |
| 622 | + serverUrl: "https://auth.example.com", |
| 623 | + authorizationCode: "code123" |
| 624 | + }); |
| 625 | + |
| 626 | + expect(mockProvider.delegateAuthorization).not.toHaveBeenCalled(); |
| 627 | + expect(mockProvider.saveTokens).toHaveBeenCalled(); |
| 628 | + }); |
| 629 | + }); |
506 | 630 | });
|
0 commit comments