54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package app
|
|
|
|
import (
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"agent-system-backend/internal/settings"
|
|
"agent-system-backend/internal/store"
|
|
)
|
|
|
|
func newTavilyKeysStore(t *testing.T) *store.Store {
|
|
t.Helper()
|
|
st, err := store.OpenSQLite(filepath.Join(t.TempDir(), "tavily.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { st.Close() })
|
|
return st
|
|
}
|
|
|
|
func TestTavilyKeyPoolFallsBackToSystemRowSecret(t *testing.T) {
|
|
st := newTavilyKeysStore(t)
|
|
if _, err := st.AddSecret(settings.OwnerSystemRow, tavilySecretService, "system", "fake-sys-a, fake-sys-b"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, want := tavilyKeyPool(st, "fake-plain"), []string{"fake-sys-a", "fake-sys-b"}; !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("system-row key pool = %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestTavilyKeyPoolPrefersOwnerRowSecret(t *testing.T) {
|
|
st := newTavilyKeysStore(t)
|
|
if _, err := st.AddSecret(settings.OwnerSystemRow, tavilySecretService, "system", "fake-sys"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := st.AddSecret(store.OwnerDefault, tavilySecretService, "owner", "fake-owner"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, want := tavilyKeyPool(st, "fake-plain"), []string{"fake-owner"}; !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("owner-row key pool = %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestTavilyKeyPoolFallsBackToPlaintextSetting(t *testing.T) {
|
|
st := newTavilyKeysStore(t)
|
|
if got, want := tavilyKeyPool(st, " fake-plain-a ,, fake-plain-b"), []string{"fake-plain-a", "fake-plain-b"}; !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("plaintext key pool = %v, want %v", got, want)
|
|
}
|
|
if got := tavilyKeyPool(st, ""); len(got) != 0 {
|
|
t.Fatalf("empty key pool = %v", got)
|
|
}
|
|
}
|