From 678c5aac5f09d83edd875953496f32918aacd4e9 Mon Sep 17 00:00:00 2001 From: rcourtman Date: Mon, 1 Dec 2025 23:40:22 +0000 Subject: [PATCH] test: Add edge cases for initializeBootstrapToken - Nil router and nil config handling (no panic) - OIDC enabled clears bootstrap token Coverage: 80% to 92%. --- internal/api/bootstrap_token_test.go | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/internal/api/bootstrap_token_test.go b/internal/api/bootstrap_token_test.go index 2d2683f..77976ea 100644 --- a/internal/api/bootstrap_token_test.go +++ b/internal/api/bootstrap_token_test.go @@ -391,6 +391,50 @@ func TestBootstrapTokenValid(t *testing.T) { }) } +func TestInitializeBootstrapToken_NilRouter(t *testing.T) { + var r *Router = nil + // Should not panic + r.initializeBootstrapToken() +} + +func TestInitializeBootstrapToken_NilConfig(t *testing.T) { + r := &Router{config: nil} + // Should not panic + r.initializeBootstrapToken() +} + +func TestInitializeBootstrapToken_OIDCEnabled(t *testing.T) { + tmpDir := t.TempDir() + + // Create a bootstrap token file first + tokenPath := filepath.Join(tmpDir, bootstrapTokenFilename) + if err := os.WriteFile(tokenPath, []byte("testtoken\n"), 0o600); err != nil { + t.Fatalf("failed to create token file: %v", err) + } + + cfg := &config.Config{ + DataPath: tmpDir, + OIDC: &config.OIDCConfig{ + Enabled: true, + }, + } + r := &Router{ + config: cfg, + bootstrapTokenPath: tokenPath, // Set path so clearBootstrapToken can delete the file + } + r.initializeBootstrapToken() + + // Token should be cleared when OIDC is enabled + if r.bootstrapTokenHash != "" { + t.Error("expected empty bootstrapTokenHash when OIDC is enabled") + } + + // Token file should be removed + if _, err := os.Stat(tokenPath); !os.IsNotExist(err) { + t.Error("expected token file to be deleted when OIDC is enabled") + } +} + func TestHandleValidateBootstrapToken_InvalidJSON(t *testing.T) { tmpDir := t.TempDir() cfg := &config.Config{DataPath: tmpDir}