Merge pull request #50 from rede5/codex/ampliar-testes-em-10-por-cento

test: expand config package tests (BuyerFeeRate, BackendHost, SwaggerSchemes)
This commit is contained in:
Tiago Yamamoto 2026-01-01 11:46:43 -03:00 committed by GitHub
commit 2ff6034e76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -65,10 +65,13 @@ func TestLoadFromEnv(t *testing.T) {
os.Setenv("BACKEND_PORT", "9999")
os.Setenv("DATABASE_URL", "postgres://test:test@localhost:5432/test")
os.Setenv("MARKETPLACE_COMMISSION", "5.0")
os.Setenv("BUYER_FEE_RATE", "0.2")
os.Setenv("JWT_SECRET", "super-secret")
os.Setenv("JWT_EXPIRES_IN", "12h")
os.Setenv("PASSWORD_PEPPER", "pepper123")
os.Setenv("CORS_ORIGINS", "https://example.com,https://app.example.com")
os.Setenv("BACKEND_HOST", "api.test.local")
os.Setenv("SWAGGER_SCHEMES", "https, http")
os.Setenv("ADMIN_NAME", "CustomAdmin")
os.Setenv("ADMIN_USERNAME", "customadmin")
os.Setenv("ADMIN_EMAIL", "custom@example.com")
@ -79,10 +82,13 @@ func TestLoadFromEnv(t *testing.T) {
os.Unsetenv("BACKEND_PORT")
os.Unsetenv("DATABASE_URL")
os.Unsetenv("MARKETPLACE_COMMISSION")
os.Unsetenv("BUYER_FEE_RATE")
os.Unsetenv("JWT_SECRET")
os.Unsetenv("JWT_EXPIRES_IN")
os.Unsetenv("PASSWORD_PEPPER")
os.Unsetenv("CORS_ORIGINS")
os.Unsetenv("BACKEND_HOST")
os.Unsetenv("SWAGGER_SCHEMES")
os.Unsetenv("ADMIN_NAME")
os.Unsetenv("ADMIN_USERNAME")
os.Unsetenv("ADMIN_EMAIL")
@ -103,6 +109,9 @@ func TestLoadFromEnv(t *testing.T) {
if cfg.MarketplaceCommission != 5.0 {
t.Errorf("expected MarketplaceCommission 5.0, got %f", cfg.MarketplaceCommission)
}
if cfg.BuyerFeeRate != 0.2 {
t.Errorf("expected BuyerFeeRate 0.2, got %f", cfg.BuyerFeeRate)
}
if cfg.JWTSecret != "super-secret" {
t.Errorf("expected JWTSecret 'super-secret', got '%s'", cfg.JWTSecret)
}
@ -115,6 +124,12 @@ func TestLoadFromEnv(t *testing.T) {
if len(cfg.CORSOrigins) != 2 {
t.Errorf("expected 2 CORS origins, got %d", len(cfg.CORSOrigins))
}
if cfg.BackendHost != "api.test.local" {
t.Errorf("expected BackendHost 'api.test.local', got '%s'", cfg.BackendHost)
}
if len(cfg.SwaggerSchemes) != 2 || cfg.SwaggerSchemes[0] != "https" || cfg.SwaggerSchemes[1] != "http" {
t.Errorf("expected SwaggerSchemes [https http], got %v", cfg.SwaggerSchemes)
}
if cfg.AdminName != "CustomAdmin" {
t.Errorf("expected AdminName 'CustomAdmin', got '%s'", cfg.AdminName)
}
@ -139,9 +154,13 @@ func TestAddr(t *testing.T) {
func TestInvalidEnvValues(t *testing.T) {
os.Setenv("MARKETPLACE_COMMISSION", "invalid")
os.Setenv("JWT_EXPIRES_IN", "not-a-duration")
os.Setenv("BUYER_FEE_RATE", "invalid-rate")
defer func() {
os.Unsetenv("MARKETPLACE_COMMISSION")
os.Unsetenv("JWT_EXPIRES_IN")
os.Unsetenv("BUYER_FEE_RATE")
}()
cfg := Load()
@ -150,6 +169,12 @@ func TestInvalidEnvValues(t *testing.T) {
if cfg.MarketplaceCommission != 2.5 {
t.Errorf("expected fallback MarketplaceCommission 2.5, got %f", cfg.MarketplaceCommission)
}
if cfg.JWTExpiresIn != 24*time.Hour {
t.Errorf("expected fallback JWTExpiresIn 24h, got %v", cfg.JWTExpiresIn)
}
if cfg.BuyerFeeRate != 0.12 {
t.Errorf("expected fallback BuyerFeeRate 0.12, got %f", cfg.BuyerFeeRate)
}
}
func TestEmptyCORSOrigins(t *testing.T) {
@ -162,3 +187,14 @@ func TestEmptyCORSOrigins(t *testing.T) {
t.Errorf("expected fallback CORSOrigins ['*'], got %v", cfg.CORSOrigins)
}
}
func TestSwaggerSchemesTrimmed(t *testing.T) {
os.Setenv("SWAGGER_SCHEMES", " https , ,http,")
defer os.Unsetenv("SWAGGER_SCHEMES")
cfg := Load()
if len(cfg.SwaggerSchemes) != 2 || cfg.SwaggerSchemes[0] != "https" || cfg.SwaggerSchemes[1] != "http" {
t.Errorf("expected SwaggerSchemes [https http], got %v", cfg.SwaggerSchemes)
}
}