Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unit tests for Casbin enforcer #12

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}

Loading