]> git.openstreetmap.org Git - rails.git/blob - test/functional/user_preference_controller_test.rb
Remove deprecated assert_blank and assert_present from tests
[rails.git] / test / functional / user_preference_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class UserPreferenceControllerTest < ActionController::TestCase
4   fixtures :users, :user_preferences
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("test@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     basic_authorization("test@openstreetmap.org", "test")
51
52     # try the read again
53     get :read
54     assert_response :success
55     assert_equal "text/xml", @response.content_type
56     assert_select "osm" do
57       assert_select "preferences", :count => 1 do
58         assert_select "preference", :count => 2
59         assert_select "preference[k=\"#{user_preferences(:a).k}\"][v=\"#{user_preferences(:a).v}\"]", :count => 1
60         assert_select "preference[k=\"#{user_preferences(:two).k}\"][v=\"#{user_preferences(:two).v}\"]", :count => 1
61       end
62     end
63   end
64
65   ##
66   # test read_one action
67   def test_read_one
68     # try a read without auth
69     get :read_one, :preference_key => "key"
70     assert_response :unauthorized, "should be authenticated"
71
72     # authenticate as a user with preferences
73     basic_authorization("test@openstreetmap.org", "test")
74
75     # try the read again
76     get :read_one, :preference_key => "key"
77     assert_response :success
78     assert_equal "text/plain", @response.content_type
79     assert_equal "value", @response.body
80
81     # try the read again for a non-existent key
82     get :read_one, :preference_key => "unknown_key"
83     assert_response :not_found
84   end
85
86   ##
87   # test update action
88   def test_update
89     # try a put without auth
90     assert_no_difference "UserPreference.count" do
91       content "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>"
92       put :update
93     end
94     assert_response :unauthorized, "should be authenticated"
95     assert_equal "value", UserPreference.find(1, "key").v
96     assert_equal "some_value", UserPreference.find(1, "some_key").v
97     assert_raises ActiveRecord::RecordNotFound do
98       UserPreference.find(1, "new_key")
99     end
100
101     # authenticate as a user with preferences
102     basic_authorization("test@openstreetmap.org", "test")
103
104     # try the put again
105     assert_no_difference "UserPreference.count" do
106       content "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>"
107       put :update
108     end
109     assert_response :success
110     assert_equal "text/plain", @response.content_type
111     assert_equal "", @response.body
112     assert_equal "new_value", UserPreference.find(1, "key").v
113     assert_equal "value", UserPreference.find(1, "new_key").v
114     assert_raises ActiveRecord::RecordNotFound do
115       UserPreference.find(1, "some_key")
116     end
117
118     # try a put with duplicate keys
119     assert_no_difference "UserPreference.count" do
120       content "<osm><preferences><preference k='key' v='value'/><preference k='key' v='newer_value'/></preferences></osm>"
121       put :update
122     end
123     assert_response :bad_request
124     assert_equal "text/plain", @response.content_type
125     assert_equal "Duplicate preferences with key key", @response.body
126     assert_equal "new_value", UserPreference.find(1, "key").v
127
128     # try a put with invalid content
129     assert_no_difference "UserPreference.count" do
130       content "nonsense"
131       put :update
132     end
133     assert_response :bad_request
134   end
135
136   ##
137   # test update_one action
138   def test_update_one
139     # try a put without auth
140     assert_no_difference "UserPreference.count" do
141       content "new_value"
142       put :update_one, :preference_key => "new_key"
143     end
144     assert_response :unauthorized, "should be authenticated"
145     assert_raises ActiveRecord::RecordNotFound do
146       UserPreference.find(1, "new_key")
147     end
148
149     # authenticate as a user with preferences
150     basic_authorization("test@openstreetmap.org", "test")
151
152     # try adding a new preference
153     assert_difference "UserPreference.count", 1 do
154       content "new_value"
155       put :update_one, :preference_key => "new_key"
156     end
157     assert_response :success
158     assert_equal "text/plain", @response.content_type
159     assert_equal "", @response.body
160     assert_equal "new_value", UserPreference.find(1, "new_key").v
161
162     # try changing the value of a preference
163     assert_no_difference "UserPreference.count" do
164       content "newer_value"
165       put :update_one, :preference_key => "new_key"
166     end
167     assert_response :success
168     assert_equal "text/plain", @response.content_type
169     assert_equal "", @response.body
170     assert_equal "newer_value", UserPreference.find(1, "new_key").v
171   end
172
173   ##
174   # test delete_one action
175   def test_delete_one
176     # try a delete without auth
177     assert_no_difference "UserPreference.count" do
178       delete :delete_one, :preference_key => "key"
179     end
180     assert_response :unauthorized, "should be authenticated"
181     assert_equal "value", UserPreference.find(1, "key").v
182
183     # authenticate as a user with preferences
184     basic_authorization("test@openstreetmap.org", "test")
185
186     # try the delete again
187     assert_difference "UserPreference.count", -1 do
188       get :delete_one, :preference_key => "key"
189     end
190     assert_response :success
191     assert_equal "text/plain", @response.content_type
192     assert_equal "", @response.body
193     assert_raises ActiveRecord::RecordNotFound do
194       UserPreference.find(1, "key")
195     end
196
197     # try the delete again for the same key
198     assert_no_difference "UserPreference.count" do
199       get :delete_one, :preference_key => "key"
200     end
201     assert_response :not_found
202     assert_raises ActiveRecord::RecordNotFound do
203       UserPreference.find(1, "key")
204     end
205   end
206 end