Skip to content

Commit afd4504

Browse files
committed
feat: add test class for casbin issue
1 parent 11a2e97 commit afd4504

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.casbin;
2+
3+
import org.casbin.jcasbin.main.Enforcer;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.io.BufferedWriter;
7+
import java.io.IOException;
8+
import java.io.OutputStreamWriter;
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
14+
public class MenuTest {
15+
@Test
16+
public void testMenu() {
17+
//model.conf and policy.csv are in the examples/casbin directory
18+
Enforcer enforcer = new Enforcer("examples/casbin/model.conf","examples/casbin/policy.csv");
19+
List<String> perms = Arrays.asList(
20+
// ROLE_ROOT ROLE_ADMIN ROLE_USER
21+
"SystemMenu", // ✅ ❌ ❌
22+
"UserMenu", // ❌ ✅ ✅
23+
"UserSubMenu_allow", // ❌ ✅ ✅
24+
"UserSubSubMenu", // ❌ ✅ ✅
25+
"UserSubMenu_deny", // ❌ ✅ ❌
26+
"AdminMenu", // ✅ ✅ ❌
27+
"AdminSubMenu_allow", // ✅ ✅ ❌
28+
"AdminSubMenu_deny" // ✅ ❌ ❌
29+
);
30+
31+
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
32+
33+
34+
List<String> actualPermissions = new ArrayList<>();
35+
36+
// Read permissions for testing
37+
for (String obj : perms) {
38+
try {
39+
// Write test results in format
40+
writer.write(obj + repeat(" ",30 - obj.length()));
41+
for (String sub : new String[]{"ROLE_ROOT", "ROLE_ADMIN", "ROLE_USER"}) {
42+
boolean ok = enforcer.enforce(sub, obj, "read");
43+
actualPermissions.add(ok ? "✅": "❌");
44+
writer.write(ok ? "✅\t": "❌\t");
45+
}
46+
writer.write("\n");
47+
} catch (IOException e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
52+
53+
try {
54+
writer.flush();
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
}
58+
/*
59+
//optional matching test
60+
// Store the expected results
61+
List<String> expectedPermissions = Arrays.asList("✅", "❌", "❌", "❌", "✅", "✅", "❌","✅", "✅","❌","✅","✅","❌","✅","❌","✅","✅","❌","✅","✅","❌","✅","❌","❌");
62+
// Compare expected results with actual results, if they do not match, the test fails
63+
if (!expectedPermissions.toString().equals(actualPermissions.toString())) {
64+
throw new RuntimeException("don't match");
65+
}
66+
*/
67+
68+
}
69+
70+
71+
72+
private String repeat(String str, int times) {
73+
StringBuilder sb = new StringBuilder();
74+
for (int i = 0; i < times; i++) {
75+
sb.append(str);
76+
}
77+
78+
return sb.toString();
79+
}
80+
}
81+

0 commit comments

Comments
 (0)