Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for view "Helper" module to improve coverage #738

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions spec/helper_spec.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +220 to +225
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Sorry about coming in this late.

I don't think we need to make any private instance methods public at all? Also the comment refers to protected methods but this does private methods.

What about this?

Suggested change
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
def self.helper(mod)
include(mod)
end
include Pundit::Authorization


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(*)
Expand Down