| 8 | ) |
| 9 | |
| 10 | func TestLoadAuthTokenFromFile(t *testing.T) { |
| 11 | tokenFile := filepath.Join(t.TempDir(), "token") |
| 12 | const want = "from-file-token-xxxxxx" |
| 13 | if err := os.WriteFile(tokenFile, []byte(" "+want+"\n"), 0o600); err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | t.Setenv("ZENNOTES_AUTH_TOKEN", "") |
| 17 | t.Setenv("ZENNOTES_AUTH_TOKEN_FILE", tokenFile) |
| 18 | t.Setenv("ZENNOTES_CONFIG_PATH", filepath.Join(t.TempDir(), "missing.json")) |
| 19 | |
| 20 | cfg := Load() |
| 21 | if cfg.AuthToken != want { |
| 22 | t.Fatalf("AuthToken = %q, want %q (whitespace must be trimmed)", cfg.AuthToken, want) |
| 23 | } |
| 24 | if cfg.AuthTokenSource != AuthTokenSourceFile { |
| 25 | t.Fatalf("AuthTokenSource = %q, want %q", cfg.AuthTokenSource, AuthTokenSourceFile) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestEnvAuthTokenWinsOverFile(t *testing.T) { |
| 30 | tokenFile := filepath.Join(t.TempDir(), "token") |