Files
Authorization/services/policy_evaluator_test.go
T
2025-12-17 10:01:58 +08:00

690 lines
18 KiB
Go

package services
import (
"authorization/models"
"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
value string
ctx *models.AuthorizationContext
expected string
}{
{
name: "resolves user attribute",
value: "${user.department}",
ctx: &models.AuthorizationContext{
UserAttributes: map[string]string{"department": "Engineering"},
},
expected: "Engineering",
},
{
name: "resolves resource attribute",
value: "${resource.owner}",
ctx: &models.AuthorizationContext{
ResourceData: map[string]string{"owner": "user123"},
},
expected: "user123",
},
{
name: "resolves environment attribute",
value: "${environment.time}",
ctx: &models.AuthorizationContext{
Environment: map[string]string{"time": "12:00"},
},
expected: "12:00",
},
{
name: "resolves multiple variables",
value: "${user.name} from ${user.department}",
ctx: &models.AuthorizationContext{
UserAttributes: map[string]string{
"name": "John",
"department": "IT",
},
},
expected: "John from IT",
},
{
name: "leaves unresolved variables unchanged",
value: "${user.nonexistent}",
ctx: &models.AuthorizationContext{UserAttributes: map[string]string{}},
expected: "${user.nonexistent}",
},
{
name: "handles no variables",
value: "plain text",
ctx: &models.AuthorizationContext{},
expected: "plain text",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := resolveVariables(tt.value, tt.ctx)
if got != tt.expected {
t.Errorf("resolveVariables() = %v, want %v", got, tt.expected)
}
})
}
}
func TestCompareEquality(t *testing.T) {
tests := []struct {
name string
actual string
expected string
operator string
want bool
}{
{"equal strings", "admin", "admin", "=", true},
{"not equal strings", "admin", "user", "=", false},
{"not equal operator true", "admin", "user", "!=", true},
{"not equal operator false", "admin", "admin", "!=", false},
{"equal with whitespace", " admin ", "admin", "=", 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(testCompareFormat, tt.actual, tt.expected, tt.operator, got, tt.want)
}
})
}
}
func TestCompareNumeric(t *testing.T) {
tests := []struct {
name string
actual string
expected string
operator string
want bool
}{
{"greater than true", "10", "5", ">", true},
{"greater than false", "5", "10", ">", false},
{"less than true", "5", "10", "<", true},
{"less than false", "10", "5", "<", false},
{"greater or equal true equal", "10", "10", ">=", true},
{"greater or equal true greater", "11", "10", ">=", true},
{"greater or equal false", "9", "10", ">=", false},
{"less or equal true equal", "10", "10", "<=", true},
{"less or equal true less", "9", "10", "<=", true},
{"less or equal false", "11", "10", "<=", false},
{"invalid number returns false", "abc", "10", ">", false},
{"float comparison", "10.5", "10.2", ">", 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(testCompareFormat, tt.actual, tt.expected, tt.operator, got, tt.want)
}
})
}
}
func TestCompareIN(t *testing.T) {
tests := []struct {
name string
actual string
expected string
want bool
}{
{"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", testAdminUserGuest, true},
{"single value match", "admin", "admin", true},
{"empty list", "admin", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := compare(tt.actual, tt.expected, "IN")
if got != tt.want {
t.Errorf("compare(%q, %q, IN) = %v, want %v", tt.actual, tt.expected, got, tt.want)
}
})
}
}
func TestCompareStringOperations(t *testing.T) {
tests := []struct {
name string
actual string
expected string
operator string
want bool
}{
{"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(testCompareFormat, tt.actual, tt.expected, tt.operator, got, tt.want)
}
})
}
}
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)
}
}
func TestNumericCompare(t *testing.T) {
tests := []struct {
name string
actual string
expected string
compareFn func(float64, float64) bool
want bool
}{
{
name: "valid numbers greater",
actual: "10",
expected: "5",
compareFn: func(a, e float64) bool { return a > e },
want: true,
},
{
name: "valid numbers less",
actual: "5",
expected: "10",
compareFn: func(a, e float64) bool { return a < e },
want: true,
},
{
name: "invalid actual number",
actual: "abc",
expected: "10",
compareFn: func(a, e float64) bool { return a > e },
want: false,
},
{
name: "invalid expected number",
actual: "10",
expected: "xyz",
compareFn: func(a, e float64) bool { return a > e },
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := numericCompare(tt.actual, tt.expected, tt.compareFn)
if got != tt.want {
t.Errorf("numericCompare(%q, %q) = %v, want %v", tt.actual, tt.expected, got, tt.want)
}
})
}
}
func TestInComparison(t *testing.T) {
tests := []struct {
name string
actual string
expected string
want bool
}{
{"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},
{"empty expected", "admin", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := inComparison(tt.actual, tt.expected)
if got != tt.want {
t.Errorf("inComparison(%q, %q) = %v, want %v", tt.actual, tt.expected, got, tt.want)
}
})
}
}
func TestEvaluatePolicy(t *testing.T) {
tests := []struct {
name string
policy models.PolicyAttribute
ctx *models.AuthorizationContext
wantSatisfied bool
wantReasonEmpty bool
}{
{
name: "user attribute satisfied",
policy: models.PolicyAttribute{
AttributeType: "user",
AttributeName: "department",
Comparison: "=",
AttributeValue: "Engineering",
},
ctx: &models.AuthorizationContext{
UserAttributes: map[string]string{"department": "Engineering"},
},
wantSatisfied: true,
wantReasonEmpty: true,
},
{
name: "user attribute not satisfied",
policy: models.PolicyAttribute{
AttributeType: "user",
AttributeName: "department",
Comparison: "=",
AttributeValue: "HR",
},
ctx: &models.AuthorizationContext{
UserAttributes: map[string]string{"department": "Engineering"},
},
wantSatisfied: false,
wantReasonEmpty: false,
},
{
name: "user attribute not found",
policy: models.PolicyAttribute{
AttributeType: "user",
AttributeName: "nonexistent",
Comparison: "=",
AttributeValue: "value",
},
ctx: &models.AuthorizationContext{
UserAttributes: map[string]string{},
},
wantSatisfied: false,
wantReasonEmpty: false,
},
{
name: "resource attribute satisfied",
policy: models.PolicyAttribute{
AttributeType: "resource",
AttributeName: "owner",
Comparison: "=",
AttributeValue: "user123",
},
ctx: &models.AuthorizationContext{
ResourceData: map[string]string{"owner": "user123"},
},
wantSatisfied: true,
wantReasonEmpty: true,
},
{
name: "environment attribute satisfied",
policy: models.PolicyAttribute{
AttributeType: "environment",
AttributeName: "location",
Comparison: "=",
AttributeValue: "US",
},
ctx: &models.AuthorizationContext{
Environment: map[string]string{"location": "US"},
},
wantSatisfied: true,
wantReasonEmpty: true,
},
{
name: "unknown attribute type",
policy: models.PolicyAttribute{
AttributeType: "unknown",
AttributeName: "attr",
Comparison: "=",
AttributeValue: "value",
},
ctx: &models.AuthorizationContext{},
wantSatisfied: false,
wantReasonEmpty: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
satisfied, reason := evaluatePolicy(tt.policy, tt.ctx)
if satisfied != tt.wantSatisfied {
t.Errorf("evaluatePolicy() satisfied = %v, want %v", satisfied, tt.wantSatisfied)
}
if tt.wantReasonEmpty && reason != "" {
t.Errorf("evaluatePolicy() reason = %q, want empty", reason)
}
if !tt.wantReasonEmpty && reason == "" {
t.Errorf("evaluatePolicy() reason is empty, want non-empty")
}
})
}
}
func TestEvaluatePolicies(t *testing.T) {
tests := []struct {
name string
policies []models.PolicyAttribute
ctx *models.AuthorizationContext
wantSatisfied bool
wantReasonEmpty bool
}{
{
name: "no policies returns true",
policies: []models.PolicyAttribute{},
ctx: &models.AuthorizationContext{},
wantSatisfied: true,
wantReasonEmpty: false,
},
{
name: "all policies satisfied",
policies: []models.PolicyAttribute{
{
AttributeType: "user",
AttributeName: "department",
Comparison: "=",
AttributeValue: "Engineering",
},
{
AttributeType: "user",
AttributeName: "level",
Comparison: ">=",
AttributeValue: "3",
},
},
ctx: &models.AuthorizationContext{
UserAttributes: map[string]string{
"department": "Engineering",
"level": "5",
},
},
wantSatisfied: true,
wantReasonEmpty: false,
},
{
name: "one policy fails",
policies: []models.PolicyAttribute{
{
AttributeType: "user",
AttributeName: "department",
Comparison: "=",
AttributeValue: "Engineering",
},
{
AttributeType: "user",
AttributeName: "level",
Comparison: ">=",
AttributeValue: "5",
},
},
ctx: &models.AuthorizationContext{
UserAttributes: map[string]string{
"department": "Engineering",
"level": "3",
},
},
wantSatisfied: false,
wantReasonEmpty: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
satisfied, reason := EvaluatePolicies(tt.policies, tt.ctx)
if satisfied != tt.wantSatisfied {
t.Errorf("EvaluatePolicies() satisfied = %v, want %v", satisfied, tt.wantSatisfied)
}
if tt.wantReasonEmpty && reason != "" {
t.Errorf("EvaluatePolicies() reason = %q, want empty", reason)
}
if !tt.wantReasonEmpty && reason == "" {
t.Errorf("EvaluatePolicies() reason is empty, want non-empty")
}
})
}
}
// 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
policy models.PolicyAttribute
ctx *models.AuthorizationContext
expectedResult bool
}{
{
name: "Empty string attribute",
policy: models.PolicyAttribute{
AttributeName: "empty",
AttributeType: "user",
Comparison: "=",
AttributeValue: "",
},
ctx: &models.AuthorizationContext{
UserAttributes: map[string]string{"empty": ""},
},
expectedResult: true,
},
{
name: "Missing attribute",
policy: models.PolicyAttribute{
AttributeName: "missing",
AttributeType: "user",
Comparison: "=",
AttributeValue: "value",
},
ctx: &models.AuthorizationContext{
UserAttributes: map[string]string{},
},
expectedResult: false,
},
{
name: "Special characters in value",
policy: models.PolicyAttribute{
AttributeName: "special",
AttributeType: "user",
Comparison: "=",
AttributeValue: "<>&\"'",
},
ctx: &models.AuthorizationContext{
UserAttributes: map[string]string{"special": "<>&\"'"},
},
expectedResult: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, _ := EvaluatePolicies([]models.PolicyAttribute{tc.policy}, tc.ctx)
if result != tc.expectedResult {
t.Errorf("EvaluatePolicies() = %v, want %v", result, tc.expectedResult)
}
})
}
}
// TestCompareCaseSensitivity tests comparison through EvaluatePolicies
func TestCompareCaseSensitivity(t *testing.T) {
testCases := []struct {
name string
attributeValue string
userAttrValue string
operator string
expected bool
}{
{"Equals case sensitive", "Test", "test", "=", false},
{"Equals same case", "Test", "Test", "=", true},
{"Not equals case", "Test", "test", "!=", true},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
policy := models.PolicyAttribute{
AttributeName: "value",
AttributeType: "user",
Comparison: tc.operator,
AttributeValue: tc.attributeValue,
}
ctx := &models.AuthorizationContext{
UserAttributes: map[string]string{"value": tc.userAttrValue},
}
result, _ := EvaluatePolicies([]models.PolicyAttribute{policy}, ctx)
if result != tc.expected {
t.Errorf("comparison(%q, %q, %q) = %v, want %v", tc.operator, tc.userAttrValue, tc.attributeValue, result, tc.expected)
}
})
}
}
// TestCompareEmptyStrings tests empty string comparisons through EvaluatePolicies
func TestCompareEmptyStrings(t *testing.T) {
testCases := []struct {
name string
operator string
userValue string
expectedValue string
expectedResult bool
}{
{"equals both empty", "=", "", "", true},
{"equals one empty", "=", "", "value", false},
{"not_equals both empty", "!=", "", "", false},
{"not_equals one empty", "!=", "", "value", true},
{"contains value in empty", "CONTAINS", "", "test", false},
{"contains empty in value", "CONTAINS", "test", "", true},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
policy := models.PolicyAttribute{
AttributeName: "value",
AttributeType: "user",
Comparison: tc.operator,
AttributeValue: tc.expectedValue,
}
ctx := &models.AuthorizationContext{
UserAttributes: map[string]string{"value": tc.userValue},
}
result, _ := EvaluatePolicies([]models.PolicyAttribute{policy}, ctx)
if result != tc.expectedResult {
t.Errorf("comparison(%q, %q, %q) = %v, want %v", tc.operator, tc.userValue, tc.expectedValue, result, tc.expectedResult)
}
})
}
}
// Note: Tests for numericCompare removed as it's an internal function.
// It's tested indirectly through public Compare and EvaluatePolicies functions.
// Note: Tests for inComparison removed as it's an internal function.
// It's tested indirectly through public Compare and Evaluate Policies functions.
func TestEvaluatePoliciesNilContext(t *testing.T) {
policies := []models.PolicyAttribute{
{AttributeName: "department", Comparison: "equals", AttributeValue: "IT"},
}
satisfied, _ := EvaluatePolicies(policies, nil)
if satisfied {
t.Error("EvaluatePolicies should return false for nil context")
}
}
func TestEvaluatePoliciesEmptyPoliciesList(t *testing.T) {
ctx := &models.AuthorizationContext{
UserAttributes: map[string]string{"department": "IT"},
}
satisfied, _ := EvaluatePolicies([]models.PolicyAttribute{}, ctx)
if !satisfied {
t.Error("EvaluatePolicies should return true for empty policies list")
}
// Note: The function returns "No policies to evaluate" as the reason even when successful
}
func TestEvaluatePoliciesComplexConditions(t *testing.T) {
ctx := &models.AuthorizationContext{
UserAttributes: map[string]string{
"department": "IT",
"level": "5",
"location": "US",
},
ResourceData: map[string]string{
"classification": "public",
},
Environment: map[string]string{
"time": "14:00",
},
}
policies := []models.PolicyAttribute{
{AttributeName: "department", AttributeType: "user", Comparison: "=", AttributeValue: "IT"},
{AttributeName: "level", AttributeType: "user", Comparison: ">=", AttributeValue: "3"},
{AttributeName: "location", AttributeType: "user", Comparison: "IN", AttributeValue: "US,UK,CA"},
}
satisfied, reason := EvaluatePolicies(policies, ctx)
if !satisfied {
t.Errorf("EvaluatePolicies should satisfy all conditions, reason: %s", reason)
}
}
// Note: Tests for compare removed as it's an internal function.
// It's tested indirectly through public EvaluatePolicies functions.
func TestResolveVariablesAllAttributeTypes(t *testing.T) {
ctx := &models.AuthorizationContext{
UserID: "user123",
Resource: "document",
Action: "read",
UserAttributes: map[string]string{
"dept": "IT",
},
ResourceData: map[string]string{
"owner": "user456",
},
Environment: map[string]string{
"ip": "192.168.1.1",
},
}
testCases := []struct {
input string
expected string
}{
{"User: ${user.dept}", "User: IT"},
{"Resource: ${resource.owner}", "Resource: user456"},
{"Env: ${environment.ip}", "Env: 192.168.1.1"},
{"Mixed: ${user.dept} ${resource.owner}", "Mixed: IT user456"},
}
for _, tc := range testCases {
result := resolveVariables(tc.input, ctx)
if result != tc.expected {
t.Errorf("resolveVariables(%q) = %q, want %q", tc.input, result, tc.expected)
}
}
}