Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript
2.6 KB
Raw Blame
import assert from "node:assert/strict";
import { test } from "node:test";

import {
  normalizeDatabaseUrl,
  readDatabasePassword,
  resolveDatabaseUrl,
  resolveMigrationDatabaseUrl,
} from "./database-url.mjs";

test("migrations prefer the direct URL that belongs to the URL in use", () => {
  assert.deepEqual(
    resolveMigrationDatabaseUrl({
      DATABASE_URL: "postgres://a",
      DATABASE_URL_UNPOOLED: "postgres://a-direct",
      POSTGRES_URL_NON_POOLING: "postgres://unrelated",
    }),
    { name: "DATABASE_URL_UNPOOLED", value: "postgres://a-direct" },
  );
  assert.deepEqual(
    resolveMigrationDatabaseUrl({
      DATABASE_URL: "postgres://a",
      POSTGRES_URL_NON_POOLING: "postgres://unrelated",
    }),
    { name: "DATABASE_URL", value: "postgres://a" },
  );
  assert.deepEqual(
    resolveMigrationDatabaseUrl({
      POSTGRES_URL: "postgres://b",
      POSTGRES_URL_NON_POOLING: "postgres://b-direct",
    }),
    { name: "POSTGRES_URL_NON_POOLING", value: "postgres://b-direct" },
  );
  assert.equal(resolveMigrationDatabaseUrl({}), null);
});

test("an unpooled URL alone is not enough for the app", () => {
  assert.equal(
    resolveDatabaseUrl({ DATABASE_URL_UNPOOLED: "postgres://a-direct" }),
    null,
  );
});

test("TLS settings are made explicit", () => {
  assert.equal(
    normalizeDatabaseUrl("postgres://u:[email protected]/app?sslmode=require"),
    "postgres://u:[email protected]/app?sslmode=verify-full",
  );
  assert.equal(
    normalizeDatabaseUrl(
      "postgres://u:[email protected]:5432/postgres?sslmode=require",
    ),
    "postgres://u:[email protected]:5432/postgres?sslmode=require&uselibpqcompat=true",
  );

  for (const unchanged of [
    "postgres://u:[email protected]:5432/app",
    "postgres://u:[email protected]/app?sslmode=disable",
    "postgres://u:[email protected]/app?sslmode=no-verify",
    "postgres://u:[email protected]/app?sslmode=require&uselibpqcompat=true",
    // Asking Supabase for certificate checks must never be downgraded.
    "postgres://u:[email protected]:5432/postgres?sslmode=verify-ca",
    "postgres://u:[email protected]:5432/postgres?sslmode=verify-full",
    "postgres://u:[email protected]:6543/postgres?sslmode=verify-ca&sslrootcert=/ca.crt",
    "not a url",
  ]) {
    assert.equal(normalizeDatabaseUrl(unchanged), unchanged);
  }
});

test("the password is read decoded", () => {
  assert.equal(readDatabasePassword("postgres://u:p%40ss@h/db"), "p@ss");
  assert.equal(readDatabasePassword("postgres://u@h/db"), undefined);
  assert.equal(readDatabasePassword("nonsense"), undefined);
});