]> git.openstreetmap.org Git - rails.git/blob - test/controllers/site_controller_test.rb
Test some missing cases in the way controller
[rails.git] / test / controllers / site_controller_test.rb
1 require "test_helper"
2
3 class SiteControllerTest < ActionController::TestCase
4   api_fixtures
5
6   ##
7   # setup oauth keys
8   def setup
9     Object.const_set("ID_KEY", client_applications(:oauth_web_app).key)
10     Object.const_set("POTLATCH2_KEY", client_applications(:oauth_web_app).key)
11   end
12
13   ##
14   # clear oauth keys
15   def teardown
16     Object.send("remove_const", "ID_KEY")
17     Object.send("remove_const", "POTLATCH2_KEY")
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 => "/export", :method => :get },
57       { :controller => "site", :action => "export" }
58     )
59     assert_recognizes(
60       { :controller => "site", :action => "export", :format => "html" },
61       { :path => "/export.html", :method => :get }
62     )
63     assert_routing(
64       { :path => "/offline", :method => :get },
65       { :controller => "site", :action => "offline" }
66     )
67     assert_routing(
68       { :path => "/key", :method => :get },
69       { :controller => "site", :action => "key" }
70     )
71     assert_routing(
72       { :path => "/go/shortcode", :method => :get },
73       { :controller => "site", :action => "permalink", :code => "shortcode" }
74     )
75     assert_routing(
76       { :path => "/preview/formatname", :method => :post },
77       { :controller => "site", :action => "preview", :format => "formatname" }
78     )
79     assert_routing(
80       { :path => "/id", :method => :get },
81       { :controller => "site", :action => "id" }
82     )
83   end
84
85   # Test the index page
86   def test_index
87     get :index
88     assert_response :success
89     assert_template "index"
90   end
91
92   # Test the index page redirects
93   def test_index_redirect
94     get :index, :lat => 4, :lon => 5
95     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=5/4/5"
96
97     get :index, :lat => 4, :lon => 5, :zoom => 3
98     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5"
99
100     get :index, :layers => "T"
101     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=T"
102
103     get :index, :notes => "yes"
104     assert_redirected_to :controller => :site, :action => :index, :anchor => "layers=N"
105
106     get :index, :lat => 4, :lon => 5, :zoom => 3, :layers => "T"
107     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4/5&layers=T"
108   end
109
110   # Test the permalink redirect
111   def test_permalink
112     get :permalink, :code => "wBz3--"
113     assert_response :redirect
114     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125"
115
116     get :permalink, :code => "wBz3--", :m => ""
117     assert_response :redirect
118     assert_redirected_to :controller => :site, :action => :index, :mlat => "4.8779296875", :mlon => "3.955078125", :anchor => "map=3/4.8779296875/3.955078125"
119
120     get :permalink, :code => "wBz3--", :layers => "T"
121     assert_response :redirect
122     assert_redirected_to :controller => :site, :action => :index, :anchor => "map=3/4.8779296875/3.955078125&layers=T"
123
124     get :permalink, :code => "wBz3--", :node => 1
125     assert_response :redirect
126     assert_redirected_to :controller => :browse, :action => :node, :id => 1, :anchor => "map=3/4.8779296875/3.955078125"
127
128     get :permalink, :code => "wBz3--", :way => 2
129     assert_response :redirect
130     assert_redirected_to :controller => :browse, :action => :way, :id => 2, :anchor => "map=3/4.8779296875/3.955078125"
131
132     get :permalink, :code => "wBz3--", :relation => 3
133     assert_response :redirect
134     assert_redirected_to :controller => :browse, :action => :relation, :id => 3, :anchor => "map=3/4.8779296875/3.955078125"
135
136     get :permalink, :code => "wBz3--", :changeset => 4
137     assert_response :redirect
138     assert_redirected_to :controller => :browse, :action => :changeset, :id => 4, :anchor => "map=3/4.8779296875/3.955078125"
139   end
140
141   # Test the key page
142   def test_key
143     xhr :get, :key
144     assert_response :success
145     assert_template "key"
146     assert_template :layout => false
147   end
148
149   # Test the edit page redirects when you aren't logged in
150   def test_edit
151     get :edit
152     assert_response :redirect
153     assert_redirected_to :controller => :user, :action => :login, :referer => "/edit"
154   end
155
156   # Test the right editor gets used when the user hasn't set a preference
157   def test_edit_without_preference
158     get :edit, nil, :user => users(:public_user).id
159     assert_response :success
160     assert_template "edit"
161     assert_template :partial => "_#{DEFAULT_EDITOR}", :count => 1
162   end
163
164   # Test the right editor gets used when the user has set a preference
165   def test_edit_with_preference
166     user = users(:public_user)
167     user.preferred_editor = "id"
168     user.save!
169
170     get :edit, nil, :user => user.id
171     assert_response :success
172     assert_template "edit"
173     assert_template :partial => "_id", :count => 1
174
175     user = users(:public_user)
176     user.preferred_editor = "potlatch2"
177     user.save!
178
179     get :edit, nil, :user => user.id
180     assert_response :success
181     assert_template "edit"
182     assert_template :partial => "_potlatch2", :count => 1
183
184     user = users(:public_user)
185     user.preferred_editor = "potlatch"
186     user.save!
187
188     get :edit, nil, :user => user.id
189     assert_response :success
190     assert_template "edit"
191     assert_template :partial => "_potlatch", :count => 1
192
193     user = users(:public_user)
194     user.preferred_editor = "remote"
195     user.save!
196
197     get :edit, nil, :user => user.id
198     assert_response :success
199     assert_template "index"
200   end
201
202   # Test the right editor gets used when the URL has an override
203   def test_edit_with_override
204     get :edit, { :editor => "id" }, { :user => users(:public_user).id }
205     assert_response :success
206     assert_template "edit"
207     assert_template :partial => "_id", :count => 1
208
209     get :edit, { :editor => "potlatch2" }, { :user => users(:public_user).id }
210     assert_response :success
211     assert_template "edit"
212     assert_template :partial => "_potlatch2", :count => 1
213
214     get :edit, { :editor => "potlatch" }, { :user => users(:public_user).id }
215     assert_response :success
216     assert_template "edit"
217     assert_template :partial => "_potlatch", :count => 1
218
219     get :edit, { :editor => "remote" }, { :user => users(:public_user).id }
220     assert_response :success
221     assert_template "index"
222   end
223
224   # Test the right editor gets used when the browser is IE
225   def test_edit_with_ie
226     @request.env["HTTP_USER_AGENT"] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"
227
228     get :edit, {}, { :user => users(:public_user).id }
229     assert_response :success
230     assert_template "edit"
231     assert_template :partial => "_potlatch2", :count => 1
232
233     get :edit, { :editor => "id" }, { :user => users(:public_user).id }
234     assert_response :success
235     assert_template "edit"
236     assert_template :partial => "_potlatch2", :count => 1
237
238     @request.env["HTTP_USER_AGENT"] = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko"
239
240     get :edit, {}, { :user => users(:public_user).id }
241     assert_response :success
242     assert_template "edit"
243     assert_template :partial => "_potlatch2", :count => 1
244
245     get :edit, { :editor => "id" }, { :user => users(:public_user).id }
246     assert_response :success
247     assert_template "edit"
248     assert_template :partial => "_potlatch2", :count => 1
249   end
250
251   # Test editing a specific node
252   def test_edit_with_node
253     user = users(:public_user)
254     node = current_nodes(:visible_node)
255
256     get :edit, { :node => node.id }, { :user => user.id }
257     assert_response :success
258     assert_template "edit"
259     assert_equal 1.0, assigns(:lat)
260     assert_equal 1.0, assigns(:lon)
261     assert_equal 18, assigns(:zoom)
262   end
263
264   # Test editing a specific way
265   def test_edit_with_way
266     user = users(:public_user)
267     way  = current_ways(:visible_way)
268
269     get :edit, { :way => way.id }, { :user => user.id }
270     assert_response :success
271     assert_template "edit"
272     assert_equal 3.0, assigns(:lat)
273     assert_equal 3.0, assigns(:lon)
274     assert_equal 17, assigns(:zoom)
275   end
276
277   # Test editing a specific note
278   def test_edit_with_note
279     user = users(:public_user)
280     note  = notes(:open_note)
281
282     get :edit, { :note => note.id }, { :user => user.id }
283     assert_response :success
284     assert_template "edit"
285     assert_equal 1.0, assigns(:lat)
286     assert_equal 1.0, assigns(:lon)
287     assert_equal 17, assigns(:zoom)
288   end
289
290   # Test editing a specific GPX trace
291   def test_edit_with_gpx
292     user = users(:public_user)
293     gpx  = gpx_files(:public_trace_file)
294
295     get :edit, { :gpx => gpx.id }, { :user => user.id }
296     assert_response :success
297     assert_template "edit"
298     assert_equal 1.0, assigns(:lat)
299     assert_equal 1.0, assigns(:lon)
300     assert_equal 16, assigns(:zoom)
301   end
302
303   # Test the edit page redirects
304   def test_edit_redirect
305     get :edit, :lat => 4, :lon => 5
306     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=5/4/5"
307
308     get :edit, :lat => 4, :lon => 5, :zoom => 3
309     assert_redirected_to :controller => :site, :action => :edit, :anchor => "map=3/4/5"
310
311     get :edit, :lat => 4, :lon => 5, :zoom => 3, :editor => "id"
312     assert_redirected_to :controller => :site, :action => :edit, :editor => "id", :anchor => "map=3/4/5"
313   end
314
315   # Test the copyright page
316   def test_copyright
317     get :copyright
318     assert_response :success
319     assert_template "copyright"
320   end
321
322   # Test the welcome page
323   def test_welcome
324     get :welcome
325     assert_response :redirect
326     assert_redirected_to :controller => :user, :action => :login, :referer => "/welcome"
327
328     get :welcome, nil, :user => users(:public_user).id
329     assert_response :success
330     assert_template "welcome"
331   end
332
333   # Test the fixthemap page
334   def test_fixthemap
335     get :fixthemap
336     assert_response :success
337     assert_template "fixthemap"
338   end
339
340   # Test the help page
341   def test_help
342     get :help
343     assert_response :success
344     assert_template "help"
345   end
346
347   # Test the about page
348   def test_about
349     get :about
350     assert_response :success
351     assert_template "about"
352   end
353
354   # Test the export page
355   def test_export
356     get :export
357     assert_response :success
358     assert_template "export"
359     assert_template :layout => "map"
360
361     xhr :get, :export
362     assert_response :success
363     assert_template "export"
364     assert_template :layout => "xhr"
365   end
366
367   # Test the offline page
368   def test_offline
369     get :offline
370     assert_response :success
371     assert_template "offline"
372   end
373
374   # Test the rich text preview
375   def test_preview
376     xhr :post, :preview, :format => "html"
377     assert_response :success
378
379     xhr :post, :preview, :format => "markdown"
380     assert_response :success
381
382     xhr :post, :preview, :format => "text"
383     assert_response :success
384   end
385
386   # Test the id frame
387   def test_id
388     get :id, nil, :user => users(:public_user).id
389     assert_response :success
390     assert_template "id"
391     assert_template :layout => false
392   end
393 end