Files
ssh-access-tui/internal/keymgr/configonly_test.go
T
2026-07-23 12:38:13 +03:00

86 lines
2.5 KiB
Go

package keymgr
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"ssh-access-tui/internal/model"
)
// TestBuildConfigOnlyArchive_ProxyJumpSelectsRenderer mirrors
// TestBuildUserArchive_ProxyJumpSelectsRenderer for the config-only refresh
// path (no keypair generated here, just config + instructions).
func TestBuildConfigOnlyArchive_ProxyJumpSelectsRenderer(t *testing.T) {
if _, err := exec.LookPath("7z"); err != nil {
t.Skip("7z not available in PATH")
}
forwards := []model.Forward{
{HostName: "cache-1", Address: "10.0.0.9", Port: 6379, ServiceName: "redis"},
}
tests := []struct {
name string
proxyJump bool
}{
{"LocalForward mode", false},
{"ProxyJump mode", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
scratch := t.TempDir()
outDir := t.TempDir()
username := "test-user"
password := "TestPassw0rd123"
res, err := BuildConfigOnlyArchive(scratch, outDir, "bastion.example.com", 2222, username, password, forwards, tt.proxyJump)
if err != nil {
t.Fatalf("BuildConfigOnlyArchive: %v", err)
}
if _, err := os.Stat(res.ArchivePath); err != nil {
t.Fatalf("archive not found on disk: %v", err)
}
if entries, err := os.ReadDir(filepath.Join(scratch, username+"_configonly")); err == nil {
t.Fatalf("expected scratch dir to be wiped, still has entries: %v", entries)
}
extractedDir := extract7z(t, res.ArchivePath, password)
cfgBytes, err := os.ReadFile(filepath.Join(extractedDir, "config"))
if err != nil {
t.Fatalf("read extracted config: %v", err)
}
cfg := string(cfgBytes)
instrBytes, err := os.ReadFile(filepath.Join(extractedDir, "INSTRUCTIONS.txt"))
if err != nil {
t.Fatalf("read extracted instructions: %v", err)
}
instr := string(instrBytes)
if tt.proxyJump {
if strings.Contains(cfg, "LocalForward") {
t.Errorf("proxyJump=true config must not contain LocalForward, got:\n%s", cfg)
}
if !strings.Contains(cfg, "ProxyJump jump") {
t.Errorf("proxyJump=true config must contain ProxyJump jump, got:\n%s", cfg)
}
if strings.Contains(instr, "127.0.0.1") {
t.Errorf("proxyJump=true instructions must not mention local ports, got:\n%s", instr)
}
} else {
if strings.Contains(cfg, "ProxyJump") {
t.Errorf("proxyJump=false config must not contain ProxyJump, got:\n%s", cfg)
}
if !strings.Contains(cfg, "LocalForward") {
t.Errorf("proxyJump=false config must contain LocalForward, got:\n%s", cfg)
}
}
})
}
}