Skip to content

Commit

Permalink
feat: add some test cases like getUsersForRoleInDomain (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
liewstar authored Nov 19, 2024
1 parent ebc0018 commit 03a9a9d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
14 changes: 14 additions & 0 deletions examples/rbac_with_domains_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[request_definition]
r = sub, dom, obj, act

[policy_definition]
p = sub, dom, obj, act

[role_definition]
g = _, _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && r.act == p.act
6 changes: 6 additions & 0 deletions examples/rbac_with_domains_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
p, admin, domain1, data1, read
p, admin, domain1, data1, write
p, admin, domain2, data2, read
p, admin, domain2, data2, write
g, alice, admin, domain1
g, bob, admin, domain2
10 changes: 8 additions & 2 deletions src/test/java/org/casbin/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,21 @@ public void testRBACApi () {

assertEquals(Client.run(new String[]{"getImplicitPermissionsForUser", "-m", "examples/rbac_model.conf", "-p", "examples/rbac_with_hierarchy_policy.csv", "alice"}), "{\"allow\":null,\"explain\":[[\"alice\",\"data1\",\"read\"],[\"data1_admin\",\"data1\",\"read\"],[\"data1_admin\",\"data1\",\"write\"],[\"data2_admin\",\"data2\",\"read\"],[\"data2_admin\",\"data2\",\"write\"]]}");


assertEquals(Client.run(new String[]{"getNamedImplicitPermissionsForUser", "-m", "examples/rbac_with_multiple_policy_model.conf", "-p", "examples/rbac_with_multiple_policy_policy.csv", "p2", "alice"}), "{\"allow\":null,\"explain\":[[\"admin\",\"create\"],[\"user\",\"view\"]]}");

}

@Test
public void testRBACWithDomainsApi () {
assertEquals(Client.run(new String[]{"getUsersForRoleInDomain", "-m", "examples/rbac_with_domains_model.conf", "-p", "examples/rbac_with_domains_policy.csv", "admin", "domain1"}), "{\"allow\":null,\"explain\":[\"alice\"]}");

assertEquals(Client.run(new String[]{"getRolesForUserInDomain", "-m", "examples/rbac_with_domains_model.conf", "-p", "examples/rbac_with_domains_policy.csv", "alice", "domain1"}), "{\"allow\":null,\"explain\":[\"admin\"]}");

assertEquals(Client.run(new String[]{"getPermissionsForUserInDomain", "-m", "examples/rbac_with_domains_model.conf", "-p", "examples/rbac_with_domains_policy.csv", "admin", "domain1"}), "{\"allow\":null,\"explain\":[[\"admin\",\"domain1\",\"data1\",\"read\"],[\"admin\",\"domain1\",\"data1\",\"write\"]]}");

assertEquals(Client.run(new String[]{"addRoleForUserInDomain", "-m", "examples/rbac_with_domains_model.conf", "-p", "examples/rbac_with_domains_policy.csv", "alice", "admin", "domain3"}), "{\"allow\":true,\"explain\":null}");


assertEquals(Client.run(new String[]{"deleteRoleForUserInDomain", "-m", "examples/rbac_with_domains_model.conf", "-p", "examples/rbac_with_domains_policy.csv", "alice", "admin", "domain3"}), "{\"allow\":true,\"explain\":null}");
}


Expand Down

0 comments on commit 03a9a9d

Please sign in to comment.