From f60130ca95e9e7a5c8cef79bdbdd3611e2ef809d Mon Sep 17 00:00:00 2001 From: ailiujiarui <115072567+ailiujiarui@users.noreply.github.com> Date: Sun, 24 Mar 2024 16:16:53 +0800 Subject: [PATCH] feat: add unit tests for Casbin enforcer (#12) * feat: add test class for casbin issue * Update MenuTest.java --------- Co-authored-by: Eric Luo --- src/test/java/org/casbin/MenuTest.java | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/test/java/org/casbin/MenuTest.java diff --git a/src/test/java/org/casbin/MenuTest.java b/src/test/java/org/casbin/MenuTest.java new file mode 100644 index 0000000..8639fb9 --- /dev/null +++ b/src/test/java/org/casbin/MenuTest.java @@ -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")); + } +} +