Skip to content

Commit

Permalink
feat: add test class for casbin issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiujiarui committed Mar 21, 2024
1 parent 11a2e97 commit afd4504
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/test/java/org/casbin/MenuTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.casbin;

import org.casbin.jcasbin.main.Enforcer;
import org.junit.jupiter.api.Test;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MenuTest {
@Test
public void testMenu() {
//model.conf and policy.csv are in the examples/casbin directory
Enforcer enforcer = new Enforcer("examples/casbin/model.conf","examples/casbin/policy.csv");
List<String> perms = Arrays.asList(
// ROLE_ROOT ROLE_ADMIN ROLE_USER
"SystemMenu", // ✅ ❌ ❌
"UserMenu", // ❌ ✅ ✅
"UserSubMenu_allow", // ❌ ✅ ✅
"UserSubSubMenu", // ❌ ✅ ✅
"UserSubMenu_deny", // ❌ ✅ ❌
"AdminMenu", // ✅ ✅ ❌
"AdminSubMenu_allow", // ✅ ✅ ❌
"AdminSubMenu_deny" // ✅ ❌ ❌
);

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));


List<String> actualPermissions = new ArrayList<>();

// Read permissions for testing
for (String obj : perms) {
try {
// Write test results in format
writer.write(obj + repeat(" ",30 - obj.length()));
for (String sub : new String[]{"ROLE_ROOT", "ROLE_ADMIN", "ROLE_USER"}) {
boolean ok = enforcer.enforce(sub, obj, "read");
actualPermissions.add(ok ? "✅": "❌");
writer.write(ok ? "✅\t": "❌\t");
}
writer.write("\n");
} catch (IOException e) {
e.printStackTrace();
}
}


try {
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
/*
//optional matching test
// Store the expected results
List<String> expectedPermissions = Arrays.asList("✅", "❌", "❌", "❌", "✅", "✅", "❌","✅", "✅","❌","✅","✅","❌","✅","❌","✅","✅","❌","✅","✅","❌","✅","❌","❌");
// Compare expected results with actual results, if they do not match, the test fails
if (!expectedPermissions.toString().equals(actualPermissions.toString())) {
throw new RuntimeException("don't match");
}
*/

}



private String repeat(String str, int times) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < times; i++) {
sb.append(str);
}

return sb.toString();
}
}

0 comments on commit afd4504

Please sign in to comment.