]> git.openstreetmap.org Git - rails.git/blob - test/controllers/site_controller_test.rb
Merge remote-tracking branch 'upstream/pull/7633'
[rails.git] / test / controllers / site_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class SiteControllerTest < ActionDispatch::IntegrationTest
6   ##
7   # test all routes which lead to this controller
8   def test_routes
9     assert_routing(
10       { :path => "/", :method => :get },
11       { :controller => "site", :action => "index" }
12     )
13     assert_routing(
14       { :path => "/", :method => :post },
15       { :controller => "site", :action => "index" }
16     )
17     assert_routing(
18       { :path => "/edit", :method => :get },
19       { :controller => "site", :action => "edit" }
20     )
21     assert_recognizes(
22       { :controller => "site", :action => "edit", :format => "html" },
23       { :path => "/edit.html", :method => :get }
24     )
25     assert_routing(
26       { :path => "/copyright", :method => :get },
27       { :controller => "site", :action => "copyright" }
28     )
29     assert_routing(
30       { :path => "/copyright/locale", :method => :get },
31       { :controller => "site", :action => "copyright", :copyright_locale => "locale" }
32     )
33     assert_routing(
34       { :path => "/welcome", :method => :get },
35       { :controller => "site", :action => "welcome" }
36     )
37     assert_routing(
38       { :path => "/fixthemap", :method => :get },
39       { :controller => "site", :action => "fixthemap" }
40     )
41     assert_routing(
42       { :path => "/help", :method => :get },
43       { :controller => "site", :action => "help" }
44     )
45     assert_routing(
46       { :path => "/about", :method => :get },
47       { :controller => "site", :action => "about" }
48     )
49     assert_routing(
50       { :path => "/about/locale", :method => :get },
51       { :controller => "site", :action => "about", :about_locale => "locale" }
52     )
53     assert_routing(
54       { :path => "/export", :method => :get },
55       { :controller => "site", :action => "export" }
56     )
57     assert_recognizes(
58       { :controller => "site", :action => "export", :format => "html" },
59       { :path => "/export.html", :method => :get }
60     )
61     assert_routing(
62       { :path => "/offline", :method => :get },
63       { :controller => "site", :action => "offline" }
64     )
65     assert_routing(
66       { :path => "/go/shortcode", :method => :get },
67       { :controller => "site", :action => "permalink", :code => "shortcode" }
68     )
69     assert_routing(
70       { :path => "/preview/typename", :method => :post },
71       { :controller => "site", :action => "preview", :type => "typename" }
72     )
73     assert_routing(
74       { :path => "/id", :method => :get },
75       { :controller => "site", :action => "id" }
76     )
77   end
78
79   # Test the index page
80   def test_index
81     get root_path
82
83     assert_response :success
84     assert_template "index"
85   end
86
87   # Test the index page redirects
88   def test_index_redirect
89     get root_path(:node => 123)
90     assert_redirected_to node_path(123)
91
92     get root_path(:node => "x")
93     assert_response :success
94     assert_template "index"
95
96     get root_path(:way => 123)
97     assert_redirected_to way_path(123)
98
99     get root_path(:way => "x")
100     assert_response :success
101     assert_template "index"
102
103     get root_path(:relation => 123)
104     assert_redirected_to relation_path(123)
105
106     get root_path(:relation => "x")
107     assert_response :success
108     assert_template "index"
109
110     get root_path(:note => 123)
111     assert_redirected_to :controller => :notes, :action => :show, :id => 123
112
113     get root_path(:note => "x")
114     assert_response :success
115     assert_template "index"
116
117     get root_path(:query => "test")
118     assert_redirected_to search_path(:query => "test")
119
120     get root_path(:lat => 4, :lon => 5)
121     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=5/4/5"
122
123     get root_path(:lat => 4, :lon => 5, :zoom => 3)
124     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5"
125
126     get root_path(:layers => "T")
127     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=T"
128
129     get root_path(:notes => "yes")
130     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=N"
131
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"
134   end
135
136   # Test the permalink redirect
137   def test_permalink
138     get permalink_path(:code => "wBz3--")
139     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
140
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"
143
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"
146
147     get permalink_path(:code => "wBz3--", :node => 1)
148     assert_redirected_to node_path(1, :anchor => "map=3/4.8779296875/3.955078125")
149
150     get permalink_path(:code => "wBz3--", :node => "x")
151     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
152
153     get permalink_path(:code => "wBz3--", :way => 2)
154     assert_redirected_to way_path(2, :anchor => "map=3/4.8779296875/3.955078125")
155
156     get permalink_path(:code => "wBz3--", :way => "x")
157     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
158
159     get permalink_path(:code => "wBz3--", :relation => 3)
160     assert_redirected_to relation_path(3, :anchor => "map=3/4.8779296875/3.955078125")
161
162     get permalink_path(:code => "wBz3--", :relation => "x")
163     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
164
165     get permalink_path(:code => "wBz3--", :changeset => 4)
166     assert_redirected_to changeset_path(4, :anchor => "map=3/4.8779296875/3.955078125")
167
168     get permalink_path(:code => "wBz3--", :changeset => "x")
169     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
170   end
171
172   # Test the edit page redirects when you aren't logged in
173   def test_edit
174     get edit_path
175
176     assert_redirected_to login_path(:referer => "/edit")
177   end
178
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))
182
183     get edit_path
184
185     assert_response :success
186     assert_template "edit"
187     assert_select "a[href='https://wiki.openstreetmap.org/wiki/Anonymous_edits']"
188   end
189
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))
193
194     get edit_path
195
196     assert_response :success
197     assert_template "edit"
198     assert_template :partial => "_#{Settings.default_editor}", :count => 1
199   end
200
201   # Test the right editor gets used when the user has set a preference
202   def test_edit_with_preference
203     user = create(:user)
204     user.preferred_editor = "id"
205     user.save!
206     session_for(user)
207
208     get edit_path
209     assert_response :success
210     assert_template "edit"
211     assert_template :partial => "_id", :count => 1
212
213     user.preferred_editor = "potlatch2"
214     user.save!
215
216     get edit_path
217     assert_response :success
218     assert_template "edit"
219     assert_template :partial => "_potlatch2", :count => 1
220
221     user.preferred_editor = "potlatch"
222     user.save!
223
224     get edit_path
225     assert_response :success
226     assert_template "edit"
227     assert_template :partial => "_potlatch", :count => 1
228
229     user.preferred_editor = "remote"
230     user.save!
231
232     get edit_path
233     assert_response :success
234     assert_template "index"
235   end
236
237   def test_edit_remote_unsupported_format
238     session_for(create(:user))
239
240     get edit_path(:editor => "remote", :format => "jpg")
241
242     assert_response :not_acceptable
243   end
244
245   # Test the right editor gets used when the URL has an override
246   def test_edit_with_override
247     session_for(create(:user))
248
249     get edit_path(:editor => "id")
250     assert_response :success
251     assert_template "edit"
252     assert_template :partial => "_id", :count => 1
253
254     get edit_path(:editor => "potlatch2")
255     assert_response :success
256     assert_template "edit"
257     assert_template :partial => "_potlatch2", :count => 1
258
259     get edit_path(:editor => "potlatch")
260     assert_response :success
261     assert_template "edit"
262     assert_template :partial => "_potlatch", :count => 1
263
264     get edit_path(:editor => "remote")
265     assert_response :success
266     assert_template "index"
267
268     get edit_path(:editor => "invalid")
269     assert_response :bad_request
270   end
271
272   # Test editing a specific node
273   def test_edit_with_node
274     user = create(:user)
275     node = create(:node, :lat => 1.0, :lon => 1.0)
276     session_for(user)
277
278     get edit_path(:node => node.id)
279
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)
285   end
286
287   # Test editing inaccessible nodes
288   def test_edit_with_inaccessible_nodes
289     user = create(:user)
290     deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false)
291     session_for(user)
292
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)
299
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)
306   end
307
308   # Test editing a specific way
309   def test_edit_with_way
310     user = create(:user)
311     node = create(:node, :lat => 3, :lon => 3)
312     way = create(:way)
313     create(:way_node, :node => node, :way => way)
314     session_for(user)
315
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)
322   end
323
324   # Test editing inaccessible ways
325   def test_edit_with_inaccessible_ways
326     user = create(:user)
327     deleted_way = create(:way, :visible => false)
328     session_for(user)
329
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)
336
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)
343   end
344
345   # Test editing a specific note
346   def test_edit_with_note
347     user = create(:user)
348     note = create(:note) do |n|
349       n.comments.create(:author_id => user.id)
350     end
351     session_for(user)
352
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)
359   end
360
361   # Test editing inaccessible notes
362   def test_edit_with_inaccessible_notes
363     user = create(:user)
364     deleted_note = create(:note, :status => "hidden") do |n|
365       n.comments.create(:author_id => user.id)
366     end
367     session_for(user)
368
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)
375
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)
382   end
383
384   # Test editing a specific GPX trace
385   def test_edit_with_gpx
386     user = create(:user)
387     gpx = create(:trace, :user => user, :latitude => 1, :longitude => 1)
388     session_for(user)
389
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)
396   end
397
398   # Test editing inaccessible GPX traces
399   def test_edit_with_inaccessible_gpxes
400     user = create(:user)
401     deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1)
402     private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "trackable")
403     session_for(user)
404
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)
411
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)
418
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)
425   end
426
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"
431
432     get edit_path(:lat => 4, :lon => 5, :zoom => 3)
433     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
434
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"
437   end
438
439   # Test the copyright page
440   def test_copyright
441     get copyright_path
442     assert_response :success
443     assert_template "copyright"
444     assert_select "div[lang='en'][dir='ltr']"
445
446     get copyright_path(:copyright_locale => "fr")
447     assert_response :success
448     assert_template "copyright"
449     assert_select "div[lang='fr'][dir='ltr']"
450
451     get copyright_path(:copyright_locale => "ar")
452     assert_response :success
453     assert_template "copyright"
454     assert_select "div[lang='ar'][dir='rtl']"
455   end
456
457   # Test the welcome page
458   def test_welcome
459     get welcome_path
460     assert_redirected_to login_path(:referer => "/welcome")
461
462     session_for(create(:user))
463     get welcome_path
464     assert_response :success
465     assert_template "welcome"
466   end
467
468   # Test the fixthemap page
469   def test_fixthemap
470     get fixthemap_path
471     assert_response :success
472     assert_template "fixthemap"
473   end
474
475   # Test the help page
476   def test_help
477     get help_path
478     assert_response :success
479     assert_template "help"
480   end
481
482   # Test the about page
483   def test_about
484     get about_path
485     assert_response :success
486     assert_template "about"
487     assert_select "div[lang='en'][dir='ltr']"
488
489     get about_path(:about_locale => "fr")
490     assert_response :success
491     assert_template "about"
492     assert_select "div[lang='fr'][dir='ltr']"
493
494     get about_path(:about_locale => "ar")
495     assert_response :success
496     assert_template "about"
497     assert_select "div[lang='ar'][dir='rtl']"
498
499     # Page should still render even with incorrect locale
500     get about_path(:about_locale => "zzz")
501     assert_response :success
502     assert_template "about"
503   end
504
505   # Test the export page
506   def test_export
507     get export_path
508     assert_response :success
509     assert_template "export"
510     assert_template :layout => "map"
511
512     get export_path, :headers => { "Turbo-Frame" => "sidebar_content_frame" }
513     assert_response :success
514     assert_template "export"
515     assert_template :layout => "xhr"
516   end
517
518   # Test that the export page lists a bulk download link for each source
519   def test_export_download_sources
520     get export_path
521     assert_response :success
522
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")
526
527       assert_select "dl dt a#export_#{source}[href='#{url}']", title
528     end
529   end
530
531   # Test the offline page
532   def test_offline
533     get offline_path
534     assert_response :success
535     assert_select ".alert-warning"
536   end
537
538   # Test the rich text preview
539   def test_preview
540     post preview_path(:type => "html"), :xhr => true
541     assert_response :success
542
543     post preview_path(:type => "markdown"), :xhr => true
544     assert_response :success
545
546     post preview_path(:type => "text"), :xhr => true
547     assert_response :success
548   end
549
550   # Test the id frame
551   def test_id
552     session_for(create(:user))
553
554     get id_path
555
556     assert_response :success
557     assert_template "id"
558     assert_template :layout => false
559   end
560
561   # Test the id frame when not logged in
562   def test_id_without_login
563     get id_path
564
565     assert_redirected_to login_path(:referer => "/id")
566   end
567 end