3 class UserPreferenceControllerTest < ActionController::TestCase
7 # test all routes which lead to this controller
10 { :path => "/api/0.6/user/preferences", :method => :get },
11 { :controller => "user_preference", :action => "read" }
14 { :path => "/api/0.6/user/preferences", :method => :put },
15 { :controller => "user_preference", :action => "update" }
18 { :path => "/api/0.6/user/preferences/key", :method => :get },
19 { :controller => "user_preference", :action => "read_one", :preference_key => "key" }
22 { :path => "/api/0.6/user/preferences/key", :method => :put },
23 { :controller => "user_preference", :action => "update_one", :preference_key => "key" }
26 { :path => "/api/0.6/user/preferences/key", :method => :delete },
27 { :controller => "user_preference", :action => "delete_one", :preference_key => "key" }
34 # first try without auth
36 assert_response :unauthorized, "should be authenticated"
38 # authenticate as a user with no preferences
39 basic_authorization("moderator@example.com", "test")
43 assert_select "osm" do
44 assert_select "preferences", :count => 1 do
45 assert_select "preference", :count => 0
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")
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
68 # test read_one action
70 create(:user_preference, :user => users(:normal_user), :k => "key", :v => "value")
72 # try a read without auth
73 get :read_one, :preference_key => "key"
74 assert_response :unauthorized, "should be authenticated"
76 # authenticate as a user with preferences
77 basic_authorization("test@openstreetmap.org", "test")
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
85 # try the read again for a non-existent key
86 get :read_one, :preference_key => "unknown_key"
87 assert_response :not_found
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")
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>"
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"])
108 # authenticate as a user with preferences
109 basic_authorization("test@openstreetmap.org", "test")
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>"
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"])
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>"
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
135 # try a put with invalid content
136 assert_no_difference "UserPreference.count" do
140 assert_response :bad_request
144 # test update_one action
146 # try a put without auth
147 assert_no_difference "UserPreference.count" do
149 put :update_one, :preference_key => "new_key"
151 assert_response :unauthorized, "should be authenticated"
152 assert_raises ActiveRecord::RecordNotFound do
153 UserPreference.find([users(:normal_user).id, "new_key"])
156 # authenticate as a user with preferences
157 basic_authorization("test@openstreetmap.org", "test")
159 # try adding a new preference
160 assert_difference "UserPreference.count", 1 do
162 put :update_one, :preference_key => "new_key"
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
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"
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
181 # test delete_one action
183 create(:user_preference, :user => users(:normal_user), :k => "key", :v => "value")
185 # try a delete without auth
186 assert_no_difference "UserPreference.count" do
187 delete :delete_one, :preference_key => "key"
189 assert_response :unauthorized, "should be authenticated"
190 assert_equal "value", UserPreference.find([users(:normal_user).id, "key"]).v
192 # authenticate as a user with preferences
193 basic_authorization("test@openstreetmap.org", "test")
195 # try the delete again
196 assert_difference "UserPreference.count", -1 do
197 get :delete_one, :preference_key => "key"
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"])
206 # try the delete again for the same key
207 assert_no_difference "UserPreference.count" do
208 get :delete_one, :preference_key => "key"
210 assert_response :not_found
211 assert_raises ActiveRecord::RecordNotFound do
212 UserPreference.find([users(:normal_user).id, "key"])