summaryrefslogtreecommitdiffstats
path: root/internal/config/config_test.go
diff options
context:
space:
mode:
authorMurilo Ijanc <[email protected]>2026-07-26 15:18:21 -0300
committerMurilo Ijanc <[email protected]>2026-07-26 15:18:21 -0300
commit3ca1d2c4370e8e79aba3940d5c8fe115960b75a8 (patch)
treeaa964675c588b80e4f81182d34130ba2d7d984e1 /internal/config/config_test.go
downloadtelegram-507b9f5009ff8720e0ea9a43c2f6bf888fd53ddb.tar.gz
Initial importv0.1.0
Diffstat (limited to 'internal/config/config_test.go')
-rw-r--r--internal/config/config_test.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
new file mode 100644
index 0000000..7ae0d95
--- /dev/null
+++ b/internal/config/config_test.go
@@ -0,0 +1,76 @@
+package config
+
+import (
+ "errors"
+ "log/slog"
+ "strings"
+ "testing"
+)
+
+func TestLoadRequiresTheToken(t *testing.T) {
+ t.Setenv(EnvToken, "")
+ if _, err := Load(); !errors.Is(err, ErrMissingToken) {
+ t.Fatalf("Load error = %v, want ErrMissingToken", err)
+ }
+}
+
+func TestLoadDefaults(t *testing.T) {
+ t.Setenv(EnvToken, "424242:not-a-real-token")
+
+ cfg, err := Load()
+ if err != nil {
+ t.Fatalf("Load: %v", err)
+ }
+ if cfg.CepisBaseURL != "https://cep.is" {
+ t.Fatalf("base URL = %q", cfg.CepisBaseURL)
+ }
+ if cfg.TelegramAPIBaseURL != "https://api.telegram.org" {
+ t.Fatalf("telegram base URL = %q", cfg.TelegramAPIBaseURL)
+ }
+ if cfg.LogLevel != slog.LevelInfo {
+ t.Fatalf("log level = %v", cfg.LogLevel)
+ }
+}
+
+func TestLoadOverrides(t *testing.T) {
+ t.Setenv(EnvToken, "424242:not-a-real-token")
+ t.Setenv(EnvCepisBaseURL, "http://localhost:8080/")
+ t.Setenv(EnvTelegramAPIBaseURL, "http://127.0.0.1:9000/")
+ t.Setenv(EnvLogLevel, "debug")
+
+ cfg, err := Load()
+ if err != nil {
+ t.Fatalf("Load: %v", err)
+ }
+ if cfg.CepisBaseURL != "http://localhost:8080" {
+ t.Fatalf("base URL = %q, the trailing slash should be gone", cfg.CepisBaseURL)
+ }
+ if cfg.TelegramAPIBaseURL != "http://127.0.0.1:9000" {
+ t.Fatalf("telegram base URL = %q", cfg.TelegramAPIBaseURL)
+ }
+ if cfg.LogLevel != slog.LevelDebug {
+ t.Fatalf("log level = %v", cfg.LogLevel)
+ }
+}
+
+func TestLoadRejectsAnUnknownLogLevel(t *testing.T) {
+ t.Setenv(EnvToken, "424242:not-a-real-token")
+ t.Setenv(EnvLogLevel, "verbose")
+
+ if _, err := Load(); err == nil {
+ t.Fatal("an unknown log level should be an error")
+ }
+}
+
+func TestLogValueRedactsTheToken(t *testing.T) {
+ cfg := Defaults()
+ cfg.Token = "424242:not-a-real-token"
+
+ rendered := cfg.LogValue().String()
+ if strings.Contains(rendered, cfg.Token) {
+ t.Fatalf("the token leaked into the log value: %s", rendered)
+ }
+ if !strings.Contains(rendered, "token_set=true") {
+ t.Fatalf("log value = %s", rendered)
+ }
+}