]> git.openstreetmap.org Git - rails.git/blob - test/controllers/user_preference_controller_test.rb
Merge remote-tracking branch 'upstream/pull/1603'
[rails.git] / test / controllers / user_preference_controller_test.rb
1 require "test_helper"
2
3 class UserPreferenceControllerTest < ActionController::TestCase
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/api/0.6/user/preferences", :method => :get },
9       { :controller => "user_preference", :action => "read" }
10     )
11     assert_routing(
12       { :path => "/api/0.6/user/preferences", :method => :put },
13       { :controller => "user_preference", :action => "update" }
14     )
15     assert_routing(
16       { :path => "/api/0.6/user/preferences/key", :method => :get },
17       { :controller => "user_preference", :action => "read_one", :preference_key => "key" }
18     )
19     assert_routing(
20       { :path => "/api/0.6/user/preferences/key", :method => :put },
21       { :controller => "user_preference", :action => "update_one", :preference_key => "key" }
22     )
23     assert_routing(
24       { :path => "/api/0.6/user/preferences/key", :method => :delete },
25       { :controller => "user_preference", :action => "delete_one", :preference_key => "key" }
26     )
27   end
28
29   ##
30   # test read action
31   def test_read
32     # first try without auth
33     get :read
34     assert_response :unauthorized, "should be authenticated"
35
36     # authenticate as a user with no preferences
37     basic_authorization create(:user).email, "test"
38
39     # try the read again
40     get :read
41     assert_select "osm" do
42       assert_select "preferences", :count => 1 do
43         assert_select "preference", :count => 0
44       end
45     end
46
47     # authenticate as a user with preferences
48     user = create(:user)
49     user_preference = create(:user_preference, :user => user)
50     user_preference2 = create(:user_preference, :user => user)
51     basic_authorization user.email, "test"
52
53     # try the read again
54     get :read
55     assert_response :success
56     assert_equal "application/xml", @response.content_type
57     assert_select "osm" do
58       assert_select "preferences", :count => 1 do
59         assert_select "preference", :count => 2
60         assert_select "preference[k=\"#{user_preference.k}\"][v=\"#{user_preference.v}\"]", :count => 1
61         assert_select "preference[k=\"#{user_preference2.k}\"][v=\"#{user_preference2.v}\"]", :count => 1
62       end
63     end
64   end
65
66   ##
67   # test read_one action
68   def test_read_one
69     user = create(:user)
70     create(:user_preference, :user => user, :k => "key", :v => "value")
71
72     # try a read without auth
73     get :read_one, :params => { :preference_key => "key" }
74     assert_response :unauthorized, "should be authenticated"
75
76     # authenticate as a user with preferences
77     basic_authorization user.email, "test"
78
79     # try the read again
80     get :read_one, :params => { :preference_key => "key" }
81     assert_response :success
82     assert_equal "text/plain", @response.content_type
83     assert_equal "value", @response.body
84
85     # try the read again for a non-existent key
86     get :read_one, :params => { :preference_key => "unknown_key" }
87     assert_response :not_found
88   end
89
90   ##
91   # test update action
92   def test_update
93     user = create(:user)
94     create(:user_preference, :user => user, :k => "key", :v => "value")
95     create(:user_preference, :user => user, :k => "some_key", :v => "some_value")
96
97     # try a put without auth
98     assert_no_difference "UserPreference.count" do
99       content "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>"
100       put :update
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       content "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>"
115       put :update
116     end
117     assert_response :success
118     assert_equal "text/plain", @response.content_type
119     assert_equal "", @response.body
120     assert_equal "new_value", UserPreference.find([user.id, "key"]).v
121     assert_equal "value", UserPreference.find([user.id, "new_key"]).v
122     assert_raises ActiveRecord::RecordNotFound do
123       UserPreference.find([user.id, "some_key"])
124     end
125
126     # try a put with duplicate keys
127     assert_no_difference "UserPreference.count" do
128       content "<osm><preferences><preference k='key' v='value'/><preference k='key' v='newer_value'/></preferences></osm>"
129       put :update
130     end
131     assert_response :bad_request
132     assert_equal "text/plain", @response.content_type
133     assert_equal "Duplicate preferences with key key", @response.body
134     assert_equal "new_value", UserPreference.find([user.id, "key"]).v
135
136     # try a put with invalid content
137     assert_no_difference "UserPreference.count" do
138       content "nonsense"
139       put :update
140     end
141     assert_response :bad_request
142   end
143
144   ##
145   # test update_one action
146   def test_update_one
147     user = create(:user)
148     create(:user_preference, :user => user)
149
150     # try a put without auth
151     assert_no_difference "UserPreference.count" do
152       content "new_value"
153       put :update_one, :params => { :preference_key => "new_key" }
154     end
155     assert_response :unauthorized, "should be authenticated"
156     assert_raises ActiveRecord::RecordNotFound do
157       UserPreference.find([user.id, "new_key"])
158     end
159
160     # authenticate as a user with preferences
161     basic_authorization user.email, "test"
162
163     # try adding a new preference
164     assert_difference "UserPreference.count", 1 do
165       content "new_value"
166       put :update_one, :params => { :preference_key => "new_key" }
167     end
168     assert_response :success
169     assert_equal "text/plain", @response.content_type
170     assert_equal "", @response.body
171     assert_equal "new_value", UserPreference.find([user.id, "new_key"]).v
172
173     # try changing the value of a preference
174     assert_no_difference "UserPreference.count" do
175       content "newer_value"
176       put :update_one, :params => { :preference_key => "new_key" }
177     end
178     assert_response :success
179     assert_equal "text/plain", @response.content_type
180     assert_equal "", @response.body
181     assert_equal "newer_value", UserPreference.find([user.id, "new_key"]).v
182   end
183
184   ##
185   # test delete_one action
186   def test_delete_one
187     user = create(:user)
188     create(:user_preference, :user => user, :k => "key", :v => "value")
189
190     # try a delete without auth
191     assert_no_difference "UserPreference.count" do
192       delete :delete_one, :params => { :preference_key => "key" }
193     end
194     assert_response :unauthorized, "should be authenticated"
195     assert_equal "value", UserPreference.find([user.id, "key"]).v
196
197     # authenticate as a user with preferences
198     basic_authorization user.email, "test"
199
200     # try the delete again
201     assert_difference "UserPreference.count", -1 do
202       get :delete_one, :params => { :preference_key => "key" }
203     end
204     assert_response :success
205     assert_equal "text/plain", @response.content_type
206     assert_equal "", @response.body
207     assert_raises ActiveRecord::RecordNotFound do
208       UserPreference.find([user.id, "key"])
209     end
210
211     # try the delete again for the same key
212     assert_no_difference "UserPreference.count" do
213       get :delete_one, :params => { :preference_key => "key" }
214     end
215     assert_response :not_found
216     assert_raises ActiveRecord::RecordNotFound do
217       UserPreference.find([user.id, "key"])
218     end
219   end
220 end