Skip to content

Commit

Permalink
feat: add KeyMatch5 test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sukidayou committed Jan 17, 2025
1 parent 65d7637 commit ed69d3d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
11 changes: 11 additions & 0 deletions examples/keymatch5_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

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

[matchers]
m = r.sub == p.sub && keyMatch5(r.obj, p.obj) && regexMatch(r.act, p.act)
2 changes: 2 additions & 0 deletions examples/keymatch5_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
p, alice, /alice_data/{resource}/.*, GET
p, alice, /alice_data2/{id}/using/{resId}/.*, GET
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<dependency>
<groupId>org.casbin</groupId>
<artifactId>jcasbin</artifactId>
<version>1.55.0</version>
<version>1.79.0</version>
</dependency>

<dependency>
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/casbin/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,10 @@ public void testABACRule() {
assertEquals(Client.run(new String[]{"enforce", "-m", "examples/abac_rule_model.conf", "-p", "examples/abac_rule_policy.csv", "{Age: 30}", "/data1", "read"}), "{\"allow\":true,\"explain\":null}");
assertEquals(Client.run(new String[]{"enforceEx", "-m", "examples/abac_rule_model.conf", "-p", "examples/abac_rule_policy.csv", "{Age: 30}", "/data1", "read"}), "{\"allow\":true,\"explain\":[\"r.sub.Age > 18 && r.sub.Age < 60\",\"/data1\",\"read\"]}");
}

@Test
public void testKeyMatch5() {
assertEquals(Client.run(new String[]{"enforceEx", "-m", "examples/keymatch5_model.conf", "-p", "examples/keymatch5_policy.csv", "alice", "/alice_data/hello/123", "GET"}), "{\"allow\":true,\"explain\":[\"alice\",\"/alice_data/{resource}/.*\",\"GET\"]}");
assertEquals(Client.run(new String[]{"enforceEx", "-m", "examples/keymatch5_model.conf", "-p", "examples/keymatch5_policy.csv", "alice", "/alice_data/hello/123", "POST"}), "{\"allow\":false,\"explain\":[]}");
}
}

0 comments on commit ed69d3d

Please sign in to comment.