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)
129 create(:changeset, :closed, :user => user)
131 get history_path(:format => "html", :display_name => user.display_name)
132 assert_response :success
133 assert_template "history"
134 assert_template :layout => "map"
135 assert_select "h2", :text => "Changesets by #{user.display_name}", :count => 1
136 assert_select "link[rel='alternate'][type='application/atom+xml']", :count => 1 do
137 assert_select "[href=?]", "http://www.example.com/user/#{ERB::Util.url_encode(user.display_name)}/history/feed"
140 get history_path(:format => "html", :display_name => user.display_name, :list => "1"), :xhr => true
141 assert_response :success
142 assert_template "index"
144 check_index_result(user.changesets)
148 # Checks the display of the user changesets listing for a private user
149 def test_index_private_user
150 private_user = create(:user, :data_public => false)
151 create(:changeset, :user => private_user)
152 create(:changeset, :closed, :user => private_user)
154 get history_path(:format => "html", :display_name => private_user.display_name)
155 assert_response :success
156 assert_template "history"
158 get history_path(:format => "html", :display_name => private_user.display_name, :list => "1"), :xhr => true
159 assert_response :success
160 assert_template "index"
162 check_index_result([])
166 # Check the not found of the index user changesets
167 def test_index_user_not_found
168 get history_path(:format => "html", :display_name => "Some random user")
169 assert_response :not_found
170 assert_template "users/no_such_user"
172 get history_path(:format => "html", :display_name => "Some random user", :list => "1"), :xhr => true
173 assert_response :not_found
174 assert_template "users/no_such_user"
178 # Checks the display of the friends changesets listing
179 def test_index_friends
180 private_user = create(:user, :data_public => true)
181 friendship = create(:friendship, :befriender => private_user)
182 changeset = create(:changeset, :user => friendship.befriendee, :num_changes => 1)
183 _changeset2 = create(:changeset, :user => create(:user), :num_changes => 1)
185 get friend_changesets_path
186 assert_response :redirect
187 assert_redirected_to login_path(:referer => friend_changesets_path)
189 session_for(private_user)
191 get friend_changesets_path
192 assert_response :success
193 assert_template "history"
195 get friend_changesets_path(:list => "1"), :xhr => true
196 assert_response :success
197 assert_template "index"
199 check_index_result([changeset])
203 # Checks the display of the nearby user changesets listing
204 def test_index_nearby
205 private_user = create(:user, :data_public => false, :home_lat => 51.1, :home_lon => 1.0)
206 user = create(:user, :home_lat => 51.0, :home_lon => 1.0)
207 far_away_user = create(:user, :home_lat => 51.0, :home_lon => 130)
208 changeset = create(:changeset, :user => user, :num_changes => 1)
209 _changeset2 = create(:changeset, :user => far_away_user, :num_changes => 1)
211 get nearby_changesets_path
212 assert_response :redirect
213 assert_redirected_to login_path(:referer => nearby_changesets_path)
215 session_for(private_user)
217 get nearby_changesets_path
218 assert_response :success
219 assert_template "history"
221 get nearby_changesets_path(:list => "1"), :xhr => true
222 assert_response :success
223 assert_template "index"
225 check_index_result([changeset])
229 # Check that we can't request later pages of the changesets index
230 def test_index_max_id
231 changeset = create(:changeset, :num_changes => 1)
232 _changeset2 = create(:changeset, :num_changes => 1)
234 get history_path(:format => "html", :max_id => changeset.id), :xhr => true
235 assert_response :success
236 assert_template "history"
237 assert_template :layout => "xhr"
238 assert_select "h2", :text => "Changesets", :count => 1
240 get history_path(:format => "html", :list => "1", :max_id => changeset.id), :xhr => true
241 assert_response :success
242 assert_template "index"
244 check_index_result([changeset])
248 # Check that a list with a next page link works
250 create_list(:changeset, 50)
252 get history_path(:format => "html")
253 assert_response :success
255 get history_path(:format => "html"), :xhr => true
256 assert_response :success
260 changeset = create(:changeset)
261 create(:changeset_tag, :changeset => changeset, :k => "comment", :v => "tested-changeset-comment")
262 commenting_user = create(:user)
263 changeset_comment = create(:changeset_comment, :changeset => changeset, :author => commenting_user, :body => "Unwanted comment")
265 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
266 assert_dom "h2", :text => "Changeset: #{changeset.id}"
267 assert_dom "p", :text => "tested-changeset-comment"
268 assert_dom "li#c#{changeset_comment.id}" do
269 assert_dom "> small", :text => /^Comment from #{commenting_user.display_name}/
273 def test_show_private_changeset
275 changeset = create(:changeset, :user => create(:user, :data_public => false))
276 create(:changeset, :user => user)
278 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
281 def test_show_element_links
282 changeset = create(:changeset)
283 node = create(:node, :with_history, :changeset => changeset)
285 sidebar_browse_check :changeset_path, changeset.id, "changesets/show"
286 assert_dom "a[href='#{node_path node}']", :count => 1
287 assert_dom "a[href='#{old_node_path node, 1}']", :count => 1
291 # This should display the last 20 non-empty changesets
293 changeset = create(:changeset, :num_changes => 1)
294 create(:changeset_tag, :changeset => changeset)
295 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
296 closed_changeset = create(:changeset, :closed, :num_changes => 1)
297 create(:changeset_tag, :changeset => closed_changeset, :k => "website", :v => "https://osm.org/")
298 _empty_changeset = create(:changeset, :num_changes => 0)
300 get history_feed_path(:format => :atom)
301 assert_response :success
302 assert_template "index"
303 assert_equal "application/atom+xml", response.media_type
305 check_feed_result([closed_changeset, changeset])
309 # This should correctly escape XML special characters in the comment
310 def test_feed_with_comment_tag
311 changeset = create(:changeset, :num_changes => 1)
312 create(:changeset_tag, :changeset => changeset, :k => "comment", :v => "tested<changeset>comment")
314 get history_feed_path(:format => :atom)
315 assert_response :success
316 assert_template "index"
317 assert_equal "application/atom+xml", response.media_type
319 check_feed_result([changeset])
323 # This should display the last 20 changesets closed in a specific area
325 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)
326 create(:changeset_tag, :changeset => changeset)
327 create(:changeset_tag, :changeset => changeset, :k => "website", :v => "http://example.com/")
328 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)
329 _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)
330 _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)
332 get history_feed_path(:format => :atom, :bbox => "4.5,4.5,5.5,5.5")
333 assert_response :success
334 assert_template "index"
335 assert_equal "application/atom+xml", response.media_type
337 check_feed_result([closed_changeset, changeset])
341 # Checks the display of the user changesets feed
344 changesets = create_list(:changeset, 3, :user => user, :num_changes => 4)
345 create(:changeset_tag, :changeset => changesets[1])
346 create(:changeset_tag, :changeset => changesets[1], :k => "website", :v => "http://example.com/")
347 _other_changeset = create(:changeset)
349 get history_feed_path(:format => :atom, :display_name => user.display_name)
351 assert_response :success
352 assert_template "index"
353 assert_equal "application/atom+xml", response.media_type
355 check_feed_result(changesets.reverse)
359 # Check the not found of the user changesets feed
360 def test_feed_user_not_found
361 get history_feed_path(:format => "atom", :display_name => "Some random user")
362 assert_response :not_found
366 # Check that we can't request later pages of the changesets feed
368 get history_feed_path(:format => "atom", :max_id => 100)
369 assert_response :redirect
370 assert_redirected_to :action => :feed
373 def test_subscribe_page
375 other_user = create(:user)
376 changeset = create(:changeset, :user => user)
377 path = changeset_subscribe_path(changeset)
380 assert_response :redirect
381 assert_redirected_to login_path(:referer => path)
383 session_for(other_user)
385 assert_response :success
386 assert_dom ".content-body" do
387 assert_dom "a[href='#{changeset_path(changeset)}']", :text => "Changeset #{changeset.id}"
388 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
392 def test_subscribe_success
394 other_user = create(:user)
395 changeset = create(:changeset, :user => user)
397 session_for(other_user)
398 assert_difference "changeset.subscribers.count", 1 do
399 post changeset_subscribe_path(changeset)
401 assert_response :redirect
402 assert_redirected_to changeset_path(changeset)
403 assert changeset.reload.subscribed?(other_user)
406 def test_subscribe_fail
408 other_user = create(:user)
410 changeset = create(:changeset, :user => user)
413 assert_no_difference "changeset.subscribers.count" do
414 post changeset_subscribe_path(changeset)
416 assert_response :forbidden
418 session_for(other_user)
421 post changeset_subscribe_path(999111)
422 assert_response :not_found
424 # trying to subscribe when already subscribed
425 post changeset_subscribe_path(changeset)
426 assert_no_difference "changeset.subscribers.count" do
427 post changeset_subscribe_path(changeset)
431 def test_unsubscribe_page
433 other_user = create(:user)
434 changeset = create(:changeset, :user => user)
435 path = changeset_unsubscribe_path(changeset)
438 assert_response :redirect
439 assert_redirected_to login_path(:referer => path)
441 session_for(other_user)
443 assert_response :success
444 assert_dom ".content-body" do
445 assert_dom "a[href='#{changeset_path(changeset)}']", :text => "Changeset #{changeset.id}"
446 assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
450 def test_unsubscribe_success
452 other_user = create(:user)
454 changeset = create(:changeset, :user => user)
455 changeset.subscribers.push(other_user)
457 session_for(other_user)
458 assert_difference "changeset.subscribers.count", -1 do
459 post changeset_unsubscribe_path(changeset)
461 assert_response :redirect
462 assert_redirected_to changeset_path(changeset)
463 assert_not changeset.reload.subscribed?(other_user)
466 def test_unsubscribe_fail
468 other_user = create(:user)
470 changeset = create(:changeset, :user => user)
473 assert_no_difference "changeset.subscribers.count" do
474 post changeset_unsubscribe_path(changeset)
476 assert_response :forbidden
478 session_for(other_user)
481 post changeset_unsubscribe_path(999111)
482 assert_response :not_found
484 # trying to unsubscribe when not subscribed
485 assert_no_difference "changeset.subscribers.count" do
486 post changeset_unsubscribe_path(changeset)
493 # check the result of a index
494 def check_index_result(changesets)
495 assert_select "ol.changesets", :count => [changesets.size, 1].min do
496 assert_select "li", :count => changesets.size
498 changesets.each do |changeset|
499 assert_select "li#changeset_#{changeset.id}", :count => 1
505 # check the result of a feed
506 def check_feed_result(changesets)
507 assert_operator changesets.size, :<=, 20
509 assert_select "feed", :count => [changesets.size, 1].min do
510 assert_select "> title", :count => 1, :text => /^Changesets/
511 assert_select "> entry", :count => changesets.size do |entries|
512 entries.zip(changesets) do |entry, changeset|
513 assert_select entry, "> id", :text => changeset_url(:id => changeset.id)
515 changeset_comment = changeset.tags["comment"]
517 assert_select entry, "> title", :count => 1, :text => "Changeset #{changeset.id} - #{changeset_comment}"
519 assert_select entry, "> title", :count => 1, :text => "Changeset #{changeset.id}"
522 assert_select entry, "> content > xhtml|div > xhtml|table" do
523 if changeset.tags.empty?
524 assert_select "> xhtml|tr > xhtml|td > xhtml|table", :count => 0
526 assert_select "> xhtml|tr > xhtml|td > xhtml|table", :count => 1 do
527 changeset.tags.each_key do |key|
528 assert_select "> xhtml|tr > xhtml|td", :text => /^#{key} = /