Skip to content

Commit

Permalink
Fixing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
killondark committed Apr 6, 2024
1 parent e3f4859 commit 86d553e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
9 changes: 5 additions & 4 deletions docs/testing.md
Expand Up @@ -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
```
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/action_policy/rspec/have_authorized_scope.rb
Expand Up @@ -27,7 +27,6 @@ def initialize(type)
@type = type
@name = :default
@scope_options = nil
@context = {}
end

def with(policy)
Expand Down Expand Up @@ -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
Expand Down
27 changes: 16 additions & 11 deletions lib/action_policy/testing.rb
Expand Up @@ -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)
Expand All @@ -23,34 +35,27 @@ 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
@target = target
@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)
policy_class == policy.class &&
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
Expand Down

0 comments on commit 86d553e

Please sign in to comment.