|
| 1 | +import { getSslSettings, isIpAddress } from '@internal/database/util' |
| 2 | + |
| 3 | +describe('database utils', () => { |
| 4 | + test.each([ |
| 5 | + ['', false], |
| 6 | + ['test', false], |
| 7 | + ['db.foobar.supabase.red', false], |
| 8 | + ['5.5.5.a', false], |
| 9 | + ['5.5.5.5', true], |
| 10 | + ['121.212.187.123', true], |
| 11 | + ['121.212.187.5', true], |
| 12 | + ['2001:db8:3333:4444:5555:6666:7777:8888', true], |
| 13 | + ['2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF', true], |
| 14 | + ['2406%3Ada18%3A4fd%3A9b09%3A2c76%3A5d38%3Ade30%3A7904', true], |
| 15 | + ])('is %s ip address, expected %s', (text: string, expected: boolean) => { |
| 16 | + expect(isIpAddress(text)).toBe(expected) |
| 17 | + }) |
| 18 | + |
| 19 | + describe('getSslSettings', () => { |
| 20 | + test('should return no SSL settings if database root CA not set', () => { |
| 21 | + expect( |
| 22 | + getSslSettings({ connectionString: 'foobar', databaseSSLRootCert: undefined }) |
| 23 | + ).toBeUndefined() |
| 24 | + }) |
| 25 | + |
| 26 | + test('should return no SSL settings if hostname is an IP address', () => { |
| 27 | + expect( |
| 28 | + getSslSettings({ |
| 29 | + connectionString: 'postgres://foo:[email protected]:5432/postgres', |
| 30 | + databaseSSLRootCert: '<cert>', |
| 31 | + }) |
| 32 | + ).toBeUndefined() |
| 33 | + }) |
| 34 | + |
| 35 | + test('should return SSL settings if hostname is not an IP address', () => { |
| 36 | + expect( |
| 37 | + getSslSettings({ |
| 38 | + connectionString: 'postgres://foo:[email protected]:5432/postgres', |
| 39 | + databaseSSLRootCert: '<cert>', |
| 40 | + }) |
| 41 | + ).toStrictEqual({ ca: '<cert>' }) |
| 42 | + }) |
| 43 | + |
| 44 | + test('should return SSL settings if hostname is not parseable', () => { |
| 45 | + expect( |
| 46 | + getSslSettings({ |
| 47 | + connectionString: 'postgres://foo:[email protected]."red:5432/postgres', |
| 48 | + databaseSSLRootCert: '<cert>', |
| 49 | + }) |
| 50 | + ).toStrictEqual({ ca: '<cert>' }) |
| 51 | + }) |
| 52 | + }) |
| 53 | +}) |
0 commit comments