1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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)
}
}
|