Skip to content

Commit baf0d08

Browse files
committed
added test for the :set_preference action of the users controller
1 parent 1c4a7c6 commit baf0d08

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

test/controllers/users_controller_test.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,28 @@ class UsersControllerTest < ActionController::TestCase
507507
assert(items.any? { |x| x.instance_of?(Flag) && x.id == declined_flag.id })
508508
end
509509

510+
test 'set_preference should correclty save valid preferences' do
511+
sign_in users(:standard_user)
512+
513+
pref_key = 'default_license'
514+
license = licenses(:cc_by_sa)
515+
516+
[nil, communities(:sample)].each do |community|
517+
try_save_preference(pref_key, license, community: community)
518+
519+
assert_response(:success)
520+
assert_valid_json_response
521+
522+
parsed_body = JSON.parse(response.body)
523+
assert_equal 'success', parsed_body['status']
524+
assert_not_nil parsed_body['preferences']
525+
526+
comm_key = community.present? ? 'community' : 'global'
527+
assert_not_nil parsed_body['preferences'][comm_key]
528+
assert_equal license.id.to_s, parsed_body['preferences'][comm_key][pref_key]
529+
end
530+
end
531+
510532
private
511533

512534
def create_other_user
@@ -516,4 +538,13 @@ def create_other_user
516538
other_user.community_users.create!(community: other_community)
517539
other_user
518540
end
541+
542+
def try_save_preference(name, value, community: nil)
543+
post :set_preference, params: {
544+
community: community,
545+
name: name,
546+
value: value,
547+
format: :json
548+
}
549+
end
519550
end

0 commit comments

Comments
 (0)