-
Notifications
You must be signed in to change notification settings - Fork 182
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
PoC kotlin data classes #1662
Closed
+333
−13
Closed
PoC kotlin data classes #1662
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
languages/java/oso/src/test/java/com/osohq/oso/OsoJavaRecordTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package com.osohq.oso; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class OsoJavaRecordTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copy paste of the existing |
||
protected Oso o; | ||
|
||
public record User(String name) { | ||
public List<Company> companies() { | ||
return List.of(new Company(1)); | ||
} | ||
} | ||
|
||
public record Widget(int id) { | ||
} | ||
|
||
public record Company(int id) { | ||
|
||
public String role(User a) { | ||
if (a.name.equals("president")) { | ||
return "admin"; | ||
} | ||
|
||
return "guest"; | ||
} | ||
} | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
try { | ||
URL testOso = getClass().getClassLoader().getResource("test_oso.polar"); | ||
|
||
o = new Oso(); | ||
o.registerClass(User.class, "User"); | ||
o.registerClass(Widget.class, "Widget"); | ||
o.registerClass(Company.class, "Company"); | ||
|
||
o.loadFile(testOso.getPath()); | ||
} catch (Exception e) { | ||
throw new Error(e); | ||
} | ||
} | ||
|
||
@Test | ||
public void testIsAllowed() throws Exception { | ||
User guest = new User("guest"); | ||
Widget resource1 = new Widget(1); | ||
assertTrue(o.isAllowed(guest, "get", resource1)); | ||
|
||
User president = new User("president"); | ||
Company company = new Company(1); | ||
assertTrue(o.isAllowed(president, "create", company)); | ||
} | ||
|
||
@Test | ||
public void testFail() throws Exception { | ||
User guest = new User("guest"); | ||
Widget widget = new Widget(1); | ||
assertFalse(o.isAllowed(guest, "not_allowed", widget)); | ||
} | ||
|
||
@Test | ||
public void testInstanceFromExternalCall() throws Exception { | ||
Company company = new Company(1); | ||
User guest = new User("guest"); | ||
assertTrue(o.isAllowed(guest, "frob", company)); | ||
|
||
// if the guest user can do it, then the dict should | ||
// create an instance of the user and be allowed | ||
HashMap<String, String> userMap = new HashMap<String, String>(); | ||
userMap.put("username", "guest"); | ||
assertTrue(o.isAllowed(userMap, "frob", company)); | ||
} | ||
|
||
@Test | ||
public void testAllowModel() throws Exception { | ||
User auditor = new User("auditor"); | ||
|
||
assertTrue(o.isAllowed(auditor, "list", Company.class)); | ||
assertFalse(o.isAllowed(auditor, "list", Widget.class)); | ||
} | ||
|
||
@Test | ||
public void testGetAllowedActions() throws Exception { | ||
|
||
Oso o = new Oso(); | ||
o.registerClass(User.class, "User"); | ||
o.registerClass(Widget.class, "Widget"); | ||
|
||
o.loadStr( | ||
"allow(_actor: User{name: \"sally\"}, action, _resource: Widget{id: 1})" | ||
+ " if action in [\"CREATE\", \"READ\"];"); | ||
|
||
User actor = new User("sally"); | ||
Widget widget = new Widget(1); | ||
HashSet<Object> actions = o.getAllowedActions(actor, widget); | ||
|
||
assertEquals(actions.size(), 2); | ||
assertTrue(actions.contains("CREATE")); | ||
assertTrue(actions.contains("READ")); | ||
|
||
o.clearRules(); | ||
|
||
o.loadStr( | ||
"allow(_actor: User{name: \"fred\"}, action, _resource: Widget{id: 2})" | ||
+ " if action in [1, 2, 3, 4];"); | ||
|
||
User actor2 = new User("fred"); | ||
Widget widget2 = new Widget(2); | ||
HashSet<Object> actions2 = o.getAllowedActions(actor2, widget2); | ||
|
||
assertEquals(actions2.size(), 4); | ||
assertTrue(actions2.contains(1)); | ||
assertTrue(actions2.contains(2)); | ||
assertTrue(actions2.contains(3)); | ||
assertTrue(actions2.contains(4)); | ||
|
||
User actor3 = new User("doug"); | ||
Widget widget3 = new Widget(4); | ||
assertTrue(o.getAllowedActions(actor3, widget3).isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testGetAllowedActionsWildcard() throws Exception { | ||
Oso o = new Oso(); | ||
|
||
o.registerClass(User.class, "User"); | ||
o.registerClass(Widget.class, "Widget"); | ||
|
||
o.loadStr("allow(_actor: User{name: \"John\"}, _action, _resource: Widget{id: 1});"); | ||
|
||
User actor = new User("John"); | ||
Widget widget = new Widget(1); | ||
|
||
assertEquals(Set.of("*"), o.getAllowedActions(actor, widget, true)); | ||
assertThrows(Exceptions.OsoException.class, () -> o.getAllowedActions(actor, widget, false)); | ||
} | ||
|
||
@Test | ||
public void testNotEqualOperator() { | ||
Oso oso = new Oso(); | ||
oso.registerClass(User.class, "User"); | ||
oso.loadStr("allow(actor: User, _action, _resource) if actor != nil;"); | ||
assertFalse(oso.isAllowed(null, "foo", "foo")); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just so I could use the locally built
0.26.5-SNAPSHOT
in other projects like a fork of #1655I built using: