|
5 | 5 | refreshAuthorization,
|
6 | 6 | registerClient,
|
7 | 7 | } from "./auth.js";
|
| 8 | +import {ServerError} from "../server/auth/errors.js"; |
8 | 9 |
|
9 | 10 | // Mock fetch globally
|
10 | 11 | const mockFetch = jest.fn();
|
@@ -112,25 +113,23 @@ describe("OAuth Authorization", () => {
|
112 | 113 | });
|
113 | 114 |
|
114 | 115 | it("throws on non-404 errors", async () => {
|
115 |
| - mockFetch.mockResolvedValueOnce({ |
116 |
| - ok: false, |
117 |
| - status: 500, |
118 |
| - }); |
| 116 | + mockFetch.mockResolvedValueOnce(new Response(null, { status: 500 })); |
119 | 117 |
|
120 | 118 | await expect(
|
121 | 119 | discoverOAuthMetadata("https://auth.example.com")
|
122 | 120 | ).rejects.toThrow("HTTP 500");
|
123 | 121 | });
|
124 | 122 |
|
125 | 123 | it("validates metadata schema", async () => {
|
126 |
| - mockFetch.mockResolvedValueOnce({ |
127 |
| - ok: true, |
128 |
| - status: 200, |
129 |
| - json: async () => ({ |
130 |
| - // Missing required fields |
131 |
| - issuer: "https://auth.example.com", |
132 |
| - }), |
133 |
| - }); |
| 124 | + mockFetch.mockResolvedValueOnce( |
| 125 | + Response.json( |
| 126 | + { |
| 127 | + // Missing required fields |
| 128 | + issuer: "https://auth.example.com", |
| 129 | + }, |
| 130 | + { status: 200 } |
| 131 | + ) |
| 132 | + ); |
134 | 133 |
|
135 | 134 | await expect(
|
136 | 135 | discoverOAuthMetadata("https://auth.example.com")
|
@@ -321,10 +320,12 @@ describe("OAuth Authorization", () => {
|
321 | 320 | });
|
322 | 321 |
|
323 | 322 | it("throws on error response", async () => {
|
324 |
| - mockFetch.mockResolvedValueOnce({ |
325 |
| - ok: false, |
326 |
| - status: 400, |
327 |
| - }); |
| 323 | + mockFetch.mockResolvedValueOnce( |
| 324 | + Response.json( |
| 325 | + new ServerError("Token exchange failed").toResponseObject(), |
| 326 | + { status: 400 } |
| 327 | + ) |
| 328 | + ); |
328 | 329 |
|
329 | 330 | await expect(
|
330 | 331 | exchangeAuthorization("https://auth.example.com", {
|
@@ -403,10 +404,12 @@ describe("OAuth Authorization", () => {
|
403 | 404 | });
|
404 | 405 |
|
405 | 406 | it("throws on error response", async () => {
|
406 |
| - mockFetch.mockResolvedValueOnce({ |
407 |
| - ok: false, |
408 |
| - status: 400, |
409 |
| - }); |
| 407 | + mockFetch.mockResolvedValueOnce( |
| 408 | + Response.json( |
| 409 | + new ServerError("Token refresh failed").toResponseObject(), |
| 410 | + { status: 400 } |
| 411 | + ) |
| 412 | + ); |
410 | 413 |
|
411 | 414 | await expect(
|
412 | 415 | refreshAuthorization("https://auth.example.com", {
|
@@ -491,10 +494,12 @@ describe("OAuth Authorization", () => {
|
491 | 494 | });
|
492 | 495 |
|
493 | 496 | it("throws on error response", async () => {
|
494 |
| - mockFetch.mockResolvedValueOnce({ |
495 |
| - ok: false, |
496 |
| - status: 400, |
497 |
| - }); |
| 497 | + mockFetch.mockResolvedValueOnce( |
| 498 | + Response.json( |
| 499 | + new ServerError("Dynamic client registration failed").toResponseObject(), |
| 500 | + { status: 400 } |
| 501 | + ) |
| 502 | + ); |
498 | 503 |
|
499 | 504 | await expect(
|
500 | 505 | registerClient("https://auth.example.com", {
|
|
0 commit comments