Skip to content

Commit

Permalink
feat: clean up test project by list pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Taoyuesong <[email protected]>
  • Loading branch information
Taoyuesong committed Oct 29, 2024
1 parent 57d53ac commit cf5b8d7
Show file tree
Hide file tree
Showing 5 changed files with 552 additions and 503 deletions.
37 changes: 22 additions & 15 deletions Casbin.UnitTests/ModelTests/EnforcerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -731,22 +731,30 @@ public async Task TestInitEmptyByInputStreamAsync()
public void TestReloadPolicy()
{
Enforcer e = new("Examples/rbac_model.conf", "Examples/rbac_policy.csv");

e.LoadPolicy();
TestGetPolicy(e,
AsList(AsList("alice", "data1", "read"), AsList("bob", "data2", "write"),
AsList("data2_admin", "data2", "read"), AsList("data2_admin", "data2", "write")));
e.TestGetPolicy(
[
["alice", "data1", "read"],
["bob", "data2", "write"],
["data2_admin", "data2", "read"],
["data2_admin", "data2", "write"]
]
);
}

[Fact]
public async Task TestReloadPolicyAsync()
{
Enforcer e = new("Examples/rbac_model.conf", "Examples/rbac_policy.csv");

await e.LoadPolicyAsync();
TestGetPolicy(e,
AsList(AsList("alice", "data1", "read"), AsList("bob", "data2", "write"),
AsList("data2_admin", "data2", "read"), AsList("data2_admin", "data2", "write")));
e.TestGetPolicy(
[
["alice", "data1", "read"],
["bob", "data2", "write"],
["data2_admin", "data2", "read"],
["data2_admin", "data2", "write"]
]
);
}

[Fact]
Expand Down Expand Up @@ -880,7 +888,7 @@ public void TestEnableAutoSave()
// Reload the policy from the storage to see the effect.
e.LoadPolicy();

Assert.True(e.Enforce("alice", "data1", "read")); // Will not be false here.
Assert.True(e.Enforce("alice", "data1", "read")); // Will not be false here.
Assert.False(e.Enforce("alice", "data1", "write"));
Assert.False(e.Enforce("alice", "data2", "read"));
Assert.False(e.Enforce("alice", "data2", "write"));
Expand Down Expand Up @@ -1068,22 +1076,21 @@ public async Task TestEnforceExApiAsync()
e.BuildRoleLinks();

await e.TestEnforceExAsync("alice", "data1", "read", new List<string> { "alice", "data1", "read", "allow" });
await e.TestEnforceExAsync("alice", "data1", "write", new List<string> { "data1_deny_group", "data1", "write", "deny" });
await e.TestEnforceExAsync("alice", "data1", "write",
new List<string> { "data1_deny_group", "data1", "write", "deny" });
await e.TestEnforceExAsync("alice", "data2", "read", new List<string>());
await e.TestEnforceExAsync("alice", "data2", "write", new List<string>());
await e.TestEnforceExAsync("bob", "data1", "write", new List<string>());
await e.TestEnforceExAsync("bob", "data2", "read", new List<string> { "data2_allow_group", "data2", "read", "allow" });
await e.TestEnforceExAsync("bob", "data2", "read",
new List<string> { "data2_allow_group", "data2", "read", "allow" });
await e.TestEnforceExAsync("bob", "data2", "write", new List<string> { "bob", "data2", "write", "deny" });
}

#if !NET452
[Fact]
public void TestEnforceExApiLog()
{
Enforcer e = new(TestModelFixture.GetBasicTestModel())
{
Logger = new MockLogger<Enforcer>(_testOutputHelper)
};
Enforcer e = new(TestModelFixture.GetBasicTestModel()) { Logger = new MockLogger<Enforcer>(_testOutputHelper) };

e.TestEnforceEx("alice", "data1", "read", new List<string> { "alice", "data1", "read" });
e.TestEnforceEx("alice", "data1", "write", new List<string>());
Expand Down
Loading

0 comments on commit cf5b8d7

Please sign in to comment.