]> git.openstreetmap.org Git - rails.git/blob - test/functional/site_controller_test.rb
78fde62d7e8fed645641a470a1e51fbcb1f545ce
[rails.git] / test / functional / site_controller_test.rb
1 require File.dirname(__FILE__) + '/../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--', :node => 1
117     assert_response :redirect
118     assert_redirected_to :controller => :browse, :action => :node, :id => 1, :anchor => 'map=3/4.8779296875/3.955078125'
119
120     get :permalink, :code => 'wBz3--', :way => 2
121     assert_response :redirect
122     assert_redirected_to :controller => :browse, :action => :way, :id => 2, :anchor => 'map=3/4.8779296875/3.955078125'
123
124     get :permalink, :code => 'wBz3--', :relation => 3
125     assert_response :redirect
126     assert_redirected_to :controller => :browse, :action => :relation, :id => 3, :anchor => 'map=3/4.8779296875/3.955078125'
127
128     get :permalink, :code => 'wBz3--', :changeset => 4
129     assert_response :redirect
130     assert_redirected_to :controller => :browse, :action => :changeset, :id => 4, :anchor => 'map=3/4.8779296875/3.955078125'
131   end
132
133   # Test the key page
134   def test_key
135     xhr :get, :key
136     assert_response :success
137     assert_template "key"
138     assert_template :layout => false
139   end
140
141   # Test the edit page redirects when you aren't logged in
142   def test_edit
143     get :edit
144     assert_response :redirect
145     assert_redirected_to :controller => :user, :action => :login, :referer => "/edit"
146   end
147
148   # Test the right editor gets used when the user hasn't set a preference
149   def test_edit_without_preference
150     get :edit, nil, { :user => users(:public_user).id }
151     assert_response :success
152     assert_template "edit"
153     assert_template :partial => "_#{DEFAULT_EDITOR}", :count => 1
154   end
155
156   # Test the right editor gets used when the user has set a preference
157   def test_edit_with_preference
158     user = users(:public_user)
159     user.preferred_editor = "id"
160     user.save!
161
162     get :edit, nil, { :user => user.id }
163     assert_response :success
164     assert_template "edit"
165     assert_template :partial => "_id", :count => 1
166
167     user = users(:public_user)
168     user.preferred_editor = "potlatch2"
169     user.save!
170
171     get :edit, nil, { :user => user.id }
172     assert_response :success
173     assert_template "edit"
174     assert_template :partial => "_potlatch2", :count => 1
175
176     user = users(:public_user)
177     user.preferred_editor = "potlatch"
178     user.save!
179
180     get :edit, nil, { :user => user.id }
181     assert_response :success
182     assert_template "edit"
183     assert_template :partial => "_potlatch", :count => 1
184
185     user = users(:public_user)
186     user.preferred_editor = "remote"
187     user.save!
188
189     get :edit, nil, { :user => user.id }
190     assert_response :success
191     assert_template "index"
192   end
193
194   # Test editing a specific node
195   def test_edit_with_node
196     user = users(:public_user)
197     node = current_nodes(:visible_node)
198
199     get :edit, { :node => node.id }, { :user => user.id }
200     assert_response :success
201     assert_template "edit"
202     assert_equal 1.0, assigns(:lat)
203     assert_equal 1.0, assigns(:lon)
204     assert_equal 18, assigns(:zoom)
205   end
206
207   # Test editing a specific way
208   def test_edit_with_way
209     user = users(:public_user)
210     way  = current_ways(:visible_way)
211
212     get :edit, { :way => way.id }, { :user => user.id }
213     assert_response :success
214     assert_template "edit"
215     assert_equal 3.0, assigns(:lat)
216     assert_equal 3.0, assigns(:lon)
217     assert_equal 17, assigns(:zoom)
218   end
219
220   # Test editing a specific note
221   def test_edit_with_note
222     user = users(:public_user)
223     note  = notes(:open_note)
224
225     get :edit, { :note => note.id }, { :user => user.id }
226     assert_response :success
227     assert_template "edit"
228     assert_equal 1.0, assigns(:lat)
229     assert_equal 1.0, assigns(:lon)
230     assert_equal 17, assigns(:zoom)
231   end
232
233   # Test editing a specific GPX trace
234   def test_edit_with_gpx
235     user = users(:public_user)
236     gpx  = gpx_files(:public_trace_file)
237
238     get :edit, { :gpx => gpx.id }, { :user => user.id }
239     assert_response :success
240     assert_template "edit"
241     assert_equal 1.0, assigns(:lat)
242     assert_equal 1.0, assigns(:lon)
243     assert_equal 16, assigns(:zoom)
244   end
245
246   # Test the edit page redirects
247   def test_edit_redirect
248     get :edit, :lat => 4, :lon => 5
249     assert_redirected_to :controller => :site, :action => :edit, :anchor => 'map=5/4/5'
250
251     get :edit, :lat => 4, :lon => 5, :zoom => 3
252     assert_redirected_to :controller => :site, :action => :edit, :anchor => 'map=3/4/5'
253
254     get :edit, :lat => 4, :lon => 5, :zoom => 3, :editor => 'id'
255     assert_redirected_to :controller => :site, :action => :edit, :editor => 'id', :anchor => 'map=3/4/5'
256   end
257
258   # Test the copyright page
259   def test_copyright
260     get :copyright
261     assert_response :success
262     assert_template "copyright"
263   end
264
265   # Test the welcome page
266   def test_welcome
267     get :welcome
268     assert_response :redirect
269     assert_redirected_to :controller => :user, :action => :login, :referer => "/welcome"
270
271     get :welcome, nil, { :user => users(:public_user).id }
272     assert_response :success
273     assert_template "welcome"
274   end
275
276   # Test the fixthemap page
277   def test_fixthemap
278     get :fixthemap
279     assert_response :success
280     assert_template "fixthemap"
281   end
282
283   # Test the help page
284   def test_help
285     get :help
286     assert_response :success
287     assert_template "help"
288   end
289
290   # Test the about page
291   def test_about
292     get :about
293     assert_response :success
294     assert_template "about"
295   end
296
297   # Test the export page
298   def test_export
299     get :export
300     assert_response :success
301     assert_template "export"
302     assert_template :layout => "map"
303
304     xhr :get, :export
305     assert_response :success
306     assert_template "export"
307     assert_template :layout => "xhr"
308   end
309
310   # Test the offline page
311   def test_offline
312     get :offline
313     assert_response :success
314     assert_template "offline"
315   end
316
317   # Test the rich text preview
318   def test_preview
319     xhr :post, :preview, :format => "html"
320     assert_response :success
321
322     xhr :post, :preview, :format => "markdown"
323     assert_response :success
324
325     xhr :post, :preview, :format => "text"
326     assert_response :success
327   end
328
329   # Test the id frame
330   def test_id
331     get :id, nil, { :user => users(:public_user).id }
332     assert_response :success
333     assert_template "id"
334     assert_template :layout => false
335   end
336 end