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