|
1 | 1 | import { describe, expect, test } from 'bun:test' |
2 | 2 | import { NextRequest } from 'next/server' |
3 | 3 |
|
4 | | -import { getFreeModeCountryAccess } from '../free-mode-country' |
| 4 | +import { |
| 5 | + getFreeModeCountryAccess, |
| 6 | + lookupIpinfoPrivacy, |
| 7 | +} from '../free-mode-country' |
5 | 8 |
|
6 | 9 | function makeReq(headers: Record<string, string> = {}): NextRequest { |
7 | 10 | return new NextRequest('http://localhost:3000/api/v1/chat/completions', { |
8 | 11 | headers, |
9 | 12 | }) |
10 | 13 | } |
11 | 14 |
|
| 15 | +const noAnonymousNetwork = { |
| 16 | + ipinfoToken: 'test-token', |
| 17 | + lookupIpPrivacy: async () => ({ signals: [] }), |
| 18 | +} |
| 19 | + |
12 | 20 | describe('free mode country access', () => { |
13 | | - test('allows allowlisted Cloudflare countries', () => { |
14 | | - const access = getFreeModeCountryAccess(makeReq({ 'cf-ipcountry': 'us' })) |
| 21 | + test('allows allowlisted Cloudflare countries', async () => { |
| 22 | + const access = await getFreeModeCountryAccess( |
| 23 | + makeReq({ 'cf-ipcountry': 'us' }), |
| 24 | + noAnonymousNetwork, |
| 25 | + ) |
15 | 26 | expect(access.allowed).toBe(true) |
16 | 27 | expect(access.countryCode).toBe('US') |
17 | 28 | expect(access.blockReason).toBe(null) |
18 | 29 | }) |
19 | 30 |
|
20 | | - test('blocks countries outside the allowlist', () => { |
21 | | - const access = getFreeModeCountryAccess(makeReq({ 'cf-ipcountry': 'FR' })) |
| 31 | + test('blocks countries outside the allowlist', async () => { |
| 32 | + const access = await getFreeModeCountryAccess( |
| 33 | + makeReq({ 'cf-ipcountry': 'FR' }), |
| 34 | + noAnonymousNetwork, |
| 35 | + ) |
22 | 36 | expect(access.allowed).toBe(false) |
23 | 37 | expect(access.countryCode).toBe('FR') |
24 | 38 | expect(access.blockReason).toBe('country_not_allowed') |
25 | 39 | }) |
26 | 40 |
|
27 | | - test('blocks anonymized Cloudflare country codes without falling back to IP geo', () => { |
28 | | - const access = getFreeModeCountryAccess( |
| 41 | + test('blocks anonymized Cloudflare country codes without falling back to IP geo', async () => { |
| 42 | + const access = await getFreeModeCountryAccess( |
29 | 43 | makeReq({ |
30 | 44 | 'cf-ipcountry': 'T1', |
31 | 45 | 'x-forwarded-for': '8.8.8.8', |
32 | 46 | }), |
| 47 | + noAnonymousNetwork, |
33 | 48 | ) |
34 | 49 | expect(access.allowed).toBe(false) |
35 | 50 | expect(access.countryCode).toBe(null) |
36 | 51 | expect(access.blockReason).toBe('anonymized_or_unknown_country') |
37 | 52 | }) |
38 | 53 |
|
39 | | - test('blocks missing client location as unknown', () => { |
40 | | - const access = getFreeModeCountryAccess(makeReq()) |
| 54 | + test('blocks missing client location as unknown', async () => { |
| 55 | + const access = await getFreeModeCountryAccess(makeReq(), noAnonymousNetwork) |
41 | 56 | expect(access.allowed).toBe(false) |
42 | 57 | expect(access.countryCode).toBe(null) |
43 | 58 | expect(access.blockReason).toBe('missing_client_ip') |
44 | 59 | }) |
| 60 | + |
| 61 | + test('blocks allowlisted countries when the client IP is an anonymous network', async () => { |
| 62 | + const access = await getFreeModeCountryAccess( |
| 63 | + makeReq({ |
| 64 | + 'cf-ipcountry': 'US', |
| 65 | + 'x-forwarded-for': '203.0.113.10', |
| 66 | + }), |
| 67 | + { |
| 68 | + ipinfoToken: 'test-token', |
| 69 | + lookupIpPrivacy: async () => ({ |
| 70 | + signals: ['vpn'], |
| 71 | + }), |
| 72 | + }, |
| 73 | + ) |
| 74 | + expect(access.allowed).toBe(false) |
| 75 | + expect(access.countryCode).toBe('US') |
| 76 | + expect(access.blockReason).toBe('anonymous_network') |
| 77 | + expect(access.ipPrivacy?.signals).toEqual(['vpn']) |
| 78 | + }) |
| 79 | + |
| 80 | + test('allows allowlisted countries when privacy lookup finds no anonymous signals', async () => { |
| 81 | + const access = await getFreeModeCountryAccess( |
| 82 | + makeReq({ |
| 83 | + 'cf-ipcountry': 'US', |
| 84 | + 'x-forwarded-for': '203.0.113.10', |
| 85 | + }), |
| 86 | + { |
| 87 | + ipinfoToken: 'test-token', |
| 88 | + lookupIpPrivacy: async () => ({ |
| 89 | + signals: [], |
| 90 | + }), |
| 91 | + }, |
| 92 | + ) |
| 93 | + expect(access.allowed).toBe(true) |
| 94 | + expect(access.blockReason).toBe(null) |
| 95 | + }) |
| 96 | + |
| 97 | + test('allows allowlisted countries when privacy lookup fails', async () => { |
| 98 | + const access = await getFreeModeCountryAccess( |
| 99 | + makeReq({ |
| 100 | + 'cf-ipcountry': 'US', |
| 101 | + 'x-forwarded-for': '203.0.113.10', |
| 102 | + }), |
| 103 | + { |
| 104 | + ipinfoToken: 'test-token', |
| 105 | + lookupIpPrivacy: async () => { |
| 106 | + throw new Error('provider unavailable') |
| 107 | + }, |
| 108 | + }, |
| 109 | + ) |
| 110 | + expect(access.allowed).toBe(true) |
| 111 | + expect(access.blockReason).toBe(null) |
| 112 | + expect(access.ipPrivacy).toBe(null) |
| 113 | + }) |
| 114 | + |
| 115 | + test('parses IPinfo privacy signals', async () => { |
| 116 | + const fetch = async () => |
| 117 | + Response.json({ |
| 118 | + vpn: true, |
| 119 | + proxy: false, |
| 120 | + tor: true, |
| 121 | + relay: false, |
| 122 | + hosting: true, |
| 123 | + service: 'Example VPN', |
| 124 | + }) |
| 125 | + |
| 126 | + const privacy = await lookupIpinfoPrivacy({ |
| 127 | + ip: '203.0.113.10', |
| 128 | + token: 'test-token', |
| 129 | + fetch: fetch as unknown as typeof globalThis.fetch, |
| 130 | + }) |
| 131 | + |
| 132 | + expect(privacy).toEqual({ |
| 133 | + signals: ['vpn', 'tor', 'hosting', 'service'], |
| 134 | + }) |
| 135 | + }) |
45 | 136 | }) |
0 commit comments