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