Test transaction buffer operations.
(t *testing.T)
| 286 | |
| 287 | // Test transaction buffer operations. |
| 288 | func TestTransactionBuffer(t *testing.T) { |
| 289 | adapter := NewMockTransactionalAdapter() |
| 290 | e, err := NewTransactionalEnforcer("examples/rbac_model.conf", adapter) |
| 291 | if err != nil { |
| 292 | t.Fatalf("Failed to create transactional enforcer: %v", err) |
| 293 | } |
| 294 | adapter.Enforcer = e.Enforcer |
| 295 | |
| 296 | ctx := context.Background() |
| 297 | |
| 298 | tx, err := e.BeginTransaction(ctx) |
| 299 | if err != nil { |
| 300 | t.Fatalf("Failed to begin transaction: %v", err) |
| 301 | } |
| 302 | |
| 303 | // Initially no operations. |
| 304 | if tx.HasOperations() { |
| 305 | t.Fatal("Transaction should have no operations initially") |
| 306 | } |
| 307 | |
| 308 | if tx.OperationCount() != 0 { |
| 309 | t.Fatal("Operation count should be 0 initially") |
| 310 | } |
| 311 | |
| 312 | // Add some operations. |
| 313 | tx.AddPolicy("alice", "data1", "read") |
| 314 | tx.AddPolicy("bob", "data2", "write") |
| 315 | |
| 316 | if !tx.HasOperations() { |
| 317 | t.Fatal("Transaction should have operations") |
| 318 | } |
| 319 | |
| 320 | if tx.OperationCount() != 2 { |
| 321 | t.Fatalf("Expected 2 operations, got %d", tx.OperationCount()) |
| 322 | } |
| 323 | |
| 324 | // Get buffered model. |
| 325 | bufferedModel, err := tx.GetBufferedModel() |
| 326 | if err != nil { |
| 327 | t.Fatalf("Failed to get buffered model: %v", err) |
| 328 | } |
| 329 | |
| 330 | // Check that buffered model contains the policies. |
| 331 | hasPolicy, _ := bufferedModel.HasPolicy("p", "p", []string{"alice", "data1", "read"}) |
| 332 | if !hasPolicy { |
| 333 | t.Fatal("Buffered model should contain the added policy") |
| 334 | } |
| 335 | |
| 336 | tx.Rollback() |
| 337 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…