|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { test } from "node:test"; |
| 3 | +import { normalizePgConnectionStringForNode } from "../../src/lib/db/connection-string.js"; |
| 4 | + |
| 5 | +test("adds uselibpqcompat for sslmode=require", () => { |
| 6 | + const input = "postgresql://user:pass@db.example.com:5432/openerrata?sslmode=require"; |
| 7 | + const output = normalizePgConnectionStringForNode(input); |
| 8 | + const url = new URL(output); |
| 9 | + |
| 10 | + assert.equal(url.searchParams.get("sslmode"), "require"); |
| 11 | + assert.equal(url.searchParams.get("uselibpqcompat"), "true"); |
| 12 | +}); |
| 13 | + |
| 14 | +test("adds uselibpqcompat for sslmode=prefer and sslmode=allow", () => { |
| 15 | + const prefer = normalizePgConnectionStringForNode( |
| 16 | + "postgresql://user:pass@db.example.com/openerrata?sslmode=prefer", |
| 17 | + ); |
| 18 | + const allow = normalizePgConnectionStringForNode( |
| 19 | + "postgresql://user:pass@db.example.com/openerrata?sslmode=allow", |
| 20 | + ); |
| 21 | + |
| 22 | + assert.equal(new URL(prefer).searchParams.get("uselibpqcompat"), "true"); |
| 23 | + assert.equal(new URL(allow).searchParams.get("uselibpqcompat"), "true"); |
| 24 | +}); |
| 25 | + |
| 26 | +test("leaves connection strings unchanged when sslmode is absent", () => { |
| 27 | + const input = "postgresql://user:pass@db.example.com:5432/openerrata"; |
| 28 | + assert.equal(normalizePgConnectionStringForNode(input), input); |
| 29 | +}); |
| 30 | + |
| 31 | +test("leaves connection strings unchanged when uselibpqcompat is already present", () => { |
| 32 | + const input = |
| 33 | + "postgresql://user:pass@db.example.com/openerrata?sslmode=require&uselibpqcompat=false"; |
| 34 | + assert.equal(normalizePgConnectionStringForNode(input), input); |
| 35 | +}); |
| 36 | + |
| 37 | +test("leaves strict sslmodes unchanged", () => { |
| 38 | + const verifyFull = |
| 39 | + "postgresql://user:pass@db.example.com/openerrata?sslmode=verify-full"; |
| 40 | + const verifyCa = |
| 41 | + "postgresql://user:pass@db.example.com/openerrata?sslmode=verify-ca"; |
| 42 | + const disable = |
| 43 | + "postgresql://user:pass@db.example.com/openerrata?sslmode=disable"; |
| 44 | + |
| 45 | + assert.equal(normalizePgConnectionStringForNode(verifyFull), verifyFull); |
| 46 | + assert.equal(normalizePgConnectionStringForNode(verifyCa), verifyCa); |
| 47 | + assert.equal(normalizePgConnectionStringForNode(disable), disable); |
| 48 | +}); |
0 commit comments