Files
tokens-reef/backend/internal/service/ops_jwt_secret_status_test.go
2026-04-24 08:32:16 +08:00

113 lines
2.5 KiB
Go

package service
import (
"context"
"testing"
"github.com/Wei-Shaw/sub2api/internal/config"
)
func TestGetJWTSecretStatus_Missing(t *testing.T) {
svc := &OpsService{
cfg: &config.Config{
JWT: config.JWTConfig{},
},
}
status, err := svc.GetJWTSecretStatus(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if status.Source != "missing" {
t.Fatalf("source = %q, want missing", status.Source)
}
if status.WarningLevel != "warning" {
t.Fatalf("warning_level = %q, want warning", status.WarningLevel)
}
if !status.NeedsRotation {
t.Fatal("expected needs_rotation to be true")
}
}
func TestGetJWTSecretStatus_AutoGeneratedFallback(t *testing.T) {
svc := &OpsService{
cfg: &config.Config{
JWT: config.JWTConfig{
Secret: "generated-jwt-secret-32bytes-long!!",
SecretConfigured: false,
},
},
}
status, err := svc.GetJWTSecretStatus(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if status.Source != "generated_or_persisted" {
t.Fatalf("source = %q, want generated_or_persisted", status.Source)
}
if !status.AutoGenerated {
t.Fatal("expected auto_generated to be true")
}
if !status.NeedsRotation {
t.Fatal("expected needs_rotation to be true")
}
}
func TestGetJWTSecretStatus_ConfiguredWeak(t *testing.T) {
svc := &OpsService{
cfg: &config.Config{
JWT: config.JWTConfig{
Secret: "change-me-in-production",
SecretConfigured: true,
},
},
}
status, err := svc.GetJWTSecretStatus(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if status.Source != "configured" {
t.Fatalf("source = %q, want configured", status.Source)
}
if !status.Weak {
t.Fatal("expected weak to be true")
}
if !status.NeedsRotation {
t.Fatal("expected needs_rotation to be true")
}
}
func TestGetJWTSecretStatus_ConfiguredStrong(t *testing.T) {
svc := &OpsService{
cfg: &config.Config{
JWT: config.JWTConfig{
Secret: "configured-jwt-secret-32bytes-long!!",
SecretConfigured: true,
},
},
}
status, err := svc.GetJWTSecretStatus(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if status.Source != "configured" {
t.Fatalf("source = %q, want configured", status.Source)
}
if status.WarningLevel != "none" {
t.Fatalf("warning_level = %q, want none", status.WarningLevel)
}
if status.NeedsRotation {
t.Fatal("expected needs_rotation to be false")
}
if status.Weak {
t.Fatal("expected weak to be false")
}
}