|
| 1 | +import { describe, expect, test } from 'bun:test' |
| 2 | + |
| 3 | +import { getLoginUrlOrigin } from '../_origin' |
| 4 | + |
| 5 | +describe('api/auth/cli/code/_origin', () => { |
| 6 | + test('uses the configured public app URL over the request origin', () => { |
| 7 | + const req = new Request('https://localhost:10000/api/auth/cli/code') |
| 8 | + |
| 9 | + expect( |
| 10 | + getLoginUrlOrigin( |
| 11 | + req, |
| 12 | + 'https://www.codebuff.com', |
| 13 | + 'https://codebuff.com', |
| 14 | + false, |
| 15 | + ), |
| 16 | + ).toBe('https://www.codebuff.com') |
| 17 | + }) |
| 18 | + |
| 19 | + test('ignores a localhost configured URL in production', () => { |
| 20 | + const req = new Request('https://localhost:10000/api/auth/cli/code') |
| 21 | + |
| 22 | + expect( |
| 23 | + getLoginUrlOrigin( |
| 24 | + req, |
| 25 | + 'https://localhost:10000', |
| 26 | + 'https://codebuff.com', |
| 27 | + false, |
| 28 | + ), |
| 29 | + ).toBe('https://codebuff.com') |
| 30 | + }) |
| 31 | + |
| 32 | + test('ignores IPv6 localhost in production', () => { |
| 33 | + const req = new Request('http://[::1]:3000/api/auth/cli/code') |
| 34 | + |
| 35 | + expect( |
| 36 | + getLoginUrlOrigin( |
| 37 | + req, |
| 38 | + 'http://[::1]:3000', |
| 39 | + 'https://codebuff.com', |
| 40 | + false, |
| 41 | + ), |
| 42 | + ).toBe('https://codebuff.com') |
| 43 | + }) |
| 44 | + |
| 45 | + test('allows a localhost configured URL outside production', () => { |
| 46 | + const req = new Request('http://localhost:3000/api/auth/cli/code') |
| 47 | + |
| 48 | + expect( |
| 49 | + getLoginUrlOrigin( |
| 50 | + req, |
| 51 | + 'http://localhost:3000', |
| 52 | + 'https://codebuff.com', |
| 53 | + true, |
| 54 | + ), |
| 55 | + ).toBe('http://localhost:3000') |
| 56 | + }) |
| 57 | + |
| 58 | + test('falls back to the request origin when configured URL is invalid', () => { |
| 59 | + const req = new Request('http://localhost:3000/api/auth/cli/code') |
| 60 | + |
| 61 | + expect( |
| 62 | + getLoginUrlOrigin(req, 'not a url', 'https://codebuff.com', true), |
| 63 | + ).toBe('http://localhost:3000') |
| 64 | + }) |
| 65 | +}) |
0 commit comments