-
-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more test coverage for admin policies
- Loading branch information
1 parent
b5564c9
commit cd621ff
Showing
10 changed files
with
137 additions
and
0 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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 | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require "test_helper" | ||
|
||
class Admin::NilClassPolicyTest < AdminPolicyTestCase | ||
def policy!(api_key) | ||
Pundit.policy!(api_key, [:api, nil]) | ||
end | ||
|
||
should "inherit from Admin::ApplicationPolicy" do | ||
assert_operator Admin::NilClassPolicy, :<, Admin::ApplicationPolicy | ||
assert_operator Admin::NilClassPolicy::Scope, :<, Admin::ApplicationPolicy::Scope | ||
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
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