-
-
Notifications
You must be signed in to change notification settings - Fork 927
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
Admin policy test coverage #5262
Open
martinemde
wants to merge
3
commits into
master
Choose a base branch
from
martinemde/increase-coverage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Admin::NilClassPolicy < Admin::ApplicationPolicy | ||
class Scope < Admin::ApplicationPolicy::Scope | ||
def resolve | ||
raise Pundit::NotDefinedError, "Cannot scope NilClass" | ||
end | ||
end | ||
|
||
# fallback to parent policy which rejects all actions | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require "test_helper" | ||
|
||
class Admin::ApplicationPolicyTest < AdminPolicyTestCase | ||
should "onle inherit from Admin::ApplicationPolicy in Admin:: namespace" do | ||
Admin.constants.each do |const| | ||
next if const == :ApplicationPolicy | ||
next unless const.to_s.end_with?("Policy") | ||
|
||
klass = Admin.const_get(const) | ||
|
||
assert_operator klass, :<, Admin::ApplicationPolicy, "#{const} does not inherit from Admin::ApplicationPolicy" | ||
assert_operator klass::Scope, :<, Admin::ApplicationPolicy::Scope, "#{const}::Scope does not inherit from Admin::ApplicationPolicy::Scope" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "test_helper" | ||
|
||
class Admin::AuditPolicyTest < AdminPolicyTestCase | ||
setup do | ||
@audit = create(:audit) | ||
@admin = create(:admin_github_user, :is_admin) | ||
@non_admin = create(:admin_github_user) | ||
end | ||
|
||
def test_scope | ||
assert_equal [@audit], policy_scope!( | ||
@admin, | ||
Audit | ||
).to_a | ||
|
||
assert_empty policy_scope!( | ||
@non_admin, | ||
Audit | ||
).to_a | ||
end | ||
|
||
def test_avo_show | ||
assert_authorizes @admin, @audit, :avo_show? | ||
|
||
refute_authorizes @non_admin, @audit, :avo_show? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "test_helper" | ||
|
||
class Admin::DependencyPolicyTest < AdminPolicyTestCase | ||
setup do | ||
@dependency = create(:dependency) | ||
@admin = create(:admin_github_user, :is_admin) | ||
@non_admin = create(:admin_github_user) | ||
end | ||
|
||
def test_scope | ||
assert_equal [@dependency], policy_scope!( | ||
@admin, | ||
Dependency | ||
).to_a | ||
end | ||
|
||
def test_avo_show | ||
assert_authorizes @admin, @dependency, :avo_show? | ||
|
||
refute_authorizes @non_admin, @dependency, :avo_show? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "test_helper" | ||
|
||
class Admin::IpAddressPolicyTest < AdminPolicyTestCase | ||
setup do | ||
@ip_address = create(:ip_address) | ||
@admin = create(:admin_github_user, :is_admin) | ||
@non_admin = create(:admin_github_user) | ||
end | ||
|
||
def test_associations | ||
assert_association @admin, @ip_address, :user_events, Admin::Events::UserEventPolicy | ||
assert_association @admin, @ip_address, :rubygem_events, Admin::Events::RubygemEventPolicy | ||
assert_association @admin, @ip_address, :organization_events, Admin::Events::OrganizationEventPolicy | ||
end | ||
|
||
def test_scope | ||
assert_equal [@ip_address], policy_scope!( | ||
@admin, | ||
IpAddress | ||
).to_a | ||
end | ||
|
||
def test_avo_index | ||
assert_authorizes @admin, @ip_address, :avo_index? | ||
|
||
refute_authorizes @non_admin, @ip_address, :avo_index? | ||
end | ||
|
||
def test_avo_show | ||
assert_authorizes @admin, @ip_address, :avo_show? | ||
|
||
refute_authorizes @non_admin, @ip_address, :avo_show? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "test_helper" | ||
|
||
class Admin::NilClassPolicyTest < AdminPolicyTestCase | ||
def policy!(api_key) | ||
Pundit.policy!(api_key, [:api, nil]) | ||
end | ||
|
||
context "::Scope.resolve" do | ||
should "raise" do | ||
assert_raises Pundit::NotDefinedError do | ||
Admin::NilClassPolicy::Scope.new(nil, nil).resolve | ||
end | ||
end | ||
end | ||
|
||
should "not authorize any avo action" do | ||
refute_authorizes nil, nil, :avo_index? | ||
refute_authorizes nil, nil, :avo_show? | ||
refute_authorizes nil, nil, :avo_create? | ||
refute_authorizes nil, nil, :avo_new? | ||
refute_authorizes nil, nil, :avo_update? | ||
refute_authorizes nil, nil, :avo_edit? | ||
refute_authorizes nil, nil, :avo_destroy? | ||
refute_authorizes nil, nil, :act_on? | ||
refute_authorizes nil, nil, :avo_search? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "test_helper" | ||
|
||
class Admin::VersionPolicyTest < AdminPolicyTestCase | ||
setup do | ||
@admin = FactoryBot.create(:admin_github_user, :is_admin) | ||
@non_admin = FactoryBot.create(:admin_github_user) | ||
@version = FactoryBot.create(:version, :yanked) | ||
end | ||
|
||
def test_scope | ||
assert_equal [@version], policy_scope!( | ||
@admin, | ||
Version | ||
).to_a | ||
assert_empty policy_scope!( | ||
@non_admin, | ||
Version | ||
).to_a | ||
end | ||
|
||
def test_avo_index | ||
assert_authorizes @admin, Version, :avo_index? | ||
|
||
refute_authorizes @non_admin, Version, :avo_index? | ||
end | ||
|
||
def test_avo_show | ||
assert_authorizes @admin, @version, :avo_show? | ||
|
||
refute_authorizes @non_admin, @version, :avo_show? | ||
end | ||
|
||
def test_act_on | ||
assert_authorizes @admin, @version, :act_on? | ||
|
||
refute_authorizes @non_admin, @version, :act_on? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intention here was to use the passed policy to assert that it refers to that policy, but I'm unclear on how that works.
Is it really getting passed the "object" of the association as the record or are we just looking up the same policy as self because it's not a different record? That would imply that record is not a RubyGem in the RubyGemPolicy when we plan on checking show_association?