]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/user_preferences_controller_test.rb
Merge remote-tracking branch 'upstream/pull/3710'
[rails.git] / test / controllers / api / user_preferences_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class UserPreferencesControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/user/preferences", :method => :get },
10         { :controller => "api/user_preferences", :action => "index" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/user/preferences.json", :method => :get },
14         { :controller => "api/user_preferences", :action => "index", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/user/preferences", :method => :put },
18         { :controller => "api/user_preferences", :action => "update_all" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/user/preferences/key", :method => :get },
22         { :controller => "api/user_preferences", :action => "show", :preference_key => "key" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/user/preferences/key", :method => :put },
26         { :controller => "api/user_preferences", :action => "update", :preference_key => "key" }
27       )
28       assert_routing(
29         { :path => "/api/0.6/user/preferences/key", :method => :delete },
30         { :controller => "api/user_preferences", :action => "destroy", :preference_key => "key" }
31       )
32     end
33
34     ##
35     # test showing all preferences
36     def test_index
37       # first try without auth
38       get user_preferences_path
39       assert_response :unauthorized, "should be authenticated"
40
41       # authenticate as a user with no preferences
42       auth_header = basic_authorization_header create(:user).email, "test"
43
44       # try the read again
45       get user_preferences_path, :headers => auth_header
46       assert_select "osm" do
47         assert_select "preferences", :count => 1 do
48           assert_select "preference", :count => 0
49         end
50       end
51
52       # authenticate as a user with preferences
53       user = create(:user)
54       user_preference = create(:user_preference, :user => user)
55       user_preference2 = create(:user_preference, :user => user)
56       auth_header = basic_authorization_header user.email, "test"
57
58       # try the read again
59       get user_preferences_path, :headers => auth_header
60       assert_response :success
61       assert_equal "application/xml", @response.media_type
62       assert_select "osm" do
63         assert_select "preferences", :count => 1 do
64           assert_select "preference", :count => 2
65           assert_select "preference[k=\"#{user_preference.k}\"][v=\"#{user_preference.v}\"]", :count => 1
66           assert_select "preference[k=\"#{user_preference2.k}\"][v=\"#{user_preference2.v}\"]", :count => 1
67         end
68       end
69
70       # Test json
71       get user_preferences_path(:format => "json"), :headers => auth_header
72       assert_response :success
73       assert_equal "application/json", @response.media_type
74
75       js = ActiveSupport::JSON.decode(@response.body)
76       assert_not_nil js
77       assert_equal 2, js["preferences"].count
78       assert_equal user_preference.v, js["preferences"][user_preference.k]
79     end
80
81     ##
82     # test showing one preference
83     def test_show
84       user = create(:user)
85       create(:user_preference, :user => user, :k => "key", :v => "value")
86
87       # try a read without auth
88       get user_preference_path(:preference_key => "key")
89       assert_response :unauthorized, "should be authenticated"
90
91       # authenticate as a user with preferences
92       auth_header = basic_authorization_header user.email, "test"
93
94       # try the read again
95       get user_preference_path(:preference_key => "key"), :headers => auth_header
96       assert_response :success
97       assert_equal "text/plain", @response.media_type
98       assert_equal "value", @response.body
99
100       # try the read again for a non-existent key
101       get user_preference_path(:preference_key => "unknown_key"), :headers => auth_header
102       assert_response :not_found
103     end
104
105     ##
106     # test bulk update action
107     def test_update_all
108       user = create(:user)
109       create(:user_preference, :user => user, :k => "key", :v => "value")
110       create(:user_preference, :user => user, :k => "some_key", :v => "some_value")
111
112       # try a put without auth
113       assert_no_difference "UserPreference.count" do
114         put user_preferences_path, :params => "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>"
115       end
116       assert_response :unauthorized, "should be authenticated"
117       assert_equal "value", UserPreference.find([user.id, "key"]).v
118       assert_equal "some_value", UserPreference.find([user.id, "some_key"]).v
119       assert_raises ActiveRecord::RecordNotFound do
120         UserPreference.find([user.id, "new_key"])
121       end
122
123       # authenticate as a user with preferences
124       auth_header = basic_authorization_header user.email, "test"
125
126       # try the put again
127       assert_no_difference "UserPreference.count" do
128         put user_preferences_path, :params => "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>", :headers => auth_header
129       end
130       assert_response :success
131       assert_equal "text/plain", @response.media_type
132       assert_equal "", @response.body
133       assert_equal "new_value", UserPreference.find([user.id, "key"]).v
134       assert_equal "value", UserPreference.find([user.id, "new_key"]).v
135       assert_raises ActiveRecord::RecordNotFound do
136         UserPreference.find([user.id, "some_key"])
137       end
138
139       # try a put with duplicate keys
140       assert_no_difference "UserPreference.count" do
141         put user_preferences_path, :params => "<osm><preferences><preference k='key' v='value'/><preference k='key' v='newer_value'/></preferences></osm>", :headers => auth_header
142       end
143       assert_response :bad_request
144       assert_equal "text/plain", @response.media_type
145       assert_equal "Duplicate preferences with key key", @response.body
146       assert_equal "new_value", UserPreference.find([user.id, "key"]).v
147
148       # try a put with invalid content
149       assert_no_difference "UserPreference.count" do
150         put user_preferences_path, :params => "nonsense", :headers => auth_header
151       end
152       assert_response :bad_request
153     end
154
155     ##
156     # test update action
157     def test_update
158       user = create(:user)
159       create(:user_preference, :user => user)
160
161       # try a put without auth
162       assert_no_difference "UserPreference.count" do
163         put user_preference_path(:preference_key => "new_key"), :params => "new_value"
164       end
165       assert_response :unauthorized, "should be authenticated"
166       assert_raises ActiveRecord::RecordNotFound do
167         UserPreference.find([user.id, "new_key"])
168       end
169
170       # authenticate as a user with preferences
171       auth_header = basic_authorization_header user.email, "test"
172
173       # try adding a new preference
174       assert_difference "UserPreference.count", 1 do
175         put user_preference_path(:preference_key => "new_key"), :params => "new_value", :headers => auth_header
176       end
177       assert_response :success
178       assert_equal "text/plain", @response.media_type
179       assert_equal "", @response.body
180       assert_equal "new_value", UserPreference.find([user.id, "new_key"]).v
181
182       # try changing the value of a preference
183       assert_no_difference "UserPreference.count" do
184         put user_preference_path(:preference_key => "new_key"), :params => "newer_value", :headers => auth_header
185       end
186       assert_response :success
187       assert_equal "text/plain", @response.media_type
188       assert_equal "", @response.body
189       assert_equal "newer_value", UserPreference.find([user.id, "new_key"]).v
190     end
191
192     ##
193     # test destroy action
194     def test_destroy
195       user = create(:user)
196       create(:user_preference, :user => user, :k => "key", :v => "value")
197
198       # try a delete without auth
199       assert_no_difference "UserPreference.count" do
200         delete user_preference_path(:preference_key => "key")
201       end
202       assert_response :unauthorized, "should be authenticated"
203       assert_equal "value", UserPreference.find([user.id, "key"]).v
204
205       # authenticate as a user with preferences
206       auth_header = basic_authorization_header user.email, "test"
207
208       # try the delete again
209       assert_difference "UserPreference.count", -1 do
210         delete user_preference_path(:preference_key => "key"), :headers => auth_header
211       end
212       assert_response :success
213       assert_equal "text/plain", @response.media_type
214       assert_equal "", @response.body
215       assert_raises ActiveRecord::RecordNotFound do
216         UserPreference.find([user.id, "key"])
217       end
218
219       # try the delete again for the same key
220       assert_no_difference "UserPreference.count" do
221         delete user_preference_path(:preference_key => "key"), :headers => auth_header
222       end
223       assert_response :not_found
224       assert_raises ActiveRecord::RecordNotFound do
225         UserPreference.find([user.id, "key"])
226       end
227     end
228
229     # Ensure that a valid access token with correct capabilities can be used to
230     # read preferences
231     def test_show_using_token
232       user = create(:user)
233       token = create(:access_token, :user => user, :allow_read_prefs => true)
234       create(:user_preference, :user => user, :k => "key", :v => "value")
235
236       signed_get user_preference_path(:preference_key => "key"), :oauth => { :token => token }
237       assert_response :success
238     end
239
240     # Ensure that a valid access token with incorrect capabilities can't be used
241     # to read preferences even, though the owner of that token could read them
242     # by other methods.
243     def test_show_using_token_fail
244       user = create(:user)
245       token = create(:access_token, :user => user, :allow_read_prefs => false)
246       create(:user_preference, :user => user, :k => "key", :v => "value")
247
248       signed_get user_preference_path(:preference_key => "key"), :oauth => { :token => token }
249       assert_response :forbidden
250     end
251   end
252 end