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 def test_edit_remote_unsupported_format
238 session_for(create(:user))
240 get edit_path(:editor => "remote", :format => "jpg")
242 assert_response :not_acceptable
245 # Test the right editor gets used when the URL has an override
246 def test_edit_with_override
247 session_for(create(:user))
249 get edit_path(:editor => "id")
250 assert_response :success
251 assert_template "edit"
252 assert_template :partial => "_id", :count => 1
254 get edit_path(:editor => "potlatch2")
255 assert_response :success
256 assert_template "edit"
257 assert_template :partial => "_potlatch2", :count => 1
259 get edit_path(:editor => "potlatch")
260 assert_response :success
261 assert_template "edit"
262 assert_template :partial => "_potlatch", :count => 1
264 get edit_path(:editor => "remote")
265 assert_response :success
266 assert_template "index"
268 get edit_path(:editor => "invalid")
269 assert_response :bad_request
272 # Test editing a specific node
273 def test_edit_with_node
275 node = create(:node, :lat => 1.0, :lon => 1.0)
278 get edit_path(:node => node.id)
280 assert_response :success
281 assert_template "edit"
282 assert_in_delta(1.0, assigns(:lat))
283 assert_in_delta(1.0, assigns(:lon))
284 assert_equal 18, assigns(:zoom)
287 # Test editing inaccessible nodes
288 def test_edit_with_inaccessible_nodes
290 deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false)
293 get edit_path(:node => 99999)
294 assert_response :success
295 assert_template "edit"
296 assert_nil assigns(:lat)
297 assert_nil assigns(:lon)
298 assert_nil assigns(:zoom)
300 get edit_path(:node => deleted_node.id)
301 assert_response :success
302 assert_template "edit"
303 assert_nil assigns(:lat)
304 assert_nil assigns(:lon)
305 assert_nil assigns(:zoom)
308 # Test editing a specific way
309 def test_edit_with_way
311 node = create(:node, :lat => 3, :lon => 3)
313 create(:way_node, :node => node, :way => way)
316 get edit_path(:way => way.id)
317 assert_response :success
318 assert_template "edit"
319 assert_in_delta(3.0, assigns(:lat))
320 assert_in_delta(3.0, assigns(:lon))
321 assert_equal 17, assigns(:zoom)
324 # Test editing inaccessible ways
325 def test_edit_with_inaccessible_ways
327 deleted_way = create(:way, :visible => false)
330 get edit_path(:way => 99999)
331 assert_response :success
332 assert_template "edit"
333 assert_nil assigns(:lat)
334 assert_nil assigns(:lon)
335 assert_nil assigns(:zoom)
337 get edit_path(:way => deleted_way.id)
338 assert_response :success
339 assert_template "edit"
340 assert_nil assigns(:lat)
341 assert_nil assigns(:lon)
342 assert_nil assigns(:zoom)
345 # Test editing a specific note
346 def test_edit_with_note
348 note = create(:note) do |n|
349 n.comments.create(:author_id => user.id)
353 get edit_path(:note => note.id)
354 assert_response :success
355 assert_template "edit"
356 assert_in_delta(1.0, assigns(:lat))
357 assert_in_delta(1.0, assigns(:lon))
358 assert_equal 17, assigns(:zoom)
361 # Test editing inaccessible notes
362 def test_edit_with_inaccessible_notes
364 deleted_note = create(:note, :status => "hidden") do |n|
365 n.comments.create(:author_id => user.id)
369 get edit_path(:note => 99999)
370 assert_response :success
371 assert_template "edit"
372 assert_nil assigns(:lat)
373 assert_nil assigns(:lon)
374 assert_nil assigns(:zoom)
376 get edit_path(:note => deleted_note.id)
377 assert_response :success
378 assert_template "edit"
379 assert_nil assigns(:lat)
380 assert_nil assigns(:lon)
381 assert_nil assigns(:zoom)
384 # Test editing a specific GPX trace
385 def test_edit_with_gpx
387 gpx = create(:trace, :user => user, :latitude => 1, :longitude => 1)
390 get edit_path(:gpx => gpx.id)
391 assert_response :success
392 assert_template "edit"
393 assert_in_delta(1.0, assigns(:lat))
394 assert_in_delta(1.0, assigns(:lon))
395 assert_equal 16, assigns(:zoom)
398 # Test editing inaccessible GPX traces
399 def test_edit_with_inaccessible_gpxes
401 deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1)
402 private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "trackable")
405 get edit_path(:gpx => 99999)
406 assert_response :success
407 assert_template "edit"
408 assert_nil assigns(:lat)
409 assert_nil assigns(:lon)
410 assert_nil assigns(:zoom)
412 get edit_path(:gpx => deleted_gpx.id)
413 assert_response :success
414 assert_template "edit"
415 assert_nil assigns(:lat)
416 assert_nil assigns(:lon)
417 assert_nil assigns(:zoom)
419 get edit_path(:gpx => private_gpx.id)
420 assert_response :success
421 assert_template "edit"
422 assert_nil assigns(:lat)
423 assert_nil assigns(:lon)
424 assert_nil assigns(:zoom)
427 # Test the edit page redirects
428 def test_edit_redirect
429 get edit_path(:lat => 4, :lon => 5)
430 assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=5/4/5"
432 get edit_path(:lat => 4, :lon => 5, :zoom => 3)
433 assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
435 get edit_path(:lat => 4, :lon => 5, :zoom => 3, :editor => "id")
436 assert_redirected_to :controller => :site, :action => :edit, :editor => "id", :anchor => "map=3/4/5"
439 # Test the copyright page
442 assert_response :success
443 assert_template "copyright"
444 assert_select "div[lang='en'][dir='ltr']"
446 get copyright_path(:copyright_locale => "fr")
447 assert_response :success
448 assert_template "copyright"
449 assert_select "div[lang='fr'][dir='ltr']"
451 get copyright_path(:copyright_locale => "ar")
452 assert_response :success
453 assert_template "copyright"
454 assert_select "div[lang='ar'][dir='rtl']"
457 # Test the welcome page
460 assert_redirected_to login_path(:referer => "/welcome")
462 session_for(create(:user))
464 assert_response :success
465 assert_template "welcome"
468 # Test the fixthemap page
471 assert_response :success
472 assert_template "fixthemap"
478 assert_response :success
479 assert_template "help"
482 # Test the about page
485 assert_response :success
486 assert_template "about"
487 assert_select "div[lang='en'][dir='ltr']"
489 get about_path(:about_locale => "fr")
490 assert_response :success
491 assert_template "about"
492 assert_select "div[lang='fr'][dir='ltr']"
494 get about_path(:about_locale => "ar")
495 assert_response :success
496 assert_template "about"
497 assert_select "div[lang='ar'][dir='rtl']"
499 # Page should still render even with incorrect locale
500 get about_path(:about_locale => "zzz")
501 assert_response :success
502 assert_template "about"
505 # Test the export page
508 assert_response :success
509 assert_template "export"
510 assert_template :layout => "map"
512 get export_path, :headers => { "Turbo-Frame" => "sidebar_content_frame" }
513 assert_response :success
514 assert_template "export"
515 assert_template :layout => "xhr"
518 # Test that the export page lists a bulk download link for each source
519 def test_export_download_sources
521 assert_response :success
523 %w[overpass planet geofabrik other].each do |source|
524 url = I18n.t("site.export.too_large.#{source}.url")
525 title = I18n.t("site.export.too_large.#{source}.title")
527 assert_select "dl dt a#export_#{source}[href='#{url}']", title
531 # Test the offline page
534 assert_response :success
535 assert_select ".alert-warning"
538 # Test the rich text preview
540 post preview_path(:type => "html"), :xhr => true
541 assert_response :success
543 post preview_path(:type => "markdown"), :xhr => true
544 assert_response :success
546 post preview_path(:type => "text"), :xhr => true
547 assert_response :success
552 session_for(create(:user))
556 assert_response :success
558 assert_template :layout => false
561 # Test the id frame when not logged in
562 def test_id_without_login
565 assert_redirected_to login_path(:referer => "/id")