diff --git a/spec/helper_spec.rb b/spec/helper_spec.rb new file mode 100644 index 00000000..ceebefa4 --- /dev/null +++ b/spec/helper_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Pundit::Helper do + let(:user) { double } + let(:post) { Post.new(user) } + let(:view) { PostsView.new(user, post) } + + describe ".policy_scope" do + it "returns an instantiated policy scope given a plain model class" do + expect(view.policy_scope(Post)).to eq :published + end + + it "raises an original error with a policy scope that contains argument error" do + expect { view.policy_scope(user, Post) }.to raise_error(ArgumentError) + end + + it "raises an exception if nil object given" do + expect { view.policy_scope(nil) }.to raise_error(Pundit::NotDefinedError, "Cannot scope NilClass") + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 192146f2..fdc9f84c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -216,6 +216,22 @@ def initialize(current_user, action_name, params) end end +class PostsView + include Pundit::Authorization + include Pundit::Helper + # Mark protected methods public so they may be called in test + # rubocop:disable Style/AccessModifierDeclarations + public(*Pundit::Authorization.private_instance_methods) + # rubocop:enable Style/AccessModifierDeclarations + + attr_reader :current_user, :posts + + def initialize(current_user, posts) + @current_user = current_user + @posts = posts + end +end + class NilClassPolicy < Struct.new(:user, :record) class Scope def initialize(*)