@@ -5,13 +5,19 @@ import (
55 "time"
66)
77
8+ const (
9+ testNatsURL = "nats://localhost:4222"
10+ testSenderEmail = "sender@example.com"
11+ unexpectedErr = "unexpected error: %v"
12+ )
13+
814func setRequiredEnv (t * testing.T ) {
915 t .Helper ()
10- t .Setenv ("NATS_URL" , "nats://localhost:4222" )
16+ t .Setenv ("NATS_URL" , testNatsURL )
1117 t .Setenv ("MS_GRAPH_TENANT_ID" , "tenant" )
1218 t .Setenv ("MS_GRAPH_CLIENT_ID" , "client" )
1319 t .Setenv ("MS_GRAPH_CLIENT_SECRET" , "secret" )
14- t .Setenv ("MS_GRAPH_SENDER_EMAIL" , "sender@example.com" )
20+ t .Setenv ("MS_GRAPH_SENDER_EMAIL" , testSenderEmail )
1521}
1622
1723func TestLoad_Success (t * testing.T ) {
@@ -20,10 +26,10 @@ func TestLoad_Success(t *testing.T) {
2026 if err != nil {
2127 t .Fatalf ("expected no error, got %v" , err )
2228 }
23- if cfg .NatsURL != "nats://localhost:4222" {
29+ if cfg .NatsURL != testNatsURL {
2430 t .Errorf ("NatsURL: got %s" , cfg .NatsURL )
2531 }
26- if cfg .MSGraphSenderEmail != "sender@example.com" {
32+ if cfg .MSGraphSenderEmail != testSenderEmail {
2733 t .Errorf ("MSGraphSenderEmail: got %s" , cfg .MSGraphSenderEmail )
2834 }
2935}
@@ -32,7 +38,7 @@ func TestLoad_Defaults(t *testing.T) {
3238 setRequiredEnv (t )
3339 cfg , err := Load ()
3440 if err != nil {
35- t .Fatalf ("unexpected error: %v" , err )
41+ t .Fatalf (unexpectedErr , err )
3642 }
3743 if cfg .Port != "8080" {
3844 t .Errorf ("Port default: want 8080, got %s" , cfg .Port )
@@ -59,7 +65,7 @@ func TestLoad_MissingNatsURL(t *testing.T) {
5965 t .Setenv ("MS_GRAPH_TENANT_ID" , "tenant" )
6066 t .Setenv ("MS_GRAPH_CLIENT_ID" , "client" )
6167 t .Setenv ("MS_GRAPH_CLIENT_SECRET" , "secret" )
62- t .Setenv ("MS_GRAPH_SENDER_EMAIL" , "sender@example.com" )
68+ t .Setenv ("MS_GRAPH_SENDER_EMAIL" , testSenderEmail )
6369
6470 _ , err := Load ()
6571 if err == nil {
@@ -68,7 +74,7 @@ func TestLoad_MissingNatsURL(t *testing.T) {
6874}
6975
7076func TestLoad_MissingGraphCredentials (t * testing.T ) {
71- t .Setenv ("NATS_URL" , "nats://localhost:4222" )
77+ t .Setenv ("NATS_URL" , testNatsURL )
7278 // MS_GRAPH_MOCK_TOKEN is not set → credentials are required
7379
7480 cases := []struct {
@@ -87,7 +93,7 @@ func TestLoad_MissingGraphCredentials(t *testing.T) {
8793 t .Setenv ("MS_GRAPH_TENANT_ID" , "tenant" )
8894 t .Setenv ("MS_GRAPH_CLIENT_ID" , "client" )
8995 t .Setenv ("MS_GRAPH_CLIENT_SECRET" , "secret" )
90- t .Setenv ("MS_GRAPH_SENDER_EMAIL" , "sender@example.com" )
96+ t .Setenv ("MS_GRAPH_SENDER_EMAIL" , testSenderEmail )
9197 t .Setenv (tc .missing , "" )
9298
9399 _ , err := Load ()
@@ -99,7 +105,7 @@ func TestLoad_MissingGraphCredentials(t *testing.T) {
99105}
100106
101107func TestLoad_MockTokenSkipsCredentialCheck (t * testing.T ) {
102- t .Setenv ("NATS_URL" , "nats://localhost:4222" )
108+ t .Setenv ("NATS_URL" , testNatsURL )
103109 t .Setenv ("MS_GRAPH_MOCK_TOKEN" , "dev-token" )
104110 // deliberately leave Graph credentials unset
105111
@@ -122,7 +128,7 @@ func TestLoad_IntEnvOverrides(t *testing.T) {
122128
123129 cfg , err := Load ()
124130 if err != nil {
125- t .Fatalf ("unexpected error: %v" , err )
131+ t .Fatalf (unexpectedErr , err )
126132 }
127133 if cfg .SpamTimeoutSeconds != 30 {
128134 t .Errorf ("SpamTimeoutSeconds: want 30, got %d" , cfg .SpamTimeoutSeconds )
@@ -147,7 +153,7 @@ func TestLoad_InvalidIntFallsToDefault(t *testing.T) {
147153
148154 cfg , err := Load ()
149155 if err != nil {
150- t .Fatalf ("unexpected error: %v" , err )
156+ t .Fatalf (unexpectedErr , err )
151157 }
152158 if cfg .SpamTimeoutSeconds != 60 {
153159 t .Errorf ("expected default 60 on parse error, got %d" , cfg .SpamTimeoutSeconds )
@@ -159,9 +165,9 @@ func TestLoad_BounceMailboxDefaultsToSenderEmail(t *testing.T) {
159165
160166 cfg , err := Load ()
161167 if err != nil {
162- t .Fatalf ("unexpected error: %v" , err )
168+ t .Fatalf (unexpectedErr , err )
163169 }
164- if cfg .MSGraphBounceMailbox != "sender@example.com" {
170+ if cfg .MSGraphBounceMailbox != testSenderEmail {
165171 t .Errorf ("BounceMailbox: want sender@example.com, got %s" , cfg .MSGraphBounceMailbox )
166172 }
167173}
@@ -172,7 +178,7 @@ func TestLoad_BounceMailboxOverride(t *testing.T) {
172178
173179 cfg , err := Load ()
174180 if err != nil {
175- t .Fatalf ("unexpected error: %v" , err )
181+ t .Fatalf (unexpectedErr , err )
176182 }
177183 if cfg .MSGraphBounceMailbox != "bounces@example.com" {
178184 t .Errorf ("BounceMailbox: want bounces@example.com, got %s" , cfg .MSGraphBounceMailbox )
@@ -185,7 +191,7 @@ func TestLoad_ProxyURL(t *testing.T) {
185191
186192 cfg , err := Load ()
187193 if err != nil {
188- t .Fatalf ("unexpected error: %v" , err )
194+ t .Fatalf (unexpectedErr , err )
189195 }
190196 if cfg .GraphProxyURL != "http://localhost:8000" {
191197 t .Errorf ("GraphProxyURL: want http://localhost:8000, got %s" , cfg .GraphProxyURL )
@@ -198,7 +204,7 @@ func TestLoad_MimeWhitelistOverride(t *testing.T) {
198204
199205 cfg , err := Load ()
200206 if err != nil {
201- t .Fatalf ("unexpected error: %v" , err )
207+ t .Fatalf (unexpectedErr , err )
202208 }
203209 if len (cfg .MimeWhitelist ) != 2 {
204210 t .Fatalf ("MimeWhitelist: want 2 entries, got %d" , len (cfg .MimeWhitelist ))
0 commit comments