fixed sonarqube issues

This commit is contained in:
2025-11-25 15:18:13 +08:00
parent 052c7e0cca
commit 00250fb7c3
4 changed files with 45 additions and 41 deletions
+1
View File
@@ -7,4 +7,5 @@ const (
MessageLabel = "message" MessageLabel = "message"
ErrorEncodingResponse = "Error encoding response" ErrorEncodingResponse = "Error encoding response"
ErrorFailedtoLogLoginEvent = "Failed to log login event" ErrorFailedtoLogLoginEvent = "Failed to log login event"
WarningLabel = "WARNING:"
) )
+9 -6
View File
@@ -30,9 +30,10 @@ func LogWarn(message string) {
if goEnv == "" { if goEnv == "" {
log.Fatal("GO_ENV is not set in error_logging LogWarn. Please set the GO_ENV environment variable.") log.Fatal("GO_ENV is not set in error_logging LogWarn. Please set the GO_ENV environment variable.")
} }
if goEnv == "production" || goEnv == "canary" { switch goEnv {
case "production", "canary":
sentry.CaptureMessage("WARNING: " + message) sentry.CaptureMessage("WARNING: " + message)
} else if goEnv == "development" || goEnv == "debug" { case "development", "debug":
log.Println("WARNING:", message) log.Println("WARNING:", message)
} }
} }
@@ -45,14 +46,15 @@ func LogError(err error, message string) {
log.Fatal("GO_ENV is not set in error_logging LogError. Please set the GO_ENV environment variable.") log.Fatal("GO_ENV is not set in error_logging LogError. Please set the GO_ENV environment variable.")
} }
if goEnv == "production" || goEnv == "canary" { switch goEnv {
case "production", "canary":
if err != nil { if err != nil {
sentry.CaptureException(err) sentry.CaptureException(err)
} else { } else {
sentry.CaptureMessage("ERROR: " + message) sentry.CaptureMessage("ERROR: " + message)
} }
log.Printf("ERROR: %s: %v", message, err) log.Printf("ERROR: %s: %v", message, err)
} else if goEnv == "development" || goEnv == "debug" { case "development", "debug":
if err != nil { if err != nil {
log.Printf("ERROR: %s: %v", message, err) log.Printf("ERROR: %s: %v", message, err)
} else { } else {
@@ -69,14 +71,15 @@ func LogFatal(err error, message string) {
log.Fatal("GO_ENV is not set in error_logging LogFatal. Please set the GO_ENV environment variable.") log.Fatal("GO_ENV is not set in error_logging LogFatal. Please set the GO_ENV environment variable.")
} }
if goEnv == "production" || goEnv == "canary" { switch goEnv {
case "production", "canary":
if err != nil { if err != nil {
sentry.CaptureException(err) sentry.CaptureException(err)
} else { } else {
sentry.CaptureMessage("FATAL: " + message) sentry.CaptureMessage("FATAL: " + message)
} }
log.Fatalf("FATAL: %s: %v", message, err) log.Fatalf("FATAL: %s: %v", message, err)
} else if goEnv == "development" || goEnv == "debug" { case "development", "debug":
if err != nil { if err != nil {
log.Fatalf("FATAL: %s: %v", message, err) log.Fatalf("FATAL: %s: %v", message, err)
} else { } else {
+21 -21
View File
@@ -17,7 +17,7 @@ func captureLogOutput(f func()) string {
return buf.String() return buf.String()
} }
func TestLogInfo_Development(t *testing.T) { func TestLogInfoDevelopment(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -36,7 +36,7 @@ func TestLogInfo_Development(t *testing.T) {
} }
} }
func TestLogInfo_Debug(t *testing.T) { func TestLogInfoDebug(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -55,7 +55,7 @@ func TestLogInfo_Debug(t *testing.T) {
} }
} }
func TestLogInfo_Production(t *testing.T) { func TestLogInfoProduction(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -74,7 +74,7 @@ func TestLogInfo_Production(t *testing.T) {
} }
} }
func TestLogInfo_NoEnv(t *testing.T) { func TestLogInfoNoEnv(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -86,7 +86,7 @@ func TestLogInfo_NoEnv(t *testing.T) {
t.Skip("Cannot test log.Fatal without subprocess") t.Skip("Cannot test log.Fatal without subprocess")
} }
func TestLogWarn_Development(t *testing.T) { func TestLogWarnDevelopment(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -96,7 +96,7 @@ func TestLogWarn_Development(t *testing.T) {
LogWarn("Test warning") LogWarn("Test warning")
}) })
if !strings.Contains(output, "WARNING:") { if !strings.Contains(output, WarningLabel) {
t.Error("Expected WARNING prefix in log output") t.Error("Expected WARNING prefix in log output")
} }
@@ -105,7 +105,7 @@ func TestLogWarn_Development(t *testing.T) {
} }
} }
func TestLogWarn_Debug(t *testing.T) { func TestLogWarnDebug(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -115,12 +115,12 @@ func TestLogWarn_Debug(t *testing.T) {
LogWarn("Debug warning") LogWarn("Debug warning")
}) })
if !strings.Contains(output, "WARNING:") { if !strings.Contains(output, WarningLabel) {
t.Error("Expected WARNING prefix in log output") t.Error("Expected WARNING prefix in log output")
} }
} }
func TestLogError_Development(t *testing.T) { func TestLogErrorDevelopment(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -145,7 +145,7 @@ func TestLogError_Development(t *testing.T) {
} }
} }
func TestLogError_NilError(t *testing.T) { func TestLogErrorNilError(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -164,7 +164,7 @@ func TestLogError_NilError(t *testing.T) {
} }
} }
func TestLogError_Debug(t *testing.T) { func TestLogErrorDebug(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -193,7 +193,7 @@ func (e *testError) Error() string {
return e.msg return e.msg
} }
func TestLogInfo_EmptyMessage(t *testing.T) { func TestLogInfoEmptyMessage(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -208,7 +208,7 @@ func TestLogInfo_EmptyMessage(t *testing.T) {
} }
} }
func TestLogWarn_EmptyMessage(t *testing.T) { func TestLogWarnEmptyMessage(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -218,12 +218,12 @@ func TestLogWarn_EmptyMessage(t *testing.T) {
LogWarn("") LogWarn("")
}) })
if !strings.Contains(output, "WARNING:") { if !strings.Contains(output, WarningLabel) {
t.Error("Expected WARNING prefix even with empty message") t.Error("Expected WARNING prefix even with empty message")
} }
} }
func TestLogError_EmptyMessage(t *testing.T) { func TestLogErrorEmptyMessage(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -238,7 +238,7 @@ func TestLogError_EmptyMessage(t *testing.T) {
} }
} }
func TestLogInfo_LongMessage(t *testing.T) { func TestLogInfoLongMessage(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -255,7 +255,7 @@ func TestLogInfo_LongMessage(t *testing.T) {
} }
} }
func TestLogWarn_SpecialCharacters(t *testing.T) { func TestLogWarnSpecialCharacters(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -267,12 +267,12 @@ func TestLogWarn_SpecialCharacters(t *testing.T) {
LogWarn(specialMsg) LogWarn(specialMsg)
}) })
if !strings.Contains(output, "WARNING:") { if !strings.Contains(output, WarningLabel) {
t.Error("Expected WARNING prefix") t.Error("Expected WARNING prefix")
} }
} }
func TestLogError_MultilineMessage(t *testing.T) { func TestLogErrorMultilineMessage(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -289,7 +289,7 @@ func TestLogError_MultilineMessage(t *testing.T) {
} }
} }
func TestLogInfo_Canary(t *testing.T) { func TestLogInfoCanary(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
@@ -329,7 +329,7 @@ func TestLogEnvironmentCheck(t *testing.T) {
} }
} }
func TestLogError_WithAndWithoutError(t *testing.T) { func TestLogErrorWithAndWithoutError(t *testing.T) {
originalEnv := os.Getenv("GO_ENV") originalEnv := os.Getenv("GO_ENV")
defer os.Setenv("GO_ENV", originalEnv) defer os.Setenv("GO_ENV", originalEnv)
+14 -14
View File
@@ -10,7 +10,7 @@ import (
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
) )
func TestExtractEmailFromToken_ValidAccessToken(t *testing.T) { func TestExtractEmailFromTokenValidAccessToken(t *testing.T) {
// Set up test environment // Set up test environment
secretKey := "test-secret-key-123" secretKey := "test-secret-key-123"
os.Setenv("JWT_SECRET_KEY", secretKey) os.Setenv("JWT_SECRET_KEY", secretKey)
@@ -45,7 +45,7 @@ func TestExtractEmailFromToken_ValidAccessToken(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_ValidMapClaims(t *testing.T) { func TestExtractEmailFromTokenValidMapClaims(t *testing.T) {
secretKey := "test-secret-key-456" secretKey := "test-secret-key-456"
os.Setenv("JWT_SECRET_KEY", secretKey) os.Setenv("JWT_SECRET_KEY", secretKey)
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")
@@ -76,7 +76,7 @@ func TestExtractEmailFromToken_ValidMapClaims(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_BearerPrefix(t *testing.T) { func TestExtractEmailFromTokenBearerPrefix(t *testing.T) {
secretKey := "test-secret-bearer" secretKey := "test-secret-bearer"
os.Setenv("JWT_SECRET_KEY", secretKey) os.Setenv("JWT_SECRET_KEY", secretKey)
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")
@@ -109,7 +109,7 @@ func TestExtractEmailFromToken_BearerPrefix(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_EmptyToken(t *testing.T) { func TestExtractEmailFromTokenEmptyToken(t *testing.T) {
os.Setenv("JWT_SECRET_KEY", "test-key") os.Setenv("JWT_SECRET_KEY", "test-key")
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")
@@ -127,7 +127,7 @@ func TestExtractEmailFromToken_EmptyToken(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_InvalidSignature(t *testing.T) { func TestExtractEmailFromTokenInvalidSignature(t *testing.T) {
os.Setenv("JWT_SECRET_KEY", "correct-secret") os.Setenv("JWT_SECRET_KEY", "correct-secret")
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")
@@ -155,7 +155,7 @@ func TestExtractEmailFromToken_InvalidSignature(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_ExpiredToken(t *testing.T) { func TestExtractEmailFromTokenExpiredToken(t *testing.T) {
secretKey := "test-expired-key" secretKey := "test-expired-key"
os.Setenv("JWT_SECRET_KEY", secretKey) os.Setenv("JWT_SECRET_KEY", secretKey)
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")
@@ -182,7 +182,7 @@ func TestExtractEmailFromToken_ExpiredToken(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_NoEmailInClaims(t *testing.T) { func TestExtractEmailFromTokenNoEmailInClaims(t *testing.T) {
secretKey := "test-no-email" secretKey := "test-no-email"
os.Setenv("JWT_SECRET_KEY", secretKey) os.Setenv("JWT_SECRET_KEY", secretKey)
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")
@@ -206,7 +206,7 @@ func TestExtractEmailFromToken_NoEmailInClaims(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_InvalidEmailFormat(t *testing.T) { func TestExtractEmailFromTokenInvalidEmailFormat(t *testing.T) {
secretKey := "test-invalid-email" secretKey := "test-invalid-email"
os.Setenv("JWT_SECRET_KEY", secretKey) os.Setenv("JWT_SECRET_KEY", secretKey)
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")
@@ -229,7 +229,7 @@ func TestExtractEmailFromToken_InvalidEmailFormat(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_NoSecretKey(t *testing.T) { func TestExtractEmailFromTokenNoSecretKey(t *testing.T) {
// Ensure no secret key is set // Ensure no secret key is set
os.Unsetenv("JWT_SECRET_KEY") os.Unsetenv("JWT_SECRET_KEY")
@@ -250,7 +250,7 @@ func TestExtractEmailFromToken_NoSecretKey(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_WrongSigningMethod(t *testing.T) { func TestExtractEmailFromTokenWrongSigningMethod(t *testing.T) {
secretKey := "test-wrong-method" secretKey := "test-wrong-method"
os.Setenv("JWT_SECRET_KEY", secretKey) os.Setenv("JWT_SECRET_KEY", secretKey)
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")
@@ -265,7 +265,7 @@ func TestExtractEmailFromToken_WrongSigningMethod(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_MalformedToken(t *testing.T) { func TestExtractEmailFromTokenMalformedToken(t *testing.T) {
os.Setenv("JWT_SECRET_KEY", "test-key") os.Setenv("JWT_SECRET_KEY", "test-key")
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")
@@ -285,7 +285,7 @@ func TestExtractEmailFromToken_MalformedToken(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_MultipleAtSymbols(t *testing.T) { func TestExtractEmailFromTokenMultipleAtSymbols(t *testing.T) {
secretKey := "test-multiple-at" secretKey := "test-multiple-at"
os.Setenv("JWT_SECRET_KEY", secretKey) os.Setenv("JWT_SECRET_KEY", secretKey)
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")
@@ -314,7 +314,7 @@ func TestExtractEmailFromToken_MultipleAtSymbols(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_WhitespaceEmail(t *testing.T) { func TestExtractEmailFromTokenWhitespaceEmail(t *testing.T) {
secretKey := "test-whitespace" secretKey := "test-whitespace"
os.Setenv("JWT_SECRET_KEY", secretKey) os.Setenv("JWT_SECRET_KEY", secretKey)
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")
@@ -340,7 +340,7 @@ func TestExtractEmailFromToken_WhitespaceEmail(t *testing.T) {
} }
} }
func TestExtractEmailFromToken_CaseInsensitiveBearer(t *testing.T) { func TestExtractEmailFromTokenCaseInsensitiveBearer(t *testing.T) {
secretKey := "test-case-bearer" secretKey := "test-case-bearer"
os.Setenv("JWT_SECRET_KEY", secretKey) os.Setenv("JWT_SECRET_KEY", secretKey)
defer os.Unsetenv("JWT_SECRET_KEY") defer os.Unsetenv("JWT_SECRET_KEY")