3 class UserPreferencesControllerTest < ActionController::TestCase
5 # test all routes which lead to this controller
8 { :path => "/api/0.6/user/preferences", :method => :get },
9 { :controller => "user_preferences", :action => "read" }
12 { :path => "/api/0.6/user/preferences", :method => :put },
13 { :controller => "user_preferences", :action => "update" }
16 { :path => "/api/0.6/user/preferences/key", :method => :get },
17 { :controller => "user_preferences", :action => "read_one", :preference_key => "key" }
20 { :path => "/api/0.6/user/preferences/key", :method => :put },
21 { :controller => "user_preferences", :action => "update_one", :preference_key => "key" }
24 { :path => "/api/0.6/user/preferences/key", :method => :delete },
25 { :controller => "user_preferences", :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"
38 grant_oauth_token :allow_read_prefs
42 assert_select "osm" do
43 assert_select "preferences", :count => 1 do
44 assert_select "preference", :count => 0
48 # authenticate as a user with preferences
50 user_preference = create(:user_preference, :user => user)
51 user_preference2 = create(:user_preference, :user => user)
52 basic_authorization user.email, "test"
56 assert_response :success
57 assert_equal "application/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
71 create(:user_preference, :user => user, :k => "key", :v => "value")
73 # try a read without auth
74 get :read_one, :params => { :preference_key => "key" }
75 assert_response :unauthorized, "should be authenticated"
77 # authenticate as a user with preferences
78 basic_authorization user.email, "test"
79 grant_oauth_token :allow_read_prefs
82 get :read_one, :params => { :preference_key => "key" }
83 assert_response :success
84 assert_equal "text/plain", @response.content_type
85 assert_equal "value", @response.body
87 # try the read again for a non-existent key
88 get :read_one, :params => { :preference_key => "unknown_key" }
89 assert_response :not_found
96 create(:user_preference, :user => user, :k => "key", :v => "value")
97 create(:user_preference, :user => user, :k => "some_key", :v => "some_value")
99 # try a put without auth
100 assert_no_difference "UserPreference.count" do
101 content "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>"
104 assert_response :unauthorized, "should be authenticated"
105 assert_equal "value", UserPreference.find([user.id, "key"]).v
106 assert_equal "some_value", UserPreference.find([user.id, "some_key"]).v
107 assert_raises ActiveRecord::RecordNotFound do
108 UserPreference.find([user.id, "new_key"])
111 # authenticate as a user with preferences
112 basic_authorization user.email, "test"
113 grant_oauth_token :allow_write_prefs
116 assert_no_difference "UserPreference.count" do
117 content "<osm><preferences><preference k='key' v='new_value'/><preference k='new_key' v='value'/></preferences></osm>"
120 assert_response :success
121 assert_equal "text/plain", @response.content_type
122 assert_equal "", @response.body
123 assert_equal "new_value", UserPreference.find([user.id, "key"]).v
124 assert_equal "value", UserPreference.find([user.id, "new_key"]).v
125 assert_raises ActiveRecord::RecordNotFound do
126 UserPreference.find([user.id, "some_key"])
129 # try a put with duplicate keys
130 assert_no_difference "UserPreference.count" do
131 content "<osm><preferences><preference k='key' v='value'/><preference k='key' v='newer_value'/></preferences></osm>"
134 assert_response :bad_request
135 assert_equal "text/plain", @response.content_type
136 assert_equal "Duplicate preferences with key key", @response.body
137 assert_equal "new_value", UserPreference.find([user.id, "key"]).v
139 # try a put with invalid content
140 assert_no_difference "UserPreference.count" do
144 assert_response :bad_request
148 # test update_one action
151 create(:user_preference, :user => user)
153 # try a put without auth
154 assert_no_difference "UserPreference.count" do
156 put :update_one, :params => { :preference_key => "new_key" }
158 assert_response :unauthorized, "should be authenticated"
159 assert_raises ActiveRecord::RecordNotFound do
160 UserPreference.find([user.id, "new_key"])
163 # authenticate as a user with preferences
164 basic_authorization user.email, "test"
165 grant_oauth_token :allow_write_prefs
167 # try adding a new preference
168 assert_difference "UserPreference.count", 1 do
170 put :update_one, :params => { :preference_key => "new_key" }
172 assert_response :success
173 assert_equal "text/plain", @response.content_type
174 assert_equal "", @response.body
175 assert_equal "new_value", UserPreference.find([user.id, "new_key"]).v
177 # try changing the value of a preference
178 assert_no_difference "UserPreference.count" do
179 content "newer_value"
180 put :update_one, :params => { :preference_key => "new_key" }
182 assert_response :success
183 assert_equal "text/plain", @response.content_type
184 assert_equal "", @response.body
185 assert_equal "newer_value", UserPreference.find([user.id, "new_key"]).v
189 # test delete_one action
192 create(:user_preference, :user => user, :k => "key", :v => "value")
194 # try a delete without auth
195 assert_no_difference "UserPreference.count" do
196 delete :delete_one, :params => { :preference_key => "key" }
198 assert_response :unauthorized, "should be authenticated"
199 assert_equal "value", UserPreference.find([user.id, "key"]).v
201 # authenticate as a user with preferences
202 basic_authorization user.email, "test"
203 grant_oauth_token :allow_write_prefs
205 # try the delete again
206 assert_difference "UserPreference.count", -1 do
207 get :delete_one, :params => { :preference_key => "key" }
209 assert_response :success
210 assert_equal "text/plain", @response.content_type
211 assert_equal "", @response.body
212 assert_raises ActiveRecord::RecordNotFound do
213 UserPreference.find([user.id, "key"])
216 # try the delete again for the same key
217 assert_no_difference "UserPreference.count" do
218 get :delete_one, :params => { :preference_key => "key" }
220 assert_response :not_found
221 assert_raises ActiveRecord::RecordNotFound do
222 UserPreference.find([user.id, "key"])