3 class UserPreferenceControllerTest < ActionController::TestCase
5 # test all routes which lead to this controller
8 { :path => "/api/0.6/user/preferences", :method => :get },
9 { :controller => "user_preference", :action => "read" }
12 { :path => "/api/0.6/user/preferences", :method => :put },
13 { :controller => "user_preference", :action => "update" }
16 { :path => "/api/0.6/user/preferences/key", :method => :get },
17 { :controller => "user_preference", :action => "read_one", :preference_key => "key" }
20 { :path => "/api/0.6/user/preferences/key", :method => :put },
21 { :controller => "user_preference", :action => "update_one", :preference_key => "key" }
24 { :path => "/api/0.6/user/preferences/key", :method => :delete },
25 { :controller => "user_preference", :action => "delete_one", :preference_key => "key" }
32 # first try without auth
34 assert_response :unauthorized, "should be authenticated"
36 # authenticate as a user with no preferences
37 basic_authorization(create(:user).email, "test")
41 assert_select "osm" do
42 assert_select "preferences", :count => 1 do
43 assert_select "preference", :count => 0
47 # authenticate as a user with preferences
49 user_preference = create(:user_preference, :user => user)
50 user_preference2 = create(:user_preference, :user => user)
51 basic_authorization(user.email, "test")
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
67 # test read_one action
70 create(:user_preference, :user => user, :k => "key", :v => "value")
72 # try a read without auth
73 get :read_one, :params => { :preference_key => "key" }
74 assert_response :unauthorized, "should be authenticated"
76 # authenticate as a user with preferences
77 basic_authorization(user.email, "test")
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
85 # try the read again for a non-existent key
86 get :read_one, :params => { :preference_key => "unknown_key" }
87 assert_response :not_found
94 create(:user_preference, :user => user, :k => "key", :v => "value")
95 create(:user_preference, :user => user, :k => "some_key", :v => "some_value")
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>"
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"])
109 # authenticate as a user with preferences
110 basic_authorization(user.email, "test")
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>"
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"])
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>"
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
136 # try a put with invalid content
137 assert_no_difference "UserPreference.count" do
141 assert_response :bad_request
145 # test update_one action
148 create(:user_preference, :user => user)
150 # try a put without auth
151 assert_no_difference "UserPreference.count" do
153 put :update_one, :params => { :preference_key => "new_key" }
155 assert_response :unauthorized, "should be authenticated"
156 assert_raises ActiveRecord::RecordNotFound do
157 UserPreference.find([user.id, "new_key"])
160 # authenticate as a user with preferences
161 basic_authorization(user.email, "test")
163 # try adding a new preference
164 assert_difference "UserPreference.count", 1 do
166 put :update_one, :params => { :preference_key => "new_key" }
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
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" }
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
185 # test delete_one action
188 create(:user_preference, :user => user, :k => "key", :v => "value")
190 # try a delete without auth
191 assert_no_difference "UserPreference.count" do
192 delete :delete_one, :params => { :preference_key => "key" }
194 assert_response :unauthorized, "should be authenticated"
195 assert_equal "value", UserPreference.find([user.id, "key"]).v
197 # authenticate as a user with preferences
198 basic_authorization(user.email, "test")
200 # try the delete again
201 assert_difference "UserPreference.count", -1 do
202 get :delete_one, :params => { :preference_key => "key" }
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"])
211 # try the delete again for the same key
212 assert_no_difference "UserPreference.count" do
213 get :delete_one, :params => { :preference_key => "key" }
215 assert_response :not_found
216 assert_raises ActiveRecord::RecordNotFound do
217 UserPreference.find([user.id, "key"])