|
| 1 | +import { OauthService } from "@canva/connect-api-ts"; |
| 2 | +import { createClient } from "@canva/connect-api-ts/client"; |
| 3 | + |
| 4 | +const mockClient = createClient({ |
| 5 | + baseUrl: "https://api.canva.com", |
| 6 | + headers: { Authorization: "Basic aaaaaaaa" }, |
| 7 | +}); |
| 8 | + |
| 9 | +describe("OAuth service body serialization", () => { |
| 10 | + let mockFetch: jest.Mock; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + mockFetch = jest.fn(); |
| 14 | + global.fetch = mockFetch; |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + jest.restoreAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe("exchangeAccessToken", () => { |
| 22 | + it("serializes the request body as application/x-www-form-urlencoded", async () => { |
| 23 | + mockFetch.mockResolvedValue( |
| 24 | + new Response( |
| 25 | + JSON.stringify({ |
| 26 | + access_token: "tok", |
| 27 | + refresh_token: "ref", |
| 28 | + token_type: "Bearer", |
| 29 | + expires_in: 3600, |
| 30 | + }), |
| 31 | + { status: 200, headers: { "Content-Type": "application/json" } }, |
| 32 | + ), |
| 33 | + ); |
| 34 | + |
| 35 | + await OauthService.exchangeAccessToken({ |
| 36 | + client: mockClient, |
| 37 | + body: { |
| 38 | + grant_type: "authorization_code", |
| 39 | + code: "test-code", |
| 40 | + code_verifier: "test-verifier", |
| 41 | + redirect_uri: "http://localhost:3001/oauth/redirect", |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + const request = mockFetch.mock.calls[0][0] as Request; |
| 46 | + const body = await request.text(); |
| 47 | + const parsed = Object.fromEntries(new URLSearchParams(body)); |
| 48 | + |
| 49 | + expect(parsed.grant_type).toBe("authorization_code"); |
| 50 | + expect(parsed.code).toBe("test-code"); |
| 51 | + expect(parsed.code_verifier).toBe("test-verifier"); |
| 52 | + expect(parsed.redirect_uri).toBe("http://localhost:3001/oauth/redirect"); |
| 53 | + // Regression guard: plain object .toString() returns "[object Object]" |
| 54 | + expect(body).not.toBe("[object Object]"); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe("revokeTokens", () => { |
| 59 | + it("serializes the request body as application/x-www-form-urlencoded", async () => { |
| 60 | + mockFetch.mockResolvedValue(new Response(null, { status: 200 })); |
| 61 | + |
| 62 | + await OauthService.revokeTokens({ |
| 63 | + client: mockClient, |
| 64 | + body: { |
| 65 | + client_id: "my-client-id", |
| 66 | + client_secret: "my-client-secret", |
| 67 | + token: "test-refresh-token", |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + const request = mockFetch.mock.calls[0][0] as Request; |
| 72 | + const body = await request.text(); |
| 73 | + const parsed = Object.fromEntries(new URLSearchParams(body)); |
| 74 | + |
| 75 | + expect(parsed.client_id).toBe("my-client-id"); |
| 76 | + expect(parsed.token).toBe("test-refresh-token"); |
| 77 | + // Regression guard: plain object .toString() returns "[object Object]" |
| 78 | + expect(body).not.toBe("[object Object]"); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments