Files
Agent-System-Backend/pkg/extension/link/link_test.go
T
agent-exec-transport 821599ffcb
governance / governance (pull_request) Successful in 14s
ci / vet-and-test (pull_request) Successful in 1m16s
agent-exec transport 7e8283631fb518cee0db0eba1be4c918/79c99324c3c7
2026-10-19 12:36:49 +00:00

114 lines
3.7 KiB
Go

package link
import (
"errors"
"os"
"path/filepath"
"testing"
"agent-system-backend/internal/config"
"agent-system-backend/pkg/extension"
)
const fakeSuperAdminPassword = "Fake-Test-Passw0rd-Only"
func clearSuperAdminEnv(t *testing.T) {
t.Helper()
for _, key := range []string{config.EnvSuperAdminOwner, config.EnvSuperAdminPassword, config.EnvFileKey} {
t.Setenv(key, "")
os.Unsetenv(key)
}
}
func writeEnvFile(t *testing.T, mode os.FileMode) string {
t.Helper()
path := filepath.Join(t.TempDir(), "super-admin.env")
content := "SUPER_ADMIN_OWNER=fake-file-owner\nSUPER_ADMIN_PASSWORD=" + fakeSuperAdminPassword + "\n"
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatal(err)
}
if err := os.Chmod(path, mode); err != nil {
t.Fatal(err)
}
return path
}
func TestRunOptionsLoadsSuperAdminFromEnv(t *testing.T) {
clearSuperAdminEnv(t)
t.Setenv(config.EnvSuperAdminOwner, "fake-env-owner")
t.Setenv(config.EnvSuperAdminPassword, fakeSuperAdminPassword)
got, err := runOptions(extension.Options{AuthMode: "on", RequireProjectContext: true})
if err != nil {
t.Fatalf("runOptions: %v", err)
}
if got.SuperAdmin.Owner != "fake-env-owner" || got.SuperAdmin.Password() == "" {
t.Fatalf("super admin not loaded from env: %v", got.SuperAdmin)
}
if !got.RequireProjectContext || got.AuthMode != "on" {
t.Fatalf("appOptions mapping lost: auth=%q requireProject=%t", got.AuthMode, got.RequireProjectContext)
}
}
func TestRunOptionsLoadsSuperAdminFromEnvFile(t *testing.T) {
clearSuperAdminEnv(t)
t.Setenv(config.EnvFileKey, writeEnvFile(t, 0o600))
got, err := runOptions(extension.Options{AuthMode: "on"})
if err != nil {
t.Fatalf("runOptions: %v", err)
}
if got.SuperAdmin.Owner != "fake-file-owner" || got.SuperAdmin.Password() == "" {
t.Fatalf("super admin not loaded from env file: %v", got.SuperAdmin)
}
}
func TestRunOptionsSkipsSuperAdminWithoutAuthentication(t *testing.T) {
clearSuperAdminEnv(t)
got, err := runOptions(extension.Options{AuthMode: "off"})
if err != nil {
t.Fatalf("runOptions without auth: %v", err)
}
if got.SuperAdmin.Owner != "" || got.SuperAdmin.Password() != "" {
t.Fatalf("super admin should be empty without auth: %v", got.SuperAdmin)
}
}
func TestRunOptionsRejectsExposedEnvFile(t *testing.T) {
clearSuperAdminEnv(t)
t.Setenv(config.EnvFileKey, writeEnvFile(t, 0o640))
_, err := runOptions(extension.Options{AuthMode: "on"})
if !errors.Is(err, config.ErrSuperAdminConfig) {
t.Fatalf("exposed env file accepted: %v", err)
}
}
func TestAppOptionsPrefersExplicitExecPaths(t *testing.T) {
t.Setenv("AGENT_SYSTEM_EXEC_DEPS_DIR", "/from/env/deps")
t.Setenv("AGENT_SYSTEM_EXEC_WORKSPACE", "overlay")
t.Setenv("AGENT_SYSTEM_EXEC_ENV_ROOT", "/from/env/root")
got := appOptions(extension.Options{
ExecDepsDir: "/explicit/deps",
ExecWorkspace: "direct",
ExecEnvRoot: "/explicit/root",
})
if got.ExecDepsDir != "/explicit/deps" || got.ExecWorkspace != "direct" || got.ExecEnvRoot != "/explicit/root" {
t.Fatalf("explicit paths lost: %+v", got)
}
}
func TestAppOptionsReadsExecEnvWhenUnset(t *testing.T) {
t.Setenv("AGENT_SYSTEM_EXEC_DEPS_DIR", "/from/env/deps")
t.Setenv("AGENT_SYSTEM_EXEC_WORKSPACE", "overlay")
t.Setenv("AGENT_SYSTEM_EXEC_ENV_ROOT", "/from/env/root")
got := appOptions(extension.Options{ExecBackend: "sandbox"})
if got.ExecBackend != "sandbox" || got.ExecDepsDir != "/from/env/deps" || got.ExecWorkspace != "overlay" || got.ExecEnvRoot != "/from/env/root" {
t.Fatalf("env fallback: %+v", got)
}
}
func TestAppOptionsPreservesProjectContextRequirement(t *testing.T) {
got := appOptions(extension.Options{RequireProjectContext: true})
if !got.RequireProjectContext {
t.Fatal("required project context was dropped at the product entry")
}
}