-
Notifications
You must be signed in to change notification settings - Fork 46
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
Some more refactoring and testing of BulletTrain::LoadsAndAuthorizesResource #516
Merged
jagthedrummer
merged 4 commits into
bullet-train-co:main
from
aaricpittman:load-and-auth-refactor
Oct 3, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a8c357d
fix: make regex_to_remove_controller_namespace a class method rather …
aaricpittman 747c0a4
refactor: BulletTrain::LoadsAndAuthorizesResource.model_namespace_fro…
aaricpittman 2ba24d2
refactor: BulletTrain::LoadsAndAuthorizesResource#load_team and add t…
aaricpittman 7cf6a20
Update bullet_train-super_load_and_authorize_resource/test/test_helpe…
jagthedrummer 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
94 changes: 87 additions & 7 deletions
94
...ize_resource/test/controllers/concerns/bullet_train/loads_and_authorizes_resource_test.rb
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 |
---|---|---|
@@ -1,23 +1,103 @@ | ||
require "test_helper" | ||
|
||
# Has to be required here because these tests get run in the context of another application in another repo. | ||
require "minitest/mock" | ||
class BulletTrain::LoadsAndAuthorizeResourceTest < ActiveSupport::TestCase | ||
class TestClass | ||
class TestControllerClass < ActionController::Base | ||
include BulletTrain::LoadsAndAuthorizesResource | ||
|
||
attr_accessor :child_object, :current_user, :parent_object, :team | ||
|
||
def self.regex_to_remove_controller_namespace | ||
end | ||
|
||
def can?(*args) | ||
end | ||
end | ||
|
||
module Users | ||
class TestControllerClass < ActionController::Base | ||
include BulletTrain::LoadsAndAuthorizesResource | ||
|
||
def self.regex_to_remove_controller_namespace | ||
/^BulletTrain::LoadsAndAuthorizeResourceTest::/ | ||
end | ||
end | ||
end | ||
|
||
test "it defines .model_namespace_from_controller_namespace" do | ||
assert TestClass.respond_to?(:model_namespace_from_controller_namespace) | ||
test "model_namespace_from_controller_namespace returns an array of modules names based on the classes namespace minus regex_to_remove_controller_namespace" do | ||
assert_equal ["BulletTrain", "LoadsAndAuthorizeResourceTest"], TestControllerClass.model_namespace_from_controller_namespace | ||
assert_equal ["Users"], Users::TestControllerClass.model_namespace_from_controller_namespace | ||
end | ||
|
||
test "it defines .account_load_and_authorize_resource" do | ||
assert TestClass.respond_to?(:account_load_and_authorize_resource) | ||
assert_respond_to TestControllerClass, :account_load_and_authorize_resource | ||
end | ||
|
||
test "it defines .regex_to_remove_controller_namespace" do | ||
assert_respond_to TestControllerClass, :regex_to_remove_controller_namespace | ||
end | ||
|
||
test "it defines #load_team" do | ||
assert TestClass.new.respond_to?(:load_team) | ||
assert_respond_to TestControllerClass.new, :load_team | ||
end | ||
|
||
test "#load_team does not set @team if child_object and parent_object are nil" do | ||
subject = TestControllerClass.new | ||
subject.child_object = nil | ||
subject.parent_object = nil | ||
|
||
assert_nil subject.instance_variable_get(:@team) | ||
end | ||
|
||
test "#load_team sets @team" do | ||
team = OpenStruct.new | ||
subject = TestControllerClass.new | ||
subject.child_object = OpenStruct.new(team: team) | ||
|
||
subject.load_team | ||
|
||
assert_equal team, subject.instance_variable_get(:@team) | ||
end | ||
|
||
test "#load_team sets Current attributes if defined" do | ||
unless defined?(::Current) | ||
temp_current_class = Class.new(ActiveSupport::CurrentAttributes) do | ||
attribute :team | ||
end | ||
Object.const_set(:Current, temp_current_class) | ||
end | ||
|
||
team = OpenStruct.new | ||
subject = TestControllerClass.new | ||
subject.child_object = OpenStruct.new(team: team) | ||
|
||
team.stub(:try, nil) do | ||
subject.load_team | ||
end | ||
kaspth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
assert_equal team, Current.team | ||
|
||
if temp_current_class | ||
Object.send(:remove_const, :Current) | ||
end | ||
end | ||
|
||
test "it defines #regex_to_remove_controller_namespace" do | ||
assert TestClass.new.respond_to?(:regex_to_remove_controller_namespace) | ||
test "#load_team updates current_user's current_team if persisted and can" do | ||
current_user = Minitest::Mock.new | ||
team = OpenStruct.new(id: 1) | ||
subject = TestControllerClass.new | ||
subject.child_object = OpenStruct.new(team: team) | ||
subject.current_user = current_user | ||
|
||
current_user.expect(:update_column, nil, [:current_team_id, team.id]) | ||
|
||
team.stub(:try, true) do | ||
subject.stub(:can?, true) do | ||
subject.load_team | ||
end | ||
end | ||
|
||
current_user.verify | ||
end | ||
end |
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
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.
Since we've inserted the
return unless @team
above, we can remove thetry
here: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.
I'm not calling
try
for null safety. I'm calling it because there is nothing in this gem to guarantee that the value of@team
responds topersisted?
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.
Seems like it'd be better to raise the
NoMethodError
exception with our expected interface to ease debugging, but this code path was already wrapped intry
so we can keep it.