From 335da03a935c001677e60a0f0f9da46d822d91c1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 7 Nov 2025 04:59:05 +0000 Subject: [PATCH 1/4] Initial plan From 9a234ba2631d7d8f20c5df13a65bc5438c3e9f78 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 7 Nov 2025 05:02:52 +0000 Subject: [PATCH 2/4] Fix SavePolicy to preserve unique indexes by using DeleteMany instead of Drop Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com> --- adapter.go | 2 +- adapter_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/adapter.go b/adapter.go index 7e4b982..37982a1 100644 --- a/adapter.go +++ b/adapter.go @@ -230,7 +230,7 @@ func (a *adapter) dropTable() error { ctx, cancel := context.WithTimeout(context.TODO(), a.timeout) defer cancel() - err := a.collection.Drop(ctx) + _, err := a.collection.DeleteMany(ctx, bson.D{{}}) if err != nil { return err } diff --git a/adapter_test.go b/adapter_test.go index bdc88f3..669d439 100644 --- a/adapter_test.go +++ b/adapter_test.go @@ -15,6 +15,7 @@ package mongodbadapter import ( + "context" "fmt" "os" "strings" @@ -634,3 +635,62 @@ func TestUpdateFilteredPoliciesTxn(t *testing.T) { e.LoadPolicy() testGetPolicyWithoutOrder(t, e, [][]string{{"alice", "data1", "write"}, {"bob", "data2", "read"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}}) } + +func TestSavePolicyPreservesIndexes(t *testing.T) { + // Initialize with some policies + initPolicy(t, getDbURL()) + + a, err := NewAdapter(getDbURL()) + if err != nil { + panic(err) + } + + e, err := casbin.NewEnforcer("examples/rbac_model.conf", a) + if err != nil { + panic(err) + } + + // Save policy which should NOT drop indexes + if err := e.SavePolicy(); err != nil { + t.Errorf("Expected SavePolicy() to be successful; got %v", err) + } + + // Try to add a duplicate policy - should fail due to unique index + adapter := a.(*adapter) + line := savePolicyLine("p", []string{"alice", "data1", "read"}) + + ctx, cancel := context.WithTimeout(context.TODO(), adapter.timeout) + defer cancel() + + // Attempting to insert a duplicate should fail + _, err = adapter.collection.InsertOne(ctx, line) + if err == nil { + t.Error("Expected InsertOne of duplicate to fail due to unique index, but it succeeded") + } +} + +func TestSavePolicyPreventsDuplicates(t *testing.T) { + // Initialize with some policies + initPolicy(t, getDbURL()) + + a, err := NewAdapter(getDbURL()) + if err != nil { + panic(err) + } + + e, err := casbin.NewEnforcer("examples/rbac_model.conf", a) + if err != nil { + panic(err) + } + + // Save policy which should NOT drop indexes + if err := e.SavePolicy(); err != nil { + t.Errorf("Expected SavePolicy() to be successful; got %v", err) + } + + // Try to add a duplicate via AddPolicy - should be prevented by unique index + _, err = e.AddPolicy("alice", "data1", "read") + if err == nil { + t.Error("Expected AddPolicy of duplicate to fail due to unique index, but it succeeded") + } +} From 3128877e1aef4ef46c2c59a8a493472cca5895a9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 7 Nov 2025 05:03:52 +0000 Subject: [PATCH 3/4] Fix formatting in test file Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com> --- adapter_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adapter_test.go b/adapter_test.go index 669d439..98a0791 100644 --- a/adapter_test.go +++ b/adapter_test.go @@ -658,10 +658,10 @@ func TestSavePolicyPreservesIndexes(t *testing.T) { // Try to add a duplicate policy - should fail due to unique index adapter := a.(*adapter) line := savePolicyLine("p", []string{"alice", "data1", "read"}) - + ctx, cancel := context.WithTimeout(context.TODO(), adapter.timeout) defer cancel() - + // Attempting to insert a duplicate should fail _, err = adapter.collection.InsertOne(ctx, line) if err == nil { From 34640bba3a27dbbb6d346ff90993ce92ea716186 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 7 Nov 2025 09:58:27 +0000 Subject: [PATCH 4/4] Fix TestSavePolicyPreventsDuplicates to test adapter directly Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com> --- adapter_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/adapter_test.go b/adapter_test.go index 98a0791..0e6c046 100644 --- a/adapter_test.go +++ b/adapter_test.go @@ -688,8 +688,10 @@ func TestSavePolicyPreventsDuplicates(t *testing.T) { t.Errorf("Expected SavePolicy() to be successful; got %v", err) } - // Try to add a duplicate via AddPolicy - should be prevented by unique index - _, err = e.AddPolicy("alice", "data1", "read") + // Try to add a duplicate directly via adapter (bypassing enforcer's in-memory check) + // This should be prevented by the unique index + adapter := a.(*adapter) + err = adapter.AddPolicy("p", "p", []string{"alice", "data1", "read"}) if err == nil { t.Error("Expected AddPolicy of duplicate to fail due to unique index, but it succeeded") }