3 class ChangesetsControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/changeset/1", :method => :get },
9 { :controller => "changesets", :action => "show", :id => "1" }
12 { :path => "/user/name/history", :method => :get },
13 { :controller => "changesets", :action => "index", :display_name => "name" }
16 { :path => "/user/name/history/feed", :method => :get },
17 { :controller => "changesets", :action => "feed", :display_name => "name", :format => :atom }
20 { :path => "/history/friends", :method => :get },
21 { :controller => "changesets", :action => "index", :friends => true, :format => :html }
24 { :path => "/history/nearby", :method => :get },
25 { :controller => "changesets", :action => "index", :nearby => true, :format => :html }
28 { :path => "/history", :method => :get },
29 { :controller => "changesets", :action => "index" }
32 { :path => "/history/feed", :method => :get },
33 { :controller => "changesets", :action => "feed", :format => :atom }
36 { :path => "/changeset/1/subscribe", :method => :get },
37 { :controller => "changesets", :action => "subscribe", :id => "1" }
40 { :path => "/changeset/1/subscribe", :method => :post },
41 { :controller => "changesets", :action => "subscribe", :id => "1" }
44 { :path => "/changeset/1/unsubscribe", :method => :get },
45 { :controller => "changesets", :action => "unsubscribe", :id => "1" }
48 { :path => "/changeset/1/unsubscribe", :method => :post },
49 { :controller => "changesets", :action => "unsubscribe", :id => "1" }
54 # This should display the last 20 changesets closed
56 changesets = create_list(:changeset, 30, :num_changes => 1)
58 get history_path(:format => "html")
59 assert_response :success
60 assert_template "history"
61 assert_template :layout => "map"
62 assert_select "h2", :text => "Changesets", :count => 1
63 assert_select "link[rel='alternate'][type='application/atom+xml']", :count => 1 do
64 assert_select "[href=?]", "http://www.example.com/history/feed"
67 get history_path(:format => "html", :list => "1"), :xhr => true
68 assert_response :success
69 assert_template "index"
71 check_index_result(changesets.last(20))
75 # This should display the last 20 changesets closed
77 changesets = create_list(:changeset, 30, :num_changes => 1)
79 get history_path(:format => "html"), :xhr => true
80 assert_response :success
81 assert_template "history"
82 assert_template :layout => "xhr"
83 assert_select "h2", :text => "Changesets", :count => 1
84 assert_select "link[rel='alternate'][type='application/atom+xml']", :count => 1 do
85 assert_select "[href=?]", "http://www.example.com/history/feed"
88 get history_path(:format => "html", :list => "1"), :xhr => true
89 assert_response :success
90 assert_template "index"
92 check_index_result(changesets.last(20))
96 # This should report an error
97 def test_index_invalid_xhr
98 %w[-1 0 fred].each do |id|
99 get history_path(:format => "html", :list => "1", :max_id => id)
100 assert_redirected_to :controller => :errors, :action => :bad_request
105 # This should display the last 20 changesets closed in a specific area
107 changesets = create_list(:changeset, 10, :num_changes => 1, :min_lat => 50000000, :max_lat => 50000001, :min_lon => 50000000, :max_lon => 50000001)
108 other_changesets = create_list(:changeset, 10, :num_changes => 1, :min_lat => 0, :max_lat => 1, :min_lon => 0, :max_lon => 1)
110 # First check they all show up without a bbox parameter
111 get history_path(:format => "html", :list => "1"), :xhr => true
112 assert_response :success
113 assert_template "index"
114 check_index_result(changesets + other_changesets)
116 # Then check with bbox parameter
117 get history_path(:format => "html", :bbox => "4.5,4.5,5.5,5.5")
118 assert_response :success
119 assert_template "history"
120 assert_template :layout => "map"
121 assert_select "h2", :text => "Changesets", :count => 1
122 assert_select "link[rel='alternate'][type='application/atom+xml']", :count => 1 do
123 assert_select "[href=?]", "http://www.example.com/history/feed?bbox=4.5%2C4.5%2C5.5%2C5.5"
126 get history_path(:format => "html", :bbox => "4.5,4.5,5.5,5.5", :list => "1"), :xhr => true
127 assert_response :success
128 assert_template "index"
130 check_index_result(changesets)
134 # Checks the display of the user changesets listing
137 create(:changeset, :user => user, :num_changes => 1)
138 create(:changeset, :closed, :user => user, :num_changes => 1)
141 get history_path(:format => "html", :display_name => user.display_name)
142 assert_response :success
143 assert_template "history"
144 assert_template :layout => "map"
145 assert_select "h2", :text => "Changesets by #{user.display_name}", :count => 1 do
146 assert_select "a[href=?]", user_path(user)
148 assert_select "link[rel='alternate'][type='application/atom+xml']", :count => 1 do
149 assert_select "[href=?]", "http://www.example.com/user/#{ERB::Util.url_encode(user.display_name)}/history/feed"
152 get history_path(:format => "html", :display_name => user.display_name, :list => "1"), :xhr => true
153 assert_response :success
154 assert_template "index"
156 check_index_result(user.changesets)
160 # Checks the display of the user changesets listing for a private user
161 def test_index_private_user
162 private_user = create(:user, :data_public => false)
163 create(:changeset, :user => private_user)
164 create(:changeset, :closed, :user => private_user)
166 get history_path(:format => "html", :display_name => private_user.display_name)
167 assert_response :success
168 assert_template "history"
170 get history_path(:format => "html", :display_name => private_user.display_name, :list => "1"), :xhr => true
171 assert_response :success
172 assert_template "index"
174 check_index_result([])
178 # Check the not found of the index user changesets
179 def test_index_user_not_found
180 get history_path(:format => "html", :display_name => "Some random user")
181 assert_response :not_found
182 assert_template "users/no_such_user"
184 get history_path(:format => "html", :display_name => "Some random user", :list => "1"), :xhr => true
185 assert_response :not_found
186 assert_template "users/no_such_user"
190 # Checks the display of the friends changesets listing
191 def test_index_friends
192 private_user = create(:user, :data_public => true)
193 friendship = create(:friendship, :befriender => private_user)
194 changeset = create(:changeset, :user => friendship.befriendee, :num_changes => 1)
195 _changeset2 = create(:changeset, :user => create(:user), :num_changes => 1)
197 get friend_changesets_path
198 assert_redirected_to login_path(:referer => friend_changesets_path)
200 session_for(private_user)
202 get friend_changesets_path
203 assert_response :success
204 assert_template "history"
206 get friend_changesets_path(:list => "1"), :xhr => true
207 assert_response :success
208 assert_template "index"
210 check_index_result([changeset])
214 # Checks the display of the nearby user changesets listing
215 def test_index_nearby
216 private_user = create(:user, :data_public => false, :home_lat => 51.1, :home_lon => 1.0)
217 user = create(:user, :home_lat => 51.0, :home_lon => 1.0)
218 far_away_user = create(:user, :home_lat => 51.0, :home_lon => 130)
219 changeset = create(:changeset, :user => user, :num_changes => 1)
220 _changeset2 = create(:changeset, :user => far_away_user, :num_changes => 1)
222 get nearby_changesets_path
223 assert_redirected_to login_path(:referer => nearby_changesets_path)
225 session_for(private_user)
227 get nearby_changesets_path
228 assert_response :success
229 assert_template "history"
231 get nearby_changesets_path(:list => "1"), :xhr => true
232 assert_response :success
233 assert_template "index"
235 check_index_result([changeset])
239 # Check that we can't request later pages of the changesets index
240 def test_index_max_id
241 changeset = create(:changeset, :num_changes => 1)
242 _changeset2 = create(:changeset, :num_changes => 1)
244 get history_path(:format => "html", :max_id => changeset.id), :xhr => true
245 assert_response :success
246 assert_template "history"
247 assert_template :layout => "xhr"
248 assert_select "h2", :text => "Changesets", :count => 1
250 get history_path(:format => "html", :list => "1", :max_id => changeset.id), :xhr => true
251 assert_response :success
252 assert_template "index"
254 check_index_result([changeset])
258 # Check that a list with a next page link works
260 create_list(:changeset, 50)
262 get history_path(:format => "html")
263 assert_response :success
265 get history_path(:format => "html"), :xhr => true
266 assert_response :success
270 changeset = create(:changeset)
271 create(:changeset_tag, :changeset => changeset, :k => "comment", :v => "tested-changeset-comment")
272 commenting_user = create(:user)
273 changeset_comment = create(:changeset_comment, :changeset => changeset, :author => commenting_user, :body => "Unwanted comment")
275 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
276 assert_dom "h2", :text => "Changeset: #{changeset.id}"
277 assert_dom "p", :text => "tested-changeset-comment"
278 assert_dom "li#c#{changeset_comment.id}" do
279 assert_dom "> small", :text => /^Comment from #{commenting_user.display_name}/
280 assert_dom "a[href='#{user_path(commenting_user)}']"
284 def test_show_closed_changeset
285 changeset = create(:changeset, :closed)
287 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
290 def test_show_private_changeset
292 changeset = create(:changeset, :user => create(:user, :data_public => false))
293 create(:changeset, :user => user)
295 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
298 def test_show_element_links
299 changeset = create(:changeset)
300 node = create(:node, :with_history, :changeset => changeset)
301 way = create(:way, :with_history, :changeset => changeset)
302 relation = create(:relation, :with_history, :changeset => changeset)
304 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
305 assert_dom "a[href='#{node_path node}']", :count => 1
306 assert_dom "a[href='#{old_node_path node, 1}']", :count => 1
307 assert_dom "a[href='#{way_path way}']", :count => 1
308 assert_dom "a[href='#{old_way_path way, 1}']", :count => 1
309 assert_dom "a[href='#{relation_path relation}']", :count => 1
310 assert_dom "a[href='#{old_relation_path relation, 1}']", :count => 1
313 def test_show_paginated_element_links
315 changeset = create(:changeset)
316 nodes = create_list(:node, page_size + 1, :with_history, :changeset => changeset)
317 ways = create_list(:way, page_size + 1, :with_history, :changeset => changeset)
318 relations = create_list(:relation, page_size + 1, :with_history, :changeset => changeset)
320 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
321 page_size.times do |i|
322 assert_dom "a[href='#{node_path nodes[i]}']", :count => 1
323 assert_dom "a[href='#{old_node_path nodes[i], 1}']", :count => 1
324 assert_dom "a[href='#{way_path ways[i]}']", :count => 1
325 assert_dom "a[href='#{old_way_path ways[i], 1}']", :count => 1
326 assert_dom "a[href='#{relation_path relations[i]}']", :count => 1
327 assert_dom "a[href='#{old_relation_path relations[i], 1}']", :count => 1
331 def test_show_adjacent_changesets
333 changesets = create_list(:changeset, 3, :user => user, :num_changes => 1)
335 sidebar_browse_check :changeset_path, changesets[1].id, "changesets/show"
336 assert_dom "a[href='#{changeset_path changesets[0]}']", :count => 1
337 assert_dom "a[href='#{changeset_path changesets[2]}']", :count => 1
340 def test_show_adjacent_nonempty_changesets
342 changeset1 = create(:changeset, :user => user, :num_changes => 1)
343 create(:changeset, :user => user, :num_changes => 0)
344 changeset3 = create(:changeset, :user => user, :num_changes => 1)
345 create(:changeset, :user => user, :num_changes => 0)
346 changeset5 = create(:changeset, :user => user, :num_changes => 1)
348 sidebar_browse_check :changeset_path, changeset3.id, "changesets/show"
349 assert_dom "a[href='#{changeset_path changeset1}']", :count => 1
350 assert_dom "a[href='#{changeset_path changeset5}']", :count => 1
354 # This should display the last 20 non-empty changesets
356 changeset = create(:changeset, :num_changes => 1)
357 create(:changeset_tag, :changeset => changeset)
358 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
359 closed_changeset = create(:changeset, :closed, :num_changes => 1)
360 create(:changeset_tag, :changeset => closed_changeset, :k => "website", :v => "https://osm.org/")
361 _empty_changeset = create(:changeset, :num_changes => 0)
363 get history_feed_path(:format => :atom)
364 assert_response :success
365 assert_template "index"
366 assert_equal "application/atom+xml", response.media_type
368 check_feed_result([closed_changeset, changeset])
372 # This should correctly escape XML special characters in the comment
373 def test_feed_with_comment_tag
374 changeset = create(:changeset, :num_changes => 1)
375 create(:changeset_tag, :changeset => changeset, :k => "comment", :v => "tested<changeset>comment")
377 get history_feed_path(:format => :atom)
378 assert_response :success
379 assert_template "index"
380 assert_equal "application/atom+xml", response.media_type
382 check_feed_result([changeset])
386 # This should display the last 20 changesets closed in a specific area
388 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)
389 create(:changeset_tag, :changeset => changeset)
390 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
391 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)
392 _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)
393 _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)
395 get history_feed_path(:format => :atom, :bbox => "4.5,4.5,5.5,5.5")
396 assert_response :success
397 assert_template "index"
398 assert_equal "application/atom+xml", response.media_type
400 check_feed_result([closed_changeset, changeset])
404 # Checks the display of the user changesets feed
407 changesets = create_list(:changeset, 3, :user => user, :num_changes => 4)
408 create(:changeset_tag, :changeset => changesets[1])
409 create(:changeset_tag, :changeset => changesets[1], :k => "website", :v => "http://example.com/")
410 _other_changeset = create(:changeset)
412 get history_feed_path(:format => :atom, :display_name => user.display_name)
414 assert_response :success
415 assert_template "index"
416 assert_equal "application/atom+xml", response.media_type
418 check_feed_result(changesets.reverse)
422 # Check the not found of the user changesets feed
423 def test_feed_user_not_found
424 get history_feed_path(:format => "atom", :display_name => "Some random user")
425 assert_response :not_found
429 # Check that we can't request later pages of the changesets feed
431 get history_feed_path(:format => "atom", :max_id => 100)
432 assert_redirected_to :action => :feed
435 def test_subscribe_page
437 other_user = create(:user)
438 changeset = create(:changeset, :user => user)
439 path = subscribe_changeset_path(changeset)
442 assert_redirected_to login_path(:referer => path)
444 session_for(other_user)
446 assert_response :success
447 assert_dom ".content-body" do
448 assert_dom "a[href='#{changeset_path(changeset)}']", :text => "Changeset #{changeset.id}"
449 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
453 def test_subscribe_success
455 other_user = create(:user)
456 changeset = create(:changeset, :user => user)
458 session_for(other_user)
459 assert_difference "changeset.subscribers.count", 1 do
460 post subscribe_changeset_path(changeset)
462 assert_redirected_to changeset_path(changeset)
463 assert changeset.reload.subscribed?(other_user)
466 def test_subscribe_fail
468 other_user = create(:user)
470 changeset = create(:changeset, :user => user)
473 assert_no_difference "changeset.subscribers.count" do
474 post subscribe_changeset_path(changeset)
476 assert_response :forbidden
478 session_for(other_user)
481 post subscribe_changeset_path(999111)
482 assert_response :not_found
484 # trying to subscribe when already subscribed
485 post subscribe_changeset_path(changeset)
486 assert_no_difference "changeset.subscribers.count" do
487 post subscribe_changeset_path(changeset)
491 def test_unsubscribe_page
493 other_user = create(:user)
494 changeset = create(:changeset, :user => user)
495 path = unsubscribe_changeset_path(changeset)
498 assert_redirected_to login_path(:referer => path)
500 session_for(other_user)
502 assert_response :success
503 assert_dom ".content-body" do
504 assert_dom "a[href='#{changeset_path(changeset)}']", :text => "Changeset #{changeset.id}"
505 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
509 def test_unsubscribe_success
511 other_user = create(:user)
513 changeset = create(:changeset, :user => user)
514 changeset.subscribers.push(other_user)
516 session_for(other_user)
517 assert_difference "changeset.subscribers.count", -1 do
518 post unsubscribe_changeset_path(changeset)
520 assert_redirected_to changeset_path(changeset)
521 assert_not changeset.reload.subscribed?(other_user)
524 def test_unsubscribe_fail
526 other_user = create(:user)
528 changeset = create(:changeset, :user => user)
531 assert_no_difference "changeset.subscribers.count" do
532 post unsubscribe_changeset_path(changeset)
534 assert_response :forbidden
536 session_for(other_user)
539 post unsubscribe_changeset_path(999111)
540 assert_response :not_found
542 # trying to unsubscribe when not subscribed
543 assert_no_difference "changeset.subscribers.count" do
544 post unsubscribe_changeset_path(changeset)
551 # check the result of a index
552 def check_index_result(changesets)
553 assert_select "ol.changesets", :count => [changesets.size, 1].min do
554 assert_select "li", :count => changesets.size
556 changesets.each do |changeset|
557 assert_select "li#changeset_#{changeset.id}", :count => 1
563 # check the result of a feed
564 def check_feed_result(changesets)
565 assert_operator changesets.size, :<=, 20
567 assert_select "feed", :count => [changesets.size, 1].min do
568 assert_select "> title", :count => 1, :text => /^Changesets/
569 assert_select "> entry", :count => changesets.size do |entries|
570 entries.zip(changesets) do |entry, changeset|
571 assert_select entry, "> id", :text => changeset_url(:id => changeset.id)
573 changeset_comment = changeset.tags["comment"]
575 assert_select entry, "> title", :count => 1, :text => "Changeset #{changeset.id} - #{changeset_comment}"
577 assert_select entry, "> title", :count => 1, :text => "Changeset #{changeset.id}"
580 assert_select entry, "> content > xhtml|div > xhtml|table" do
581 if changeset.tags.empty?
582 assert_select "> xhtml|tr > xhtml|td > xhtml|table", :count => 0
584 assert_select "> xhtml|tr > xhtml|td > xhtml|table", :count => 1 do
585 changeset.tags.each_key do |key|
586 assert_select "> xhtml|tr > xhtml|td", :text => /^#{key} = /