Skip to content

Commit

Permalink
feat: add unit tests for Casbin enforcer (#12)
Browse files Browse the repository at this point in the history
* feat: add test class for casbin issue

* Update MenuTest.java

---------

Co-authored-by: Eric Luo <[email protected]>
  • Loading branch information
ailiujiarui and hsluoyz authored Mar 24, 2024
1 parent 9ab6039 commit f60130c
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/test/java/org/casbin/MenuTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.casbin;

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

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");
// Test each permission individually

assertTrue(enforcer.enforce("ROLE_ROOT", "SystemMenu", "read"));
assertFalse(enforcer.enforce("ROLE_ADMIN", "SystemMenu", "read"));
assertFalse(enforcer.enforce("ROLE_USER", "SystemMenu", "read"));

assertFalse(enforcer.enforce("ROLE_ROOT", "UserMenu", "read"));
assertTrue(enforcer.enforce("ROLE_ADMIN", "UserMenu", "read"));
assertFalse(enforcer.enforce("ROLE_USER", "UserMenu", "read"));

assertFalse(enforcer.enforce("ROLE_ROOT", "UserSubMenu_allow", "read"));
assertTrue(enforcer.enforce("ROLE_ADMIN", "UserSubMenu_allow", "read"));
assertTrue(enforcer.enforce("ROLE_USER", "UserSubMenu_allow", "read"));

assertFalse(enforcer.enforce("ROLE_ROOT", "UserSubSubMenu", "read"));
assertTrue(enforcer.enforce("ROLE_ADMIN", "UserSubSubMenu", "read"));
assertTrue(enforcer.enforce("ROLE_USER", "UserSubSubMenu", "read"));

assertFalse(enforcer.enforce("ROLE_ROOT", "UserSubMenu_deny", "read"));
assertTrue(enforcer.enforce("ROLE_ADMIN", "UserSubMenu_deny", "read"));
assertFalse(enforcer.enforce("ROLE_USER", "UserSubMenu_deny", "read"));

assertTrue(enforcer.enforce("ROLE_ROOT", "AdminMenu", "read"));
assertTrue(enforcer.enforce("ROLE_ADMIN", "AdminMenu", "read"));
assertFalse(enforcer.enforce("ROLE_USER", "AdminMenu", "read"));

assertTrue(enforcer.enforce("ROLE_ROOT", "AdminSubMenu_allow", "read"));
assertTrue(enforcer.enforce("ROLE_ADMIN", "AdminSubMenu_allow", "read"));
assertFalse(enforcer.enforce("ROLE_USER", "AdminSubMenu_allow", "read"));

assertTrue(enforcer.enforce("ROLE_ROOT", "AdminSubMenu_deny", "read"));
assertFalse(enforcer.enforce("ROLE_ADMIN", "AdminSubMenu_deny", "read"));
assertFalse(enforcer.enforce("ROLE_USER", "AdminSubMenu_deny", "read"));
}
}

0 comments on commit f60130c

Please sign in to comment.