MCPcopy Index your code
hub / github.com/apache/casbin / TestEnableAutoSave

Function TestEnableAutoSave

enforcer_test.go:294–329  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

292}
293
294func TestEnableAutoSave(t *testing.T) {
295 e, _ := NewEnforcer("examples/basic_model.conf", "examples/basic_policy.csv")
296
297 e.EnableAutoSave(false)
298 // Because AutoSave is disabled, the policy change only affects the policy in Casbin enforcer,
299 // it doesn't affect the policy in the storage.
300 _, _ = e.RemovePolicy("alice", "data1", "read")
301 // Reload the policy from the storage to see the effect.
302 _ = e.LoadPolicy()
303 testEnforce(t, e, "alice", "data1", "read", true)
304 testEnforce(t, e, "alice", "data1", "write", false)
305 testEnforce(t, e, "alice", "data2", "read", false)
306 testEnforce(t, e, "alice", "data2", "write", false)
307 testEnforce(t, e, "bob", "data1", "read", false)
308 testEnforce(t, e, "bob", "data1", "write", false)
309 testEnforce(t, e, "bob", "data2", "read", false)
310 testEnforce(t, e, "bob", "data2", "write", true)
311
312 e.EnableAutoSave(true)
313 // Because AutoSave is enabled, the policy change not only affects the policy in Casbin enforcer,
314 // but also affects the policy in the storage.
315 _, _ = e.RemovePolicy("alice", "data1", "read")
316
317 // However, the file adapter doesn't implement the AutoSave feature, so enabling it has no effect at all here.
318
319 // Reload the policy from the storage to see the effect.
320 _ = e.LoadPolicy()
321 testEnforce(t, e, "alice", "data1", "read", true) // Will not be false here.
322 testEnforce(t, e, "alice", "data1", "write", false)
323 testEnforce(t, e, "alice", "data2", "read", false)
324 testEnforce(t, e, "alice", "data2", "write", false)
325 testEnforce(t, e, "bob", "data1", "read", false)
326 testEnforce(t, e, "bob", "data1", "write", false)
327 testEnforce(t, e, "bob", "data2", "read", false)
328 testEnforce(t, e, "bob", "data2", "write", true)
329}
330
331func TestInitWithAdapter(t *testing.T) {
332 adapter := fileadapter.NewAdapter("examples/basic_policy.csv")

Callers

nothing calls this directly

Calls 5

EnableAutoSaveMethod · 0.95
RemovePolicyMethod · 0.95
LoadPolicyMethod · 0.95
NewEnforcerFunction · 0.85
testEnforceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…