1 require File.dirname(__FILE__) + '/../test_helper'
3 class UserPreferenceControllerTest < ActionController::TestCase
4 fixtures :users, :user_preferences
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("test@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 basic_authorization("test@openstreetmap.org", "test")
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
66 # test read_one action
68 # try a read without auth
69 get :read_one, :preference_key => "key"
70 assert_response :unauthorized, "should be authenticated"
72 # authenticate as a user with preferences
73 basic_authorization("test@openstreetmap.org", "test")
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
81 # try the read again for a non-existent key
82 get :read_one, :preference_key => "unknown_key"
83 assert_response :not_found
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>"
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")
101 # authenticate as a user with preferences
102 basic_authorization("test@openstreetmap.org", "test")
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>"
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")
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>"
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
128 # try a put with invalid content
129 assert_no_difference "UserPreference.count" do
133 assert_response :bad_request
137 # test update_one action
139 # try a put without auth
140 assert_no_difference "UserPreference.count" do
142 put :update_one, :preference_key => "new_key"
144 assert_response :unauthorized, "should be authenticated"
145 assert_raises ActiveRecord::RecordNotFound do
146 UserPreference.find(1, "new_key")
149 # authenticate as a user with preferences
150 basic_authorization("test@openstreetmap.org", "test")
152 # try adding a new preference
153 assert_difference "UserPreference.count", 1 do
155 put :update_one, :preference_key => "new_key"
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
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"
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
174 # test delete_one action
176 # try a delete without auth
177 assert_no_difference "UserPreference.count" do
178 delete :delete_one, :preference_key => "key"
180 assert_response :unauthorized, "should be authenticated"
181 assert_equal "value", UserPreference.find(1, "key").v
183 # authenticate as a user with preferences
184 basic_authorization("test@openstreetmap.org", "test")
186 # try the delete again
187 assert_difference "UserPreference.count", -1 do
188 get :delete_one, :preference_key => "key"
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")
197 # try the delete again for the same key
198 assert_no_difference "UserPreference.count" do
199 get :delete_one, :preference_key => "key"
201 assert_response :not_found
202 assert_raises ActiveRecord::RecordNotFound do
203 UserPreference.find(1, "key")