|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Ping Identity Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * This software may be modified and distributed under the terms |
| 5 | + * of the MIT license. See the LICENSE file for details. |
| 6 | + */ |
| 7 | +import { describe, it, expect } from 'vitest'; |
| 8 | +import { shouldUsePar, buildParBody, buildParAuthorizeUrl } from './par.utils.js'; |
| 9 | +import type { Endpoints } from './wellknown.types.js'; |
| 10 | + |
| 11 | +const baseEndpoints: Endpoints = { |
| 12 | + authorize: 'https://example.com/as/authorize', |
| 13 | + issuer: 'https://example.com/as', |
| 14 | + introspection: 'https://example.com/as/introspect', |
| 15 | + tokens: 'https://example.com/as/token', |
| 16 | + userinfo: 'https://example.com/as/userinfo', |
| 17 | +}; |
| 18 | + |
| 19 | +describe('shouldUsePar', () => { |
| 20 | + it('returns false when no PAR endpoint exists', () => { |
| 21 | + expect(shouldUsePar(baseEndpoints)).toBe(false); |
| 22 | + }); |
| 23 | + |
| 24 | + it('returns false when PAR endpoint exists but requirePar is false', () => { |
| 25 | + expect( |
| 26 | + shouldUsePar({ ...baseEndpoints, par: 'https://example.com/as/par', requirePar: false }), |
| 27 | + ).toBe(false); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns true when requirePar is true, even if par endpoint is absent', () => { |
| 31 | + expect(shouldUsePar({ ...baseEndpoints, requirePar: true })).toBe(true); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns true when requirePar is true and par endpoint is present', () => { |
| 35 | + expect( |
| 36 | + shouldUsePar({ ...baseEndpoints, par: 'https://example.com/as/par', requirePar: true }), |
| 37 | + ).toBe(true); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe('buildParBody', () => { |
| 42 | + it('encodes all provided params as form-urlencoded', () => { |
| 43 | + const body = buildParBody({ |
| 44 | + client_id: 'myClient', |
| 45 | + redirect_uri: 'https://app.example.com/callback', |
| 46 | + response_type: 'code', |
| 47 | + scope: 'openid profile', |
| 48 | + code_challenge: 'abc123', |
| 49 | + code_challenge_method: 'S256', |
| 50 | + state: 'xyz', |
| 51 | + }); |
| 52 | + |
| 53 | + expect(body.get('client_id')).toBe('myClient'); |
| 54 | + expect(body.get('redirect_uri')).toBe('https://app.example.com/callback'); |
| 55 | + expect(body.get('response_type')).toBe('code'); |
| 56 | + expect(body.get('scope')).toBe('openid profile'); |
| 57 | + expect(body.get('code_challenge')).toBe('abc123'); |
| 58 | + expect(body.get('code_challenge_method')).toBe('S256'); |
| 59 | + expect(body.get('state')).toBe('xyz'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('omits empty string values', () => { |
| 63 | + const body = buildParBody({ |
| 64 | + client_id: 'myClient', |
| 65 | + response_type: 'code', |
| 66 | + prompt: '', |
| 67 | + response_mode: '', |
| 68 | + }); |
| 69 | + |
| 70 | + expect(body.has('prompt')).toBe(false); |
| 71 | + expect(body.has('response_mode')).toBe(false); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('buildParAuthorizeUrl', () => { |
| 76 | + it('builds a minimal authorize URL with client_id and request_uri only', () => { |
| 77 | + const url = buildParAuthorizeUrl( |
| 78 | + 'https://example.com/as/authorize', |
| 79 | + 'myClient', |
| 80 | + 'urn:ietf:params:oauth:request_uri:abc123', |
| 81 | + ); |
| 82 | + |
| 83 | + const parsed = new URL(url); |
| 84 | + expect(parsed.origin + parsed.pathname).toBe('https://example.com/as/authorize'); |
| 85 | + expect(parsed.searchParams.get('client_id')).toBe('myClient'); |
| 86 | + expect(parsed.searchParams.get('request_uri')).toBe('urn:ietf:params:oauth:request_uri:abc123'); |
| 87 | + expect(parsed.searchParams.size).toBe(2); |
| 88 | + }); |
| 89 | +}); |
0 commit comments