diff --git a/docs/testing.md b/docs/testing.md index d8da240..413a824 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -324,8 +324,9 @@ class UsersController < ApplicationController @user = authorized(User.all) end - def favorite - authorized_scope(User.all, context: {favorite: true}) + def for_user + user = User.find(params[:id]) + authorized_scope(User.all, context: {user:}) end end ``` @@ -415,9 +416,9 @@ expect { subject }.to have_authorized_scope(:scope) Also can use the `with_context` options: ```ruby -expect { get :favorite }.to have_authorized_scope(:scope) +expect { get :for_user, params: {id: user.id} }.to have_authorized_scope(:scope) .with_scope_options(matching(with_deleted: a_falsey_value)) - .with_context(favorite: true) + .with_context(a_hash_including(user:)) ``` ## Testing views diff --git a/lib/action_policy/rspec/have_authorized_scope.rb b/lib/action_policy/rspec/have_authorized_scope.rb index 2b8a2bf..1e9ab73 100644 --- a/lib/action_policy/rspec/have_authorized_scope.rb +++ b/lib/action_policy/rspec/have_authorized_scope.rb @@ -27,7 +27,6 @@ def initialize(type) @type = type @name = :default @scope_options = nil - @context = {} end def with(policy) @@ -105,7 +104,7 @@ def scope_options_message end def context_message - context.empty? ? "without context" : "with context: #{context}" + context.blank? ? "without context" : "with context: #{context}" end def actual_scopes_message diff --git a/lib/action_policy/testing.rb b/lib/action_policy/testing.rb index cf57ecb..fe216ee 100644 --- a/lib/action_policy/testing.rb +++ b/lib/action_policy/testing.rb @@ -5,7 +5,19 @@ module ActionPolicy module Testing # Collects all Authorizer calls module AuthorizeTracker + module Context + private + + def context_matches?(context, actual) + return true unless context + + context === actual || actual >= context + end + end + class Call # :nodoc: + include Context + attr_reader :policy, :rule def initialize(policy, rule) @@ -23,18 +35,12 @@ def inspect "#{policy.record.inspect} was authorized with #{policy.class}##{rule} " \ "and context #{policy.authorization_context.inspect}" end - - private - - def context_matches?(context, actual) - return true unless context - - context === actual || actual >= context - end end class Scoping # :nodoc: - attr_reader :policy, :target, :type, :name, :scope_options, :context + include Context + + attr_reader :policy, :target, :type, :name, :scope_options def initialize(policy, target, type, name, scope_options) @policy = policy @@ -42,7 +48,6 @@ def initialize(policy, target, type, name, scope_options) @type = type @name = name @scope_options = scope_options - @context = policy.authorization_context end def matches?(policy_class, actual_type, actual_name, actual_scope_options, actual_context) @@ -50,7 +55,7 @@ def matches?(policy_class, actual_type, actual_name, actual_scope_options, actua type == actual_type && name == actual_name && actual_scope_options === scope_options && - actual_context.all? { |key, value| context[key] == value } + context_matches?(actual_context, policy.authorization_context) end def inspect