]> git.openstreetmap.org Git - rails.git/blob - test/controllers/site_controller_test.rb
Drop unused description parameter
[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   # Test the right editor gets used when the URL has an override
238   def test_edit_with_override
239     session_for(create(:user))
240
241     get edit_path(:editor => "id")
242     assert_response :success
243     assert_template "edit"
244     assert_template :partial => "_id", :count => 1
245
246     get edit_path(:editor => "potlatch2")
247     assert_response :success
248     assert_template "edit"
249     assert_template :partial => "_potlatch2", :count => 1
250
251     get edit_path(:editor => "potlatch")
252     assert_response :success
253     assert_template "edit"
254     assert_template :partial => "_potlatch", :count => 1
255
256     get edit_path(:editor => "remote")
257     assert_response :success
258     assert_template "index"
259   end
260
261   # Test editing a specific node
262   def test_edit_with_node
263     user = create(:user)
264     node = create(:node, :lat => 1.0, :lon => 1.0)
265     session_for(user)
266
267     get edit_path(:node => node.id)
268
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)
274   end
275
276   # Test editing inaccessible nodes
277   def test_edit_with_inaccessible_nodes
278     user = create(:user)
279     deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false)
280     session_for(user)
281
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)
288
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)
295   end
296
297   # Test editing a specific way
298   def test_edit_with_way
299     user = create(:user)
300     node = create(:node, :lat => 3, :lon => 3)
301     way = create(:way)
302     create(:way_node, :node => node, :way => way)
303     session_for(user)
304
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)
311   end
312
313   # Test editing inaccessible ways
314   def test_edit_with_inaccessible_ways
315     user = create(:user)
316     deleted_way = create(:way, :visible => false)
317     session_for(user)
318
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)
325
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)
332   end
333
334   # Test editing a specific note
335   def test_edit_with_note
336     user = create(:user)
337     note = create(:note) do |n|
338       n.comments.create(:author_id => user.id)
339     end
340     session_for(user)
341
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)
348   end
349
350   # Test editing inaccessible notes
351   def test_edit_with_inaccessible_notes
352     user = create(:user)
353     deleted_note = create(:note, :status => "hidden") do |n|
354       n.comments.create(:author_id => user.id)
355     end
356     session_for(user)
357
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)
364
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)
371   end
372
373   # Test editing a specific GPX trace
374   def test_edit_with_gpx
375     user = create(:user)
376     gpx = create(:trace, :user => user, :latitude => 1, :longitude => 1)
377     session_for(user)
378
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)
385   end
386
387   # Test editing inaccessible GPX traces
388   def test_edit_with_inaccessible_gpxes
389     user = create(:user)
390     deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1)
391     private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "trackable")
392     session_for(user)
393
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)
400
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)
407
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)
414   end
415
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"
420
421     get edit_path(:lat => 4, :lon => 5, :zoom => 3)
422     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
423
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"
426   end
427
428   # Test the copyright page
429   def test_copyright
430     get copyright_path
431     assert_response :success
432     assert_template "copyright"
433     assert_select "div[lang='en'][dir='ltr']"
434
435     get copyright_path(:copyright_locale => "fr")
436     assert_response :success
437     assert_template "copyright"
438     assert_select "div[lang='fr'][dir='ltr']"
439
440     get copyright_path(:copyright_locale => "ar")
441     assert_response :success
442     assert_template "copyright"
443     assert_select "div[lang='ar'][dir='rtl']"
444   end
445
446   # Test the welcome page
447   def test_welcome
448     get welcome_path
449     assert_redirected_to login_path(:referer => "/welcome")
450
451     session_for(create(:user))
452     get welcome_path
453     assert_response :success
454     assert_template "welcome"
455   end
456
457   # Test the fixthemap page
458   def test_fixthemap
459     get fixthemap_path
460     assert_response :success
461     assert_template "fixthemap"
462   end
463
464   # Test the help page
465   def test_help
466     get help_path
467     assert_response :success
468     assert_template "help"
469   end
470
471   # Test the about page
472   def test_about
473     get about_path
474     assert_response :success
475     assert_template "about"
476     assert_select "div[lang='en'][dir='ltr']"
477
478     get about_path(:about_locale => "fr")
479     assert_response :success
480     assert_template "about"
481     assert_select "div[lang='fr'][dir='ltr']"
482
483     get about_path(:about_locale => "ar")
484     assert_response :success
485     assert_template "about"
486     assert_select "div[lang='ar'][dir='rtl']"
487
488     # Page should still render even with incorrect locale
489     get about_path(:about_locale => "zzz")
490     assert_response :success
491     assert_template "about"
492   end
493
494   # Test the export page
495   def test_export
496     get export_path
497     assert_response :success
498     assert_template "export"
499     assert_template :layout => "map"
500
501     get export_path, :headers => { "Turbo-Frame" => "sidebar_content_frame" }
502     assert_response :success
503     assert_template "export"
504     assert_template :layout => "xhr"
505   end
506
507   # Test that the export page lists a bulk download link for each source
508   def test_export_download_sources
509     get export_path
510     assert_response :success
511
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")
515
516       assert_select "dl dt a#export_#{source}[href='#{url}']", title
517     end
518   end
519
520   # Test the offline page
521   def test_offline
522     get offline_path
523     assert_response :success
524     assert_select ".alert-warning"
525   end
526
527   # Test the rich text preview
528   def test_preview
529     post preview_path(:type => "html"), :xhr => true
530     assert_response :success
531
532     post preview_path(:type => "markdown"), :xhr => true
533     assert_response :success
534
535     post preview_path(:type => "text"), :xhr => true
536     assert_response :success
537   end
538
539   # Test the id frame
540   def test_id
541     session_for(create(:user))
542
543     get id_path
544
545     assert_response :success
546     assert_template "id"
547     assert_template :layout => false
548   end
549
550   # Test the id frame when not logged in
551   def test_id_without_login
552     get id_path
553
554     assert_redirected_to login_path(:referer => "/id")
555   end
556 end