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