Skip to content

Commit

Permalink
Reorganize specs to make them easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
Burgestrand committed Oct 9, 2024
1 parent 26e4521 commit 73c2a83
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions spec/pundit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,6 @@
expect(Pundit.authorize(user, Post, :show?)).to eq(Post)
end

it "can be given a different policy class" do
expect(Pundit.authorize(user, post, :create?, policy_class: PublicationPolicy)).to be_truthy
end

it "can be given a different policy class using namespaces" do
expect(PublicationPolicy).to receive(:new).with(user, comment).and_call_original
expect(Pundit.authorize(user, [:project, comment], :create?, policy_class: PublicationPolicy)).to be_truthy
end

it "works with anonymous class policies" do
expect(Pundit.authorize(user, article_tag, :show?)).to be_truthy
expect { Pundit.authorize(user, article_tag, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)
Expand Down Expand Up @@ -112,6 +103,29 @@
end.to raise_error(Pundit::InvalidConstructorError, "Invalid #<WikiPolicy> constructor is called")
end

context "when passed a policy class" do
it "uses the passed policy class" do
expect(Pundit.authorize(user, post, :create?, policy_class: PublicationPolicy)).to be_truthy
end

# This is documenting past behaviour.
it "doesn't cache the policy class" do
cache = {}

Pundit.authorize(user, post, :create?, policy_class: PublicationPolicy, cache: cache)
Pundit.authorize(user, post, :create?, policy_class: PublicationPolicy, cache: cache)

expect(PublicationPolicy.instances).to eq 2
end
end

context "when passed a policy class while simultaenously passing a namespace" do
it "uses the passed policy class" do
expect(PublicationPolicy).to receive(:new).with(user, comment).and_call_original
expect(Pundit.authorize(user, [:project, comment], :create?, policy_class: PublicationPolicy)).to be_truthy
end
end

context "when passed an explicit cache" do
it "uses the hash assignment interface on the cache" do
custom_cache = CustomCache.new
Expand Down

0 comments on commit 73c2a83

Please sign in to comment.