Skip to content

Commit

Permalink
chore: clean up unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: sagilio <[email protected]>
  • Loading branch information
sagilio0728 committed May 18, 2024
1 parent 5a358a4 commit 9535875
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 270 deletions.
211 changes: 94 additions & 117 deletions Casbin.UnitTests/Fixtures/TestModelFixture.cs

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions Casbin.UnitTests/GenericTests/SupportCountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ namespace Casbin.UnitTests.GenericTests;
[Collection("Model collection")]
public class SupportCountTest
{
private readonly TestModelFixture _testModelFixture;

public SupportCountTest(TestModelFixture testModelFixture) => _testModelFixture = testModelFixture;

[Fact]
public void TestSupportCount()
{
for (int i = 1; i <= 13; i++)
{
IEnforcer enforcer = new Enforcer(DefaultModel.CreateFromText(_testModelFixture._supportCountModelText));
IEnforcer enforcer = new Enforcer(DefaultModel.CreateFromText(TestModelFixture.SupportCountModelText));
string policyType = $"p{i}";
string requestType = $"r{i}";
string matcherType = $"m{i}";
Expand Down
12 changes: 6 additions & 6 deletions Casbin.UnitTests/ModelTests/CachedEnforcerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ namespace Casbin.UnitTests.ModelTests;
[Collection("Model collection")]
public class CachedEnforcerTest
{
private readonly TestModelFixture _testModelFixture;
private readonly TestModelFixture TestModelFixture;
private readonly ITestOutputHelper _testOutputHelper;

public CachedEnforcerTest(ITestOutputHelper testOutputHelper, TestModelFixture testModelFixture)
{
_testOutputHelper = testOutputHelper;
_testModelFixture = testModelFixture;
TestModelFixture = testModelFixture;
}

[Fact]
public void TestEnforceWithCache()
{
#if !NET452
Enforcer e = new(_testModelFixture.GetBasicTestModel())
Enforcer e = new(TestModelFixture.GetBasicTestModel())
{
Logger = new MockLogger<Enforcer>(_testOutputHelper)
};
#else
var e = new Enforcer(_testModelFixture.GetBasicTestModel());
var e = new Enforcer(TestModelFixture.GetBasicTestModel());
#endif
e.EnableCache(true);
e.EnableAutoCleanEnforceCache(false);
Expand Down Expand Up @@ -60,12 +60,12 @@ public void TestEnforceWithCache()
public void TestAutoCleanCache()
{
#if !NET452
Enforcer e = new(_testModelFixture.GetBasicTestModel())
Enforcer e = new(TestModelFixture.GetBasicTestModel())
{
Logger = new MockLogger<Enforcer>(_testOutputHelper)
};
#else
var e = new Enforcer(_testModelFixture.GetBasicTestModel());
var e = new Enforcer(TestModelFixture.GetBasicTestModel());
#endif
e.EnableCache(true);

Expand Down
60 changes: 29 additions & 31 deletions Casbin.UnitTests/ModelTests/EnforcerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@ namespace Casbin.UnitTests.ModelTests;
[Collection("Model collection")]
public class EnforcerTest
{
private readonly TestModelFixture _testModelFixture;
private readonly ITestOutputHelper _testOutputHelper;

public EnforcerTest(ITestOutputHelper testOutputHelper, TestModelFixture testModelFixture)
public EnforcerTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_testModelFixture = testModelFixture;
}

[Fact]
public void TestEnforceWithMultipleRoleManager()
{
Enforcer e = new(TestModelFixture.GetNewTestModel(
_testModelFixture._rbacMultipleModelText,
_testModelFixture._rbacMultiplePolicyText));
TestModelFixture.RbacMultipleModelText,
TestModelFixture.RbacMultiplePolicyText));

DefaultRoleManager roleManager = new(5);
roleManager.AddMatchingFunc((arg1, arg2) => arg1.Equals(arg2));
Expand All @@ -50,8 +48,8 @@ public void TestEnforceWithMultipleRoleManager()
public void TestEnforceWithMultipleEval()
{
Enforcer e = new(TestModelFixture.GetNewTestModel(
_testModelFixture._rbacMultipleEvalModelText,
_testModelFixture._rbacMultipleEvalPolicyText));
TestModelFixture.RbacMultipleEvalModelText,
TestModelFixture.RbacMultipleEvalPolicyText));

bool result = e.Enforce(
"domain1",
Expand Down Expand Up @@ -83,8 +81,8 @@ public void TestEnforceWithoutAutoLoadPolicy()
public void TestEnforceSubjectPriority()
{
Enforcer e = new(TestModelFixture.GetNewTestModel(
_testModelFixture._subjectPriorityModelText,
_testModelFixture._subjectPriorityPolicyText));
TestModelFixture.SubjectPriorityModelText,
TestModelFixture.SubjectPriorityPolicyText));

TestEnforce(e, "jane", "data1", "read", true);
TestEnforce(e, "alice", "data1", "read", true);
Expand Down Expand Up @@ -253,8 +251,8 @@ public void TestKeyMatchModelInMemoryDeny()
public void TestInOperator()
{
Enforcer e = new(TestModelFixture.GetNewTestModel(
_testModelFixture._rbacInOperatorModelText,
_testModelFixture._rbacInOperatorPolicyText));
TestModelFixture.RbacInOperatorModelText,
TestModelFixture.RbacInOperatorPolicyText));

TestEnforce(e, new { Name = "Alice", Amount = 5100, Roles = new[] { "Manager", "DepartmentDirector" } },
"authorization", "grant", true);
Expand Down Expand Up @@ -344,7 +342,7 @@ public void TestRbacBatchEnforceInMemory()
e.AddRoleForUser("alice", "data2_admin");

IEnumerable<(RequestValues<string, string, string>, bool)> testCases =
new (RequestValues<string, string, string>, bool)[]
new[]
{
(Request.CreateValues("alice", "data1", "read"), true),
(Request.CreateValues("alice", "data1", "write"), false),
Expand Down Expand Up @@ -378,7 +376,7 @@ public void TestRbacParallelBatchEnforceInMemory()
e.AddRoleForUser("alice", "data2_admin");

IEnumerable<(RequestValues<string, string, string>, bool)> testCases =
new (RequestValues<string, string, string>, bool)[]
new[]
{
(Request.CreateValues("alice", "data1", "read"), true),
(Request.CreateValues("alice", "data1", "write"), false),
Expand Down Expand Up @@ -440,7 +438,7 @@ public void TestRbacBatchEnforceInMemoryAsync()
e.AddRoleForUserAsync("alice", "data2_admin");

IEnumerable<(RequestValues<string, string, string>, bool)> testCases =
new (RequestValues<string, string, string>, bool)[]
new[]
{
(Request.CreateValues("alice", "data1", "read"), true),
(Request.CreateValues("alice", "data1", "write"), false),
Expand Down Expand Up @@ -1069,7 +1067,7 @@ public async Task TestSetAdapterFromFileAsync()
[Fact]
public void TestEnforceExApi()
{
Enforcer e = new(_testModelFixture.GetBasicTestModel());
Enforcer e = new(TestModelFixture.GetBasicTestModel());

TestEnforceEx(e, "alice", "data1", "read", new List<string> { "alice", "data1", "read" });
TestEnforceEx(e, "alice", "data1", "write", new List<string>());
Expand All @@ -1080,7 +1078,7 @@ public void TestEnforceExApi()
TestEnforceEx(e, "bob", "data2", "read", new List<string>());
TestEnforceEx(e, "bob", "data2", "write", new List<string> { "bob", "data2", "write" });

e = new Enforcer(_testModelFixture.GetNewRbacTestModel());
e = new Enforcer(TestModelFixture.GetNewRbacTestModel());

TestEnforceEx(e, "alice", "data1", "read", new List<string> { "alice", "data1", "read" });
TestEnforceEx(e, "alice", "data1", "write", new List<string>());
Expand All @@ -1091,7 +1089,7 @@ public void TestEnforceExApi()
TestEnforceEx(e, "bob", "data2", "read", new List<string>());
TestEnforceEx(e, "bob", "data2", "write", new List<string> { "bob", "data2", "write" });

e = new Enforcer(_testModelFixture.GetNewPriorityTestModel());
e = new Enforcer(TestModelFixture.GetNewPriorityTestModel());
e.BuildRoleLinks();

TestEnforceEx(e, "alice", "data1", "read", new List<string> { "alice", "data1", "read", "allow" });
Expand All @@ -1108,7 +1106,7 @@ public void TestEnforceExApi()
[Fact]
public async Task TestEnforceExApiAsync()
{
Enforcer e = new(_testModelFixture.GetBasicTestModel());
Enforcer e = new(TestModelFixture.GetBasicTestModel());

await TestEnforceExAsync(e, "alice", "data1", "read", new List<string> { "alice", "data1", "read" });
await TestEnforceExAsync(e, "alice", "data1", "write", new List<string>());
Expand All @@ -1119,7 +1117,7 @@ public async Task TestEnforceExApiAsync()
await TestEnforceExAsync(e, "bob", "data2", "read", new List<string>());
await TestEnforceExAsync(e, "bob", "data2", "write", new List<string> { "bob", "data2", "write" });

e = new Enforcer(_testModelFixture.GetNewRbacTestModel());
e = new Enforcer(TestModelFixture.GetNewRbacTestModel());

await TestEnforceExAsync(e, "alice", "data1", "read", new List<string> { "alice", "data1", "read" });
await TestEnforceExAsync(e, "alice", "data1", "write", new List<string>());
Expand All @@ -1130,7 +1128,7 @@ public async Task TestEnforceExApiAsync()
await TestEnforceExAsync(e, "bob", "data2", "read", new List<string>());
await TestEnforceExAsync(e, "bob", "data2", "write", new List<string> { "bob", "data2", "write" });

e = new Enforcer(_testModelFixture.GetNewPriorityTestModel());
e = new Enforcer(TestModelFixture.GetNewPriorityTestModel());
e.BuildRoleLinks();

await TestEnforceExAsync(e, "alice", "data1", "read", new List<string> { "alice", "data1", "read", "allow" });
Expand All @@ -1148,7 +1146,7 @@ await TestEnforceExAsync(e, "bob", "data2", "read",
[Fact]
public void TestEnforceExApiLog()
{
Enforcer e = new(_testModelFixture.GetBasicTestModel())
Enforcer e = new(TestModelFixture.GetBasicTestModel())
{
Logger = new MockLogger<Enforcer>(_testOutputHelper)
};
Expand All @@ -1173,7 +1171,7 @@ public void TestEnforceExApiLog()
[Fact]
public void TestEnforceWithMatcherApi()
{
Enforcer e = new(_testModelFixture.GetBasicTestModel());
Enforcer e = new(TestModelFixture.GetBasicTestModel());
string matcher = "r.sub != p.sub && r.obj == p.obj && r.act == p.act";

e.TestEnforceWithMatcher(matcher, "alice", "data1", "read", false);
Expand All @@ -1189,11 +1187,11 @@ public void TestEnforceWithMatcherApi()
[Fact]
public void TestBatchEnforceWithMatcherApi()
{
Enforcer e = new(_testModelFixture.GetBasicTestModel());
Enforcer e = new(TestModelFixture.GetBasicTestModel());
string matcher = "r.sub != p.sub && r.obj == p.obj && r.act == p.act";

IEnumerable<(RequestValues<string, string, string>, bool)> testCases =
new (RequestValues<string, string, string>, bool)[]
new[]
{
(Request.CreateValues("alice", "data1", "read"), false),
(Request.CreateValues("alice", "data1", "write"), false),
Expand All @@ -1211,11 +1209,11 @@ public void TestBatchEnforceWithMatcherApi()
[Fact]
public void TestBatchEnforceWithMatcherParallel()
{
Enforcer e = new(_testModelFixture.GetBasicTestModel());
Enforcer e = new(TestModelFixture.GetBasicTestModel());
string matcher = "r.sub != p.sub && r.obj == p.obj && r.act == p.act";

IEnumerable<(RequestValues<string, string, string>, bool)> testCases =
new (RequestValues<string, string, string>, bool)[]
new[]
{
(Request.CreateValues("alice", "data1", "read"), false),
(Request.CreateValues("alice", "data1", "write"), false),
Expand All @@ -1233,7 +1231,7 @@ public void TestBatchEnforceWithMatcherParallel()
[Fact]
public async Task TestEnforceWithMatcherAsync()
{
Enforcer e = new(_testModelFixture.GetBasicTestModel());
Enforcer e = new(TestModelFixture.GetBasicTestModel());
string matcher = "r.sub != p.sub && r.obj == p.obj && r.act == p.act";

await e.TestEnforceWithMatcherAsync(matcher, "alice", "data1", "read", false);
Expand All @@ -1249,11 +1247,11 @@ public async Task TestEnforceWithMatcherAsync()
[Fact]
public void TestBatchEnforceWithMatcherApiAsync()
{
Enforcer e = new(_testModelFixture.GetBasicTestModel());
Enforcer e = new(TestModelFixture.GetBasicTestModel());
string matcher = "r.sub != p.sub && r.obj == p.obj && r.act == p.act";

IEnumerable<(RequestValues<string, string, string>, bool)> testCases =
new (RequestValues<string, string, string>, bool)[]
new[]
{
(Request.CreateValues("alice", "data1", "read"), false),
(Request.CreateValues("alice", "data1", "write"), false),
Expand All @@ -1271,7 +1269,7 @@ public void TestBatchEnforceWithMatcherApiAsync()
[Fact]
public void TestEnforceExWithMatcherApi()
{
Enforcer e = new(_testModelFixture.GetBasicTestModel());
Enforcer e = new(TestModelFixture.GetBasicTestModel());
string matcher = "r.sub != p.sub && r.obj == p.obj && r.act == p.act";

e.TestEnforceExWithMatcher(matcher, "alice", "data1", "read", new List<string>());
Expand All @@ -1287,7 +1285,7 @@ public void TestEnforceExWithMatcherApi()
[Fact]
public async Task TestEnforceExWithMatcherAsync()
{
Enforcer e = new(_testModelFixture.GetBasicTestModel());
Enforcer e = new(TestModelFixture.GetBasicTestModel());
string matcher = "r.sub != p.sub && r.obj == p.obj && r.act == p.act";

await e.TestEnforceExWithMatcherAsync(matcher, "alice", "data1", "read", new List<string>());
Expand Down
20 changes: 8 additions & 12 deletions Casbin.UnitTests/ModelTests/ManagementApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ namespace Casbin.UnitTests.ModelTests;
[Collection("Model collection")]
public class ManagementApiTest
{
private readonly TestModelFixture _testModelFixture;

public ManagementApiTest(TestModelFixture testModelFixture) => _testModelFixture = testModelFixture;

[Fact]
public void TestGetList()
{
Enforcer e = new(_testModelFixture.GetNewRbacTestModel());
Enforcer e = new(TestModelFixture.GetNewRbacTestModel());
e.BuildRoleLinks();
TestStringList(e.GetAllSubjects, AsList("alice", "bob", "data2_admin"));
TestStringList(e.GetAllObjects, AsList("data1", "data2"));
Expand All @@ -29,7 +25,7 @@ public void TestGetList()
[Fact]
public void TestGetPolicyApi()
{
Enforcer e = new(_testModelFixture.GetNewRbacTestModel());
Enforcer e = new(TestModelFixture.GetNewRbacTestModel());
e.BuildRoleLinks();

TestGetPolicy(e, AsList(
Expand Down Expand Up @@ -80,7 +76,7 @@ public void TestGetPolicyApi()
[Fact]
public void TestModifyPolicy()
{
Enforcer e = new(_testModelFixture.GetNewRbacTestModel());
Enforcer e = new(TestModelFixture.GetNewRbacTestModel());
e.BuildRoleLinks();

TestGetPolicy(e, AsList(
Expand Down Expand Up @@ -207,7 +203,7 @@ public void TestModifyPolicy()
[Fact]
public async Task TestModifyPolicyAsync()
{
Enforcer e = new(_testModelFixture.GetNewRbacTestModel());
Enforcer e = new(TestModelFixture.GetNewRbacTestModel());
e.BuildRoleLinks();

TestGetPolicy(e, AsList(
Expand Down Expand Up @@ -330,7 +326,7 @@ public async Task TestModifyPolicyAsync()
[Fact]
public void TestModifyGroupingPolicy()
{
Enforcer e = new(_testModelFixture.GetNewRbacTestModel());
Enforcer e = new(TestModelFixture.GetNewRbacTestModel());
e.BuildRoleLinks();

TestGetRoles(e, "alice", AsList("data2_admin"));
Expand Down Expand Up @@ -443,7 +439,7 @@ public void TestModifyGroupingPolicy()
[Fact]
public async Task TestModifyGroupingPolicyAsync()
{
Enforcer e = new(_testModelFixture.GetNewRbacTestModel());
Enforcer e = new(TestModelFixture.GetNewRbacTestModel());
e.BuildRoleLinks();

TestGetRoles(e, "alice", AsList("data2_admin"));
Expand Down Expand Up @@ -557,7 +553,7 @@ public async Task TestModifyGroupingPolicyAsync()
[Fact]
public void TestModifySpecialPolicy()
{
Enforcer e = new(TestModelFixture.GetNewTestModel(_testModelFixture._rbacModelText));
Enforcer e = new(TestModelFixture.GetNewTestModel(TestModelFixture.RbacModelText));

e.AddPolicy("alice", "data1");
e.AddPolicy("alice", "data1", "read");
Expand All @@ -573,7 +569,7 @@ public void TestModifySpecialPolicy()
[Fact]
public async Task TestModifySpecialPolicyAsync()
{
Enforcer e = new(_testModelFixture.GetNewRbacTestModel());
Enforcer e = new(TestModelFixture.GetNewRbacTestModel());
e.ClearPolicy();

await e.AddPolicyAsync("alice", "data1");
Expand Down
Loading

0 comments on commit 9535875

Please sign in to comment.