import { afterEach, describe, expect, it } from 'bun:test'; import { Hono } from 'hono'; import { isPublicApiRequest } from '@/http/session-auth'; import returnRoute from '@/modules/billing/features/return/route'; import { getApiPublicOrigin } from '@/modules/billing/lib'; const previousWebUrl = process.env.WEB_URL; function makeApp() { return new Hono().basePath('/api/v1').route('/billing', returnRoute); } describe('GET /billing/return', () => { afterEach(() => { process.env.WEB_URL = previousWebUrl; }); it('bounces the browser to the billing screen of the web', async () => { process.env.WEB_URL = 'https://app.example.com'; const res = await makeApp().request('/api/v1/billing/return?to=billing&preapproval_id=pre-1'); expect(res.status).toBe(302); expect(res.headers.get('location')).toBe('https://app.example.com/billing?sub=return'); }); it('goes back to the onboarding step when it started there', async () => { process.env.WEB_URL = 'https://app.example.com'; const res = await makeApp().request('/api/v1/billing/return?to=onboarding'); expect(res.headers.get('location')).toBe('https://app.example.com/onboarding?sub=return'); }); it('never redirects anywhere but the web', async () => { process.env.WEB_URL = 'https://app.example.com'; const res = await makeApp().request('/api/v1/billing/return?to=https://evil.example'); expect(res.headers.get('location')).toBe('https://app.example.com/billing?sub=return'); }); it('is reachable without a session', () => { expect(isPublicApiRequest('GET', '/api/v1/billing/return')).toBe(true); expect(isPublicApiRequest('POST', '/api/v1/billing/return')).toBe(false); }); }); describe('getApiPublicOrigin', () => { it('prefers API_PUBLIC_URL, then BETTER_AUTH_URL, then localhost', () => { expect( getApiPublicOrigin({ API_PUBLIC_URL: 'https://x.ngrok-free.app/', BETTER_AUTH_URL: 'http://localhost:4000' }), ).toBe('https://x.ngrok-free.app'); expect(getApiPublicOrigin({ BETTER_AUTH_URL: 'https://app.example.com' })).toBe('https://app.example.com'); expect(getApiPublicOrigin({})).toBe('http://localhost:4000'); expect(getApiPublicOrigin({ API_PUBLIC_URL: 'not a url' })).toBe('http://localhost:4000'); }); });