Verified Commit f9e90933 authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻
Browse files

add helper for actions in controller for policy tests

parent a74d9cc8
Pipeline #13897 canceled with stage
in 9 minutes and 56 seconds
......@@ -15,7 +15,6 @@ class ClientPolicy < ApplicationPolicy
alias_method :show?, :superadmin_or_department_manager_or_social_worker?
alias_method :edit?, :superadmin_or_record_owner?
alias_method :update?, :superadmin_or_record_owner?
alias_method :termination?, :superadmin_or_department_managers_record?
alias_method :set_terminated?, :superadmin_or_department_managers_record?
# suplementary policies
......
......@@ -2,32 +2,22 @@ require 'test_helper'
class ClientPolicyTest < PolicyAssertions::Test
test 'superadmin_can_use_all_actions' do
assert_permit(create(:user), Client,
'index?', 'search?', 'new?', 'create?', 'show?', 'edit?', 'update?', 'termination?',
'superadmin_privileges?', 'set_terminated?')
assert_permit(create(:user), Client, 'superadmin_privileges?', *actions_list.values)
end
test 'department_manager_has_limited_access' do
department_manager = create :department_manager, :with_clients
assert_permit(department_manager, department_manager.clients.first,
'edit?', 'update?', 'termination?', 'set_terminated?')
assert_permit(department_manager, Client,
'index?', 'search?', 'new?', 'create?', 'show?')
refute_permit(department_manager, create(:client),
'edit?', 'update?', 'termination?', 'set_terminated?')
refute_permit(department_manager, Client,
'superadmin_privileges?', 'set_terminated?')
manager = create :department_manager, :with_clients
assert_permit(manager, manager.clients.first, *actions_list(:edit, :update, :set_terminated))
assert_permit(manager, Client, *actions_list(:index, :search, :new, :create, :show))
refute_permit(manager, create(:client), *actions_list(:edit, :update, :set_terminated))
refute_permit(manager, Client, 'superadmin_privileges?', *actions_list(:set_terminated))
end
test 'social_worker_has_limited_access' do
social_worker = create :social_worker, :with_clients
assert_permit(social_worker, Client,
'index?', 'search?', 'new?', 'create?', 'show?')
assert_permit(social_worker, social_worker.clients.first,
'edit?', 'update?')
refute_permit(social_worker, create(:client),
'edit?', 'update?', 'set_terminated?')
refute_permit(social_worker, Client,
'termination?', 'superadmin_privileges?', 'set_terminated?')
assert_permit(social_worker, Client, *actions_list(:index, :search, :new, :create, :show))
assert_permit(social_worker, social_worker.clients.first, *actions_list(:edit, :update))
refute_permit(social_worker, create(:client), *actions_list(:edit, :update, :set_terminated))
refute_permit(social_worker, Client, 'superadmin_privileges?', *actions_list(:set_terminated))
end
end
......@@ -59,4 +59,21 @@ class ActiveSupport::TestCase
def assert_xls_row_empty(wb, row, cols = 8)
(1..cols).to_a.each { |column| assert_nil wb.cell(row, column) }
end
def controllers_action_list(controller = nil)
controller ||= self.class.name.remove('PolicyTest').underscore.pluralize
controller_routes = Rails.application.routes.routes.find_all do |route|
route.defaults[:controller] == controller.to_s
end
acts = controller_routes.map { |route| route.defaults[:action] }.uniq
acts.map { |action| [action.to_sym, "#{action}?"] }.to_h
end
def actions_list(*choices)
if choices.any?
controllers_action_list.values_at(*choices)
else
controllers_action_list
end
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment