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