-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor result keeping in policies
Store the result object in the execution context instead of the ivar to avoid problems in fiber-concurrent execution environments This is a temporary workaroudn with some preparation for 1.0; ideally, we should get rid of the local-global state completely Ref palkan/action_policy-graphql#47
- Loading branch information
Showing
11 changed files
with
137 additions
and
53 deletions.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class TestBasePolicy < Minitest::Test | ||
class User < Struct.new(:kind) | ||
end | ||
|
||
class FiberUser < User | ||
def kind | ||
Fiber.yield | ||
super | ||
end | ||
end | ||
|
||
class TestPolicy < ActionPolicy::Base | ||
self.identifier = :test | ||
|
||
authorize :user | ||
|
||
def milk? | ||
check?(:cat?) | ||
end | ||
|
||
def honey? | ||
check?(:bee?) | ||
end | ||
|
||
def cat? = user.kind == "cat" | ||
|
||
def bee? = user.kind == "bee" | ||
end | ||
|
||
def test_baseline | ||
user = User.new("cat") | ||
policy = TestPolicy.new(user:) | ||
|
||
assert policy.apply(:milk?) | ||
refute policy.apply(:honey?) | ||
assert_equal({test: [:bee?]}, policy.result.reasons.details) | ||
|
||
# it's cached | ||
user.kind = "bee" | ||
|
||
assert policy.apply(:milk?) | ||
refute policy.apply(:honey?) | ||
end | ||
|
||
def test_fiber_concurrency | ||
user = FiberUser.new("cat") | ||
policy = TestPolicy.new(user:) | ||
|
||
f1 = Fiber.new do | ||
refute policy.apply(:honey?) | ||
assert_equal({test: [:bee?]}, policy.result.reasons.details) | ||
end | ||
|
||
f2 = Fiber.new do | ||
assert policy.apply(:milk?) | ||
end | ||
|
||
f1.resume | ||
f2.resume | ||
f2.resume | ||
f1.resume | ||
end | ||
end |