3 class SiteControllerTest < ActionController::TestCase
9 Settings.id_key = create(:client_application).key
10 Settings.potlatch2_key = create(:client_application).key
17 Settings.potlatch2_key = nil
21 # test all routes which lead to this controller
24 { :path => "/", :method => :get },
25 { :controller => "site", :action => "index" }
28 { :path => "/", :method => :post },
29 { :controller => "site", :action => "index" }
32 { :path => "/edit", :method => :get },
33 { :controller => "site", :action => "edit" }
36 { :controller => "site", :action => "edit", :format => "html" },
37 { :path => "/edit.html", :method => :get }
40 { :path => "/copyright", :method => :get },
41 { :controller => "site", :action => "copyright" }
44 { :path => "/copyright/locale", :method => :get },
45 { :controller => "site", :action => "copyright", :copyright_locale => "locale" }
48 { :path => "/welcome", :method => :get },
49 { :controller => "site", :action => "welcome" }
52 { :path => "/fixthemap", :method => :get },
53 { :controller => "site", :action => "fixthemap" }
56 { :path => "/help", :method => :get },
57 { :controller => "site", :action => "help" }
60 { :path => "/about", :method => :get },
61 { :controller => "site", :action => "about" }
64 { :path => "/about/locale", :method => :get },
65 { :controller => "site", :action => "about", :about_locale => "locale" }
68 { :path => "/export", :method => :get },
69 { :controller => "site", :action => "export" }
72 { :controller => "site", :action => "export", :format => "html" },
73 { :path => "/export.html", :method => :get }
76 { :path => "/offline", :method => :get },
77 { :controller => "site", :action => "offline" }
80 { :path => "/key", :method => :get },
81 { :controller => "site", :action => "key" }
84 { :path => "/go/shortcode", :method => :get },
85 { :controller => "site", :action => "permalink", :code => "shortcode" }
88 { :path => "/preview/typename", :method => :post },
89 { :controller => "site", :action => "preview", :type => "typename" }
92 { :path => "/id", :method => :get },
93 { :controller => "site", :action => "id" }
100 assert_response :success
101 assert_template "index"
104 # Test the index page redirects
105 def test_index_redirect
106 get :index, :params => { :node => 123 }
107 assert_redirected_to :controller => :browse, :action => :node, :id => 123
109 get :index, :params => { :way => 123 }
110 assert_redirected_to :controller => :browse, :action => :way, :id => 123
112 get :index, :params => { :relation => 123 }
113 assert_redirected_to :controller => :browse, :action => :relation, :id => 123
115 get :index, :params => { :note => 123 }
116 assert_redirected_to :controller => :browse, :action => :note, :id => 123
118 get :index, :params => { :query => "test" }
119 assert_redirected_to :controller => :geocoder, :action => :search, :query => "test"
121 get :index, :params => { :lat => 4, :lon => 5 }
122 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=5/4/5"
124 get :index, :params => { :lat => 4, :lon => 5, :zoom => 3 }
125 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5"
127 get :index, :params => { :layers => "T" }
128 assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=T"
130 get :index, :params => { :notes => "yes" }
131 assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=N"
133 get :index, :params => { :lat => 4, :lon => 5, :zoom => 3, :layers => "T" }
134 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5&layers=T"
137 # Test the permalink redirect
139 get :permalink, :params => { :code => "wBz3--" }
140 assert_response :redirect
141 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
143 get :permalink, :params => { :code => "wBz3--", :m => "" }
144 assert_response :redirect
145 assert_redirected_to :controller => :site, :action => :index, :mlat => "4.8779296875", :mlon => "3.955078125", :anchor => "map=3/4.8779296875/3.955078125"
147 get :permalink, :params => { :code => "wBz3--", :layers => "T" }
148 assert_response :redirect
149 assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125&layers=T"
151 get :permalink, :params => { :code => "wBz3--", :node => 1 }
152 assert_response :redirect
153 assert_redirected_to :controller => :browse, :action => :node, :id => 1, :anchor => "map=3/4.8779296875/3.955078125"
155 get :permalink, :params => { :code => "wBz3--", :way => 2 }
156 assert_response :redirect
157 assert_redirected_to :controller => :browse, :action => :way, :id => 2, :anchor => "map=3/4.8779296875/3.955078125"
159 get :permalink, :params => { :code => "wBz3--", :relation => 3 }
160 assert_response :redirect
161 assert_redirected_to :controller => :browse, :action => :relation, :id => 3, :anchor => "map=3/4.8779296875/3.955078125"
163 get :permalink, :params => { :code => "wBz3--", :changeset => 4 }
164 assert_response :redirect
165 assert_redirected_to :controller => :browse, :action => :changeset, :id => 4, :anchor => "map=3/4.8779296875/3.955078125"
170 get :key, :xhr => true
171 assert_response :success
172 assert_template "key"
173 assert_template :layout => false
176 # Test the edit page redirects when you aren't logged in
179 assert_response :redirect
180 assert_redirected_to :controller => :users, :action => :login, :referer => "/edit"
183 # Test the error when trying to edit without public edits
184 def test_edit_non_public
185 get :edit, :session => { :user => create(:user, :data_public => false) }
186 assert_response :success
187 assert_template "edit"
188 assert_select "a[href='https://wiki.openstreetmap.org/wiki/Disabling_anonymous_edits']"
191 # Test the right editor gets used when the user hasn't set a preference
192 def test_edit_without_preference
193 get :edit, :session => { :user => create(:user) }
194 assert_response :success
195 assert_template "edit"
196 assert_template :partial => "_#{Settings.default_editor}", :count => 1
199 # Test the right editor gets used when the user has set a preference
200 def test_edit_with_preference
202 user.preferred_editor = "id"
205 get :edit, :session => { :user => user }
206 assert_response :success
207 assert_template "edit"
208 assert_template :partial => "_id", :count => 1
211 user.preferred_editor = "potlatch2"
214 get :edit, :session => { :user => user }
215 assert_response :success
216 assert_template "edit"
217 assert_template :partial => "_potlatch2", :count => 1
220 user.preferred_editor = "potlatch"
223 get :edit, :session => { :user => user }
224 assert_response :success
225 assert_template "edit"
226 assert_template :partial => "_potlatch", :count => 1
229 user.preferred_editor = "remote"
232 get :edit, :session => { :user => user }
233 assert_response :success
234 assert_template "index"
237 # Test the right editor gets used when the URL has an override
238 def test_edit_with_override
239 get :edit, :params => { :editor => "id" }, :session => { :user => create(:user) }
240 assert_response :success
241 assert_template "edit"
242 assert_template :partial => "_id", :count => 1
244 get :edit, :params => { :editor => "potlatch2" }, :session => { :user => create(:user) }
245 assert_response :success
246 assert_template "edit"
247 assert_template :partial => "_potlatch2", :count => 1
249 get :edit, :params => { :editor => "potlatch" }, :session => { :user => create(:user) }
250 assert_response :success
251 assert_template "edit"
252 assert_template :partial => "_potlatch", :count => 1
254 get :edit, :params => { :editor => "remote" }, :session => { :user => create(:user) }
255 assert_response :success
256 assert_template "index"
259 # Test editing a specific node
260 def test_edit_with_node
262 node = create(:node, :lat => 1.0, :lon => 1.0)
264 get :edit, :params => { :node => node.id }, :session => { :user => user }
265 assert_response :success
266 assert_template "edit"
267 assert_equal 1.0, assigns(:lat)
268 assert_equal 1.0, assigns(:lon)
269 assert_equal 18, assigns(:zoom)
272 # Test editing inaccessible nodes
273 def test_edit_with_inaccessible_nodes
275 deleted_node = create(:node, :lat => 1.0, :lon => 1.0, :visible => false)
277 get :edit, :params => { :node => 99999 }, :session => { :user => user }
278 assert_response :success
279 assert_template "edit"
280 assert_nil assigns(:lat)
281 assert_nil assigns(:lon)
282 assert_nil assigns(:zoom)
284 get :edit, :params => { :node => deleted_node.id }, :session => { :user => user }
285 assert_response :success
286 assert_template "edit"
287 assert_nil assigns(:lat)
288 assert_nil assigns(:lon)
289 assert_nil assigns(:zoom)
292 # Test editing a specific way
293 def test_edit_with_way
295 node = create(:node, :lat => 3, :lon => 3)
297 create(:way_node, :node => node, :way => way)
299 get :edit, :params => { :way => way.id }, :session => { :user => user }
300 assert_response :success
301 assert_template "edit"
302 assert_equal 3.0, assigns(:lat)
303 assert_equal 3.0, assigns(:lon)
304 assert_equal 17, assigns(:zoom)
307 # Test editing inaccessible ways
308 def test_edit_with_inaccessible_ways
310 deleted_way = create(:way, :visible => false)
312 get :edit, :params => { :way => 99999 }, :session => { :user => user }
313 assert_response :success
314 assert_template "edit"
315 assert_nil assigns(:lat)
316 assert_nil assigns(:lon)
317 assert_nil assigns(:zoom)
319 get :edit, :params => { :way => deleted_way.id }, :session => { :user => user }
320 assert_response :success
321 assert_template "edit"
322 assert_nil assigns(:lat)
323 assert_nil assigns(:lon)
324 assert_nil assigns(:zoom)
327 # Test editing a specific note
328 def test_edit_with_note
330 note = create(:note) do |n|
331 n.comments.create(:author_id => user.id)
334 get :edit, :params => { :note => note.id }, :session => { :user => user }
335 assert_response :success
336 assert_template "edit"
337 assert_equal 1.0, assigns(:lat)
338 assert_equal 1.0, assigns(:lon)
339 assert_equal 17, assigns(:zoom)
342 # Test editing inaccessible notes
343 def test_edit_with_inaccessible_notes
345 deleted_note = create(:note, :status => "hidden") do |n|
346 n.comments.create(:author_id => user.id)
349 get :edit, :params => { :note => 99999 }, :session => { :user => user }
350 assert_response :success
351 assert_template "edit"
352 assert_nil assigns(:lat)
353 assert_nil assigns(:lon)
354 assert_nil assigns(:zoom)
356 get :edit, :params => { :note => deleted_note.id }, :session => { :user => user }
357 assert_response :success
358 assert_template "edit"
359 assert_nil assigns(:lat)
360 assert_nil assigns(:lon)
361 assert_nil assigns(:zoom)
364 # Test editing a specific GPX trace
365 def test_edit_with_gpx
367 gpx = create(:trace, :latitude => 1, :longitude => 1)
369 get :edit, :params => { :gpx => gpx.id }, :session => { :user => user }
370 assert_response :success
371 assert_template "edit"
372 assert_equal 1.0, assigns(:lat)
373 assert_equal 1.0, assigns(:lon)
374 assert_equal 16, assigns(:zoom)
377 # Test editing inaccessible GPX traces
378 def test_edit_with_inaccessible_gpxes
380 deleted_gpx = create(:trace, :deleted, :latitude => 1, :longitude => 1)
381 private_gpx = create(:trace, :latitude => 1, :longitude => 1, :visibility => "private")
383 get :edit, :params => { :gpx => 99999 }, :session => { :user => user }
384 assert_response :success
385 assert_template "edit"
386 assert_nil assigns(:lat)
387 assert_nil assigns(:lon)
388 assert_nil assigns(:zoom)
390 get :edit, :params => { :gpx => deleted_gpx.id }, :session => { :user => user }
391 assert_response :success
392 assert_template "edit"
393 assert_nil assigns(:lat)
394 assert_nil assigns(:lon)
395 assert_nil assigns(:zoom)
397 get :edit, :params => { :gpx => private_gpx.id }, :session => { :user => user }
398 assert_response :success
399 assert_template "edit"
400 assert_nil assigns(:lat)
401 assert_nil assigns(:lon)
402 assert_nil assigns(:zoom)
405 # Test the edit page redirects
406 def test_edit_redirect
407 get :edit, :params => { :lat => 4, :lon => 5 }
408 assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=5/4/5"
410 get :edit, :params => { :lat => 4, :lon => 5, :zoom => 3 }
411 assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
413 get :edit, :params => { :lat => 4, :lon => 5, :zoom => 3, :editor => "id" }
414 assert_redirected_to :controller => :site, :action => :edit, :editor => "id", :anchor => "map=3/4/5"
417 # Test the copyright page
420 assert_response :success
421 assert_template "copyright"
422 assert_select "div[lang='en'][dir='ltr']"
424 get :copyright, :params => { :copyright_locale => "fr" }
425 assert_response :success
426 assert_template "copyright"
427 assert_select "div[lang='fr'][dir='ltr']"
429 get :copyright, :params => { :copyright_locale => "ar" }
430 assert_response :success
431 assert_template "copyright"
432 assert_select "div[lang='ar'][dir='rtl']"
435 # Test the welcome page
438 assert_response :redirect
439 assert_redirected_to :controller => :users, :action => :login, :referer => "/welcome"
441 get :welcome, :session => { :user => create(:user) }
442 assert_response :success
443 assert_template "welcome"
446 # Test the fixthemap page
449 assert_response :success
450 assert_template "fixthemap"
456 assert_response :success
457 assert_template "help"
460 # Test the about page
463 assert_response :success
464 assert_template "about"
465 assert_select "div[lang='en'][dir='ltr']"
467 get :about, :params => { :about_locale => "fr" }
468 assert_response :success
469 assert_template "about"
470 assert_select "div[lang='fr'][dir='ltr']"
472 get :about, :params => { :about_locale => "ar" }
473 assert_response :success
474 assert_template "about"
475 assert_select "div[lang='ar'][dir='rtl']"
478 # Test the export page
481 assert_response :success
482 assert_template "export"
483 assert_template :layout => "map"
485 get :export, :xhr => true
486 assert_response :success
487 assert_template "export"
488 assert_template :layout => "xhr"
491 # Test the offline page
494 assert_response :success
495 assert_template "offline"
498 # Test the rich text preview
500 post :preview, :xhr => true, :params => { :type => "html" }
501 assert_response :success
503 post :preview, :xhr => true, :params => { :type => "markdown" }
504 assert_response :success
506 post :preview, :xhr => true, :params => { :type => "text" }
507 assert_response :success
512 get :id, :session => { :user => create(:user) }
513 assert_response :success
515 assert_template :layout => false