3 class ChangesetsControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/user/name/history", :method => :get },
9 { :controller => "changesets", :action => "index", :display_name => "name" }
12 { :path => "/user/name/history/feed", :method => :get },
13 { :controller => "changesets", :action => "feed", :display_name => "name", :format => :atom }
16 { :path => "/history/friends", :method => :get },
17 { :controller => "changesets", :action => "index", :friends => true, :format => :html }
20 { :path => "/history/nearby", :method => :get },
21 { :controller => "changesets", :action => "index", :nearby => true, :format => :html }
24 { :path => "/history", :method => :get },
25 { :controller => "changesets", :action => "index" }
28 { :path => "/history/feed", :method => :get },
29 { :controller => "changesets", :action => "feed", :format => :atom }
32 { :path => "/changeset/1/subscribe", :method => :get },
33 { :controller => "changesets", :action => "subscribe", :id => "1" }
36 { :path => "/changeset/1/subscribe", :method => :post },
37 { :controller => "changesets", :action => "subscribe", :id => "1" }
40 { :path => "/changeset/1/unsubscribe", :method => :get },
41 { :controller => "changesets", :action => "unsubscribe", :id => "1" }
44 { :path => "/changeset/1/unsubscribe", :method => :post },
45 { :controller => "changesets", :action => "unsubscribe", :id => "1" }
50 # This should display the last 20 changesets closed
52 changesets = create_list(:changeset, 30, :num_changes => 1)
54 get history_path(:format => "html")
55 assert_response :success
56 assert_template "history"
57 assert_template :layout => "map"
58 assert_select "h2", :text => "Changesets", :count => 1
59 assert_select "link[rel='alternate'][type='application/atom+xml']", :count => 1 do
60 assert_select "[href=?]", "http://www.example.com/history/feed"
63 get history_path(:format => "html", :list => "1"), :xhr => true
64 assert_response :success
65 assert_template "index"
67 check_index_result(changesets.last(20))
71 # This should display the last 20 changesets closed
73 changesets = create_list(:changeset, 30, :num_changes => 1)
75 get history_path(:format => "html"), :xhr => true
76 assert_response :success
77 assert_template "history"
78 assert_template :layout => "xhr"
79 assert_select "h2", :text => "Changesets", :count => 1
80 assert_select "link[rel='alternate'][type='application/atom+xml']", :count => 1 do
81 assert_select "[href=?]", "http://www.example.com/history/feed"
84 get history_path(:format => "html", :list => "1"), :xhr => true
85 assert_response :success
86 assert_template "index"
88 check_index_result(changesets.last(20))
92 # This should display the last 20 changesets closed in a specific area
94 changesets = create_list(:changeset, 10, :num_changes => 1, :min_lat => 50000000, :max_lat => 50000001, :min_lon => 50000000, :max_lon => 50000001)
95 other_changesets = create_list(:changeset, 10, :num_changes => 1, :min_lat => 0, :max_lat => 1, :min_lon => 0, :max_lon => 1)
97 # First check they all show up without a bbox parameter
98 get history_path(:format => "html", :list => "1"), :xhr => true
99 assert_response :success
100 assert_template "index"
101 check_index_result(changesets + other_changesets)
103 # Then check with bbox parameter
104 get history_path(:format => "html", :bbox => "4.5,4.5,5.5,5.5")
105 assert_response :success
106 assert_template "history"
107 assert_template :layout => "map"
108 assert_select "h2", :text => "Changesets", :count => 1
109 assert_select "link[rel='alternate'][type='application/atom+xml']", :count => 1 do
110 assert_select "[href=?]", "http://www.example.com/history/feed?bbox=4.5%2C4.5%2C5.5%2C5.5"
113 get history_path(:format => "html", :bbox => "4.5,4.5,5.5,5.5", :list => "1"), :xhr => true
114 assert_response :success
115 assert_template "index"
117 check_index_result(changesets)
121 # Checks the display of the user changesets listing
124 create(:changeset, :user => user)
125 create(:changeset, :closed, :user => user)
127 get history_path(:format => "html", :display_name => user.display_name)
128 assert_response :success
129 assert_template "history"
130 assert_template :layout => "map"
131 assert_select "h2", :text => "Changesets by #{user.display_name}", :count => 1
132 assert_select "link[rel='alternate'][type='application/atom+xml']", :count => 1 do
133 assert_select "[href=?]", "http://www.example.com/user/#{ERB::Util.url_encode(user.display_name)}/history/feed"
136 get history_path(:format => "html", :display_name => user.display_name, :list => "1"), :xhr => true
137 assert_response :success
138 assert_template "index"
140 check_index_result(user.changesets)
144 # Checks the display of the user changesets listing for a private user
145 def test_index_private_user
146 private_user = create(:user, :data_public => false)
147 create(:changeset, :user => private_user)
148 create(:changeset, :closed, :user => private_user)
150 get history_path(:format => "html", :display_name => private_user.display_name)
151 assert_response :success
152 assert_template "history"
154 get history_path(:format => "html", :display_name => private_user.display_name, :list => "1"), :xhr => true
155 assert_response :success
156 assert_template "index"
158 check_index_result([])
162 # Check the not found of the index user changesets
163 def test_index_user_not_found
164 get history_path(:format => "html", :display_name => "Some random user")
165 assert_response :not_found
166 assert_template "users/no_such_user"
168 get history_path(:format => "html", :display_name => "Some random user", :list => "1"), :xhr => true
169 assert_response :not_found
170 assert_template "users/no_such_user"
174 # Checks the display of the friends changesets listing
175 def test_index_friends
176 private_user = create(:user, :data_public => true)
177 friendship = create(:friendship, :befriender => private_user)
178 changeset = create(:changeset, :user => friendship.befriendee, :num_changes => 1)
179 _changeset2 = create(:changeset, :user => create(:user), :num_changes => 1)
181 get friend_changesets_path
182 assert_response :redirect
183 assert_redirected_to login_path(:referer => friend_changesets_path)
185 session_for(private_user)
187 get friend_changesets_path
188 assert_response :success
189 assert_template "history"
191 get friend_changesets_path(:list => "1"), :xhr => true
192 assert_response :success
193 assert_template "index"
195 check_index_result([changeset])
199 # Checks the display of the nearby user changesets listing
200 def test_index_nearby
201 private_user = create(:user, :data_public => false, :home_lat => 51.1, :home_lon => 1.0)
202 user = create(:user, :home_lat => 51.0, :home_lon => 1.0)
203 far_away_user = create(:user, :home_lat => 51.0, :home_lon => 130)
204 changeset = create(:changeset, :user => user, :num_changes => 1)
205 _changeset2 = create(:changeset, :user => far_away_user, :num_changes => 1)
207 get nearby_changesets_path
208 assert_response :redirect
209 assert_redirected_to login_path(:referer => nearby_changesets_path)
211 session_for(private_user)
213 get nearby_changesets_path
214 assert_response :success
215 assert_template "history"
217 get nearby_changesets_path(:list => "1"), :xhr => true
218 assert_response :success
219 assert_template "index"
221 check_index_result([changeset])
225 # Check that we can't request later pages of the changesets index
226 def test_index_max_id
227 changeset = create(:changeset, :num_changes => 1)
228 _changeset2 = create(:changeset, :num_changes => 1)
230 get history_path(:format => "html", :max_id => changeset.id), :xhr => true
231 assert_response :success
232 assert_template "history"
233 assert_template :layout => "xhr"
234 assert_select "h2", :text => "Changesets", :count => 1
236 get history_path(:format => "html", :list => "1", :max_id => changeset.id), :xhr => true
237 assert_response :success
238 assert_template "index"
240 check_index_result([changeset])
244 # Check that a list with a next page link works
246 create_list(:changeset, 50)
248 get history_path(:format => "html")
249 assert_response :success
251 get history_path(:format => "html"), :xhr => true
252 assert_response :success
256 # This should display the last 20 non-empty changesets
258 changeset = create(:changeset, :num_changes => 1)
259 create(:changeset_tag, :changeset => changeset)
260 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
261 closed_changeset = create(:changeset, :closed, :num_changes => 1)
262 create(:changeset_tag, :changeset => closed_changeset, :k => "website", :v => "https://osm.org/")
263 _empty_changeset = create(:changeset, :num_changes => 0)
265 get history_feed_path(:format => :atom)
266 assert_response :success
267 assert_template "index"
268 assert_equal "application/atom+xml", response.media_type
270 check_feed_result([closed_changeset, changeset])
274 # This should correctly escape XML special characters in the comment
275 def test_feed_with_comment_tag
276 changeset = create(:changeset, :num_changes => 1)
277 create(:changeset_tag, :changeset => changeset, :k => "comment", :v => "tested<changeset>comment")
279 get history_feed_path(:format => :atom)
280 assert_response :success
281 assert_template "index"
282 assert_equal "application/atom+xml", response.media_type
284 check_feed_result([changeset])
288 # This should display the last 20 changesets closed in a specific area
290 changeset = create(:changeset, :num_changes => 1, :min_lat => 5 * GeoRecord::SCALE, :min_lon => 5 * GeoRecord::SCALE, :max_lat => 5 * GeoRecord::SCALE, :max_lon => 5 * GeoRecord::SCALE)
291 create(:changeset_tag, :changeset => changeset)
292 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
293 closed_changeset = create(:changeset, :closed, :num_changes => 1, :min_lat => 5 * GeoRecord::SCALE, :min_lon => 5 * GeoRecord::SCALE, :max_lat => 5 * GeoRecord::SCALE, :max_lon => 5 * GeoRecord::SCALE)
294 _elsewhere_changeset = create(:changeset, :num_changes => 1, :min_lat => -5 * GeoRecord::SCALE, :min_lon => -5 * GeoRecord::SCALE, :max_lat => -5 * GeoRecord::SCALE, :max_lon => -5 * GeoRecord::SCALE)
295 _empty_changeset = create(:changeset, :num_changes => 0, :min_lat => -5 * GeoRecord::SCALE, :min_lon => -5 * GeoRecord::SCALE, :max_lat => -5 * GeoRecord::SCALE, :max_lon => -5 * GeoRecord::SCALE)
297 get history_feed_path(:format => :atom, :bbox => "4.5,4.5,5.5,5.5")
298 assert_response :success
299 assert_template "index"
300 assert_equal "application/atom+xml", response.media_type
302 check_feed_result([closed_changeset, changeset])
306 # Checks the display of the user changesets feed
309 changesets = create_list(:changeset, 3, :user => user, :num_changes => 4)
310 create(:changeset_tag, :changeset => changesets[1])
311 create(:changeset_tag, :changeset => changesets[1], :k => "website", :v => "http://example.com/")
312 _other_changeset = create(:changeset)
314 get history_feed_path(:format => :atom, :display_name => user.display_name)
316 assert_response :success
317 assert_template "index"
318 assert_equal "application/atom+xml", response.media_type
320 check_feed_result(changesets.reverse)
324 # Check the not found of the user changesets feed
325 def test_feed_user_not_found
326 get history_feed_path(:format => "atom", :display_name => "Some random user")
327 assert_response :not_found
331 # Check that we can't request later pages of the changesets feed
333 get history_feed_path(:format => "atom", :max_id => 100)
334 assert_response :redirect
335 assert_redirected_to :action => :feed
338 def test_subscribe_page
340 other_user = create(:user)
341 changeset = create(:changeset, :user => user)
342 path = changeset_subscribe_path(changeset)
345 assert_response :redirect
346 assert_redirected_to login_path(:referer => path)
348 session_for(other_user)
350 assert_response :success
351 assert_dom ".content-body" do
352 assert_dom "a[href='#{changeset_path(changeset)}']", :text => "Changeset #{changeset.id}"
353 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
357 def test_subscribe_success
359 other_user = create(:user)
360 changeset = create(:changeset, :user => user)
362 session_for(other_user)
363 assert_difference "changeset.subscribers.count", 1 do
364 post changeset_subscribe_path(changeset)
366 assert_response :redirect
367 assert_redirected_to changeset_path(changeset)
368 assert changeset.reload.subscribed?(other_user)
371 def test_subscribe_fail
373 other_user = create(:user)
375 changeset = create(:changeset, :user => user)
378 assert_no_difference "changeset.subscribers.count" do
379 post changeset_subscribe_path(changeset)
381 assert_response :forbidden
383 session_for(other_user)
386 post changeset_subscribe_path(999111)
387 assert_response :not_found
389 # trying to subscribe when already subscribed
390 post changeset_subscribe_path(changeset)
391 assert_no_difference "changeset.subscribers.count" do
392 post changeset_subscribe_path(changeset)
396 def test_unsubscribe_page
398 other_user = create(:user)
399 changeset = create(:changeset, :user => user)
400 path = changeset_unsubscribe_path(changeset)
403 assert_response :redirect
404 assert_redirected_to login_path(:referer => path)
406 session_for(other_user)
408 assert_response :success
409 assert_dom ".content-body" do
410 assert_dom "a[href='#{changeset_path(changeset)}']", :text => "Changeset #{changeset.id}"
411 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
415 def test_unsubscribe_success
417 other_user = create(:user)
419 changeset = create(:changeset, :user => user)
420 changeset.subscribers.push(other_user)
422 session_for(other_user)
423 assert_difference "changeset.subscribers.count", -1 do
424 post changeset_unsubscribe_path(changeset)
426 assert_response :redirect
427 assert_redirected_to changeset_path(changeset)
428 assert_not changeset.reload.subscribed?(other_user)
431 def test_unsubscribe_fail
433 other_user = create(:user)
435 changeset = create(:changeset, :user => user)
438 assert_no_difference "changeset.subscribers.count" do
439 post changeset_unsubscribe_path(changeset)
441 assert_response :forbidden
443 session_for(other_user)
446 post changeset_unsubscribe_path(999111)
447 assert_response :not_found
449 # trying to unsubscribe when not subscribed
450 assert_no_difference "changeset.subscribers.count" do
451 post changeset_unsubscribe_path(changeset)
458 # check the result of a index
459 def check_index_result(changesets)
460 assert_select "ol.changesets", :count => [changesets.size, 1].min do
461 assert_select "li", :count => changesets.size
463 changesets.each do |changeset|
464 assert_select "li#changeset_#{changeset.id}", :count => 1
470 # check the result of a feed
471 def check_feed_result(changesets)
472 assert_operator changesets.size, :<=, 20
474 assert_select "feed", :count => [changesets.size, 1].min do
475 assert_select "> title", :count => 1, :text => /^Changesets/
476 assert_select "> entry", :count => changesets.size do |entries|
477 entries.zip(changesets) do |entry, changeset|
478 assert_select entry, "> id", :text => changeset_url(:id => changeset.id)
480 changeset_comment = changeset.tags["comment"]
482 assert_select entry, "> title", :count => 1, :text => "Changeset #{changeset.id} - #{changeset_comment}"
484 assert_select entry, "> title", :count => 1, :text => "Changeset #{changeset.id}"
487 assert_select entry, "> content > xhtml|div > xhtml|table" do
488 if changeset.tags.empty?
489 assert_select "> xhtml|tr > xhtml|td > xhtml|table", :count => 0
491 assert_select "> xhtml|tr > xhtml|td > xhtml|table", :count => 1 do
492 changeset.tags.each_key do |key|
493 assert_select "> xhtml|tr > xhtml|td", :text => /^#{key} = /