|
1
|
import { getDatabaseURL } from "../database" |
|
2
|
|
|
3
|
describe("getDatabaseURL", () => { |
|
4
|
const originalEnv = process.env |
|
5
|
|
|
6
|
beforeEach(() => { |
|
7
|
process.env = { |
|
8
|
...originalEnv, |
|
9
|
DB_HOST: "localhost", |
|
10
|
DB_PORT: "5432", |
|
11
|
DB_TEMP_NAME: "test_db", |
|
12
|
} |
|
13
|
delete process.env.DB_USERNAME |
|
14
|
delete process.env.DB_PASSWORD |
|
15
|
}) |
|
16
|
|
|
17
|
afterEach(() => { |
|
18
|
process.env = originalEnv |
|
19
|
}) |
|
20
|
|
|
21
|
it("should build a valid URL with plain credentials", () => { |
|
22
|
process.env.DB_USERNAME = "postgres" |
|
23
|
process.env.DB_PASSWORD = "password" |
|
24
|
|
|
25
|
const url = getDatabaseURL() |
|
26
|
|
|
27
|
expect(url).toBe("postgres://postgres:password@localhost:5432/test_db") |
|
28
|
expect(() => new URL(url)).not.toThrow() |
|
29
|
}) |
|
30
|
|
|
31
|
it("should encode a # in the password", () => { |
|
32
|
process.env.DB_USERNAME = "postgres" |
|
33
|
process.env.DB_PASSWORD = "pass#word" |
|
34
|
|
|
35
|
const url = getDatabaseURL() |
|
36
|
|
|
37
|
expect(url).toBe("postgres://postgres:pass%23word@localhost:5432/test_db") |
|
38
|
expect(() => new URL(url)).not.toThrow() |
|
39
|
expect(decodeURIComponent(new URL(url).password)).toBe("pass#word") |
|
40
|
}) |
|
41
|
|
|
42
|
it("should encode @ in the password", () => { |
|
43
|
process.env.DB_USERNAME = "postgres" |
|
44
|
process.env.DB_PASSWORD = "pass@word" |
|
45
|
|
|
46
|
const url = getDatabaseURL() |
|
47
|
|
|
48
|
expect(() => new URL(url)).not.toThrow() |
|
49
|
expect(decodeURIComponent(new URL(url).password)).toBe("pass@word") |
|
50
|
}) |
|
51
|
|
|
52
|
it("should encode : in the password", () => { |
|
53
|
process.env.DB_USERNAME = "postgres" |
|
54
|
process.env.DB_PASSWORD = "pass:word" |
|
55
|
|
|
56
|
const url = getDatabaseURL() |
|
57
|
|
|
58
|
expect(() => new URL(url)).not.toThrow() |
|
59
|
expect(decodeURIComponent(new URL(url).password)).toBe("pass:word") |
|
60
|
}) |
|
61
|
|
|
62
|
it("should encode / in the password", () => { |
|
63
|
process.env.DB_USERNAME = "postgres" |
|
64
|
process.env.DB_PASSWORD = "pass/word" |
|
65
|
|
|
66
|
const url = getDatabaseURL() |
|
67
|
|
|
68
|
expect(() => new URL(url)).not.toThrow() |
|
69
|
expect(decodeURIComponent(new URL(url).password)).toBe("pass/word") |
|
70
|
}) |
|
71
|
|
|
72
|
it("should omit the password segment when DB_PASSWORD is empty", () => { |
|
73
|
process.env.DB_USERNAME = "postgres" |
|
74
|
process.env.DB_PASSWORD = "" |
|
75
|
|
|
76
|
const url = getDatabaseURL() |
|
77
|
|
|
78
|
expect(url).toBe("postgres://postgres@localhost:5432/test_db") |
|
79
|
expect(() => new URL(url)).not.toThrow() |
|
80
|
}) |
|
81
|
|
|
82
|
it("should encode special characters in the username", () => { |
|
83
|
process.env.DB_USERNAME = "user@domain" |
|
84
|
process.env.DB_PASSWORD = "" |
|
85
|
|
|
86
|
const url = getDatabaseURL() |
|
87
|
|
|
88
|
expect(() => new URL(url)).not.toThrow() |
|
89
|
expect(decodeURIComponent(new URL(url).username)).toBe("user@domain") |
|
90
|
}) |
|
91
|
|
|
92
|
it("should use the dbName argument when provided", () => { |
|
93
|
process.env.DB_USERNAME = "postgres" |
|
94
|
process.env.DB_PASSWORD = "" |
|
95
|
|
|
96
|
const url = getDatabaseURL("my_custom_db") |
|
97
|
|
|
98
|
expect(url).toContain("/my_custom_db") |
|
99
|
expect(() => new URL(url)).not.toThrow() |
|
100
|
}) |
|
101
|
}) |