-
Notifications
You must be signed in to change notification settings - Fork 93
/
connection_params_test.ts
263 lines (232 loc) · 6.69 KB
/
connection_params_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { assertEquals, assertThrows } from "./test_deps.ts";
import { createParams } from "../connection/connection_params.ts";
import { ConnectionParamsError } from "../client/error.ts";
import { has_env_access } from "./constants.ts";
/**
* This function is ment to be used as a container for env based tests.
* It will mutate the env state and run the callback passed to it, then
* reset the env variables to it's original state
*
* It can only be used in tests that run with env permissions
*/
const withEnv = (env: {
database: string;
host: string;
user: string;
port: string;
}, fn: () => void) => {
const PGDATABASE = Deno.env.get("PGDATABASE");
const PGHOST = Deno.env.get("PGHOST");
const PGPORT = Deno.env.get("PGPORT");
const PGUSER = Deno.env.get("PGUSER");
Deno.env.set("PGDATABASE", env.database);
Deno.env.set("PGHOST", env.host);
Deno.env.set("PGPORT", env.port);
Deno.env.set("PGUSER", env.user);
fn();
// Reset to original state
PGDATABASE
? Deno.env.set("PGDATABASE", PGDATABASE)
: Deno.env.delete("PGDATABASE");
PGHOST ? Deno.env.set("PGHOST", PGHOST) : Deno.env.delete("PGHOST");
PGPORT ? Deno.env.set("PGPORT", PGPORT) : Deno.env.delete("PGPORT");
PGUSER ? Deno.env.set("PGUSER", PGUSER) : Deno.env.delete("PGUSER");
};
function withNotAllowedEnv(fn: () => void) {
return () => {
const getEnv = Deno.env.get;
Deno.env.get = (_key: string) => {
throw new Deno.errors.PermissionDenied("");
};
try {
fn();
} finally {
Deno.env.get = getEnv;
}
};
}
Deno.test("Parses connection string", function () {
const p = createParams(
"postgres://some_user@some_host:10101/deno_postgres",
);
assertEquals(p.database, "deno_postgres");
assertEquals(p.user, "some_user");
assertEquals(p.hostname, "some_host");
assertEquals(p.port, 10101);
});
Deno.test('Parses connection string with "postgresql" as driver', function () {
const p = createParams(
"postgresql://some_user@some_host:10101/deno_postgres",
);
assertEquals(p.database, "deno_postgres");
assertEquals(p.user, "some_user");
assertEquals(p.hostname, "some_host");
assertEquals(p.port, 10101);
});
Deno.test("Parses connection string without port", function () {
const p = createParams(
"postgres://some_user@some_host/deno_postgres",
);
assertEquals(p.database, "deno_postgres");
assertEquals(p.user, "some_user");
assertEquals(p.hostname, "some_host");
assertEquals(p.port, 5432);
});
Deno.test("Parses connection string with application name", function () {
const p = createParams(
"postgres://some_user@some_host:10101/deno_postgres?application_name=test_app",
);
assertEquals(p.database, "deno_postgres");
assertEquals(p.user, "some_user");
assertEquals(p.hostname, "some_host");
assertEquals(p.applicationName, "test_app");
assertEquals(p.port, 10101);
});
Deno.test("Parses connection string with sslmode required", function () {
const p = createParams(
"postgres://some_user@some_host:10101/deno_postgres?sslmode=require",
);
assertEquals(p.tls.enabled, true);
assertEquals(p.tls.enforce, true);
});
Deno.test("Throws on connection string with invalid driver", function () {
assertThrows(
() =>
createParams(
"somedriver://some_user@some_host:10101/deno_postgres",
),
undefined,
"Supplied DSN has invalid driver: somedriver.",
);
});
Deno.test("Throws on connection string with invalid port", function () {
assertThrows(
() =>
createParams(
"postgres://some_user@some_host:abc/deno_postgres",
),
undefined,
"Invalid URL",
);
});
Deno.test("Throws on connection string with invalid ssl mode", function () {
assertThrows(
() =>
createParams(
"postgres://some_user@some_host:10101/deno_postgres?sslmode=verify-full",
),
undefined,
"Supplied DSN has invalid sslmode 'verify-full'. Only 'disable', 'require', and 'prefer' are supported",
);
});
Deno.test("Parses connection options", function () {
const p = createParams({
user: "some_user",
hostname: "some_host",
port: 10101,
database: "deno_postgres",
});
assertEquals(p.database, "deno_postgres");
assertEquals(p.user, "some_user");
assertEquals(p.hostname, "some_host");
assertEquals(p.port, 10101);
});
Deno.test("Throws on invalid tls options", function () {
assertThrows(
() =>
createParams({
tls: {
enabled: false,
enforce: true,
},
}),
ConnectionParamsError,
"Can't enforce TLS when client has TLS encryption is disabled",
);
});
Deno.test({
name: "Parses env connection options",
ignore: !has_env_access,
fn() {
withEnv({
database: "deno_postgres",
host: "some_host",
port: "10101",
user: "some_user",
}, () => {
const p = createParams();
assertEquals(p.database, "deno_postgres");
assertEquals(p.hostname, "some_host");
assertEquals(p.port, 10101);
assertEquals(p.user, "some_user");
});
},
});
Deno.test({
name: "Throws on env connection options with invalid port",
ignore: !has_env_access,
fn() {
const port = "abc";
withEnv({
database: "deno_postgres",
host: "some_host",
port,
user: "some_user",
}, () => {
assertThrows(
() => createParams(),
ConnectionParamsError,
`"${port}" is not a valid port number`,
);
});
},
});
Deno.test(
"Parses mixed connection options and env connection options",
withNotAllowedEnv(function () {
const p = createParams({
database: "deno_postgres",
user: "deno_postgres",
});
assertEquals(p.database, "deno_postgres");
assertEquals(p.user, "deno_postgres");
assertEquals(p.hostname, "127.0.0.1");
assertEquals(p.port, 5432);
}),
);
Deno.test("Uses default connection options", function () {
const database = "deno_postgres";
const user = "deno_postgres";
const p = createParams({
database,
user,
});
assertEquals(p.database, database);
assertEquals(p.user, user);
assertEquals(
p.hostname,
has_env_access ? (Deno.env.get("PGHOST") ?? "127.0.0.1") : "127.0.0.1",
);
assertEquals(p.port, 5432);
assertEquals(
p.password,
has_env_access ? Deno.env.get("PGPASSWORD") : undefined,
);
});
Deno.test("Throws when required options are not passed", function () {
if (has_env_access) {
if (!(Deno.env.get("PGUSER") && Deno.env.get("PGDATABASE"))) {
assertThrows(
() => createParams(),
ConnectionParamsError,
"Missing connection parameters:",
);
}
} else {
assertThrows(
() => createParams(),
ConnectionParamsError,
"Missing connection parameters: database, user",
);
}
});