|
1 | 1 | import { describe, expect, test } from 'vitest'; |
2 | 2 | import { normalizeMongoConfig } from '../../src/types/types.js'; |
| 3 | +import { LookupAddress } from 'node:dns'; |
| 4 | +import { ErrorCode, ServiceError } from '@powersync/lib-services-framework'; |
3 | 5 |
|
4 | 6 | describe('config', () => { |
5 | | - test('Should resolve database', () => { |
| 7 | + test('Should normalize a simple URI', () => { |
| 8 | + const uri = 'mongodb://localhost:27017/powersync_test'; |
6 | 9 | const normalized = normalizeMongoConfig({ |
7 | 10 | type: 'mongodb', |
8 | | - uri: 'mongodb://localhost:27017/powersync_test' |
| 11 | + uri |
9 | 12 | }); |
| 13 | + expect(normalized.uri).equals(uri); |
10 | 14 | expect(normalized.database).equals('powersync_test'); |
11 | 15 | }); |
| 16 | + |
| 17 | + test('Should normalize an URI with auth', () => { |
| 18 | + const uri = 'mongodb://user:pass@localhost:27017/powersync_test'; |
| 19 | + const normalized = normalizeMongoConfig({ |
| 20 | + type: 'mongodb', |
| 21 | + uri |
| 22 | + }); |
| 23 | + expect(normalized.uri).equals(uri.replace('user:pass@', '')); |
| 24 | + expect(normalized.database).equals('powersync_test'); |
| 25 | + }); |
| 26 | + |
| 27 | + test('Should normalize an URI with query params', () => { |
| 28 | + const uri = 'mongodb://localhost:27017/powersync_test?query=test'; |
| 29 | + const normalized = normalizeMongoConfig({ |
| 30 | + type: 'mongodb', |
| 31 | + uri |
| 32 | + }); |
| 33 | + expect(normalized.uri).equals(uri); |
| 34 | + expect(normalized.database).equals('powersync_test'); |
| 35 | + }); |
| 36 | + |
| 37 | + test('Should normalize a replica set URI', () => { |
| 38 | + const uri = |
| 39 | + 'mongodb://mongodb-0.mongodb.powersync.svc.cluster.local:27017,mongodb-1.mongodb.powersync.svc.cluster.local:27017,mongodb-2.mongodb.powersync.svc.cluster.local:27017/powersync_test?replicaSet=rs0'; |
| 40 | + const normalized = normalizeMongoConfig({ |
| 41 | + type: 'mongodb', |
| 42 | + uri |
| 43 | + }); |
| 44 | + expect(normalized.uri).equals(uri); |
| 45 | + expect(normalized.database).equals('powersync_test'); |
| 46 | + }); |
| 47 | + |
| 48 | + test('Should normalize a replica set URI with auth', () => { |
| 49 | + const uri = |
| 50 | + 'mongodb://user:[email protected]:27017,mongodb-1.mongodb.powersync.svc.cluster.local:27017,mongodb-2.mongodb.powersync.svc.cluster.local:27017/powersync_test?replicaSet=rs0'; |
| 51 | + const normalized = normalizeMongoConfig({ |
| 52 | + type: 'mongodb', |
| 53 | + uri |
| 54 | + }); |
| 55 | + expect(normalized.uri).equals(uri.replace('user:pass@', '')); |
| 56 | + expect(normalized.database).equals('powersync_test'); |
| 57 | + expect(normalized.username).equals('user'); |
| 58 | + expect(normalized.password).equals('pass'); |
| 59 | + }); |
| 60 | + |
| 61 | + test('Should normalize a +srv URI', () => { |
| 62 | + const uri = 'mongodb+srv://user:pass@localhost/powersync_test'; |
| 63 | + const normalized = normalizeMongoConfig({ |
| 64 | + type: 'mongodb', |
| 65 | + uri |
| 66 | + }); |
| 67 | + expect(normalized.uri).equals(uri.replace('user:pass@', '')); |
| 68 | + expect(normalized.database).equals('powersync_test'); |
| 69 | + expect(normalized.username).equals('user'); |
| 70 | + expect(normalized.password).equals('pass'); |
| 71 | + }); |
| 72 | + |
| 73 | + test('Should prioritize username and password that are specified explicitly', () => { |
| 74 | + const uri = 'mongodb://user:pass@localhost:27017/powersync_test'; |
| 75 | + const normalized = normalizeMongoConfig({ |
| 76 | + type: 'mongodb', |
| 77 | + uri, |
| 78 | + username: 'user2', |
| 79 | + password: 'pass2' |
| 80 | + }); |
| 81 | + expect(normalized.uri).equals(uri.replace('user:pass@', '')); |
| 82 | + expect(normalized.database).equals('powersync_test'); |
| 83 | + expect(normalized.username).equals('user2'); |
| 84 | + expect(normalized.password).equals('pass2'); |
| 85 | + }); |
| 86 | + |
| 87 | + test('Should make a lookup function for a single IP host', async () => { |
| 88 | + let err: ServiceError | undefined; |
| 89 | + try { |
| 90 | + normalizeMongoConfig({ |
| 91 | + type: 'mongodb', |
| 92 | + uri: 'mongodb://127.0.0.1/powersync_test', |
| 93 | + reject_ip_ranges: ['127.0.0.1/0'] |
| 94 | + }); |
| 95 | + } catch (e) { |
| 96 | + err = e as ServiceError; |
| 97 | + } |
| 98 | + |
| 99 | + expect(err?.toJSON().code).toEqual(ErrorCode.PSYNC_S2203); |
| 100 | + }); |
| 101 | + |
| 102 | + test('Should make a lookup function for a single hostname', async () => { |
| 103 | + const lookup = normalizeMongoConfig({ |
| 104 | + type: 'mongodb', |
| 105 | + uri: 'mongodb://host/powersync_test', |
| 106 | + reject_ip_ranges: ['host'] |
| 107 | + }).lookup; |
| 108 | + |
| 109 | + const result = await new Promise((resolve, reject) => { |
| 110 | + lookup!('host', {}, (e, address) => { |
| 111 | + resolve(e); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + expect(result instanceof Error).toBe(true); |
| 116 | + }); |
| 117 | + |
| 118 | + test('Should make a lookup function for multiple IP hosts', async () => { |
| 119 | + let err: ServiceError | undefined; |
| 120 | + try { |
| 121 | + normalizeMongoConfig({ |
| 122 | + type: 'mongodb', |
| 123 | + uri: 'mongodb://127.0.0.1,127.0.0.2/powersync_test', |
| 124 | + reject_ip_ranges: ['127.0.0.1/0'] |
| 125 | + }); |
| 126 | + } catch (e) { |
| 127 | + err = e as ServiceError; |
| 128 | + } |
| 129 | + |
| 130 | + expect(err?.toJSON().code).toEqual(ErrorCode.PSYNC_S2203); |
| 131 | + }); |
| 132 | + |
| 133 | + test('Should make a lookup function for multiple hosts', async () => { |
| 134 | + const lookup = normalizeMongoConfig({ |
| 135 | + type: 'mongodb', |
| 136 | + uri: 'mongodb://host1,host2/powersync_test', |
| 137 | + reject_ip_ranges: ['host1'] |
| 138 | + }).lookup; |
| 139 | + |
| 140 | + const result = await new Promise((resolve, reject) => { |
| 141 | + lookup!('host1', {}, (e, address) => { |
| 142 | + resolve(e); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + expect(result instanceof Error).toBe(true); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('errors', () => { |
| 150 | + test('Should throw error when no database specified', () => { |
| 151 | + ['mongodb://localhost:27017', 'mongodb://localhost:27017/'].forEach((uri) => { |
| 152 | + expect(() => |
| 153 | + normalizeMongoConfig({ |
| 154 | + type: 'mongodb', |
| 155 | + uri |
| 156 | + }) |
| 157 | + ).toThrow('[PSYNC_S1105] MongoDB connection: database required'); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + test('Should throw error when URI has invalid scheme', () => { |
| 162 | + expect(() => |
| 163 | + normalizeMongoConfig({ |
| 164 | + type: 'mongodb', |
| 165 | + uri: 'not-a-uri' |
| 166 | + }) |
| 167 | + ).toThrow('[PSYNC_S1109] MongoDB connection: invalid URI'); |
| 168 | + }); |
| 169 | + |
| 170 | + test('Should throw error when URI has invalid host', () => { |
| 171 | + expect(() => |
| 172 | + normalizeMongoConfig({ |
| 173 | + type: 'mongodb', |
| 174 | + uri: 'mongodb://' |
| 175 | + }) |
| 176 | + ).toThrow('[PSYNC_S1109] MongoDB connection: invalid URI'); |
| 177 | + }); |
| 178 | + }); |
12 | 179 | }); |
0 commit comments