2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-08-30 20:04:11 +00:00
|
|
|
RSpec.shared_context :general_shared_user_context do
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:user_as_np_admin) do
|
2018-08-30 20:04:11 +00:00
|
|
|
__create_admin(nonprofit)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:user_as_other_np_admin) do
|
2018-08-30 20:04:11 +00:00
|
|
|
__create_admin(other_nonprofit)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:user_as_np_associate) do
|
2018-08-30 20:04:11 +00:00
|
|
|
__create_associate(nonprofit)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:user_as_other_np_associate) do
|
2018-08-30 20:04:11 +00:00
|
|
|
__create_associate(other_nonprofit)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:unauth_user) do
|
2018-08-30 20:04:11 +00:00
|
|
|
force_create(:user)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:campaign_editor) do
|
2018-08-30 20:04:11 +00:00
|
|
|
__create(:campaign_editor, campaign)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:confirmed_user) do
|
2018-08-30 20:04:11 +00:00
|
|
|
force_create(:user, confirmed_at: Time.current)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:event_editor) do
|
|
|
|
__create(:event_editor, event)
|
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:super_admin) do
|
2018-08-30 20:04:11 +00:00
|
|
|
__create(:super_admin, other_nonprofit)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:user_with_profile) do
|
2018-08-30 20:04:11 +00:00
|
|
|
u = force_create(:user)
|
|
|
|
force_create(:profile, user: u)
|
|
|
|
u
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
|
|
|
let(:all_users) do
|
2019-07-30 21:29:24 +00:00
|
|
|
{ user_as_np_admin: user_as_np_admin,
|
|
|
|
user_as_other_np_admin: user_as_other_np_admin,
|
|
|
|
user_as_np_associate: user_as_np_associate,
|
|
|
|
user_as_other_np_associate: user_as_other_np_associate,
|
|
|
|
unauth_user: unauth_user,
|
|
|
|
campaign_editor: campaign_editor,
|
|
|
|
event_editor: event_editor,
|
|
|
|
super_admin: super_admin,
|
|
|
|
user_with_profile: user_with_profile }
|
2018-08-30 20:04:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:roles__open_to_all) do
|
|
|
|
[nil, :user_as_np_admin,
|
|
|
|
:user_as_other_np_admin,
|
|
|
|
:user_as_np_associate,
|
|
|
|
:user_as_other_np_associate,
|
|
|
|
:unauth_user,
|
|
|
|
:campaign_editor,
|
|
|
|
:event_editor,
|
|
|
|
:super_admin,
|
2019-07-30 21:29:24 +00:00
|
|
|
:user_with_profile]
|
2018-08-30 20:04:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:roles__open_to_np_associate) do
|
2019-07-30 21:29:24 +00:00
|
|
|
%i[user_as_np_admin
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
user_as_np_associate
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
super_admin]
|
2018-08-30 20:04:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:roles__open_to_campaign_editor) do
|
2019-07-30 21:29:24 +00:00
|
|
|
%i[user_as_np_admin
|
|
|
|
user_as_np_associate
|
|
|
|
campaign_editor
|
|
|
|
super_admin]
|
2018-08-30 20:04:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def __create(name, host)
|
|
|
|
u = force_create(:user)
|
2019-07-30 21:29:24 +00:00
|
|
|
force_create(:role, user: u, name: name, host: host)
|
2018-08-30 20:04:11 +00:00
|
|
|
u
|
|
|
|
end
|
|
|
|
|
|
|
|
def __create_admin(host)
|
|
|
|
u = force_create(:user)
|
2019-07-30 21:29:24 +00:00
|
|
|
force_create(:role, user: u, name: :nonprofit_admin, host: host)
|
2018-08-30 20:04:11 +00:00
|
|
|
u
|
|
|
|
end
|
|
|
|
|
|
|
|
def __create_associate(host)
|
|
|
|
u = force_create(:user)
|
2019-07-30 21:29:24 +00:00
|
|
|
force_create(:role, user: u, name: :nonprofit_associate, host: host)
|
2018-08-30 20:04:11 +00:00
|
|
|
u
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_authorization_tests(details, &block)
|
|
|
|
@method = details[:method]
|
|
|
|
@successful_users = details[:successful_users]
|
|
|
|
@action = details[:action]
|
2019-07-30 21:29:24 +00:00
|
|
|
@block_to_get_arguments_to_run = block || ->(_) {} # no-op
|
2018-08-30 20:04:11 +00:00
|
|
|
accept_test_for_nil = false
|
2019-07-30 21:29:24 +00:00
|
|
|
all_users.each do |k, v|
|
2018-08-30 20:04:11 +00:00
|
|
|
os = OpenStruct.new
|
|
|
|
os.key = k
|
|
|
|
os.value = v
|
|
|
|
|
|
|
|
if k.nil?
|
2019-07-30 21:29:24 +00:00
|
|
|
accept(user_to_signin: nil, method: @method, action: @action, args: @block_to_get_arguments_to_run.call(v))
|
2018-08-30 20:04:11 +00:00
|
|
|
accept_test_for_nil = true
|
|
|
|
end
|
|
|
|
if @successful_users.include? k
|
2019-07-30 21:29:24 +00:00
|
|
|
accept(user_to_signin: os, method: @method, action: @action, args: @block_to_get_arguments_to_run.call(v))
|
2018-08-30 20:04:11 +00:00
|
|
|
else
|
2019-07-30 21:29:24 +00:00
|
|
|
reject(user_to_signin: os, method: @method, action: @action, args: @block_to_get_arguments_to_run.call(v))
|
2018-08-30 20:04:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless accept_test_for_nil
|
2019-07-30 21:29:24 +00:00
|
|
|
reject(user_to_signin: nil, method: @method, action: @action, args: @block_to_get_arguments_to_run.call(nil))
|
2018-08-30 20:04:11 +00:00
|
|
|
end
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|