]> git.openstreetmap.org Git - rails.git/blob - test/controllers/site_controller_test.rb
Merge remote-tracking branch 'upstream/pull/3564'
[rails.git] / test / controllers / site_controller_test.rb
1 require "test_helper"
2
3 class SiteControllerTest < ActionDispatch::IntegrationTest
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/", :method => :get },
9       { :controller => "site", :action => "index" }
10     )
11     assert_routing(
12       { :path => "/", :method => :post },
13       { :controller => "site", :action => "index" }
14     )
15     assert_routing(
16       { :path => "/edit", :method => :get },
17       { :controller => "site", :action => "edit" }
18     )
19     assert_recognizes(
20       { :controller => "site", :action => "edit", :format => "html" },
21       { :path => "/edit.html", :method => :get }
22     )
23     assert_routing(
24       { :path => "/copyright", :method => :get },
25       { :controller => "site", :action => "copyright" }
26     )
27     assert_routing(
28       { :path => "/copyright/locale", :method => :get },
29       { :controller => "site", :action => "copyright", :copyright_locale => "locale" }
30     )
31     assert_routing(
32       { :path => "/welcome", :method => :get },
33       { :controller => "site", :action => "welcome" }
34     )
35     assert_routing(
36       { :path => "/fixthemap", :method => :get },
37       { :controller => "site", :action => "fixthemap" }
38     )
39     assert_routing(
40       { :path => "/help", :method => :get },
41       { :controller => "site", :action => "help" }
42     )
43     assert_routing(
44       { :path => "/about", :method => :get },
45       { :controller => "site", :action => "about" }
46     )
47     assert_routing(
48       { :path => "/about/locale", :method => :get },
49       { :controller => "site", :action => "about", :about_locale => "locale" }
50     )
51     assert_routing(
52       { :path => "/export", :method => :get },
53       { :controller => "site", :action => "export" }
54     )
55     assert_recognizes(
56       { :controller => "site", :action => "export", :format => "html" },
57       { :path => "/export.html", :method => :get }
58     )
59     assert_routing(
60       { :path => "/offline", :method => :get },
61       { :controller => "site", :action => "offline" }
62     )
63     assert_routing(
64       { :path => "/key", :method => :get },
65       { :controller => "site", :action => "key" }
66     )
67     assert_routing(
68       { :path => "/go/shortcode", :method => :get },
69       { :controller => "site", :action => "permalink", :code => "shortcode" }
70     )
71     assert_routing(
72       { :path => "/preview/typename", :method => :post },
73       { :controller => "site", :action => "preview", :type => "typename" }
74     )
75     assert_routing(
76       { :path => "/id", :method => :get },
77       { :controller => "site", :action => "id" }
78     )
79   end
80
81   # Test the index page
82   def test_index
83     get root_path
84
85     assert_response :success
86     assert_template "index"
87   end
88
89   # Test the index page redirects
90   def test_index_redirect
91     get root_path(:node => 123)
92     assert_redirected_to :controller => :browse, :action => :node, :id => 123
93
94     get root_path(:way => 123)
95     assert_redirected_to :controller => :browse, :action => :way, :id => 123
96
97     get root_path(:relation => 123)
98     assert_redirected_to :controller => :browse, :action => :relation, :id => 123
99
100     get root_path(:note => 123)
101     assert_redirected_to :controller => :browse, :action => :note, :id => 123
102
103     get root_path(:query => "test")
104     assert_redirected_to :controller => :geocoder, :action => :search, :query => "test"
105
106     get root_path(:lat => 4, :lon => 5)
107     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=5/4/5"
108
109     get root_path(:lat => 4, :lon => 5, :zoom => 3)
110     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5"
111
112     get root_path(:layers => "T")
113     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=T"
114
115     get root_path(:notes => "yes")
116     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=N"
117
118     get root_path(:lat => 4, :lon => 5, :zoom => 3, :layers => "T")
119     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5&layers=T"
120   end
121
122   # Test the permalink redirect
123   def test_permalink
124     get permalink_path(:code => "wBz3--")
125     assert_response :redirect
126     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
127
128     get permalink_path(:code => "wBz3--", :m => "")
129     assert_response :redirect
130     assert_redirected_to :controller => :site, :action => :index, :mlat => "4.8779296875", :mlon => "3.955078125", :anchor => "map=3/4.8779296875/3.955078125"
131
132     get permalink_path(:code => "wBz3--", :layers => "T")
133     assert_response :redirect
134     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125&layers=T"
135
136     get permalink_path(:code => "wBz3--", :node => 1)
137     assert_response :redirect
138     assert_redirected_to :controller => :browse, :action => :node, :id => 1, :anchor => "map=3/4.8779296875/3.955078125"
139
140     get permalink_path(:code => "wBz3--", :way => 2)
141     assert_response :redirect
142     assert_redirected_to :controller => :browse, :action => :way, :id => 2, :anchor => "map=3/4.8779296875/3.955078125"
143
144     get permalink_path(:code => "wBz3--", :relation => 3)
145     assert_response :redirect
146     assert_redirected_to :controller => :browse, :action => :relation, :id => 3, :anchor => "map=3/4.8779296875/3.955078125"
147
148     get permalink_path(:code => "wBz3--", :changeset => 4)
149     assert_response :redirect
150     assert_redirected_to :controller => :browse, :action => :changeset, :id => 4, :anchor => "map=3/4.8779296875/3.955078125"
151   end
152
153   # Test the key page
154   def test_key
155     get key_path, :xhr => true
156
157     assert_response :success
158     assert_template "key"
159     assert_template :layout => false
160   end
161
162   # Test the edit page redirects when you aren't logged in
163   def test_edit
164     get edit_path
165
166     assert_response :redirect
167     assert_redirected_to login_path(:referer => "/edit")
168   end
169
170   # Test the error when trying to edit without public edits
171   def test_edit_non_public
172     session_for(create(:user, :data_public => false))
173
174     get edit_path
175
176     assert_response :success
177     assert_template "edit"
178     assert_select "a[href='https://wiki.openstreetmap.org/wiki/Disabling_anonymous_edits']"
179   end
180
181   # Test the right editor gets used when the user hasn't set a preference
182   def test_edit_without_preference
183     session_for(create(:user))
184
185     get edit_path
186
187     assert_response :success
188     assert_template "edit"
189     assert_template :partial => "_#{Settings.default_editor}", :count => 1
190   end
191
192   # Test the right editor gets used when the user has set a preference
193   def test_edit_with_preference
194     user = create(:user)
195     user.preferred_editor = "id"
196     user.save!
197     session_for(user)
198
199     get edit_path
200     assert_response :success
201     assert_template "edit"
202     assert_template :partial => "_id", :count => 1
203
204     user.preferred_editor = "potlatch2"
205     user.save!
206
207     get edit_path
208     assert_response :success
209     assert_template "edit"
210     assert_template :partial => "_potlatch2", :count => 1
211
212     user.preferred_editor = "potlatch"
213     user.save!
214
215     get edit_path
216     assert_response :success
217     assert_template "edit"
218     assert_template :partial => "_potlatch", :count => 1
219
220     user.preferred_editor = "remote"
221     user.save!
222
223     get edit_path
224     assert_response :success
225     assert_template "index"
226   end
227
228   # Test the right editor gets used when the URL has an override
229   def test_edit_with_override
230     session_for(create(:user))
231
232     get edit_path(:editor => "id")
233     assert_response :success
234     assert_template "edit"
235     assert_template :partial => "_id", :count => 1
236
237     get edit_path(:editor => "potlatch2")
238     assert_response :success
239     assert_template "edit"
240     assert_template :partial => "_potlatch2", :count => 1
241
242     get edit_path(:editor => "potlatch")
243     assert_response :success
244     assert_template "edit"
245     assert_template :partial => "_potlatch", :count => 1
246
247     get edit_path(:editor => "remote")
248     assert_response :success
249     assert_template "index"
250   end
251
252   # Test editing a specific node
253   def test_edit_with_node
254     user = create(:user)
255     node = create(:node, :lat => 1.0, :lon => 1.0)
256     session_for(user)
257
258     get edit_path(:node => node.id)
259
260     assert_response :success
261     assert_template "edit"
262     assert_in_delta(1.0, assigns(:lat))
263     assert_in_delta(1.0, assigns(:lon))
264     assert_equal 18, assigns(:zoom)
265   end
266
267   # Test editing inaccessible nodes
268   def test_edit_with_inaccessible_nodes
269     user = create(:user)
270     deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false)
271     session_for(user)
272
273     get edit_path(:node => 99999)
274     assert_response :success
275     assert_template "edit"
276     assert_nil assigns(:lat)
277     assert_nil assigns(:lon)
278     assert_nil assigns(:zoom)
279
280     get edit_path(:node => deleted_node.id)
281     assert_response :success
282     assert_template "edit"
283     assert_nil assigns(:lat)
284     assert_nil assigns(:lon)
285     assert_nil assigns(:zoom)
286   end
287
288   # Test editing a specific way
289   def test_edit_with_way
290     user = create(:user)
291     node = create(:node, :lat => 3, :lon => 3)
292     way = create(:way)
293     create(:way_node, :node => node, :way => way)
294     session_for(user)
295
296     get edit_path(:way => way.id)
297     assert_response :success
298     assert_template "edit"
299     assert_in_delta(3.0, assigns(:lat))
300     assert_in_delta(3.0, assigns(:lon))
301     assert_equal 17, assigns(:zoom)
302   end
303
304   # Test editing inaccessible ways
305   def test_edit_with_inaccessible_ways
306     user = create(:user)
307     deleted_way = create(:way, :visible => false)
308     session_for(user)
309
310     get edit_path(:way => 99999)
311     assert_response :success
312     assert_template "edit"
313     assert_nil assigns(:lat)
314     assert_nil assigns(:lon)
315     assert_nil assigns(:zoom)
316
317     get edit_path(:way => deleted_way.id)
318     assert_response :success
319     assert_template "edit"
320     assert_nil assigns(:lat)
321     assert_nil assigns(:lon)
322     assert_nil assigns(:zoom)
323   end
324
325   # Test editing a specific note
326   def test_edit_with_note
327     user = create(:user)
328     note = create(:note) do |n|
329       n.comments.create(:author_id => user.id)
330     end
331     session_for(user)
332
333     get edit_path(:note => note.id)
334     assert_response :success
335     assert_template "edit"
336     assert_in_delta(1.0, assigns(:lat))
337     assert_in_delta(1.0, assigns(:lon))
338     assert_equal 17, assigns(:zoom)
339   end
340
341   # Test editing inaccessible notes
342   def test_edit_with_inaccessible_notes
343     user = create(:user)
344     deleted_note = create(:note, :status => "hidden") do |n|
345       n.comments.create(:author_id => user.id)
346     end
347     session_for(user)
348
349     get edit_path(:note => 99999)
350     assert_response :success
351     assert_template "edit"
352     assert_nil assigns(:lat)
353     assert_nil assigns(:lon)
354     assert_nil assigns(:zoom)
355
356     get edit_path(:note => deleted_note.id)
357     assert_response :success
358     assert_template "edit"
359     assert_nil assigns(:lat)
360     assert_nil assigns(:lon)
361     assert_nil assigns(:zoom)
362   end
363
364   # Test editing a specific GPX trace
365   def test_edit_with_gpx
366     user = create(:user)
367     gpx = create(:trace, :latitude => 1, :longitude => 1)
368     session_for(user)
369
370     get edit_path(:gpx => gpx.id)
371     assert_response :success
372     assert_template "edit"
373     assert_in_delta(1.0, assigns(:lat))
374     assert_in_delta(1.0, assigns(:lon))
375     assert_equal 16, assigns(:zoom)
376   end
377
378   # Test editing inaccessible GPX traces
379   def test_edit_with_inaccessible_gpxes
380     user = create(:user)
381     deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1)
382     private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "private")
383     session_for(user)
384
385     get edit_path(:gpx => 99999)
386     assert_response :success
387     assert_template "edit"
388     assert_nil assigns(:lat)
389     assert_nil assigns(:lon)
390     assert_nil assigns(:zoom)
391
392     get edit_path(:gpx => deleted_gpx.id)
393     assert_response :success
394     assert_template "edit"
395     assert_nil assigns(:lat)
396     assert_nil assigns(:lon)
397     assert_nil assigns(:zoom)
398
399     get edit_path(:gpx => private_gpx.id)
400     assert_response :success
401     assert_template "edit"
402     assert_nil assigns(:lat)
403     assert_nil assigns(:lon)
404     assert_nil assigns(:zoom)
405   end
406
407   # Test the edit page redirects
408   def test_edit_redirect
409     get edit_path(:lat => 4, :lon => 5)
410     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=5/4/5"
411
412     get edit_path(:lat => 4, :lon => 5, :zoom => 3)
413     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
414
415     get edit_path(:lat => 4, :lon => 5, :zoom => 3, :editor => "id")
416     assert_redirected_to :controller => :site, :action => :edit, :editor => "id", :anchor => "map=3/4/5"
417   end
418
419   # Test the copyright page
420   def test_copyright
421     get copyright_path
422     assert_response :success
423     assert_template "copyright"
424     assert_select "div[lang='en'][dir='ltr']"
425
426     get copyright_path(:copyright_locale => "fr")
427     assert_response :success
428     assert_template "copyright"
429     assert_select "div[lang='fr'][dir='ltr']"
430
431     get copyright_path(:copyright_locale => "ar")
432     assert_response :success
433     assert_template "copyright"
434     assert_select "div[lang='ar'][dir='rtl']"
435   end
436
437   # Test the welcome page
438   def test_welcome
439     get welcome_path
440     assert_response :redirect
441     assert_redirected_to login_path(:referer => "/welcome")
442
443     session_for(create(:user))
444     get welcome_path
445     assert_response :success
446     assert_template "welcome"
447   end
448
449   # Test the fixthemap page
450   def test_fixthemap
451     get fixthemap_path
452     assert_response :success
453     assert_template "fixthemap"
454   end
455
456   # Test the help page
457   def test_help
458     get help_path
459     assert_response :success
460     assert_template "help"
461   end
462
463   # Test the about page
464   def test_about
465     get about_path
466     assert_response :success
467     assert_template "about"
468     assert_select "div[lang='en'][dir='ltr']"
469
470     get about_path(:about_locale => "fr")
471     assert_response :success
472     assert_template "about"
473     assert_select "div[lang='fr'][dir='ltr']"
474
475     get about_path(:about_locale => "ar")
476     assert_response :success
477     assert_template "about"
478     assert_select "div[lang='ar'][dir='rtl']"
479   end
480
481   # Test the export page
482   def test_export
483     get export_path
484     assert_response :success
485     assert_template "export"
486     assert_template :layout => "map"
487
488     get export_path, :xhr => true
489     assert_response :success
490     assert_template "export"
491     assert_template :layout => "xhr"
492   end
493
494   # Test the offline page
495   def test_offline
496     get offline_path
497     assert_response :success
498     assert_template "offline"
499   end
500
501   # Test the rich text preview
502   def test_preview
503     post preview_path(:type => "html"), :xhr => true
504     assert_response :success
505
506     post preview_path(:type => "markdown"), :xhr => true
507     assert_response :success
508
509     post preview_path(:type => "text"), :xhr => true
510     assert_response :success
511   end
512
513   # Test the id frame
514   def test_id
515     session_for(create(:user))
516
517     get id_path
518
519     assert_response :success
520     assert_template "id"
521     assert_template :layout => false
522   end
523 end