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