Skip to content

Commit

Permalink
fix: minor have_authorized_scope.with_scope improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Apr 19, 2024
1 parent 64edca8 commit a3660e8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Add `.with_context` modifier to the `#have_authorized_scope` matcher. ([@killondark][])

## 0.6.8 (2024-01-17)

- Do not preload Rails base classes, use load hooks everywhere. ([@palkan][])
Expand Down Expand Up @@ -509,3 +511,4 @@ This value is now stored in a cache (if any) instead of just the call result (`t
[@skojin]: https://github.com/skojin
[@tomdalling]: https://github.com/tomdalling
[@matsales28]: https://github.com/matsales28
[@killondark]: https://github.com/killondark
6 changes: 1 addition & 5 deletions lib/action_policy/rspec/have_authorized_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def supports_block_expectations?() = true
def failure_message
"expected a scoping named :#{name} for type :#{type} " \
"#{scope_options_message} " \
"and #{context_message} " \
"#{context ? "and context #{context.inspect} " : ""}" \
"from #{policy} to have been applied, " \
"but #{actual_scopes_message}"
end
Expand All @@ -103,10 +103,6 @@ def scope_options_message
end
end

def context_message
context.blank? ? "without context" : "with context: #{context}"
end

def actual_scopes_message
if actual_scopes.empty?
"no scopings have been made"
Expand Down
13 changes: 4 additions & 9 deletions spec/action_policy/rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,11 @@ def filter_with_context(users, context:)
end

specify "with context" do
expect { subject.filter_with_context(target, context: { all_users: false, able_to_yell: true }) }
expect { subject.filter_with_context(target, context: {all_users: false, able_to_yell: true}) }
.to have_authorized_scope(:data)
.with(TestService::CustomPolicy).as(:all)
.with_context(all_users: false, able_to_yell: true)
end
end
end

context "when no scoping performed" do
Expand All @@ -319,7 +319,6 @@ def filter_with_context(users, context:)
end.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
Regexp.new("expected a scoping named :default for type :datum without scope options " \
"and without context " \
"from TestService::CustomPolicy to have been applied")
)
end
Expand All @@ -331,7 +330,6 @@ def filter_with_context(users, context:)
end.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
Regexp.new("expected a scoping named :default for type :data without scope options " \
"and without context " \
"from UserPolicy to have been applied")
)
end
Expand All @@ -343,7 +341,6 @@ def filter_with_context(users, context:)
end.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
Regexp.new("expected a scoping named :default for type :data without scope options " \
"and without context " \
"from UserPolicy to have been applied")
)
end
Expand All @@ -357,7 +354,6 @@ def filter_with_context(users, context:)
RSpec::Expectations::ExpectationNotMetError,
Regexp.new("expected a scoping named :default for type :data " \
"with scope options {:with_admins=>false} " \
"and without context " \
"from TestService::CustomPolicy to have been applied")
)
end
Expand All @@ -371,7 +367,6 @@ def filter_with_context(users, context:)
RSpec::Expectations::ExpectationNotMetError,
Regexp.new("expected a scoping named :default for type :data " \
"with scope options matching {:with_admins=>\\(a falsey value\\)} " \
"and without context " \
"from TestService::CustomPolicy to have been applied")
)
end
Expand All @@ -391,14 +386,14 @@ def filter_with_context(users, context:)

specify "context mismatch" do
expect do
expect { subject.filter_with_context(target, context: { all_users: true }) }
expect { subject.filter_with_context(target, context: {all_users: true}) }
.to have_authorized_scope(:data)
.with(TestService::CustomPolicy)
.with_context(all_users: false)
end.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
Regexp.new("expected a scoping named :default for type :data without scope options " \
"and with context: {:all_users=>false} " \
"and context {:all_users=>false} " \
"from TestService::CustomPolicy to have been applied")
)
end
Expand Down

0 comments on commit a3660e8

Please sign in to comment.