|
| 1 | +// |
| 2 | +// DatabaseConnectionSSHTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import Testing |
| 8 | +@testable import TablePro |
| 9 | + |
| 10 | +@Suite("DatabaseConnection effectiveSSHConfig") |
| 11 | +struct DatabaseConnectionSSHTests { |
| 12 | + @Test("No profile and no sshProfileId returns inline sshConfig") |
| 13 | + func inlineSSHConfigWithoutProfile() { |
| 14 | + var conn = TestFixtures.makeConnection() |
| 15 | + conn.sshConfig = SSHConfiguration() |
| 16 | + conn.sshConfig.enabled = true |
| 17 | + conn.sshConfig.host = "inline-host.example.com" |
| 18 | + conn.sshConfig.port = 2_222 |
| 19 | + conn.sshConfig.username = "inline-user" |
| 20 | + conn.sshProfileId = nil |
| 21 | + |
| 22 | + let result = conn.effectiveSSHConfig(profile: nil) |
| 23 | + |
| 24 | + #expect(result.host == "inline-host.example.com") |
| 25 | + #expect(result.port == 2_222) |
| 26 | + #expect(result.username == "inline-user") |
| 27 | + #expect(result.enabled == true) |
| 28 | + } |
| 29 | + |
| 30 | + @Test("Profile provided and sshProfileId set returns profile config") |
| 31 | + func profileOverridesInlineConfig() { |
| 32 | + let profileId = UUID() |
| 33 | + var conn = TestFixtures.makeConnection() |
| 34 | + conn.sshConfig = SSHConfiguration() |
| 35 | + conn.sshConfig.enabled = true |
| 36 | + conn.sshConfig.host = "inline-host.example.com" |
| 37 | + conn.sshConfig.username = "inline-user" |
| 38 | + conn.sshProfileId = profileId |
| 39 | + |
| 40 | + let profile = SSHProfile( |
| 41 | + id: profileId, |
| 42 | + name: "Production SSH", |
| 43 | + host: "profile-host.example.com", |
| 44 | + port: 2_200, |
| 45 | + username: "profile-user", |
| 46 | + authMethod: .privateKey, |
| 47 | + privateKeyPath: "~/.ssh/id_ed25519" |
| 48 | + ) |
| 49 | + |
| 50 | + let result = conn.effectiveSSHConfig(profile: profile) |
| 51 | + |
| 52 | + #expect(result.host == "profile-host.example.com") |
| 53 | + #expect(result.port == 2_200) |
| 54 | + #expect(result.username == "profile-user") |
| 55 | + #expect(result.authMethod == .privateKey) |
| 56 | + #expect(result.privateKeyPath == "~/.ssh/id_ed25519") |
| 57 | + } |
| 58 | + |
| 59 | + @Test("sshProfileId set but profile nil falls back to inline config") |
| 60 | + func deletedProfileFallsBackToInline() { |
| 61 | + var conn = TestFixtures.makeConnection() |
| 62 | + conn.sshConfig = SSHConfiguration() |
| 63 | + conn.sshConfig.enabled = true |
| 64 | + conn.sshConfig.host = "fallback-host.example.com" |
| 65 | + conn.sshConfig.username = "fallback-user" |
| 66 | + conn.sshProfileId = UUID() |
| 67 | + |
| 68 | + let result = conn.effectiveSSHConfig(profile: nil) |
| 69 | + |
| 70 | + #expect(result.host == "fallback-host.example.com") |
| 71 | + #expect(result.username == "fallback-user") |
| 72 | + } |
| 73 | + |
| 74 | + @Test("sshProfileId nil ignores provided profile and returns inline config") |
| 75 | + func noProfileIdIgnoresProfile() { |
| 76 | + var conn = TestFixtures.makeConnection() |
| 77 | + conn.sshConfig = SSHConfiguration() |
| 78 | + conn.sshConfig.enabled = true |
| 79 | + conn.sshConfig.host = "inline-host.example.com" |
| 80 | + conn.sshConfig.username = "inline-user" |
| 81 | + conn.sshProfileId = nil |
| 82 | + |
| 83 | + let profile = SSHProfile( |
| 84 | + id: UUID(), |
| 85 | + name: "Ignored Profile", |
| 86 | + host: "profile-host.example.com", |
| 87 | + username: "profile-user" |
| 88 | + ) |
| 89 | + |
| 90 | + let result = conn.effectiveSSHConfig(profile: profile) |
| 91 | + |
| 92 | + #expect(result.host == "inline-host.example.com") |
| 93 | + #expect(result.username == "inline-user") |
| 94 | + } |
| 95 | + |
| 96 | + @Test("toSSHConfiguration sets enabled to true") |
| 97 | + func profileConfigHasEnabledTrue() { |
| 98 | + let profile = SSHProfile( |
| 99 | + name: "Test Profile", |
| 100 | + host: "ssh.example.com", |
| 101 | + port: 22, |
| 102 | + username: "testuser" |
| 103 | + ) |
| 104 | + |
| 105 | + let config = profile.toSSHConfiguration() |
| 106 | + |
| 107 | + #expect(config.enabled == true) |
| 108 | + #expect(config.host == "ssh.example.com") |
| 109 | + #expect(config.username == "testuser") |
| 110 | + } |
| 111 | +} |
0 commit comments