fixed sonarqube issues

This commit is contained in:
2025-12-17 10:01:58 +08:00
parent e6b3e3b3ae
commit c4c03163ea
4 changed files with 85 additions and 78 deletions
+1 -1
View File
@@ -201,7 +201,7 @@ func TestRateLimiterMiddlewareRedisNotAvailable(t *testing.T) {
}
}
func TestRateLimiterMiddleware_AllowsRequest(t *testing.T) {
func TestRateLimiterMiddlewareAllowsRequest(t *testing.T) {
db, mock := redismock.NewClientMock()
originalRedis := redisclient.RDB
redisclient.RDB = db
+18 -18
View File
@@ -56,7 +56,7 @@ func TestInitDefaultValues(t *testing.T) {
}
}
func TestInit_WithPassword(t *testing.T) {
func TestInitWithPassword(t *testing.T) {
// Save original values
originalHost := os.Getenv("REDIS_HOST")
originalPort := os.Getenv("REDIS_PORT")
@@ -101,7 +101,7 @@ func TestInit_WithPassword(t *testing.T) {
}
}
func TestInit_CustomHostAndPort(t *testing.T) {
func TestInitCustomHostAndPort(t *testing.T) {
// Save original values
originalHost := os.Getenv("REDIS_HOST")
originalPort := os.Getenv("REDIS_PORT")
@@ -142,7 +142,7 @@ func TestInit_CustomHostAndPort(t *testing.T) {
}
}
func TestInit_ConnectionFailure(t *testing.T) {
func TestInitConnectionFailure(t *testing.T) {
// Save original values
originalHost := os.Getenv("REDIS_HOST")
originalPort := os.Getenv("REDIS_PORT")
@@ -171,7 +171,7 @@ func TestInit_ConnectionFailure(t *testing.T) {
Init()
}
func TestInit_SecuritySettings(t *testing.T) {
func TestInitSecuritySettings(t *testing.T) {
// Save original values
originalHost := os.Getenv("REDIS_HOST")
originalPort := os.Getenv("REDIS_PORT")
@@ -209,7 +209,7 @@ func TestInit_SecuritySettings(t *testing.T) {
}
}
func TestInit_DBNumber(t *testing.T) {
func TestInitDBNumber(t *testing.T) {
// Save original values
originalHost := os.Getenv("REDIS_HOST")
originalPort := os.Getenv("REDIS_PORT")
@@ -244,7 +244,7 @@ func TestInit_DBNumber(t *testing.T) {
}
}
func TestRDB_GlobalVariable(t *testing.T) {
func TestRDBGlobalVariable(t *testing.T) {
// Test that RDB is a package-level variable
originalRDB := RDB
defer func() { RDB = originalRDB }()
@@ -263,7 +263,7 @@ func TestRDB_GlobalVariable(t *testing.T) {
}
}
func TestInit_EnvironmentDefaults(t *testing.T) {
func TestInitEnvironmentDefaults(t *testing.T) {
tests := []struct {
name string
redisHost string
@@ -351,7 +351,7 @@ func TestInit_EnvironmentDefaults(t *testing.T) {
// Additional comprehensive test cases
func TestInit_SetGetOperations(t *testing.T) {
func TestInitSetGetOperations(t *testing.T) {
originalRDB := RDB
defer func() { RDB = originalRDB }()
@@ -388,7 +388,7 @@ func TestInit_SetGetOperations(t *testing.T) {
}
}
func TestInit_KeyExpiration(t *testing.T) {
func TestInitKeyExpiration(t *testing.T) {
originalRDB := RDB
defer func() { RDB = originalRDB }()
@@ -425,7 +425,7 @@ func TestInit_KeyExpiration(t *testing.T) {
}
}
func TestInit_MultipleKeys(t *testing.T) {
func TestInitMultipleKeys(t *testing.T) {
originalRDB := RDB
defer func() { RDB = originalRDB }()
@@ -472,7 +472,7 @@ func TestInit_MultipleKeys(t *testing.T) {
}
}
func TestInit_DeleteOperation(t *testing.T) {
func TestInitDeleteOperation(t *testing.T) {
originalRDB := RDB
defer func() { RDB = originalRDB }()
@@ -508,7 +508,7 @@ func TestInit_DeleteOperation(t *testing.T) {
}
}
func TestInit_LargeValue(t *testing.T) {
func TestInitLargeValue(t *testing.T) {
originalRDB := RDB
defer func() { RDB = originalRDB }()
@@ -549,7 +549,7 @@ func TestInit_LargeValue(t *testing.T) {
}
}
func TestInit_SpecialCharactersInKey(t *testing.T) {
func TestInitSpecialCharactersInKey(t *testing.T) {
originalRDB := RDB
defer func() { RDB = originalRDB }()
@@ -594,7 +594,7 @@ func TestInit_SpecialCharactersInKey(t *testing.T) {
}
}
func TestInit_ConcurrentOperations(t *testing.T) {
func TestInitConcurrentOperations(t *testing.T) {
originalRDB := RDB
defer func() { RDB = originalRDB }()
@@ -644,7 +644,7 @@ func TestInit_ConcurrentOperations(t *testing.T) {
}
}
func TestInit_ExistsOperation(t *testing.T) {
func TestInitExistsOperation(t *testing.T) {
originalRDB := RDB
defer func() { RDB = originalRDB }()
@@ -686,7 +686,7 @@ func TestInit_ExistsOperation(t *testing.T) {
}
}
func TestInit_TTLOperation(t *testing.T) {
func TestInitTTLOperation(t *testing.T) {
originalRDB := RDB
defer func() { RDB = originalRDB }()
@@ -720,7 +720,7 @@ func TestInit_TTLOperation(t *testing.T) {
}
}
func TestInit_InvalidPortFormat(t *testing.T) {
func TestInitInvalidPortFormat(t *testing.T) {
originalRDB := RDB
defer func() { RDB = originalRDB }()
@@ -744,7 +744,7 @@ func TestInit_InvalidPortFormat(t *testing.T) {
Init()
}
func TestInit_EmptyHostAndPort(t *testing.T) {
func TestInitEmptyHostAndPort(t *testing.T) {
originalRDB := RDB
defer func() { RDB = originalRDB }()
+27 -27
View File
@@ -28,7 +28,7 @@ func setupMockDB(t *testing.T) (sqlmock.Sqlmock, func()) {
return mock, cleanup
}
func TestGetPermissionByResourceAndAction_Success(t *testing.T) {
func TestGetPermissionByResourceAndActionSuccess(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -62,7 +62,7 @@ func TestGetPermissionByResourceAndAction_Success(t *testing.T) {
}
}
func TestGetPermissionByResourceAndAction_NotFound(t *testing.T) {
func TestGetPermissionByResourceAndActionNotFound(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -80,7 +80,7 @@ func TestGetPermissionByResourceAndAction_NotFound(t *testing.T) {
}
}
func TestGetPermissionByResourceAndAction_DatabaseError(t *testing.T) {
func TestGetPermissionByResourceAndActionDatabaseError(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -98,7 +98,7 @@ func TestGetPermissionByResourceAndAction_DatabaseError(t *testing.T) {
}
}
func TestGetPolicyAttributesByPermission_Success(t *testing.T) {
func TestGetPolicyAttributesByPermissionSuccess(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -123,7 +123,7 @@ func TestGetPolicyAttributesByPermission_Success(t *testing.T) {
}
}
func TestGetPolicyAttributesByPermission_Empty(t *testing.T) {
func TestGetPolicyAttributesByPermissionEmpty(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -143,7 +143,7 @@ func TestGetPolicyAttributesByPermission_Empty(t *testing.T) {
}
}
func TestGetUserAttributes_Success(t *testing.T) {
func TestGetUserAttributesSuccess(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -171,7 +171,7 @@ func TestGetUserAttributes_Success(t *testing.T) {
}
}
func TestGetUserByID_Success(t *testing.T) {
func TestGetUserByIDSuccess(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -209,7 +209,7 @@ func TestGetUserByID_Success(t *testing.T) {
}
}
func TestGetUserByID_NotFound(t *testing.T) {
func TestGetUserByIDNotFound(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -227,7 +227,7 @@ func TestGetUserByID_NotFound(t *testing.T) {
}
}
func TestGetAllPermissions_Success(t *testing.T) {
func TestGetAllPermissionsSuccess(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -248,7 +248,7 @@ func TestGetAllPermissions_Success(t *testing.T) {
}
}
func TestGetAllPolicyAttributes_Success(t *testing.T) {
func TestGetAllPolicyAttributesSuccess(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -276,7 +276,7 @@ func TestGetAllPolicyAttributes_Success(t *testing.T) {
}
}
func TestGetAllPolicyAttributes_Empty(t *testing.T) {
func TestGetAllPolicyAttributesEmpty(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -297,7 +297,7 @@ func TestGetAllPolicyAttributes_Empty(t *testing.T) {
// Additional comprehensive test cases
func TestGetPermissionByResourceAndAction_EmptyResource(t *testing.T) {
func TestGetPermissionByResourceAndActionEmptyResource(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -318,7 +318,7 @@ func TestGetPermissionByResourceAndAction_EmptyResource(t *testing.T) {
}
}
func TestGetPermissionByResourceAndAction_EmptyAction(t *testing.T) {
func TestGetPermissionByResourceAndActionEmptyAction(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -339,7 +339,7 @@ func TestGetPermissionByResourceAndAction_EmptyAction(t *testing.T) {
}
}
func TestGetPermissionByResourceAndAction_SpecialCharacters(t *testing.T) {
func TestGetPermissionByResourceAndActionSpecialCharacters(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -360,7 +360,7 @@ func TestGetPermissionByResourceAndAction_SpecialCharacters(t *testing.T) {
}
}
func TestGetPolicyAttributesByPermission_InvalidID(t *testing.T) {
func TestGetPolicyAttributesByPermissionInvalidID(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -381,7 +381,7 @@ func TestGetPolicyAttributesByPermission_InvalidID(t *testing.T) {
}
}
func TestGetPolicyAttributesByPermission_DatabaseError(t *testing.T) {
func TestGetPolicyAttributesByPermissionDatabaseError(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -399,7 +399,7 @@ func TestGetPolicyAttributesByPermission_DatabaseError(t *testing.T) {
}
}
func TestGetUserAttributes_EmptyUserID(t *testing.T) {
func TestGetUserAttributesEmptyUserID(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -420,7 +420,7 @@ func TestGetUserAttributes_EmptyUserID(t *testing.T) {
}
}
func TestGetUserAttributes_MultipleAttributes(t *testing.T) {
func TestGetUserAttributesMultipleAttributes(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -446,7 +446,7 @@ func TestGetUserAttributes_MultipleAttributes(t *testing.T) {
}
}
func TestGetUserByID_EmptyID(t *testing.T) {
func TestGetUserByIDEmptyID(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -473,7 +473,7 @@ func TestGetUserByID_EmptyID(t *testing.T) {
}
}
func TestGetUserByID_DatabaseError(t *testing.T) {
func TestGetUserByIDDatabaseError(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -491,7 +491,7 @@ func TestGetUserByID_DatabaseError(t *testing.T) {
}
}
func TestGetAllPermissions_DatabaseError(t *testing.T) {
func TestGetAllPermissionsDatabaseError(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -508,7 +508,7 @@ func TestGetAllPermissions_DatabaseError(t *testing.T) {
}
}
func TestGetAllPermissions_Empty(t *testing.T) {
func TestGetAllPermissionsEmpty(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -527,7 +527,7 @@ func TestGetAllPermissions_Empty(t *testing.T) {
}
}
func TestGetAllPermissions_LargeDataset(t *testing.T) {
func TestGetAllPermissionsLargeDataset(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -549,7 +549,7 @@ func TestGetAllPermissions_LargeDataset(t *testing.T) {
}
}
func TestGetAllPolicyAttributes_DatabaseError(t *testing.T) {
func TestGetAllPolicyAttributesDatabaseError(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -566,7 +566,7 @@ func TestGetAllPolicyAttributes_DatabaseError(t *testing.T) {
}
}
func TestGetAllPolicyAttributes_ManyPermissions(t *testing.T) {
func TestGetAllPolicyAttributesManyPermissions(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -599,7 +599,7 @@ func TestGetAllPolicyAttributes_ManyPermissions(t *testing.T) {
}
}
func TestGetUserAttributes_DatabaseError(t *testing.T) {
func TestGetUserAttributesDatabaseError(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
@@ -617,7 +617,7 @@ func TestGetUserAttributes_DatabaseError(t *testing.T) {
}
}
func TestGetPermissionByResourceAndAction_ScanError(t *testing.T) {
func TestGetPermissionByResourceAndActionScanError(t *testing.T) {
mock, cleanup := setupMockDB(t)
defer cleanup()
+39 -32
View File
@@ -5,6 +5,13 @@ import (
"testing"
)
const (
testCompareFormat = "compare(%q, %q, %q) = %v, want %v"
testAdminUserGuest = "admin,user,guest"
testHelloWorld = "hello world"
testHelloWorldCased = "Hello World"
)
func TestResolveVariables(t *testing.T) {
tests := []struct {
name string
@@ -71,7 +78,7 @@ func TestResolveVariables(t *testing.T) {
}
}
func TestCompare_Equality(t *testing.T) {
func TestCompareEquality(t *testing.T) {
tests := []struct {
name string
actual string
@@ -90,13 +97,13 @@ func TestCompare_Equality(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got := compare(tt.actual, tt.expected, tt.operator)
if got != tt.want {
t.Errorf("compare(%q, %q, %q) = %v, want %v", tt.actual, tt.expected, tt.operator, got, tt.want)
t.Errorf(testCompareFormat, tt.actual, tt.expected, tt.operator, got, tt.want)
}
})
}
}
func TestCompare_Numeric(t *testing.T) {
func TestCompareNumeric(t *testing.T) {
tests := []struct {
name string
actual string
@@ -122,23 +129,23 @@ func TestCompare_Numeric(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got := compare(tt.actual, tt.expected, tt.operator)
if got != tt.want {
t.Errorf("compare(%q, %q, %q) = %v, want %v", tt.actual, tt.expected, tt.operator, got, tt.want)
t.Errorf(testCompareFormat, tt.actual, tt.expected, tt.operator, got, tt.want)
}
})
}
}
func TestCompare_IN(t *testing.T) {
func TestCompareIN(t *testing.T) {
tests := []struct {
name string
actual string
expected string
want bool
}{
{"value in list", "admin", "admin,user,guest", true},
{"value not in list", "superuser", "admin,user,guest", false},
{"value in list", "admin", testAdminUserGuest, true},
{"value not in list", "superuser", testAdminUserGuest, false},
{"value in list with spaces", "admin", " admin , user , guest ", true},
{"case insensitive match", "ADMIN", "admin,user,guest", true},
{"case insensitive match", "ADMIN", testAdminUserGuest, true},
{"single value match", "admin", "admin", true},
{"empty list", "admin", "", false},
}
@@ -153,7 +160,7 @@ func TestCompare_IN(t *testing.T) {
}
}
func TestCompare_StringOperations(t *testing.T) {
func TestCompareStringOperations(t *testing.T) {
tests := []struct {
name string
actual string
@@ -161,28 +168,28 @@ func TestCompare_StringOperations(t *testing.T) {
operator string
want bool
}{
{"contains true", "hello world", "world", "CONTAINS", true},
{"contains false", "hello world", "xyz", "CONTAINS", false},
{"contains case insensitive", "Hello World", "WORLD", "CONTAINS", true},
{"starts with true", "hello world", "hello", "STARTS_WITH", true},
{"starts with false", "hello world", "world", "STARTS_WITH", false},
{"starts with case insensitive", "Hello World", "HELLO", "STARTS_WITH", true},
{"ends with true", "hello world", "world", "ENDS_WITH", true},
{"ends with false", "hello world", "hello", "ENDS_WITH", false},
{"ends with case insensitive", "Hello World", "WORLD", "ENDS_WITH", true},
{"contains true", testHelloWorld, "world", "CONTAINS", true},
{"contains false", testHelloWorld, "xyz", "CONTAINS", false},
{"contains case insensitive", testHelloWorldCased, "WORLD", "CONTAINS", true},
{"starts with true", testHelloWorld, "hello", "STARTS_WITH", true},
{"starts with false", testHelloWorld, "world", "STARTS_WITH", false},
{"starts with case insensitive", testHelloWorldCased, "HELLO", "STARTS_WITH", true},
{"ends with true", testHelloWorld, "world", "ENDS_WITH", true},
{"ends with false", testHelloWorld, "hello", "ENDS_WITH", false},
{"ends with case insensitive", testHelloWorldCased, "WORLD", "ENDS_WITH", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := compare(tt.actual, tt.expected, tt.operator)
if got != tt.want {
t.Errorf("compare(%q, %q, %q) = %v, want %v", tt.actual, tt.expected, tt.operator, got, tt.want)
t.Errorf(testCompareFormat, tt.actual, tt.expected, tt.operator, got, tt.want)
}
})
}
}
func TestCompare_UnknownOperator(t *testing.T) {
func TestCompareUnknownOperator(t *testing.T) {
got := compare("value", "value", "UNKNOWN")
if got != false {
t.Errorf("compare with unknown operator should return false, got %v", got)
@@ -244,8 +251,8 @@ func TestInComparison(t *testing.T) {
expected string
want bool
}{
{"match in list", "admin", "admin,user,guest", true},
{"no match in list", "superuser", "admin,user,guest", false},
{"match in list", "admin", testAdminUserGuest, true},
{"no match in list", "superuser", testAdminUserGuest, false},
{"case insensitive", "ADMIN", "admin,user", true},
{"with whitespace", " admin ", " admin , user ", true},
{"single item match", "admin", "admin", true},
@@ -459,8 +466,8 @@ func TestEvaluatePolicies(t *testing.T) {
}
}
// TestResolveVariables_EdgeCases tests variable resolution indirectly through EvaluatePolicies
func TestResolveVariables_EdgeCases(t *testing.T) {
// TestResolveVariablesEdgeCases tests variable resolution indirectly through EvaluatePolicies
func TestResolveVariablesEdgeCases(t *testing.T) {
// Instead of testing the private function directly, test it through EvaluatePolicies
testCases := []struct {
name string
@@ -519,8 +526,8 @@ func TestResolveVariables_EdgeCases(t *testing.T) {
}
}
// TestCompare_CaseSensitivity tests comparison through EvaluatePolicies
func TestCompare_CaseSensitivity(t *testing.T) {
// TestCompareCaseSensitivity tests comparison through EvaluatePolicies
func TestCompareCaseSensitivity(t *testing.T) {
testCases := []struct {
name string
attributeValue string
@@ -552,8 +559,8 @@ func TestCompare_CaseSensitivity(t *testing.T) {
}
}
// TestCompare_EmptyStrings tests empty string comparisons through EvaluatePolicies
func TestCompare_EmptyStrings(t *testing.T) {
// TestCompareEmptyStrings tests empty string comparisons through EvaluatePolicies
func TestCompareEmptyStrings(t *testing.T) {
testCases := []struct {
name string
operator string
@@ -594,7 +601,7 @@ func TestCompare_EmptyStrings(t *testing.T) {
// Note: Tests for inComparison removed as it's an internal function.
// It's tested indirectly through public Compare and Evaluate Policies functions.
func TestEvaluatePolicies_NilContext(t *testing.T) {
func TestEvaluatePoliciesNilContext(t *testing.T) {
policies := []models.PolicyAttribute{
{AttributeName: "department", Comparison: "equals", AttributeValue: "IT"},
}
@@ -605,7 +612,7 @@ func TestEvaluatePolicies_NilContext(t *testing.T) {
}
}
func TestEvaluatePolicies_EmptyPoliciesList(t *testing.T) {
func TestEvaluatePoliciesEmptyPoliciesList(t *testing.T) {
ctx := &models.AuthorizationContext{
UserAttributes: map[string]string{"department": "IT"},
}
@@ -617,7 +624,7 @@ func TestEvaluatePolicies_EmptyPoliciesList(t *testing.T) {
// Note: The function returns "No policies to evaluate" as the reason even when successful
}
func TestEvaluatePolicies_ComplexConditions(t *testing.T) {
func TestEvaluatePoliciesComplexConditions(t *testing.T) {
ctx := &models.AuthorizationContext{
UserAttributes: map[string]string{
"department": "IT",
@@ -647,7 +654,7 @@ func TestEvaluatePolicies_ComplexConditions(t *testing.T) {
// Note: Tests for compare removed as it's an internal function.
// It's tested indirectly through public EvaluatePolicies functions.
func TestResolveVariables_AllAttributeTypes(t *testing.T) {
func TestResolveVariablesAllAttributeTypes(t *testing.T) {
ctx := &models.AuthorizationContext{
UserID: "user123",
Resource: "document",