]> git.openstreetmap.org Git - rails.git/blob - test/controllers/user_preferences_controller_test.rb
Merge branch 'master' into cancancan
[rails.git] / test / controllers / user_preferences_controller_test.rb
1 require "test_helper"
2
3 class UserPreferencesControllerTest < ActionController::TestCase
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/api/0.6/user/preferences", :method => :get },
9       { :controller => "user_preferences", :action => "read" }
10     )
11     assert_routing(
12       { :path => "/api/0.6/user/preferences", :method => :put },
13       { :controller => "user_preferences", :action => "update" }
14     )
15     assert_routing(
16       { :path => "/api/0.6/user/preferences/key", :method => :get },
17       { :controller => "user_preferences", :action => "read_one", :preference_key => "key" }
18     )
19     assert_routing(
20       { :path => "/api/0.6/user/preferences/key", :method => :put },
21       { :controller => "user_preferences", :action => "update_one", :preference_key => "key" }
22     )
23     assert_routing(
24       { :path => "/api/0.6/user/preferences/key", :method => :delete },
25       { :controller => "user_preferences", :action => "delete_one", :preference_key => "key" }
26     )
27   end
28
29   ##
30   # test read action
31   def test_read
32     # first try without auth
33     get :read
34     assert_response :unauthorized, "should be authenticated"
35
36     # authenticate as a user with no preferences
37     basic_authorization create(:user).email, "test"
38     grant_oauth_token :allow_read_prefs
39
40     # try the read again
41     get :read
42     assert_select "osm" do
43       assert_select "preferences", :count => 1 do
44         assert_select "preference", :count => 0
45       end
46     end
47
48     # authenticate as a user with preferences
49     user = create(:user)
50     user_preference = create(:user_preference, :user => user)
51     user_preference2 = create(:user_preference, :user => user)
52     basic_authorization user.email, "test"
53
54     # try the read again
55     get :read
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
63       end
64     end
65   end
66
67   ##
68   # test read_one action
69   def test_read_one
70     user = create(:user)
71     create(:user_preference, :user => user, :k => "key", :v => "value")
72
73     # try a read without auth
74     get :read_one, :params => { :preference_key => "key" }
75     assert_response :unauthorized, "should be authenticated"
76
77     # authenticate as a user with preferences
78     basic_authorization user.email, "test"
79     grant_oauth_token :allow_read_prefs
80
81     # try the read again
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
86
87     # try the read again for a non-existent key
88     get :read_one, :params => { :preference_key => "unknown_key" }
89     assert_response :not_found
90   end
91
92   ##
93   # test update action
94   def test_update
95     user = create(:user)
96     create(:user_preference, :user => user, :k => "key", :v => "value")
97     create(:user_preference, :user => user, :k => "some_key", :v => "some_value")
98
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>"
102       put :update
103     end
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"])
109     end
110
111     # authenticate as a user with preferences
112     basic_authorization user.email, "test"
113     grant_oauth_token :allow_write_prefs
114
115     # try the put again
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>"
118       put :update
119     end
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"])
127     end
128
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>"
132       put :update
133     end
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
138
139     # try a put with invalid content
140     assert_no_difference "UserPreference.count" do
141       content "nonsense"
142       put :update
143     end
144     assert_response :bad_request
145   end
146
147   ##
148   # test update_one action
149   def test_update_one
150     user = create(:user)
151     create(:user_preference, :user => user)
152
153     # try a put without auth
154     assert_no_difference "UserPreference.count" do
155       content "new_value"
156       put :update_one, :params => { :preference_key => "new_key" }
157     end
158     assert_response :unauthorized, "should be authenticated"
159     assert_raises ActiveRecord::RecordNotFound do
160       UserPreference.find([user.id, "new_key"])
161     end
162
163     # authenticate as a user with preferences
164     basic_authorization user.email, "test"
165     grant_oauth_token :allow_write_prefs
166
167     # try adding a new preference
168     assert_difference "UserPreference.count", 1 do
169       content "new_value"
170       put :update_one, :params => { :preference_key => "new_key" }
171     end
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
176
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" }
181     end
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
186   end
187
188   ##
189   # test delete_one action
190   def test_delete_one
191     user = create(:user)
192     create(:user_preference, :user => user, :k => "key", :v => "value")
193
194     # try a delete without auth
195     assert_no_difference "UserPreference.count" do
196       delete :delete_one, :params => { :preference_key => "key" }
197     end
198     assert_response :unauthorized, "should be authenticated"
199     assert_equal "value", UserPreference.find([user.id, "key"]).v
200
201     # authenticate as a user with preferences
202     basic_authorization user.email, "test"
203     grant_oauth_token :allow_write_prefs
204
205     # try the delete again
206     assert_difference "UserPreference.count", -1 do
207       get :delete_one, :params => { :preference_key => "key" }
208     end
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"])
214     end
215
216     # try the delete again for the same key
217     assert_no_difference "UserPreference.count" do
218       get :delete_one, :params => { :preference_key => "key" }
219     end
220     assert_response :not_found
221     assert_raises ActiveRecord::RecordNotFound do
222       UserPreference.find([user.id, "key"])
223     end
224   end
225 end