]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/user_preferences_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / api / user_preferences_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Api
6   class UserPreferencesControllerTest < ActionDispatch::IntegrationTest
7     ##
8     # test all routes which lead to this controller
9     def test_routes
10       assert_routing(
11         { :path => "/api/0.6/user/preferences", :method => :get },
12         { :controller => "api/user_preferences", :action => "index" }
13       )
14       assert_routing(
15         { :path => "/api/0.6/user/preferences.json", :method => :get },
16         { :controller => "api/user_preferences", :action => "index", :format => "json" }
17       )
18       assert_routing(
19         { :path => "/api/0.6/user/preferences", :method => :put },
20         { :controller => "api/user_preferences", :action => "update_all" }
21       )
22       assert_routing(
23         { :path => "/api/0.6/user/preferences/key", :method => :get },
24         { :controller => "api/user_preferences", :action => "show", :preference_key => "key" }
25       )
26       assert_routing(
27         { :path => "/api/0.6/user/preferences/key", :method => :put },
28         { :controller => "api/user_preferences", :action => "update", :preference_key => "key" }
29       )
30       assert_routing(
31         { :path => "/api/0.6/user/preferences/key", :method => :delete },
32         { :controller => "api/user_preferences", :action => "destroy", :preference_key => "key" }
33       )
34     end
35
36     ##
37     # test showing all preferences
38     def test_index
39       # first try without auth
40       get api_user_preferences_path
41       assert_response :unauthorized, "should be authenticated"
42
43       # authenticate as a user with no preferences
44       auth_header = bearer_authorization_header
45
46       # try the read again
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
51         end
52       end
53
54       # authenticate as a user with preferences
55       user = create(:user)
56       user_preference = create(:user_preference, :user => user)
57       user_preference2 = create(:user_preference, :user => user)
58       auth_header = bearer_authorization_header(user)
59
60       # try the read again
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
69         end
70       end
71
72       # Test json
73       get api_user_preferences_path(:format => "json"), :headers => auth_header
74       assert_response :success
75       assert_equal "application/json", @response.media_type
76
77       js = ActiveSupport::JSON.decode(@response.body)
78       assert_not_nil js
79       assert_equal 2, js["preferences"].count
80       assert_equal user_preference.v, js["preferences"][user_preference.k]
81     end
82
83     ##
84     # test showing one preference
85     def test_show
86       user = create(:user)
87       create(:user_preference, :user => user, :k => "key", :v => "value")
88
89       # try a read without auth
90       get api_user_preference_path(:preference_key => "key")
91       assert_response :unauthorized, "should be authenticated"
92
93       # authenticate as a user with preferences
94       auth_header = bearer_authorization_header(user)
95
96       # try the read again
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
101
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
105     end
106
107     ##
108     # test bulk update action
109     def test_update_all
110       user = create(:user)
111       create(:user_preference, :user => user, :k => "key", :v => "value")
112       create(:user_preference, :user => user, :k => "some_key", :v => "some_value")
113
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>"
117       end
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"])
123       end
124
125       # authenticate as a user with preferences
126       auth_header = bearer_authorization_header(user)
127
128       # try the put again
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
131       end
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"])
139       end
140
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
144       end
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
149
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
153       end
154       assert_response :bad_request
155
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
159       end
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"])
167       end
168     end
169
170     ##
171     # test update action
172     def test_update
173       user = create(:user)
174       create(:user_preference, :user => user)
175
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"
179       end
180       assert_response :unauthorized, "should be authenticated"
181       assert_raises ActiveRecord::RecordNotFound do
182         UserPreference.find([user.id, "new_key"])
183       end
184
185       # authenticate as a user with preferences
186       auth_header = bearer_authorization_header(user)
187
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
191       end
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
196
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
200       end
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
205
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
209       end
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
214     end
215
216     ##
217     # test destroy action
218     def test_destroy
219       user = create(:user)
220       create(:user_preference, :user => user, :k => "key", :v => "value")
221
222       # try a delete without auth
223       assert_no_difference "UserPreference.count" do
224         delete api_user_preference_path(:preference_key => "key")
225       end
226       assert_response :unauthorized, "should be authenticated"
227       assert_equal "value", UserPreference.find([user.id, "key"]).v
228
229       # authenticate as a user with preferences
230       auth_header = bearer_authorization_header(user)
231
232       # try the delete again
233       assert_difference "UserPreference.count", -1 do
234         delete api_user_preference_path(:preference_key => "key"), :headers => auth_header
235       end
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"])
241       end
242
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
246       end
247       assert_response :not_found
248       assert_raises ActiveRecord::RecordNotFound do
249         UserPreference.find([user.id, "key"])
250       end
251     end
252
253     # Ensure that a valid access token with correct capabilities can be used to
254     # read preferences
255     def test_show_using_token
256       user = create(:user)
257       auth_header = bearer_authorization_header(user, :scopes => %w[read_prefs])
258       create(:user_preference, :user => user, :k => "key", :v => "value")
259
260       get api_user_preference_path(:preference_key => "key"), :headers => auth_header
261       assert_response :success
262     end
263
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
266     # by other methods.
267     def test_show_using_token_fail
268       user = create(:user)
269       auth_header = bearer_authorization_header(user, :scopes => %w[])
270       create(:user_preference, :user => user, :k => "key", :v => "value")
271
272       get api_user_preference_path(:preference_key => "key"), :headers => auth_header
273       assert_response :forbidden
274     end
275   end
276 end