1 # frozen_string_literal: true
6 class UserPreferencesControllerTest < ActionDispatch::IntegrationTest
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/user/preferences", :method => :get },
12 { :controller => "api/user_preferences", :action => "index" }
15 { :path => "/api/0.6/user/preferences.json", :method => :get },
16 { :controller => "api/user_preferences", :action => "index", :format => "json" }
19 { :path => "/api/0.6/user/preferences", :method => :put },
20 { :controller => "api/user_preferences", :action => "update_all" }
23 { :path => "/api/0.6/user/preferences/key", :method => :get },
24 { :controller => "api/user_preferences", :action => "show", :preference_key => "key" }
27 { :path => "/api/0.6/user/preferences/key", :method => :put },
28 { :controller => "api/user_preferences", :action => "update", :preference_key => "key" }
31 { :path => "/api/0.6/user/preferences/key", :method => :delete },
32 { :controller => "api/user_preferences", :action => "destroy", :preference_key => "key" }
37 # test showing all preferences
39 # first try without auth
40 get api_user_preferences_path
41 assert_response :unauthorized, "should be authenticated"
43 # authenticate as a user with no preferences
44 auth_header = bearer_authorization_header
47 get api_user_preferences_path, :headers => auth_header
48 assert_select "osm" do
49 assert_select "preferences", :count => 1 do
50 assert_select "preference", :count => 0
54 # authenticate as a user with preferences
56 user_preference = create(:user_preference, :user => user)
57 user_preference2 = create(:user_preference, :user => user)
58 auth_header = bearer_authorization_header(user)
61 get api_user_preferences_path, :headers => auth_header
62 assert_response :success
63 assert_equal "application/xml", @response.media_type
64 assert_select "osm" do
65 assert_select "preferences", :count => 1 do
66 assert_select "preference", :count => 2
67 assert_select "preference[k=\"#{user_preference.k}\"][v=\"#{user_preference.v}\"]", :count => 1
68 assert_select "preference[k=\"#{user_preference2.k}\"][v=\"#{user_preference2.v}\"]", :count => 1
73 get api_user_preferences_path(:format => "json"), :headers => auth_header
74 assert_response :success
75 assert_equal "application/json", @response.media_type
77 js = ActiveSupport::JSON.decode(@response.body)
79 assert_equal 2, js["preferences"].count
80 assert_equal user_preference.v, js["preferences"][user_preference.k]
84 # test showing one preference
87 create(:user_preference, :user => user, :k => "key", :v => "value")
89 # try a read without auth
90 get api_user_preference_path(:preference_key => "key")
91 assert_response :unauthorized, "should be authenticated"
93 # authenticate as a user with preferences
94 auth_header = bearer_authorization_header(user)
97 get api_user_preference_path(:preference_key => "key"), :headers => auth_header
98 assert_response :success
99 assert_equal "text/plain", @response.media_type
100 assert_equal "value", @response.body
102 # try the read again for a non-existent key
103 get api_user_preference_path(:preference_key => "unknown_key"), :headers => auth_header
104 assert_response :not_found
108 # test bulk update action
111 create(:user_preference, :user => user, :k => "key", :v => "value")
112 create(:user_preference, :user => user, :k => "some_key", :v => "some_value")
114 # try a put without auth
115 assert_no_difference "UserPreference.count" do
116 put api_user_preferences_path, :params => "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>"
118 assert_response :unauthorized, "should be authenticated"
119 assert_equal "value", UserPreference.find([user.id, "key"]).v
120 assert_equal "some_value", UserPreference.find([user.id, "some_key"]).v
121 assert_raises ActiveRecord::RecordNotFound do
122 UserPreference.find([user.id, "new_key"])
125 # authenticate as a user with preferences
126 auth_header = bearer_authorization_header(user)
129 assert_no_difference "UserPreference.count" do
130 put api_user_preferences_path, :params => "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>", :headers => auth_header
132 assert_response :success
133 assert_equal "text/plain", @response.media_type
134 assert_equal "", @response.body
135 assert_equal "new_value", UserPreference.find([user.id, "key"]).v
136 assert_equal "value", UserPreference.find([user.id, "new_key"]).v
137 assert_raises ActiveRecord::RecordNotFound do
138 UserPreference.find([user.id, "some_key"])
141 # try a put with duplicate keys
142 assert_no_difference "UserPreference.count" do
143 put api_user_preferences_path, :params => "<osm><preferences><preference k='key' v='value'/><preference k='key' v='newer_value'/></preferences></osm>", :headers => auth_header
145 assert_response :bad_request
146 assert_equal "text/plain", @response.media_type
147 assert_equal "Duplicate preferences with key key", @response.body
148 assert_equal "new_value", UserPreference.find([user.id, "key"]).v
150 # try a put with invalid content
151 assert_no_difference "UserPreference.count" do
152 put api_user_preferences_path, :params => "nonsense", :headers => auth_header
154 assert_response :bad_request
156 # try a put with unicode characters
157 assert_no_difference "UserPreference.count" do
158 put api_user_preferences_path, :params => "<osm><preferences><preference k='kêy' v='néw_vâlué'/><preference k='nêw_kêy' v='vâlué'/></preferences></osm>", :headers => auth_header
160 assert_response :success
161 assert_equal "text/plain", @response.media_type
162 assert_equal "", @response.body
163 assert_equal "néw_vâlué", UserPreference.find([user.id, "kêy"]).v
164 assert_equal "vâlué", UserPreference.find([user.id, "nêw_kêy"]).v
165 assert_raises ActiveRecord::RecordNotFound do
166 UserPreference.find([user.id, "some_key"])
174 create(:user_preference, :user => user)
176 # try a put without auth
177 assert_no_difference "UserPreference.count" do
178 put api_user_preference_path(:preference_key => "new_key"), :params => "new_value"
180 assert_response :unauthorized, "should be authenticated"
181 assert_raises ActiveRecord::RecordNotFound do
182 UserPreference.find([user.id, "new_key"])
185 # authenticate as a user with preferences
186 auth_header = bearer_authorization_header(user)
188 # try adding a new preference
189 assert_difference "UserPreference.count", 1 do
190 put api_user_preference_path(:preference_key => "new_key"), :params => "new_value", :headers => auth_header
192 assert_response :success
193 assert_equal "text/plain", @response.media_type
194 assert_equal "", @response.body
195 assert_equal "new_value", UserPreference.find([user.id, "new_key"]).v
197 # try changing the value of a preference
198 assert_no_difference "UserPreference.count" do
199 put api_user_preference_path(:preference_key => "new_key"), :params => "newer_value", :headers => auth_header
201 assert_response :success
202 assert_equal "text/plain", @response.media_type
203 assert_equal "", @response.body
204 assert_equal "newer_value", UserPreference.find([user.id, "new_key"]).v
206 # try changing the value of a preference to include unicode characters
207 assert_difference "UserPreference.count", 1 do
208 put api_user_preference_path(:preference_key => "nêw_kêy"), :params => "néwèr_vâlué", :headers => auth_header
210 assert_response :success
211 assert_equal "text/plain", @response.media_type
212 assert_equal "", @response.body
213 assert_equal "néwèr_vâlué", UserPreference.find([user.id, "nêw_kêy"]).v
217 # test destroy action
220 create(:user_preference, :user => user, :k => "key", :v => "value")
222 # try a delete without auth
223 assert_no_difference "UserPreference.count" do
224 delete api_user_preference_path(:preference_key => "key")
226 assert_response :unauthorized, "should be authenticated"
227 assert_equal "value", UserPreference.find([user.id, "key"]).v
229 # authenticate as a user with preferences
230 auth_header = bearer_authorization_header(user)
232 # try the delete again
233 assert_difference "UserPreference.count", -1 do
234 delete api_user_preference_path(:preference_key => "key"), :headers => auth_header
236 assert_response :success
237 assert_equal "text/plain", @response.media_type
238 assert_equal "", @response.body
239 assert_raises ActiveRecord::RecordNotFound do
240 UserPreference.find([user.id, "key"])
243 # try the delete again for the same key
244 assert_no_difference "UserPreference.count" do
245 delete api_user_preference_path(:preference_key => "key"), :headers => auth_header
247 assert_response :not_found
248 assert_raises ActiveRecord::RecordNotFound do
249 UserPreference.find([user.id, "key"])
253 # Ensure that a valid access token with correct capabilities can be used to
255 def test_show_using_token
257 auth_header = bearer_authorization_header(user, :scopes => %w[read_prefs])
258 create(:user_preference, :user => user, :k => "key", :v => "value")
260 get api_user_preference_path(:preference_key => "key"), :headers => auth_header
261 assert_response :success
264 # Ensure that a valid access token with incorrect capabilities can't be used
265 # to read preferences even, though the owner of that token could read them
267 def test_show_using_token_fail
269 auth_header = bearer_authorization_header(user, :scopes => %w[])
270 create(:user_preference, :user => user, :k => "key", :v => "value")
272 get api_user_preference_path(:preference_key => "key"), :headers => auth_header
273 assert_response :forbidden