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 display the last 20 changesets closed in a specific area
98 changesets = create_list(:changeset, 10, :num_changes => 1, :min_lat => 50000000, :max_lat => 50000001, :min_lon => 50000000, :max_lon => 50000001)
99 other_changesets = create_list(:changeset, 10, :num_changes => 1, :min_lat => 0, :max_lat => 1, :min_lon => 0, :max_lon => 1)
101 # First check they all show up without a bbox parameter
102 get history_path(:format => "html", :list => "1"), :xhr => true
103 assert_response :success
104 assert_template "index"
105 check_index_result(changesets + other_changesets)
107 # Then check with bbox parameter
108 get history_path(:format => "html", :bbox => "4.5,4.5,5.5,5.5")
109 assert_response :success
110 assert_template "history"
111 assert_template :layout => "map"
112 assert_select "h2", :text => "Changesets", :count => 1
113 assert_select "link[rel='alternate'][type='application/atom+xml']", :count => 1 do
114 assert_select "[href=?]", "http://www.example.com/history/feed?bbox=4.5%2C4.5%2C5.5%2C5.5"
117 get history_path(:format => "html", :bbox => "4.5,4.5,5.5,5.5", :list => "1"), :xhr => true
118 assert_response :success
119 assert_template "index"
121 check_index_result(changesets)
125 # Checks the display of the user changesets listing
128 create(:changeset, :user => user, :num_changes => 1)
129 create(:changeset, :closed, :user => user, :num_changes => 1)
132 get history_path(:format => "html", :display_name => user.display_name)
133 assert_response :success
134 assert_template "history"
135 assert_template :layout => "map"
136 assert_select "h2", :text => "Changesets by #{user.display_name}", :count => 1 do
137 assert_select "a[href=?]", user_path(user)
139 assert_select "link[rel='alternate'][type='application/atom+xml']", :count => 1 do
140 assert_select "[href=?]", "http://www.example.com/user/#{ERB::Util.url_encode(user.display_name)}/history/feed"
143 get history_path(:format => "html", :display_name => user.display_name, :list => "1"), :xhr => true
144 assert_response :success
145 assert_template "index"
147 check_index_result(user.changesets)
151 # Checks the display of the user changesets listing for a private user
152 def test_index_private_user
153 private_user = create(:user, :data_public => false)
154 create(:changeset, :user => private_user)
155 create(:changeset, :closed, :user => private_user)
157 get history_path(:format => "html", :display_name => private_user.display_name)
158 assert_response :success
159 assert_template "history"
161 get history_path(:format => "html", :display_name => private_user.display_name, :list => "1"), :xhr => true
162 assert_response :success
163 assert_template "index"
165 check_index_result([])
169 # Check the not found of the index user changesets
170 def test_index_user_not_found
171 get history_path(:format => "html", :display_name => "Some random user")
172 assert_response :not_found
173 assert_template "users/no_such_user"
175 get history_path(:format => "html", :display_name => "Some random user", :list => "1"), :xhr => true
176 assert_response :not_found
177 assert_template "users/no_such_user"
181 # Checks the display of the friends changesets listing
182 def test_index_friends
183 private_user = create(:user, :data_public => true)
184 friendship = create(:friendship, :befriender => private_user)
185 changeset = create(:changeset, :user => friendship.befriendee, :num_changes => 1)
186 _changeset2 = create(:changeset, :user => create(:user), :num_changes => 1)
188 get friend_changesets_path
189 assert_redirected_to login_path(:referer => friend_changesets_path)
191 session_for(private_user)
193 get friend_changesets_path
194 assert_response :success
195 assert_template "history"
197 get friend_changesets_path(:list => "1"), :xhr => true
198 assert_response :success
199 assert_template "index"
201 check_index_result([changeset])
205 # Checks the display of the nearby user changesets listing
206 def test_index_nearby
207 private_user = create(:user, :data_public => false, :home_lat => 51.1, :home_lon => 1.0)
208 user = create(:user, :home_lat => 51.0, :home_lon => 1.0)
209 far_away_user = create(:user, :home_lat => 51.0, :home_lon => 130)
210 changeset = create(:changeset, :user => user, :num_changes => 1)
211 _changeset2 = create(:changeset, :user => far_away_user, :num_changes => 1)
213 get nearby_changesets_path
214 assert_redirected_to login_path(:referer => nearby_changesets_path)
216 session_for(private_user)
218 get nearby_changesets_path
219 assert_response :success
220 assert_template "history"
222 get nearby_changesets_path(:list => "1"), :xhr => true
223 assert_response :success
224 assert_template "index"
226 check_index_result([changeset])
230 # Check that we can't request later pages of the changesets index
231 def test_index_max_id
232 changeset = create(:changeset, :num_changes => 1)
233 _changeset2 = create(:changeset, :num_changes => 1)
235 get history_path(:format => "html", :max_id => changeset.id), :xhr => true
236 assert_response :success
237 assert_template "history"
238 assert_template :layout => "xhr"
239 assert_select "h2", :text => "Changesets", :count => 1
241 get history_path(:format => "html", :list => "1", :max_id => changeset.id), :xhr => true
242 assert_response :success
243 assert_template "index"
245 check_index_result([changeset])
249 # Check that a list with a next page link works
251 create_list(:changeset, 50)
253 get history_path(:format => "html")
254 assert_response :success
256 get history_path(:format => "html"), :xhr => true
257 assert_response :success
261 changeset = create(:changeset)
262 create(:changeset_tag, :changeset => changeset, :k => "comment", :v => "tested-changeset-comment")
263 commenting_user = create(:user)
264 changeset_comment = create(:changeset_comment, :changeset => changeset, :author => commenting_user, :body => "Unwanted comment")
266 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
267 assert_dom "h2", :text => "Changeset: #{changeset.id}"
268 assert_dom "p", :text => "tested-changeset-comment"
269 assert_dom "li#c#{changeset_comment.id}" do
270 assert_dom "> small", :text => /^Comment from #{commenting_user.display_name}/
271 assert_dom "a[href='#{user_path(commenting_user)}']"
275 def test_show_closed_changeset
276 changeset = create(:changeset, :closed)
278 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
281 def test_show_private_changeset
283 changeset = create(:changeset, :user => create(:user, :data_public => false))
284 create(:changeset, :user => user)
286 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
289 def test_show_element_links
290 changeset = create(:changeset)
291 node = create(:node, :with_history, :changeset => changeset)
292 way = create(:way, :with_history, :changeset => changeset)
293 relation = create(:relation, :with_history, :changeset => changeset)
295 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
296 assert_dom "a[href='#{node_path node}']", :count => 1
297 assert_dom "a[href='#{old_node_path node, 1}']", :count => 1
298 assert_dom "a[href='#{way_path way}']", :count => 1
299 assert_dom "a[href='#{old_way_path way, 1}']", :count => 1
300 assert_dom "a[href='#{relation_path relation}']", :count => 1
301 assert_dom "a[href='#{old_relation_path relation, 1}']", :count => 1
304 def test_show_paginated_element_links
306 changeset = create(:changeset)
307 nodes = create_list(:node, page_size + 1, :with_history, :changeset => changeset)
308 ways = create_list(:way, page_size + 1, :with_history, :changeset => changeset)
309 relations = create_list(:relation, page_size + 1, :with_history, :changeset => changeset)
311 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
312 page_size.times do |i|
313 assert_dom "a[href='#{node_path nodes[i]}']", :count => 1
314 assert_dom "a[href='#{old_node_path nodes[i], 1}']", :count => 1
315 assert_dom "a[href='#{way_path ways[i]}']", :count => 1
316 assert_dom "a[href='#{old_way_path ways[i], 1}']", :count => 1
317 assert_dom "a[href='#{relation_path relations[i]}']", :count => 1
318 assert_dom "a[href='#{old_relation_path relations[i], 1}']", :count => 1
322 def test_show_adjacent_changesets
324 changesets = create_list(:changeset, 3, :user => user)
326 sidebar_browse_check :changeset_path, changesets[1].id, "changesets/show"
327 assert_dom "a[href='#{changeset_path changesets[0]}']", :count => 1
328 assert_dom "a[href='#{changeset_path changesets[2]}']", :count => 1
332 # This should display the last 20 non-empty changesets
334 changeset = create(:changeset, :num_changes => 1)
335 create(:changeset_tag, :changeset => changeset)
336 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
337 closed_changeset = create(:changeset, :closed, :num_changes => 1)
338 create(:changeset_tag, :changeset => closed_changeset, :k => "website", :v => "https://osm.org/")
339 _empty_changeset = create(:changeset, :num_changes => 0)
341 get history_feed_path(:format => :atom)
342 assert_response :success
343 assert_template "index"
344 assert_equal "application/atom+xml", response.media_type
346 check_feed_result([closed_changeset, changeset])
350 # This should correctly escape XML special characters in the comment
351 def test_feed_with_comment_tag
352 changeset = create(:changeset, :num_changes => 1)
353 create(:changeset_tag, :changeset => changeset, :k => "comment", :v => "tested<changeset>comment")
355 get history_feed_path(:format => :atom)
356 assert_response :success
357 assert_template "index"
358 assert_equal "application/atom+xml", response.media_type
360 check_feed_result([changeset])
364 # This should display the last 20 changesets closed in a specific area
366 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)
367 create(:changeset_tag, :changeset => changeset)
368 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
369 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)
370 _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)
371 _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)
373 get history_feed_path(:format => :atom, :bbox => "4.5,4.5,5.5,5.5")
374 assert_response :success
375 assert_template "index"
376 assert_equal "application/atom+xml", response.media_type
378 check_feed_result([closed_changeset, changeset])
382 # Checks the display of the user changesets feed
385 changesets = create_list(:changeset, 3, :user => user, :num_changes => 4)
386 create(:changeset_tag, :changeset => changesets[1])
387 create(:changeset_tag, :changeset => changesets[1], :k => "website", :v => "http://example.com/")
388 _other_changeset = create(:changeset)
390 get history_feed_path(:format => :atom, :display_name => user.display_name)
392 assert_response :success
393 assert_template "index"
394 assert_equal "application/atom+xml", response.media_type
396 check_feed_result(changesets.reverse)
400 # Check the not found of the user changesets feed
401 def test_feed_user_not_found
402 get history_feed_path(:format => "atom", :display_name => "Some random user")
403 assert_response :not_found
407 # Check that we can't request later pages of the changesets feed
409 get history_feed_path(:format => "atom", :max_id => 100)
410 assert_redirected_to :action => :feed
413 def test_subscribe_page
415 other_user = create(:user)
416 changeset = create(:changeset, :user => user)
417 path = subscribe_changeset_path(changeset)
420 assert_redirected_to login_path(:referer => path)
422 session_for(other_user)
424 assert_response :success
425 assert_dom ".content-body" do
426 assert_dom "a[href='#{changeset_path(changeset)}']", :text => "Changeset #{changeset.id}"
427 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
431 def test_subscribe_success
433 other_user = create(:user)
434 changeset = create(:changeset, :user => user)
436 session_for(other_user)
437 assert_difference "changeset.subscribers.count", 1 do
438 post subscribe_changeset_path(changeset)
440 assert_redirected_to changeset_path(changeset)
441 assert changeset.reload.subscribed?(other_user)
444 def test_subscribe_fail
446 other_user = create(:user)
448 changeset = create(:changeset, :user => user)
451 assert_no_difference "changeset.subscribers.count" do
452 post subscribe_changeset_path(changeset)
454 assert_response :forbidden
456 session_for(other_user)
459 post subscribe_changeset_path(999111)
460 assert_response :not_found
462 # trying to subscribe when already subscribed
463 post subscribe_changeset_path(changeset)
464 assert_no_difference "changeset.subscribers.count" do
465 post subscribe_changeset_path(changeset)
469 def test_unsubscribe_page
471 other_user = create(:user)
472 changeset = create(:changeset, :user => user)
473 path = unsubscribe_changeset_path(changeset)
476 assert_redirected_to login_path(:referer => path)
478 session_for(other_user)
480 assert_response :success
481 assert_dom ".content-body" do
482 assert_dom "a[href='#{changeset_path(changeset)}']", :text => "Changeset #{changeset.id}"
483 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
487 def test_unsubscribe_success
489 other_user = create(:user)
491 changeset = create(:changeset, :user => user)
492 changeset.subscribers.push(other_user)
494 session_for(other_user)
495 assert_difference "changeset.subscribers.count", -1 do
496 post unsubscribe_changeset_path(changeset)
498 assert_redirected_to changeset_path(changeset)
499 assert_not changeset.reload.subscribed?(other_user)
502 def test_unsubscribe_fail
504 other_user = create(:user)
506 changeset = create(:changeset, :user => user)
509 assert_no_difference "changeset.subscribers.count" do
510 post unsubscribe_changeset_path(changeset)
512 assert_response :forbidden
514 session_for(other_user)
517 post unsubscribe_changeset_path(999111)
518 assert_response :not_found
520 # trying to unsubscribe when not subscribed
521 assert_no_difference "changeset.subscribers.count" do
522 post unsubscribe_changeset_path(changeset)
529 # check the result of a index
530 def check_index_result(changesets)
531 assert_select "ol.changesets", :count => [changesets.size, 1].min do
532 assert_select "li", :count => changesets.size
534 changesets.each do |changeset|
535 assert_select "li#changeset_#{changeset.id}", :count => 1
541 # check the result of a feed
542 def check_feed_result(changesets)
543 assert_operator changesets.size, :<=, 20
545 assert_select "feed", :count => [changesets.size, 1].min do
546 assert_select "> title", :count => 1, :text => /^Changesets/
547 assert_select "> entry", :count => changesets.size do |entries|
548 entries.zip(changesets) do |entry, changeset|
549 assert_select entry, "> id", :text => changeset_url(:id => changeset.id)
551 changeset_comment = changeset.tags["comment"]
553 assert_select entry, "> title", :count => 1, :text => "Changeset #{changeset.id} - #{changeset_comment}"
555 assert_select entry, "> title", :count => 1, :text => "Changeset #{changeset.id}"
558 assert_select entry, "> content > xhtml|div > xhtml|table" do
559 if changeset.tags.empty?
560 assert_select "> xhtml|tr > xhtml|td > xhtml|table", :count => 0
562 assert_select "> xhtml|tr > xhtml|td > xhtml|table", :count => 1 do
563 changeset.tags.each_key do |key|
564 assert_select "> xhtml|tr > xhtml|td", :text => /^#{key} = /