3 class PreferencesControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/preferences", :method => :get },
9 { :controller => "preferences", :action => "show" }
13 { :path => "/preferences", :method => :put },
14 { :controller => "preferences", :action => "update" }
18 def test_update_preferred_editor
19 user = create(:user, :languages => [])
20 user.preferences.create(:k => "site.color_scheme", :v => "light")
21 user.preferences.create(:k => "map.color_scheme", :v => "light")
24 # Changing to a invalid editor should fail
25 put preferences_path, :params => { :user => { :preferred_editor => "unknown", :languages => [] } }
26 assert_response :success
28 assert_select ".alert-success", false
29 assert_select ".alert-danger", true
31 assert_nil user.preferred_editor
32 assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v
33 assert_equal "light", user.preferences.find_by(:k => "map.color_scheme")&.v
35 # Changing to a valid editor should work
36 put preferences_path, :params => { :user => { :preferred_editor => "id", :languages => [] } }
37 assert_redirected_to preferences_path
40 assert_select ".alert-success", /^Preferences updated/
42 assert_equal "id", user.preferred_editor
43 assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v
44 assert_equal "light", user.preferences.find_by(:k => "map.color_scheme")&.v
46 # Changing to the default editor should work
47 put preferences_path, :params => { :user => { :preferred_editor => "default", :languages => [] } }
48 assert_redirected_to preferences_path
51 assert_select ".alert-success", /^Preferences updated/
53 assert_nil user.preferred_editor
54 assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v
55 assert_equal "light", user.preferences.find_by(:k => "map.color_scheme")&.v
58 def test_update_preferred_site_color_scheme
59 user = create(:user, :languages => [])
61 assert_nil user.preferences.find_by(:k => "site.color_scheme")
63 # Changing when previously not defined
64 put preferences_path, :params => { :user => user.attributes, :site_color_scheme => "light" }
65 assert_redirected_to preferences_path
68 assert_select ".alert-success", /^Preferences updated/
69 assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v
71 # Changing when previously defined
72 put preferences_path, :params => { :user => user.attributes, :site_color_scheme => "auto" }
73 assert_redirected_to preferences_path
76 assert_select ".alert-success", /^Preferences updated/
77 assert_equal "auto", user.preferences.find_by(:k => "site.color_scheme")&.v
80 def test_update_preferred_map_color_scheme
81 user = create(:user, :languages => [])
83 assert_nil user.preferences.find_by(:k => "map.color_scheme")
85 # Changing when previously not defined
86 put preferences_path, :params => { :user => user.attributes, :map_color_scheme => "light" }
87 assert_redirected_to preferences_path
90 assert_select ".alert-success", /^Preferences updated/
91 assert_equal "light", user.preferences.find_by(:k => "map.color_scheme")&.v
93 # Changing when previously defined
94 put preferences_path, :params => { :user => user.attributes, :map_color_scheme => "auto" }
95 assert_redirected_to preferences_path
98 assert_select ".alert-success", /^Preferences updated/
99 assert_equal "auto", user.preferences.find_by(:k => "map.color_scheme")&.v