From f0688db9af13dc67586cb07cfcd769983e7798fe Mon Sep 17 00:00:00 2001 From: rcourtman Date: Mon, 1 Dec 2025 23:54:42 +0000 Subject: [PATCH] test: Add edge case for recovery token load read error Tests the error logging path when os.ReadFile fails with an error other than ErrNotExist. Uses a directory in place of the file. --- internal/api/recovery_tokens_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/internal/api/recovery_tokens_test.go b/internal/api/recovery_tokens_test.go index b41f131..52cf37a 100644 --- a/internal/api/recovery_tokens_test.go +++ b/internal/api/recovery_tokens_test.go @@ -574,6 +574,33 @@ func TestRecoveryTokenStore_Load_InvalidJSON(t *testing.T) { } } +func TestRecoveryTokenStore_Load_ReadError(t *testing.T) { + tmpDir := t.TempDir() + + // Create a directory where the tokens file should be - reading it will fail + tokensPath := filepath.Join(tmpDir, "recovery_tokens.json") + if err := os.Mkdir(tokensPath, 0755); err != nil { + t.Fatalf("failed to create directory: %v", err) + } + + store := &RecoveryTokenStore{ + tokens: make(map[string]*RecoveryToken), + dataPath: tmpDir, + stopCleanup: make(chan struct{}), + } + + // Should not panic and should log error + store.load() + + store.mu.RLock() + count := len(store.tokens) + store.mu.RUnlock() + + if count != 0 { + t.Errorf("store should be empty after read error, got %d tokens", count) + } +} + func TestRecoveryTokenStore_StopCleanup(t *testing.T) { store := newTestRecoveryStore(t)