c69ff49758
- Updated `FileTokenStore` and related stores (`objectstore`, `gitstore`, `postgresstore`) to include the `disabled` flag in metadata for token storage. - Adjusted `Auth` metadata handling to initialize empty maps when absent. - Refined logic in `auto_refresh_loop` and `conductor` to exclude `disabled` tokens from refresh checks. - Added comprehensive unit tests to verify proper handling of the `disabled` flag in storage and retrieval operations.
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
|
|
)
|
|
|
|
type testTokenStorage struct {
|
|
meta map[string]any
|
|
}
|
|
|
|
func (s *testTokenStorage) SetMetadata(meta map[string]any) { s.meta = meta }
|
|
|
|
func (s *testTokenStorage) SaveTokenToFile(authFilePath string) error {
|
|
raw, err := json.Marshal(s.meta)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(authFilePath, raw, 0o600)
|
|
}
|
|
|
|
func TestFileTokenStore_Save_DisabledPersistsFlagForTokenStorage(t *testing.T) {
|
|
ctx := context.Background()
|
|
baseDir := t.TempDir()
|
|
path := filepath.Join(baseDir, "disabled.json")
|
|
|
|
if err := os.WriteFile(path, []byte(`{"type":"test","disabled":true}`), 0o600); err != nil {
|
|
t.Fatalf("seed auth file: %v", err)
|
|
}
|
|
|
|
store := NewFileTokenStore()
|
|
store.SetBaseDir(baseDir)
|
|
storage := &testTokenStorage{}
|
|
|
|
auth := &cliproxyauth.Auth{
|
|
ID: "disabled.json",
|
|
Provider: "test",
|
|
FileName: "disabled.json",
|
|
Disabled: true,
|
|
Storage: storage,
|
|
Metadata: map[string]any{"type": "test"},
|
|
}
|
|
|
|
if _, err := store.Save(ctx, auth); err != nil {
|
|
t.Fatalf("Save() error: %v", err)
|
|
}
|
|
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("read auth file: %v", err)
|
|
}
|
|
var meta map[string]any
|
|
if err := json.Unmarshal(raw, &meta); err != nil {
|
|
t.Fatalf("unmarshal auth file: %v", err)
|
|
}
|
|
if disabled, _ := meta["disabled"].(bool); !disabled {
|
|
t.Fatalf("disabled=%v, want true (raw=%s)", meta["disabled"], string(raw))
|
|
}
|
|
}
|