]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/user_preferences_controller_test.rb
Rubocop fixes
[rails.git] / test / controllers / api / user_preferences_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class UserPreferencesControllerTest < ActionController::TestCase
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 => "read" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/user/preferences", :method => :put },
14         { :controller => "api/user_preferences", :action => "update" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/user/preferences/key", :method => :get },
18         { :controller => "api/user_preferences", :action => "read_one", :preference_key => "key" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/user/preferences/key", :method => :put },
22         { :controller => "api/user_preferences", :action => "update_one", :preference_key => "key" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/user/preferences/key", :method => :delete },
26         { :controller => "api/user_preferences", :action => "delete_one", :preference_key => "key" }
27       )
28     end
29
30     ##
31     # test read action
32     def test_read
33       # first try without auth
34       get :read
35       assert_response :unauthorized, "should be authenticated"
36
37       # authenticate as a user with no preferences
38       basic_authorization create(:user).email, "test"
39
40       # try the read again
41       get :read
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       basic_authorization user.email, "test"
53
54       # try the read again
55       get :read
56       assert_response :success
57       assert_equal "application/xml", @response.content_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     end
66
67     ##
68     # test read_one action
69     def test_read_one
70       user = create(:user)
71       create(:user_preference, :user => user, :k => "key", :v => "value")
72
73       # try a read without auth
74       get :read_one, :params => { :preference_key => "key" }
75       assert_response :unauthorized, "should be authenticated"
76
77       # authenticate as a user with preferences
78       basic_authorization user.email, "test"
79
80       # try the read again
81       get :read_one, :params => { :preference_key => "key" }
82       assert_response :success
83       assert_equal "text/plain", @response.content_type
84       assert_equal "value", @response.body
85
86       # try the read again for a non-existent key
87       get :read_one, :params => { :preference_key => "unknown_key" }
88       assert_response :not_found
89     end
90
91     ##
92     # test update action
93     def test_update
94       user = create(:user)
95       create(:user_preference, :user => user, :k => "key", :v => "value")
96       create(:user_preference, :user => user, :k => "some_key", :v => "some_value")
97
98       # try a put without auth
99       assert_no_difference "UserPreference.count" do
100         put :update, :body => "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>"
101       end
102       assert_response :unauthorized, "should be authenticated"
103       assert_equal "value", UserPreference.find([user.id, "key"]).v
104       assert_equal "some_value", UserPreference.find([user.id, "some_key"]).v
105       assert_raises ActiveRecord::RecordNotFound do
106         UserPreference.find([user.id, "new_key"])
107       end
108
109       # authenticate as a user with preferences
110       basic_authorization user.email, "test"
111
112       # try the put again
113       assert_no_difference "UserPreference.count" do
114         put :update, :body => "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>"
115       end
116       assert_response :success
117       assert_equal "text/plain", @response.content_type
118       assert_equal "", @response.body
119       assert_equal "new_value", UserPreference.find([user.id, "key"]).v
120       assert_equal "value", UserPreference.find([user.id, "new_key"]).v
121       assert_raises ActiveRecord::RecordNotFound do
122         UserPreference.find([user.id, "some_key"])
123       end
124
125       # try a put with duplicate keys
126       assert_no_difference "UserPreference.count" do
127         put :update, :body => "<osm><preferences><preference k='key' v='value'/><preference k='key' v='newer_value'/></preferences></osm>"
128       end
129       assert_response :bad_request
130       assert_equal "text/plain", @response.content_type
131       assert_equal "Duplicate preferences with key key", @response.body
132       assert_equal "new_value", UserPreference.find([user.id, "key"]).v
133
134       # try a put with invalid content
135       assert_no_difference "UserPreference.count" do
136         put :update, :body => "nonsense"
137       end
138       assert_response :bad_request
139     end
140
141     ##
142     # test update_one action
143     def test_update_one
144       user = create(:user)
145       create(:user_preference, :user => user)
146
147       # try a put without auth
148       assert_no_difference "UserPreference.count" do
149         put :update_one, :params => { :preference_key => "new_key" }, :body => "new_value"
150       end
151       assert_response :unauthorized, "should be authenticated"
152       assert_raises ActiveRecord::RecordNotFound do
153         UserPreference.find([user.id, "new_key"])
154       end
155
156       # authenticate as a user with preferences
157       basic_authorization user.email, "test"
158
159       # try adding a new preference
160       assert_difference "UserPreference.count", 1 do
161         put :update_one, :params => { :preference_key => "new_key" }, :body => "new_value"
162       end
163       assert_response :success
164       assert_equal "text/plain", @response.content_type
165       assert_equal "", @response.body
166       assert_equal "new_value", UserPreference.find([user.id, "new_key"]).v
167
168       # try changing the value of a preference
169       assert_no_difference "UserPreference.count" do
170         put :update_one, :params => { :preference_key => "new_key" }, :body => "newer_value"
171       end
172       assert_response :success
173       assert_equal "text/plain", @response.content_type
174       assert_equal "", @response.body
175       assert_equal "newer_value", UserPreference.find([user.id, "new_key"]).v
176     end
177
178     ##
179     # test delete_one action
180     def test_delete_one
181       user = create(:user)
182       create(:user_preference, :user => user, :k => "key", :v => "value")
183
184       # try a delete without auth
185       assert_no_difference "UserPreference.count" do
186         delete :delete_one, :params => { :preference_key => "key" }
187       end
188       assert_response :unauthorized, "should be authenticated"
189       assert_equal "value", UserPreference.find([user.id, "key"]).v
190
191       # authenticate as a user with preferences
192       basic_authorization user.email, "test"
193
194       # try the delete again
195       assert_difference "UserPreference.count", -1 do
196         get :delete_one, :params => { :preference_key => "key" }
197       end
198       assert_response :success
199       assert_equal "text/plain", @response.content_type
200       assert_equal "", @response.body
201       assert_raises ActiveRecord::RecordNotFound do
202         UserPreference.find([user.id, "key"])
203       end
204
205       # try the delete again for the same key
206       assert_no_difference "UserPreference.count" do
207         get :delete_one, :params => { :preference_key => "key" }
208       end
209       assert_response :not_found
210       assert_raises ActiveRecord::RecordNotFound do
211         UserPreference.find([user.id, "key"])
212       end
213     end
214
215     # Ensure that a valid access token with correct capabilities can be used to
216     # read preferences
217     def test_read_one_using_token
218       user = create(:user)
219       token = create(:access_token, :user => user, :allow_read_prefs => true)
220       create(:user_preference, :user => user, :k => "key", :v => "value")
221
222       # Hack together an oauth request - an alternative would be to sign the request properly
223       @request.env["oauth.version"] = 1
224       @request.env["oauth.strategies"] = [:token]
225       @request.env["oauth.token"] = token
226
227       get :read_one, :params => { :preference_key => "key" }
228       assert_response :success
229     end
230
231     # Ensure that a valid access token with incorrect capabilities can't be used
232     # to read preferences even, though the owner of that token could read them
233     # by other methods.
234     def test_read_one_using_token_fail
235       user = create(:user)
236       token = create(:access_token, :user => user, :allow_read_prefs => false)
237       create(:user_preference, :user => user, :k => "key", :v => "value")
238       @request.env["oauth.version"] = 1
239       @request.env["oauth.strategies"] = [:token]
240       @request.env["oauth.token"] = token
241
242       get :read_one, :params => { :preference_key => "key" }
243       assert_response :forbidden
244     end
245   end
246 end