Pulse/internal/license/pubkey_test.go
rcourtman d3eb6a7148 test: improve test coverage for AI, license, config, and monitoring packages
New test files:
- internal/ai/providers/gemini_test.go: Comprehensive Gemini provider tests
- internal/api/ai_intelligence_handlers_test.go: AI intelligence endpoint tests
- internal/api/ai_patrol_handlers_test.go: AI patrol endpoint tests
- internal/api/license_handlers_test.go: License API handler tests
- internal/api/security_oidc_response_test.go: OIDC response formatting tests
- internal/config/ai_config_test.go: AI configuration function tests
- internal/config/persistence_ai_test.go: AI config persistence tests
- internal/config/persistence_extended_test.go: Extended persistence tests
- internal/license/persistence_test.go: License persistence tests
- internal/license/pubkey_test.go: Public key handling tests
- internal/monitoring/host_agent_temps_test.go: Temperature processing tests

Enhanced existing files:
- internal/api/updates_test.go: Added update handler tests
- internal/license/license_test.go: Added Service method tests

Coverage improvements:
- ai/providers: 57.3% -> 73.0% (+15.7%)
- license: 78.3% -> 85.9% (+7.6%)
- config: 49.7% -> 53.9% (+4.2%)
- monitoring: 49.8% -> 50.8% (+1.0%)
- api: 28.4% -> 29.8% (+1.4%)
2025-12-19 22:49:30 +00:00

117 lines
2.4 KiB
Go

package license
import (
"crypto/ed25519"
"encoding/base64"
"os"
"testing"
)
func TestInitPublicKey(t *testing.T) {
// Generate a temporary key pair for testing
pub, _, _ := ed25519.GenerateKey(nil)
base64Pub := base64.StdEncoding.EncodeToString(pub)
tests := []struct {
name string
envKey string
embeddedKey string
devMode string
expectedLoaded bool
}{
{
name: "load from environment",
envKey: base64Pub,
expectedLoaded: true,
},
{
name: "load from embedded",
embeddedKey: base64Pub,
expectedLoaded: true,
},
{
name: "dev mode",
devMode: "true",
expectedLoaded: false, // In dev mode it doesn't set the key
},
{
name: "no key available",
expectedLoaded: false,
},
{
name: "malformed env key falls back",
envKey: "not-base64",
embeddedKey: base64Pub,
expectedLoaded: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clear state
SetPublicKey(nil)
os.Unsetenv("PULSE_LICENSE_PUBLIC_KEY")
os.Unsetenv("PULSE_LICENSE_DEV_MODE")
EmbeddedPublicKey = tt.embeddedKey
if tt.envKey != "" {
os.Setenv("PULSE_LICENSE_PUBLIC_KEY", tt.envKey)
}
if tt.devMode != "" {
os.Setenv("PULSE_LICENSE_DEV_MODE", tt.devMode)
}
InitPublicKey()
loaded := publicKey != nil
if loaded != tt.expectedLoaded {
t.Errorf("expectedLoaded = %v, got %v", tt.expectedLoaded, loaded)
}
})
}
// Clean up for other tests
os.Unsetenv("PULSE_LICENSE_PUBLIC_KEY")
os.Unsetenv("PULSE_LICENSE_DEV_MODE")
EmbeddedPublicKey = ""
}
func TestDecodePublicKey(t *testing.T) {
pub, _, _ := ed25519.GenerateKey(nil)
tests := []struct {
name string
input string
wantErr bool
}{
{
name: "valid standard base64",
input: base64.StdEncoding.EncodeToString(pub),
wantErr: false,
},
{
name: "valid URL-safe base64",
input: base64.RawURLEncoding.EncodeToString(pub),
wantErr: false,
},
{
name: "invalid base64",
input: "!!!",
wantErr: true,
},
{
name: "wrong size",
input: base64.StdEncoding.EncodeToString([]byte("too-short")),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := decodePublicKey(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("decodePublicKey() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}