From 73c2a831cb5bb38657f7c35572b8acb88924735a Mon Sep 17 00:00:00 2001 From: Kim Burgestrand Date: Wed, 9 Oct 2024 16:13:27 +0200 Subject: [PATCH] Reorganize specs to make them easier to read --- spec/pundit_spec.rb | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/spec/pundit_spec.rb b/spec/pundit_spec.rb index 59cd9e20..d859148f 100644 --- a/spec/pundit_spec.rb +++ b/spec/pundit_spec.rb @@ -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) @@ -112,6 +103,29 @@ end.to raise_error(Pundit::InvalidConstructorError, "Invalid # 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