fixed sonarqube issues
This commit is contained in:
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user