1 # frozen_string_literal: true
5 class SiteControllerTest < ActionDispatch::IntegrationTest
7 # test all routes which lead to this controller
10 { :path => "/", :method => :get },
11 { :controller => "site", :action => "index" }
14 { :path => "/", :method => :post },
15 { :controller => "site", :action => "index" }
18 { :path => "/edit", :method => :get },
19 { :controller => "site", :action => "edit" }
22 { :controller => "site", :action => "edit", :format => "html" },
23 { :path => "/edit.html", :method => :get }
26 { :path => "/copyright", :method => :get },
27 { :controller => "site", :action => "copyright" }
30 { :path => "/copyright/locale", :method => :get },
31 { :controller => "site", :action => "copyright", :copyright_locale => "locale" }
34 { :path => "/welcome", :method => :get },
35 { :controller => "site", :action => "welcome" }
38 { :path => "/fixthemap", :method => :get },
39 { :controller => "site", :action => "fixthemap" }
42 { :path => "/help", :method => :get },
43 { :controller => "site", :action => "help" }
46 { :path => "/about", :method => :get },
47 { :controller => "site", :action => "about" }
50 { :path => "/about/locale", :method => :get },
51 { :controller => "site", :action => "about", :about_locale => "locale" }
54 { :path => "/export", :method => :get },
55 { :controller => "site", :action => "export" }
58 { :controller => "site", :action => "export", :format => "html" },
59 { :path => "/export.html", :method => :get }
62 { :path => "/offline", :method => :get },
63 { :controller => "site", :action => "offline" }
66 { :path => "/go/shortcode", :method => :get },
67 { :controller => "site", :action => "permalink", :code => "shortcode" }
70 { :path => "/preview/typename", :method => :post },
71 { :controller => "site", :action => "preview", :type => "typename" }
74 { :path => "/id", :method => :get },
75 { :controller => "site", :action => "id" }
83 assert_response :success
84 assert_template "index"
87 # Test the index page redirects
88 def test_index_redirect
89 get root_path(:node => 123)
90 assert_redirected_to node_path(123)
92 get root_path(:node => "x")
93 assert_response :success
94 assert_template "index"
96 get root_path(:way => 123)
97 assert_redirected_to way_path(123)
99 get root_path(:way => "x")
100 assert_response :success
101 assert_template "index"
103 get root_path(:relation => 123)
104 assert_redirected_to relation_path(123)
106 get root_path(:relation => "x")
107 assert_response :success
108 assert_template "index"
110 get root_path(:note => 123)
111 assert_redirected_to :controller => :notes, :action => :show, :id => 123
113 get root_path(:note => "x")
114 assert_response :success
115 assert_template "index"
117 get root_path(:query => "test")
118 assert_redirected_to search_path(:query => "test")
120 get root_path(:lat => 4, :lon => 5)
121 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=5/4/5"
123 get root_path(:lat => 4, :lon => 5, :zoom => 3)
124 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5"
126 get root_path(:layers => "T")
127 assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=T"
129 get root_path(:notes => "yes")
130 assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=N"
132 get root_path(:lat => 4, :lon => 5, :zoom => 3, :layers => "T")
133 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5&layers=T"
136 # Test the permalink redirect
138 get permalink_path(:code => "wBz3--")
139 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
141 get permalink_path(:code => "wBz3--", :m => "")
142 assert_redirected_to :controller => :site, :action => :index, :mlat => "4.8779296875", :mlon => "3.955078125", :anchor => "map=3/4.8779296875/3.955078125"
144 get permalink_path(:code => "wBz3--", :layers => "T")
145 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125&layers=T"
147 get permalink_path(:code => "wBz3--", :node => 1)
148 assert_redirected_to node_path(1, :anchor => "map=3/4.8779296875/3.955078125")
150 get permalink_path(:code => "wBz3--", :node => "x")
151 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
153 get permalink_path(:code => "wBz3--", :way => 2)
154 assert_redirected_to way_path(2, :anchor => "map=3/4.8779296875/3.955078125")
156 get permalink_path(:code => "wBz3--", :way => "x")
157 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
159 get permalink_path(:code => "wBz3--", :relation => 3)
160 assert_redirected_to relation_path(3, :anchor => "map=3/4.8779296875/3.955078125")
162 get permalink_path(:code => "wBz3--", :relation => "x")
163 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
165 get permalink_path(:code => "wBz3--", :changeset => 4)
166 assert_redirected_to changeset_path(4, :anchor => "map=3/4.8779296875/3.955078125")
168 get permalink_path(:code => "wBz3--", :changeset => "x")
169 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
172 # Test the edit page redirects when you aren't logged in
176 assert_redirected_to login_path(:referer => "/edit")
179 # Test the error when trying to edit without public edits
180 def test_edit_non_public
181 session_for(create(:user, :data_public => false))
185 assert_response :success
186 assert_template "edit"
187 assert_select "a[href='https://wiki.openstreetmap.org/wiki/Anonymous_edits']"
190 # Test the right editor gets used when the user hasn't set a preference
191 def test_edit_without_preference
192 session_for(create(:user))
196 assert_response :success
197 assert_template "edit"
198 assert_template :partial => "_#{Settings.default_editor}", :count => 1
201 # Test the right editor gets used when the user has set a preference
202 def test_edit_with_preference
204 user.preferred_editor = "id"
209 assert_response :success
210 assert_template "edit"
211 assert_template :partial => "_id", :count => 1
213 user.preferred_editor = "potlatch2"
217 assert_response :success
218 assert_template "edit"
219 assert_template :partial => "_potlatch2", :count => 1
221 user.preferred_editor = "potlatch"
225 assert_response :success
226 assert_template "edit"
227 assert_template :partial => "_potlatch", :count => 1
229 user.preferred_editor = "remote"
233 assert_response :success
234 assert_template "index"
237 # Test the right editor gets used when the URL has an override
238 def test_edit_with_override
239 session_for(create(:user))
241 get edit_path(:editor => "id")
242 assert_response :success
243 assert_template "edit"
244 assert_template :partial => "_id", :count => 1
246 get edit_path(:editor => "potlatch2")
247 assert_response :success
248 assert_template "edit"
249 assert_template :partial => "_potlatch2", :count => 1
251 get edit_path(:editor => "potlatch")
252 assert_response :success
253 assert_template "edit"
254 assert_template :partial => "_potlatch", :count => 1
256 get edit_path(:editor => "remote")
257 assert_response :success
258 assert_template "index"
261 # Test editing a specific node
262 def test_edit_with_node
264 node = create(:node, :lat => 1.0, :lon => 1.0)
267 get edit_path(:node => node.id)
269 assert_response :success
270 assert_template "edit"
271 assert_in_delta(1.0, assigns(:lat))
272 assert_in_delta(1.0, assigns(:lon))
273 assert_equal 18, assigns(:zoom)
276 # Test editing inaccessible nodes
277 def test_edit_with_inaccessible_nodes
279 deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false)
282 get edit_path(:node => 99999)
283 assert_response :success
284 assert_template "edit"
285 assert_nil assigns(:lat)
286 assert_nil assigns(:lon)
287 assert_nil assigns(:zoom)
289 get edit_path(:node => deleted_node.id)
290 assert_response :success
291 assert_template "edit"
292 assert_nil assigns(:lat)
293 assert_nil assigns(:lon)
294 assert_nil assigns(:zoom)
297 # Test editing a specific way
298 def test_edit_with_way
300 node = create(:node, :lat => 3, :lon => 3)
302 create(:way_node, :node => node, :way => way)
305 get edit_path(:way => way.id)
306 assert_response :success
307 assert_template "edit"
308 assert_in_delta(3.0, assigns(:lat))
309 assert_in_delta(3.0, assigns(:lon))
310 assert_equal 17, assigns(:zoom)
313 # Test editing inaccessible ways
314 def test_edit_with_inaccessible_ways
316 deleted_way = create(:way, :visible => false)
319 get edit_path(:way => 99999)
320 assert_response :success
321 assert_template "edit"
322 assert_nil assigns(:lat)
323 assert_nil assigns(:lon)
324 assert_nil assigns(:zoom)
326 get edit_path(:way => deleted_way.id)
327 assert_response :success
328 assert_template "edit"
329 assert_nil assigns(:lat)
330 assert_nil assigns(:lon)
331 assert_nil assigns(:zoom)
334 # Test editing a specific note
335 def test_edit_with_note
337 note = create(:note) do |n|
338 n.comments.create(:author_id => user.id)
342 get edit_path(:note => note.id)
343 assert_response :success
344 assert_template "edit"
345 assert_in_delta(1.0, assigns(:lat))
346 assert_in_delta(1.0, assigns(:lon))
347 assert_equal 17, assigns(:zoom)
350 # Test editing inaccessible notes
351 def test_edit_with_inaccessible_notes
353 deleted_note = create(:note, :status => "hidden") do |n|
354 n.comments.create(:author_id => user.id)
358 get edit_path(:note => 99999)
359 assert_response :success
360 assert_template "edit"
361 assert_nil assigns(:lat)
362 assert_nil assigns(:lon)
363 assert_nil assigns(:zoom)
365 get edit_path(:note => deleted_note.id)
366 assert_response :success
367 assert_template "edit"
368 assert_nil assigns(:lat)
369 assert_nil assigns(:lon)
370 assert_nil assigns(:zoom)
373 # Test editing a specific GPX trace
374 def test_edit_with_gpx
376 gpx = create(:trace, :user => user, :latitude => 1, :longitude => 1)
379 get edit_path(:gpx => gpx.id)
380 assert_response :success
381 assert_template "edit"
382 assert_in_delta(1.0, assigns(:lat))
383 assert_in_delta(1.0, assigns(:lon))
384 assert_equal 16, assigns(:zoom)
387 # Test editing inaccessible GPX traces
388 def test_edit_with_inaccessible_gpxes
390 deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1)
391 private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "trackable")
394 get edit_path(:gpx => 99999)
395 assert_response :success
396 assert_template "edit"
397 assert_nil assigns(:lat)
398 assert_nil assigns(:lon)
399 assert_nil assigns(:zoom)
401 get edit_path(:gpx => deleted_gpx.id)
402 assert_response :success
403 assert_template "edit"
404 assert_nil assigns(:lat)
405 assert_nil assigns(:lon)
406 assert_nil assigns(:zoom)
408 get edit_path(:gpx => private_gpx.id)
409 assert_response :success
410 assert_template "edit"
411 assert_nil assigns(:lat)
412 assert_nil assigns(:lon)
413 assert_nil assigns(:zoom)
416 # Test the edit page redirects
417 def test_edit_redirect
418 get edit_path(:lat => 4, :lon => 5)
419 assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=5/4/5"
421 get edit_path(:lat => 4, :lon => 5, :zoom => 3)
422 assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
424 get edit_path(:lat => 4, :lon => 5, :zoom => 3, :editor => "id")
425 assert_redirected_to :controller => :site, :action => :edit, :editor => "id", :anchor => "map=3/4/5"
428 # Test the copyright page
431 assert_response :success
432 assert_template "copyright"
433 assert_select "div[lang='en'][dir='ltr']"
435 get copyright_path(:copyright_locale => "fr")
436 assert_response :success
437 assert_template "copyright"
438 assert_select "div[lang='fr'][dir='ltr']"
440 get copyright_path(:copyright_locale => "ar")
441 assert_response :success
442 assert_template "copyright"
443 assert_select "div[lang='ar'][dir='rtl']"
446 # Test the welcome page
449 assert_redirected_to login_path(:referer => "/welcome")
451 session_for(create(:user))
453 assert_response :success
454 assert_template "welcome"
457 # Test the fixthemap page
460 assert_response :success
461 assert_template "fixthemap"
467 assert_response :success
468 assert_template "help"
471 # Test the about page
474 assert_response :success
475 assert_template "about"
476 assert_select "div[lang='en'][dir='ltr']"
478 get about_path(:about_locale => "fr")
479 assert_response :success
480 assert_template "about"
481 assert_select "div[lang='fr'][dir='ltr']"
483 get about_path(:about_locale => "ar")
484 assert_response :success
485 assert_template "about"
486 assert_select "div[lang='ar'][dir='rtl']"
488 # Page should still render even with incorrect locale
489 get about_path(:about_locale => "zzz")
490 assert_response :success
491 assert_template "about"
494 # Test the export page
497 assert_response :success
498 assert_template "export"
499 assert_template :layout => "map"
501 get export_path, :headers => { "Turbo-Frame" => "sidebar_content_frame" }
502 assert_response :success
503 assert_template "export"
504 assert_template :layout => "xhr"
507 # Test that the export page lists a bulk download link for each source
508 def test_export_download_sources
510 assert_response :success
512 %w[overpass planet geofabrik other].each do |source|
513 url = I18n.t("site.export.too_large.#{source}.url")
514 title = I18n.t("site.export.too_large.#{source}.title")
516 assert_select "dl dt a#export_#{source}[href='#{url}']", title
520 # Test the offline page
523 assert_response :success
524 assert_select ".alert-warning"
527 # Test the rich text preview
529 post preview_path(:type => "html"), :xhr => true
530 assert_response :success
532 post preview_path(:type => "markdown"), :xhr => true
533 assert_response :success
535 post preview_path(:type => "text"), :xhr => true
536 assert_response :success
541 session_for(create(:user))
545 assert_response :success
547 assert_template :layout => false
550 # Test the id frame when not logged in
551 def test_id_without_login
554 assert_redirected_to login_path(:referer => "/id")