-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathssh-tunnel.test.ts
More file actions
91 lines (83 loc) · 2.67 KB
/
Copy pathssh-tunnel.test.ts
File metadata and controls
91 lines (83 loc) · 2.67 KB
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
import { describe, test, expect } from "bun:test"
import {
extractSshConfig,
closeTunnel,
closeAllTunnels,
getActiveTunnel,
} from "../../src/altimate/native/connections/ssh-tunnel"
describe("SSH tunnel: extractSshConfig", () => {
test("returns null when no ssh_host present", () => {
const result = extractSshConfig({ type: "postgres", host: "db.example.com", port: 5432 })
expect(result).toBeNull()
})
test("extracts SSH config with all fields", () => {
const config = {
type: "postgres",
host: "db.internal",
port: 5432,
ssh_host: "bastion.example.com",
ssh_port: 2222,
ssh_user: "deploy",
ssh_password: "secret",
}
const result = extractSshConfig(config)
expect(result).toEqual({
ssh_host: "bastion.example.com",
ssh_port: 2222,
ssh_user: "deploy",
ssh_password: "secret",
ssh_private_key: undefined,
host: "db.internal",
port: 5432,
})
})
test("uses defaults for optional ssh_port and ssh_user", () => {
const result = extractSshConfig({
type: "postgres",
host: "db.internal",
port: 5432,
ssh_host: "bastion.example.com",
})
expect(result!.ssh_port).toBe(22)
expect(result!.ssh_user).toBe("root")
})
test("uses default host and port when not specified", () => {
const result = extractSshConfig({
type: "postgres",
ssh_host: "bastion.example.com",
})
expect(result!.host).toBe("127.0.0.1")
expect(result!.port).toBe(5432)
})
test("throws when ssh_host used with connection_string", () => {
expect(() =>
extractSshConfig({
type: "postgres",
ssh_host: "bastion.example.com",
connection_string: "postgresql://user:pass@host/db",
}),
).toThrow("Cannot use SSH tunnel with connection_string")
})
test("passes through both ssh_private_key and ssh_password", () => {
const result = extractSshConfig({
type: "postgres",
host: "db.internal",
port: 5432,
ssh_host: "bastion.example.com",
ssh_private_key: "-----BEGIN RSA PRIVATE KEY-----\nfake\n-----END RSA PRIVATE KEY-----",
ssh_password: "also-provided",
})
expect(result!.ssh_private_key).toContain("BEGIN RSA PRIVATE KEY")
expect(result!.ssh_password).toBe("also-provided")
})
})
describe("SSH tunnel: lifecycle helpers (no real SSH)", () => {
test("closeTunnel is a no-op for unknown name", () => {
closeTunnel("nonexistent-tunnel-name")
expect(getActiveTunnel("nonexistent-tunnel-name")).toBeUndefined()
})
test("closeAllTunnels is safe when no tunnels exist", () => {
closeAllTunnels()
expect(getActiveTunnel("any")).toBeUndefined()
})
})