2019-07-30 21:29:24 +00:00
# frozen_string_literal: true
2018-08-30 20:04:11 +00:00
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
require 'controllers/support/general_shared_user_context'
RSpec . shared_context :api_shared_user_verification do
include_context :general_shared_user_context
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
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 sign_in ( user_to_signin )
2019-07-30 21:29:24 +00:00
post_via_redirect 'users/sign_in' , 'user[email]' = > user_to_signin . email , 'user[password]' = > user_to_signin . password , format : 'json'
2018-08-30 20:04:11 +00:00
end
def sign_out
send ( :get , 'users/sign_out' )
end
def send ( method , * args )
case method
when :get
2019-07-30 21:29:24 +00:00
xhr ( :get , * args )
2018-08-30 20:04:11 +00:00
when :post
2019-07-30 21:29:24 +00:00
xhr ( :post , * args )
2018-08-30 20:04:11 +00:00
when :delete
2019-07-30 21:29:24 +00:00
xhr ( :delete , * args )
2018-08-30 20:04:11 +00:00
when :put
2019-07-30 21:29:24 +00:00
xhr ( :put , * args )
2018-08-30 20:04:11 +00:00
end
end
def accept ( user_to_signin : , method : , action : , args : )
new_user = user_to_signin
2019-07-30 21:29:24 +00:00
if ! user_to_signin . nil? && user_to_signin . is_a? ( OpenStruct )
2018-08-30 20:04:11 +00:00
new_user = user_to_signin . value
end
sign_in new_user if new_user
# allows us to run the helpers but ignore what the controller action does
#
send ( method , action , args )
2019-07-30 21:29:24 +00:00
expect ( response . status ) . to eq ( 200 ) , " expcted success for user: #{ ( user_to_signin . is_a? ( OpenStruct ) ? user_to_signin . key . to_s + ':' : '' ) } #{ new_user & . attributes } "
2018-08-30 20:04:11 +00:00
sign_out
end
def reject ( user_to_signin : , method : , action : , args : )
new_user = user_to_signin
2019-07-30 21:29:24 +00:00
if ! user_to_signin . nil? && user_to_signin . is_a? ( OpenStruct )
2018-08-30 20:04:11 +00:00
new_user = user_to_signin . value
end
sign_in new_user if new_user
send ( method , action , args )
2019-07-30 21:29:24 +00:00
expect ( response . status ) . to eq ( 401 ) , " expected failure for user: #{ ( user_to_signin . is_a? ( OpenStruct ) ? user_to_signin . key . to_s + ':' : '' ) } #{ new_user & . attributes } "
2018-08-30 20:04:11 +00:00
sign_out
end
alias_method :redirects_to , :reject
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
end