@@ -939,6 +939,59 @@ public async Task TestEnableAutoSaveAsync()
939939 Assert . True ( await e . EnforceAsync ( "bob" , "data2" , "write" ) ) ;
940940 }
941941
942+ [ Fact ]
943+ public void TestAutoSaveGroupingPolicy ( )
944+ {
945+ // This test verifies that AddGroupingPolicy() respects the AutoSave flag.
946+ // When AutoSave is disabled, grouping policy changes should not be saved to the adapter.
947+
948+ MockSingleAdapter adapter = new ( "Examples/rbac_policy.csv" ) ;
949+ Enforcer e = new ( "Examples/rbac_model.conf" , adapter ) ;
950+
951+ // Verify initial state: alice has data2_admin role
952+ Assert . True ( e . HasGroupingPolicy ( "alice" , "data2_admin" ) ) ;
953+ Assert . False ( e . HasGroupingPolicy ( "bob" , "data2_admin" ) ) ;
954+
955+ adapter . ClearSavedPolicies ( ) ;
956+ e . EnableAutoSave ( false ) ;
957+
958+ // Because AutoSave is disabled, the grouping policy change should only affect
959+ // the policy in Casbin enforcer, it should NOT call the adapter.
960+ e . AddGroupingPolicy ( "bob" , "data2_admin" ) ;
961+
962+ // Verify the change is in memory
963+ Assert . True ( e . HasGroupingPolicy ( "bob" , "data2_admin" ) ) ;
964+
965+ // Verify the adapter was NOT called because AutoSave is disabled
966+ Assert . Empty ( adapter . SavedPolicies ) ;
967+ }
968+
969+ [ Fact ]
970+ public async Task TestAutoSaveGroupingPolicyAsync ( )
971+ {
972+ // This test verifies that AddGroupingPolicyAsync() respects the AutoSave flag.
973+ // When AutoSave is disabled, grouping policy changes should not be saved to the adapter.
974+
975+ MockSingleAdapter adapter = new ( "Examples/rbac_policy.csv" ) ;
976+ Enforcer e = new ( "Examples/rbac_model.conf" , adapter ) ;
977+
978+ // Verify initial state
979+ Assert . True ( e . HasGroupingPolicy ( "alice" , "data2_admin" ) ) ;
980+ Assert . False ( e . HasGroupingPolicy ( "bob" , "data2_admin" ) ) ;
981+
982+ adapter . ClearSavedPolicies ( ) ;
983+ e . EnableAutoSave ( false ) ;
984+
985+ // Add grouping policy with AutoSave disabled
986+ await e . AddGroupingPolicyAsync ( "bob" , "data2_admin" ) ;
987+
988+ // Verify the change is in memory
989+ Assert . True ( e . HasGroupingPolicy ( "bob" , "data2_admin" ) ) ;
990+
991+ // Verify the adapter was NOT called because AutoSave is disabled
992+ Assert . Empty ( adapter . SavedPolicies ) ;
993+ }
994+
942995 [ Fact ]
943996 public void TestInitWithAdapter ( )
944997 {
0 commit comments