]> git.openstreetmap.org Git - rails.git/blob - test/controllers/site_controller_test.rb
Add frozen_string_literal comments to ruby files
[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(:way => 123)
93     assert_redirected_to way_path(123)
94
95     get root_path(:relation => 123)
96     assert_redirected_to relation_path(123)
97
98     get root_path(:note => 123)
99     assert_redirected_to :controller => :notes, :action => :show, :id => 123
100
101     get root_path(:query => "test")
102     assert_redirected_to search_path(:query => "test")
103
104     get root_path(:lat => 4, :lon => 5)
105     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=5/4/5"
106
107     get root_path(:lat => 4, :lon => 5, :zoom => 3)
108     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5"
109
110     get root_path(:layers => "T")
111     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=T"
112
113     get root_path(:notes => "yes")
114     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=N"
115
116     get root_path(:lat => 4, :lon => 5, :zoom => 3, :layers => "T")
117     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5&layers=T"
118   end
119
120   # Test the permalink redirect
121   def test_permalink
122     get permalink_path(:code => "wBz3--")
123     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
124
125     get permalink_path(:code => "wBz3--", :m => "")
126     assert_redirected_to :controller => :site, :action => :index, :mlat => "4.8779296875", :mlon => "3.955078125", :anchor => "map=3/4.8779296875/3.955078125"
127
128     get permalink_path(:code => "wBz3--", :layers => "T")
129     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125&layers=T"
130
131     get permalink_path(:code => "wBz3--", :node => 1)
132     assert_redirected_to node_path(1, :anchor => "map=3/4.8779296875/3.955078125")
133
134     get permalink_path(:code => "wBz3--", :way => 2)
135     assert_redirected_to way_path(2, :anchor => "map=3/4.8779296875/3.955078125")
136
137     get permalink_path(:code => "wBz3--", :relation => 3)
138     assert_redirected_to relation_path(3, :anchor => "map=3/4.8779296875/3.955078125")
139
140     get permalink_path(:code => "wBz3--", :changeset => 4)
141     assert_redirected_to changeset_path(4, :anchor => "map=3/4.8779296875/3.955078125")
142   end
143
144   # Test the edit page redirects when you aren't logged in
145   def test_edit
146     get edit_path
147
148     assert_redirected_to login_path(:referer => "/edit")
149   end
150
151   # Test the error when trying to edit without public edits
152   def test_edit_non_public
153     session_for(create(:user, :data_public => false))
154
155     get edit_path
156
157     assert_response :success
158     assert_template "edit"
159     assert_select "a[href='https://wiki.openstreetmap.org/wiki/Anonymous_edits']"
160   end
161
162   # Test the right editor gets used when the user hasn't set a preference
163   def test_edit_without_preference
164     session_for(create(:user))
165
166     get edit_path
167
168     assert_response :success
169     assert_template "edit"
170     assert_template :partial => "_#{Settings.default_editor}", :count => 1
171   end
172
173   # Test the right editor gets used when the user has set a preference
174   def test_edit_with_preference
175     user = create(:user)
176     user.preferred_editor = "id"
177     user.save!
178     session_for(user)
179
180     get edit_path
181     assert_response :success
182     assert_template "edit"
183     assert_template :partial => "_id", :count => 1
184
185     user.preferred_editor = "potlatch2"
186     user.save!
187
188     get edit_path
189     assert_response :success
190     assert_template "edit"
191     assert_template :partial => "_potlatch2", :count => 1
192
193     user.preferred_editor = "potlatch"
194     user.save!
195
196     get edit_path
197     assert_response :success
198     assert_template "edit"
199     assert_template :partial => "_potlatch", :count => 1
200
201     user.preferred_editor = "remote"
202     user.save!
203
204     get edit_path
205     assert_response :success
206     assert_template "index"
207   end
208
209   # Test the right editor gets used when the URL has an override
210   def test_edit_with_override
211     session_for(create(:user))
212
213     get edit_path(:editor => "id")
214     assert_response :success
215     assert_template "edit"
216     assert_template :partial => "_id", :count => 1
217
218     get edit_path(:editor => "potlatch2")
219     assert_response :success
220     assert_template "edit"
221     assert_template :partial => "_potlatch2", :count => 1
222
223     get edit_path(:editor => "potlatch")
224     assert_response :success
225     assert_template "edit"
226     assert_template :partial => "_potlatch", :count => 1
227
228     get edit_path(:editor => "remote")
229     assert_response :success
230     assert_template "index"
231   end
232
233   # Test editing a specific node
234   def test_edit_with_node
235     user = create(:user)
236     node = create(:node, :lat => 1.0, :lon => 1.0)
237     session_for(user)
238
239     get edit_path(:node => node.id)
240
241     assert_response :success
242     assert_template "edit"
243     assert_in_delta(1.0, assigns(:lat))
244     assert_in_delta(1.0, assigns(:lon))
245     assert_equal 18, assigns(:zoom)
246   end
247
248   # Test editing inaccessible nodes
249   def test_edit_with_inaccessible_nodes
250     user = create(:user)
251     deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false)
252     session_for(user)
253
254     get edit_path(:node => 99999)
255     assert_response :success
256     assert_template "edit"
257     assert_nil assigns(:lat)
258     assert_nil assigns(:lon)
259     assert_nil assigns(:zoom)
260
261     get edit_path(:node => deleted_node.id)
262     assert_response :success
263     assert_template "edit"
264     assert_nil assigns(:lat)
265     assert_nil assigns(:lon)
266     assert_nil assigns(:zoom)
267   end
268
269   # Test editing a specific way
270   def test_edit_with_way
271     user = create(:user)
272     node = create(:node, :lat => 3, :lon => 3)
273     way = create(:way)
274     create(:way_node, :node => node, :way => way)
275     session_for(user)
276
277     get edit_path(:way => way.id)
278     assert_response :success
279     assert_template "edit"
280     assert_in_delta(3.0, assigns(:lat))
281     assert_in_delta(3.0, assigns(:lon))
282     assert_equal 17, assigns(:zoom)
283   end
284
285   # Test editing inaccessible ways
286   def test_edit_with_inaccessible_ways
287     user = create(:user)
288     deleted_way = create(:way, :visible => false)
289     session_for(user)
290
291     get edit_path(:way => 99999)
292     assert_response :success
293     assert_template "edit"
294     assert_nil assigns(:lat)
295     assert_nil assigns(:lon)
296     assert_nil assigns(:zoom)
297
298     get edit_path(:way => deleted_way.id)
299     assert_response :success
300     assert_template "edit"
301     assert_nil assigns(:lat)
302     assert_nil assigns(:lon)
303     assert_nil assigns(:zoom)
304   end
305
306   # Test editing a specific note
307   def test_edit_with_note
308     user = create(:user)
309     note = create(:note) do |n|
310       n.comments.create(:author_id => user.id)
311     end
312     session_for(user)
313
314     get edit_path(:note => note.id)
315     assert_response :success
316     assert_template "edit"
317     assert_in_delta(1.0, assigns(:lat))
318     assert_in_delta(1.0, assigns(:lon))
319     assert_equal 17, assigns(:zoom)
320   end
321
322   # Test editing inaccessible notes
323   def test_edit_with_inaccessible_notes
324     user = create(:user)
325     deleted_note = create(:note, :status => "hidden") do |n|
326       n.comments.create(:author_id => user.id)
327     end
328     session_for(user)
329
330     get edit_path(:note => 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(:note => deleted_note.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 GPX trace
346   def test_edit_with_gpx
347     user = create(:user)
348     gpx = create(:trace, :latitude => 1, :longitude => 1)
349     session_for(user)
350
351     get edit_path(:gpx => gpx.id)
352     assert_response :success
353     assert_template "edit"
354     assert_in_delta(1.0, assigns(:lat))
355     assert_in_delta(1.0, assigns(:lon))
356     assert_equal 16, assigns(:zoom)
357   end
358
359   # Test editing inaccessible GPX traces
360   def test_edit_with_inaccessible_gpxes
361     user = create(:user)
362     deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1)
363     private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "private")
364     session_for(user)
365
366     get edit_path(:gpx => 99999)
367     assert_response :success
368     assert_template "edit"
369     assert_nil assigns(:lat)
370     assert_nil assigns(:lon)
371     assert_nil assigns(:zoom)
372
373     get edit_path(:gpx => deleted_gpx.id)
374     assert_response :success
375     assert_template "edit"
376     assert_nil assigns(:lat)
377     assert_nil assigns(:lon)
378     assert_nil assigns(:zoom)
379
380     get edit_path(:gpx => private_gpx.id)
381     assert_response :success
382     assert_template "edit"
383     assert_nil assigns(:lat)
384     assert_nil assigns(:lon)
385     assert_nil assigns(:zoom)
386   end
387
388   # Test the edit page redirects
389   def test_edit_redirect
390     get edit_path(:lat => 4, :lon => 5)
391     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=5/4/5"
392
393     get edit_path(:lat => 4, :lon => 5, :zoom => 3)
394     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
395
396     get edit_path(:lat => 4, :lon => 5, :zoom => 3, :editor => "id")
397     assert_redirected_to :controller => :site, :action => :edit, :editor => "id", :anchor => "map=3/4/5"
398   end
399
400   # Test the copyright page
401   def test_copyright
402     get copyright_path
403     assert_response :success
404     assert_template "copyright"
405     assert_select "div[lang='en'][dir='ltr']"
406
407     get copyright_path(:copyright_locale => "fr")
408     assert_response :success
409     assert_template "copyright"
410     assert_select "div[lang='fr'][dir='ltr']"
411
412     get copyright_path(:copyright_locale => "ar")
413     assert_response :success
414     assert_template "copyright"
415     assert_select "div[lang='ar'][dir='rtl']"
416   end
417
418   # Test the welcome page
419   def test_welcome
420     get welcome_path
421     assert_redirected_to login_path(:referer => "/welcome")
422
423     session_for(create(:user))
424     get welcome_path
425     assert_response :success
426     assert_template "welcome"
427   end
428
429   # Test the fixthemap page
430   def test_fixthemap
431     get fixthemap_path
432     assert_response :success
433     assert_template "fixthemap"
434   end
435
436   # Test the help page
437   def test_help
438     get help_path
439     assert_response :success
440     assert_template "help"
441   end
442
443   # Test the about page
444   def test_about
445     get about_path
446     assert_response :success
447     assert_template "about"
448     assert_select "div[lang='en'][dir='ltr']"
449
450     get about_path(:about_locale => "fr")
451     assert_response :success
452     assert_template "about"
453     assert_select "div[lang='fr'][dir='ltr']"
454
455     get about_path(:about_locale => "ar")
456     assert_response :success
457     assert_template "about"
458     assert_select "div[lang='ar'][dir='rtl']"
459
460     # Page should still render even with incorrect locale
461     get about_path(:about_locale => "zzz")
462     assert_response :success
463     assert_template "about"
464   end
465
466   # Test the export page
467   def test_export
468     get export_path
469     assert_response :success
470     assert_template "export"
471     assert_template :layout => "map"
472
473     get export_path, :xhr => true
474     assert_response :success
475     assert_template "export"
476     assert_template :layout => "xhr"
477   end
478
479   # Test the offline page
480   def test_offline
481     get offline_path
482     assert_response :success
483     assert_select ".alert-warning"
484   end
485
486   # Test the rich text preview
487   def test_preview
488     post preview_path(:type => "html"), :xhr => true
489     assert_response :success
490
491     post preview_path(:type => "markdown"), :xhr => true
492     assert_response :success
493
494     post preview_path(:type => "text"), :xhr => true
495     assert_response :success
496   end
497
498   # Test the id frame
499   def test_id
500     session_for(create(:user))
501
502     get id_path
503
504     assert_response :success
505     assert_template "id"
506     assert_template :layout => false
507   end
508
509   # Test the id frame when not logged in
510   def test_id_without_login
511     get id_path
512
513     assert_redirected_to login_path(:referer => "/id")
514   end
515 end