Skip to content

Commit

Permalink
Add missing test around cache usage
Browse files Browse the repository at this point in the history
This is not strictly a feature that was intended, but it's how it works so it needs to be supported in the future.
  • Loading branch information
Burgestrand committed May 8, 2024
1 parent 17616e6 commit 4f41278
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
28 changes: 22 additions & 6 deletions spec/authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
require "spec_helper"

describe Pundit::Authorization do
let(:controller) { Controller.new(user, "update", {}) }
def to_params(*args, **kwargs, &block)
ActionController::Parameters.new(*args, **kwargs, &block)
end

let(:controller) { Controller.new(user, "update", to_params({})) }
let(:user) { double }
let(:post) { Post.new(user) }
let(:customer_post) { Customer::Post.new(user) }
let(:comment) { Comment.new }
let(:article) { Article.new }
let(:article_tag) { ArticleTag.new }
Expand Down Expand Up @@ -188,7 +191,7 @@

describe "#permitted_attributes" do
it "checks policy for permitted attributes" do
params = ActionController::Parameters.new(
params = to_params(
post: {
title: "Hello",
votes: 5,
Expand All @@ -206,7 +209,8 @@
end

it "checks policy for permitted attributes for record of a ActiveModel type" do
params = ActionController::Parameters.new(
customer_post = Customer::Post.new(user)
params = to_params(
customer_post: {
title: "Hello",
votes: 5,
Expand All @@ -224,11 +228,23 @@
"votes" => 5
)
end

it "goes through the policy cache" do
params = to_params(post: { title: "Hello" })
user = double
post = Post.new(user)
controller = Controller.new(user, "update", params)

expect do
expect(controller.permitted_attributes(post)).to be_truthy
expect(controller.permitted_attributes(post)).to be_truthy
end.to change { PostPolicy.instances }.by(1)
end
end

describe "#permitted_attributes_for_action" do
it "is checked if it is defined in the policy" do
params = ActionController::Parameters.new(
params = to_params(
post: {
title: "Hello",
body: "blah",
Expand All @@ -242,7 +258,7 @@
end

it "can be explicitly set" do
params = ActionController::Parameters.new(
params = to_params(
post: {
title: "Hello",
body: "blah",
Expand Down
23 changes: 23 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,31 @@
require "active_model/naming"
require "action_controller/metal/strong_parameters"

module InstanceTracking
module ClassMethods
def instances
@instances || 0
end

attr_writer :instances
end

def self.prepended(other)
other.extend(ClassMethods)
end

def initialize(*args, **kwargs, &block)
self.class.instances += 1
super(*args, **kwargs, &block)
end
end

class BasePolicy
prepend InstanceTracking

class BaseScope
prepend InstanceTracking

def initialize(user, scope)
@user = user
@scope = scope
Expand Down

0 comments on commit 4f41278

Please sign in to comment.