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