Skip to content

Commit

Permalink
refactor: BulletTrain::LoadsAndAuthorizesResource#load_team and add t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
aaricpittman committed Sep 2, 2023
1 parent 57c3821 commit 7162f65
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,13 @@ def account_load_and_authorize_resource(model, options, old_options = {})
end

def load_team
# Not all objects that need to be authorized belong to a team,
# so we give @team a nil value if no association is found.
begin
# Sometimes `@team` has already been populated by earlier `before_action` steps.
@team ||= @child_object&.team || @parent_object&.team
rescue NoMethodError
@team = nil
end
@team ||= @child_object&.try(:team) || @parent_object&.try(:team)

return unless @team

# Update current attributes.
Current.team = @team
if defined?(Current) && Current.respond_to?(:team=)
Current.team = @team
end

# If the currently loaded team is saved to the database, make that the user's new current team.
if @team.try(:persisted?)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
require "test_helper"

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 TestClass
class TestControllerClass < ActionController::Base
include BulletTrain::LoadsAndAuthorizesResource

def self.regex_to_remove_controller_namespace
Expand All @@ -19,19 +24,64 @@ def self.regex_to_remove_controller_namespace
end

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"], TestClass.model_namespace_from_controller_namespace
assert_equal ["Users"], Users::TestClass.model_namespace_from_controller_namespace
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 TestControllerClass.respond_to?(:account_load_and_authorize_resource)
end

test "it defines .regex_to_remove_controller_namespace" do
assert TestClass.respond_to?(:regex_to_remove_controller_namespace)
assert TestControllerClass.respond_to?(:regex_to_remove_controller_namespace)
end

test "it defines #load_team" do
assert TestClass.new.respond_to?(:load_team)
assert TestControllerClass.new.respond_to?(:load_team)
end

test "#load_team returns nil child_object and parent_object are nil" do
subject = TestControllerClass.new
subject.child_object = nil
subject.parent_object = nil

assert_nil subject.load_team
end

test "#load_team sets Current attributes if defined" do
current_class = Class.new(ActiveSupport::CurrentAttributes) do
attribute :team
end
Object.const_set(:Current, current_class)

team = OpenStruct.new
subject = TestControllerClass.new
subject.child_object = OpenStruct.new(team: team)

team.stub(:try, nil) do
subject.load_team
end

assert_equal team, Current.team

Object.send(:remove_const, :Current)
end

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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require_relative "../test/dummy/config/environment"
ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)]
require "rails/test_help"
require "minitest/mock"
require "pry"

# Load fixtures from the engine
if ActiveSupport::TestCase.respond_to?(:fixture_path=)
Expand Down

0 comments on commit 7162f65

Please sign in to comment.