From 00250fb7c39368d1984a7b22e6a5ebbe99df05dc Mon Sep 17 00:00:00 2001 From: F04C Date: Tue, 25 Nov 2025 15:18:13 +0800 Subject: [PATCH] fixed sonarqube issues --- helper/constants.go | 1 + helper/error_logging.go | 15 +++++---- helper/error_logging_test.go | 42 ++++++++++++------------- helper/extract_email_from_token_test.go | 28 ++++++++--------- 4 files changed, 45 insertions(+), 41 deletions(-) diff --git a/helper/constants.go b/helper/constants.go index 385daeb..4b7748d 100644 --- a/helper/constants.go +++ b/helper/constants.go @@ -7,4 +7,5 @@ const ( MessageLabel = "message" ErrorEncodingResponse = "Error encoding response" ErrorFailedtoLogLoginEvent = "Failed to log login event" + WarningLabel = "WARNING:" ) diff --git a/helper/error_logging.go b/helper/error_logging.go index 62ede5c..c6bc698 100644 --- a/helper/error_logging.go +++ b/helper/error_logging.go @@ -30,9 +30,10 @@ func LogWarn(message string) { if goEnv == "" { 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) - } else if goEnv == "development" || goEnv == "debug" { + case "development", "debug": 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.") } - if goEnv == "production" || goEnv == "canary" { + switch goEnv { + case "production", "canary": if err != nil { sentry.CaptureException(err) } else { sentry.CaptureMessage("ERROR: " + message) } log.Printf("ERROR: %s: %v", message, err) - } else if goEnv == "development" || goEnv == "debug" { + case "development", "debug": if err != nil { log.Printf("ERROR: %s: %v", message, err) } 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.") } - if goEnv == "production" || goEnv == "canary" { + switch goEnv { + case "production", "canary": if err != nil { sentry.CaptureException(err) } else { sentry.CaptureMessage("FATAL: " + message) } log.Fatalf("FATAL: %s: %v", message, err) - } else if goEnv == "development" || goEnv == "debug" { + case "development", "debug": if err != nil { log.Fatalf("FATAL: %s: %v", message, err) } else { diff --git a/helper/error_logging_test.go b/helper/error_logging_test.go index 01de749..ba5186f 100644 --- a/helper/error_logging_test.go +++ b/helper/error_logging_test.go @@ -17,7 +17,7 @@ func captureLogOutput(f func()) string { return buf.String() } -func TestLogInfo_Development(t *testing.T) { +func TestLogInfoDevelopment(t *testing.T) { originalEnv := os.Getenv("GO_ENV") 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") 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") 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") defer os.Setenv("GO_ENV", originalEnv) @@ -86,7 +86,7 @@ func TestLogInfo_NoEnv(t *testing.T) { t.Skip("Cannot test log.Fatal without subprocess") } -func TestLogWarn_Development(t *testing.T) { +func TestLogWarnDevelopment(t *testing.T) { originalEnv := os.Getenv("GO_ENV") defer os.Setenv("GO_ENV", originalEnv) @@ -96,7 +96,7 @@ func TestLogWarn_Development(t *testing.T) { LogWarn("Test warning") }) - if !strings.Contains(output, "WARNING:") { + if !strings.Contains(output, WarningLabel) { 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") defer os.Setenv("GO_ENV", originalEnv) @@ -115,12 +115,12 @@ func TestLogWarn_Debug(t *testing.T) { LogWarn("Debug warning") }) - if !strings.Contains(output, "WARNING:") { + if !strings.Contains(output, WarningLabel) { t.Error("Expected WARNING prefix in log output") } } -func TestLogError_Development(t *testing.T) { +func TestLogErrorDevelopment(t *testing.T) { originalEnv := os.Getenv("GO_ENV") 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") 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") defer os.Setenv("GO_ENV", originalEnv) @@ -193,7 +193,7 @@ func (e *testError) Error() string { return e.msg } -func TestLogInfo_EmptyMessage(t *testing.T) { +func TestLogInfoEmptyMessage(t *testing.T) { originalEnv := os.Getenv("GO_ENV") 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") defer os.Setenv("GO_ENV", originalEnv) @@ -218,12 +218,12 @@ func TestLogWarn_EmptyMessage(t *testing.T) { LogWarn("") }) - if !strings.Contains(output, "WARNING:") { + if !strings.Contains(output, WarningLabel) { 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") 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") 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") defer os.Setenv("GO_ENV", originalEnv) @@ -267,12 +267,12 @@ func TestLogWarn_SpecialCharacters(t *testing.T) { LogWarn(specialMsg) }) - if !strings.Contains(output, "WARNING:") { + if !strings.Contains(output, WarningLabel) { t.Error("Expected WARNING prefix") } } -func TestLogError_MultilineMessage(t *testing.T) { +func TestLogErrorMultilineMessage(t *testing.T) { originalEnv := os.Getenv("GO_ENV") 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") 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") defer os.Setenv("GO_ENV", originalEnv) diff --git a/helper/extract_email_from_token_test.go b/helper/extract_email_from_token_test.go index 6772943..f3d94e5 100644 --- a/helper/extract_email_from_token_test.go +++ b/helper/extract_email_from_token_test.go @@ -10,7 +10,7 @@ import ( "github.com/golang-jwt/jwt/v5" ) -func TestExtractEmailFromToken_ValidAccessToken(t *testing.T) { +func TestExtractEmailFromTokenValidAccessToken(t *testing.T) { // Set up test environment secretKey := "test-secret-key-123" 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" os.Setenv("JWT_SECRET_KEY", secretKey) 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" os.Setenv("JWT_SECRET_KEY", secretKey) 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") 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") 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" os.Setenv("JWT_SECRET_KEY", secretKey) 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" os.Setenv("JWT_SECRET_KEY", secretKey) 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" os.Setenv("JWT_SECRET_KEY", secretKey) 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 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" os.Setenv("JWT_SECRET_KEY", secretKey) 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") 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" os.Setenv("JWT_SECRET_KEY", secretKey) 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" os.Setenv("JWT_SECRET_KEY", secretKey) 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" os.Setenv("JWT_SECRET_KEY", secretKey) defer os.Unsetenv("JWT_SECRET_KEY")