]> git.openstreetmap.org Git - rails.git/blob - test/controllers/notes_controller_test.rb
Reorganise tests to match modern rails test layout
[rails.git] / test / controllers / notes_controller_test.rb
1 require 'test_helper'
2
3 class NotesControllerTest < ActionController::TestCase
4   fixtures :users, :notes, :note_comments
5
6   ##
7   # test all routes which lead to this controller
8   def test_routes
9     assert_routing(
10       { :path => "/api/0.6/notes", :method => :post },
11       { :controller => "notes", :action => "create", :format => "xml" }
12     )
13     assert_routing(
14       { :path => "/api/0.6/notes/1", :method => :get },
15       { :controller => "notes", :action => "show", :id => "1", :format => "xml" }
16     )
17     assert_recognizes(
18       { :controller => "notes", :action => "show", :id => "1", :format => "xml" },
19       { :path => "/api/0.6/notes/1.xml", :method => :get }
20     )
21     assert_routing(
22       { :path => "/api/0.6/notes/1.rss", :method => :get },
23       { :controller => "notes", :action => "show", :id => "1", :format => "rss" }
24     )
25     assert_routing(
26       { :path => "/api/0.6/notes/1.json", :method => :get },
27       { :controller => "notes", :action => "show", :id => "1", :format => "json" }
28     )
29     assert_routing(
30       { :path => "/api/0.6/notes/1.gpx", :method => :get },
31       { :controller => "notes", :action => "show", :id => "1", :format => "gpx" }
32     )
33     assert_routing(
34       { :path => "/api/0.6/notes/1/comment", :method => :post },
35       { :controller => "notes", :action => "comment", :id => "1", :format => "xml" }
36     )
37     assert_routing(
38       { :path => "/api/0.6/notes/1/close", :method => :post },
39       { :controller => "notes", :action => "close", :id => "1", :format => "xml" }
40     )
41     assert_routing(
42       { :path => "/api/0.6/notes/1/reopen", :method => :post },
43       { :controller => "notes", :action => "reopen", :id => "1", :format => "xml" }
44     )
45     assert_routing(
46       { :path => "/api/0.6/notes/1", :method => :delete },
47       { :controller => "notes", :action => "destroy", :id => "1", :format => "xml" }
48     )
49
50     assert_routing(
51       { :path => "/api/0.6/notes", :method => :get },
52       { :controller => "notes", :action => "index", :format => "xml" }
53     )
54     assert_recognizes(
55       { :controller => "notes", :action => "index", :format => "xml" },
56       { :path => "/api/0.6/notes.xml", :method => :get }
57     )
58     assert_routing(
59       { :path => "/api/0.6/notes.rss", :method => :get },
60       { :controller => "notes", :action => "index", :format => "rss" }
61     )
62     assert_routing(
63       { :path => "/api/0.6/notes.json", :method => :get },
64       { :controller => "notes", :action => "index", :format => "json" }
65     )
66     assert_routing(
67       { :path => "/api/0.6/notes.gpx", :method => :get },
68       { :controller => "notes", :action => "index", :format => "gpx" }
69     )
70
71     assert_routing(
72       { :path => "/api/0.6/notes/search", :method => :get },
73       { :controller => "notes", :action => "search", :format => "xml" }
74     )
75     assert_recognizes(
76       { :controller => "notes", :action => "search", :format => "xml" },
77       { :path => "/api/0.6/notes/search.xml", :method => :get }
78     )
79     assert_routing(
80       { :path => "/api/0.6/notes/search.rss", :method => :get },
81       { :controller => "notes", :action => "search", :format => "rss" }
82     )
83     assert_routing(
84       { :path => "/api/0.6/notes/search.json", :method => :get },
85       { :controller => "notes", :action => "search", :format => "json" }
86     )
87     assert_routing(
88       { :path => "/api/0.6/notes/search.gpx", :method => :get },
89       { :controller => "notes", :action => "search", :format => "gpx" }
90     )
91
92     assert_routing(
93       { :path => "/api/0.6/notes/feed", :method => :get },
94       { :controller => "notes", :action => "feed", :format => "rss" }
95     )
96
97     assert_recognizes(
98       { :controller => "notes", :action => "create" },
99       { :path => "/api/0.6/notes/addPOIexec", :method => :post }
100     )
101     assert_recognizes(
102       { :controller => "notes", :action => "close" },
103       { :path => "/api/0.6/notes/closePOIexec", :method => :post }
104     )
105     assert_recognizes(
106       { :controller => "notes", :action => "comment" },
107       { :path => "/api/0.6/notes/editPOIexec", :method => :post }
108     )
109     assert_recognizes(
110       { :controller => "notes", :action => "index", :format => "gpx" },
111       { :path => "/api/0.6/notes/getGPX", :method => :get }
112     )
113     assert_recognizes(
114       { :controller => "notes", :action => "feed", :format => "rss" },
115       { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
116     )
117
118     assert_routing(
119       { :path => "/user/username/notes", :method => :get },
120       { :controller => "notes", :action => "mine", :display_name => "username" }
121     )
122   end
123
124   def test_create_success
125     assert_difference('Note.count') do
126       assert_difference('NoteComment.count') do
127         post :create, {:lat => -1.0, :lon => -1.0, :text => "This is a comment", :format => "json"}
128       end
129     end
130     assert_response :success
131     js = ActiveSupport::JSON.decode(@response.body)
132     assert_not_nil js
133     assert_equal "Feature", js["type"]
134     assert_equal "Point", js["geometry"]["type"]
135     assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
136     assert_equal "open", js["properties"]["status"]
137     assert_equal 1, js["properties"]["comments"].count
138     assert_equal "opened", js["properties"]["comments"].last["action"]
139     assert_equal "This is a comment", js["properties"]["comments"].last["text"]
140     assert_nil js["properties"]["comments"].last["user"]
141     id = js["properties"]["id"]
142
143     get :show, {:id => id, :format => "json"}
144     assert_response :success
145     js = ActiveSupport::JSON.decode(@response.body)
146     assert_not_nil js
147     assert_equal "Feature", js["type"]
148     assert_equal "Point", js["geometry"]["type"]
149     assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
150     assert_equal id, js["properties"]["id"]
151     assert_equal "open", js["properties"]["status"]
152     assert_equal 1, js["properties"]["comments"].count
153     assert_equal "opened", js["properties"]["comments"].last["action"]
154     assert_equal "This is a comment", js["properties"]["comments"].last["text"]
155     assert_nil js["properties"]["comments"].last["user"]
156   end
157
158   def test_create_fail
159     assert_no_difference('Note.count') do
160       assert_no_difference('NoteComment.count') do
161         post :create, {:lon => -1.0, :text => "This is a comment"}
162       end
163     end
164     assert_response :bad_request
165
166     assert_no_difference('Note.count') do
167       assert_no_difference('NoteComment.count') do
168         post :create, {:lat => -1.0, :text => "This is a comment"}
169       end
170     end
171     assert_response :bad_request
172
173     assert_no_difference('Note.count') do
174       assert_no_difference('NoteComment.count') do
175         post :create, {:lat => -1.0, :lon => -1.0}
176       end
177     end
178     assert_response :bad_request
179
180     assert_no_difference('Note.count') do
181       assert_no_difference('NoteComment.count') do
182         post :create, {:lat => -1.0, :lon => -1.0, :text => ""}
183       end
184     end
185     assert_response :bad_request
186
187     assert_no_difference('Note.count') do
188       assert_no_difference('NoteComment.count') do
189         post :create, {:lat => -100.0, :lon => -1.0, :text => "This is a comment"}
190       end
191     end
192     assert_response :bad_request
193
194     assert_no_difference('Note.count') do
195       assert_no_difference('NoteComment.count') do
196         post :create, {:lat => -1.0, :lon => -200.0, :text => "This is a comment"}
197       end
198     end
199     assert_response :bad_request
200
201     assert_no_difference('Note.count') do
202       assert_no_difference('NoteComment.count') do
203         post :create, {:lat => 'abc', :lon => -1.0, :text => "This is a comment"}
204       end
205     end
206     assert_response :bad_request
207
208     assert_no_difference('Note.count') do
209       assert_no_difference('NoteComment.count') do
210         post :create, {:lat => -1.0, :lon => 'abc', :text => "This is a comment"}
211       end
212     end
213     assert_response :bad_request
214   end
215
216   def test_comment_success
217     assert_difference('NoteComment.count') do
218       post :comment, {:id => notes(:open_note_with_comment).id, :text => "This is an additional comment", :format => "json"}
219     end
220     assert_response :success
221     js = ActiveSupport::JSON.decode(@response.body)
222     assert_not_nil js
223     assert_equal "Feature", js["type"]
224     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
225     assert_equal "open", js["properties"]["status"]
226     assert_equal 3, js["properties"]["comments"].count
227     assert_equal "commented", js["properties"]["comments"].last["action"]
228     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
229     assert_nil js["properties"]["comments"].last["user"]
230
231     get :show, {:id => notes(:open_note_with_comment).id, :format => "json"}
232     assert_response :success
233     js = ActiveSupport::JSON.decode(@response.body)
234     assert_not_nil js
235     assert_equal "Feature", js["type"]
236     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
237     assert_equal "open", js["properties"]["status"]
238     assert_equal 3, js["properties"]["comments"].count
239     assert_equal "commented", js["properties"]["comments"].last["action"]
240     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
241     assert_nil js["properties"]["comments"].last["user"]
242   end
243
244   def test_comment_fail
245     assert_no_difference('NoteComment.count') do
246       post :comment, {:text => "This is an additional comment"}
247     end
248     assert_response :bad_request
249
250     assert_no_difference('NoteComment.count') do
251       post :comment, {:id => notes(:open_note_with_comment).id}
252     end
253     assert_response :bad_request
254
255     assert_no_difference('NoteComment.count') do
256       post :comment, {:id => notes(:open_note_with_comment).id, :text => ""}
257     end
258     assert_response :bad_request
259
260     assert_no_difference('NoteComment.count') do
261       post :comment, {:id => 12345, :text => "This is an additional comment"}
262     end
263     assert_response :not_found
264
265     assert_no_difference('NoteComment.count') do
266       post :comment, {:id => notes(:hidden_note_with_comment).id, :text => "This is an additional comment"}
267     end
268     assert_response :gone
269
270     assert_no_difference('NoteComment.count') do
271       post :comment, {:id => notes(:closed_note_with_comment).id, :text => "This is an additional comment"}
272     end
273     assert_response :conflict
274   end
275
276   def test_close_success
277     post :close, {:id => notes(:open_note_with_comment).id, :text => "This is a close comment", :format => "json"}
278     assert_response :unauthorized
279
280     basic_authorization(users(:public_user).email, "test")
281
282     post :close, {:id => notes(:open_note_with_comment).id, :text => "This is a close comment", :format => "json"}
283     assert_response :success
284     js = ActiveSupport::JSON.decode(@response.body)
285     assert_not_nil js
286     assert_equal "Feature", js["type"]
287     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
288     assert_equal "closed", js["properties"]["status"]
289     assert_equal 3, js["properties"]["comments"].count
290     assert_equal "closed", js["properties"]["comments"].last["action"]
291     assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
292     assert_equal "test2", js["properties"]["comments"].last["user"]
293
294     get :show, {:id => notes(:open_note_with_comment).id, :format => "json"}
295     assert_response :success
296     js = ActiveSupport::JSON.decode(@response.body)
297     assert_not_nil js
298     assert_equal "Feature", js["type"]
299     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
300     assert_equal "closed", js["properties"]["status"]
301     assert_equal 3, js["properties"]["comments"].count
302     assert_equal "closed", js["properties"]["comments"].last["action"]
303     assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
304     assert_equal "test2", js["properties"]["comments"].last["user"]
305   end
306
307   def test_close_fail
308     post :close
309     assert_response :unauthorized
310
311     basic_authorization(users(:public_user).email, "test")
312
313     post :close
314     assert_response :bad_request
315
316     post :close, {:id => 12345}
317     assert_response :not_found
318
319     post :close, {:id => notes(:hidden_note_with_comment).id}
320     assert_response :gone
321
322     post :close, {:id => notes(:closed_note_with_comment).id}
323     assert_response :conflict
324   end
325
326   def test_reopen_success
327     post :reopen, {:id => notes(:closed_note_with_comment).id, :text => "This is a reopen comment", :format => "json"}
328     assert_response :unauthorized
329
330     basic_authorization(users(:public_user).email, "test")
331
332     post :reopen, {:id => notes(:closed_note_with_comment).id, :text => "This is a reopen comment", :format => "json"}
333     assert_response :success
334     js = ActiveSupport::JSON.decode(@response.body)
335     assert_not_nil js
336     assert_equal "Feature", js["type"]
337     assert_equal notes(:closed_note_with_comment).id, js["properties"]["id"]
338     assert_equal "open", js["properties"]["status"]
339     assert_equal 2, js["properties"]["comments"].count
340     assert_equal "reopened", js["properties"]["comments"].last["action"]
341     assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
342     assert_equal "test2", js["properties"]["comments"].last["user"]
343
344     get :show, {:id => notes(:closed_note_with_comment).id, :format => "json"}
345     assert_response :success
346     js = ActiveSupport::JSON.decode(@response.body)
347     assert_not_nil js
348     assert_equal "Feature", js["type"]
349     assert_equal notes(:closed_note_with_comment).id, js["properties"]["id"]
350     assert_equal "open", js["properties"]["status"]
351     assert_equal 2, js["properties"]["comments"].count
352     assert_equal "reopened", js["properties"]["comments"].last["action"]
353     assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
354     assert_equal "test2", js["properties"]["comments"].last["user"]
355   end
356
357   def test_reopen_fail
358     post :reopen, {:id => notes(:hidden_note_with_comment).id}
359     assert_response :unauthorized
360
361     basic_authorization(users(:public_user).email, "test")
362
363     post :reopen, {:id => 12345}
364     assert_response :not_found
365
366     post :reopen, {:id => notes(:hidden_note_with_comment).id}
367     assert_response :gone
368
369     post :reopen, {:id => notes(:open_note_with_comment).id}
370     assert_response :conflict
371   end
372
373   def test_show_success
374     get :show, {:id => notes(:open_note).id, :format => "xml"}
375     assert_response :success
376     assert_equal "application/xml", @response.content_type
377     assert_select "osm", :count => 1 do
378       assert_select "note[lat=#{notes(:open_note).lat}][lon=#{notes(:open_note).lon}]", :count => 1 do
379         assert_select "id", notes(:open_note).id
380         assert_select "url", note_url(notes(:open_note), :format => "xml")
381         assert_select "comment_url", comment_note_url(notes(:open_note), :format => "xml")
382         assert_select "close_url", close_note_url(notes(:open_note), :format => "xml")
383         assert_select "date_created", notes(:open_note).created_at.to_s
384         assert_select "status", notes(:open_note).status
385         assert_select "comments", :count => 1 do
386           assert_select "comment", :count => 1
387         end
388       end
389     end
390
391     get :show, {:id => notes(:open_note).id, :format => "rss"}
392     assert_response :success
393     assert_equal "application/rss+xml", @response.content_type
394     assert_select "rss", :count => 1 do
395       assert_select "channel", :count => 1 do
396         assert_select "item", :count => 1 do
397           assert_select "link", browse_note_url(notes(:open_note))
398           assert_select "guid", note_url(notes(:open_note))
399           assert_select "pubDate", notes(:open_note).created_at.to_s(:rfc822)
400 #          assert_select "geo:lat", notes(:open_note).lat.to_s
401 #          assert_select "geo:long", notes(:open_note).lon
402 #          assert_select "georss:point", "#{notes(:open_note).lon} #{notes(:open_note).lon}"
403         end
404       end
405     end
406
407     get :show, {:id => notes(:open_note).id, :format => "json"}
408     assert_response :success
409     assert_equal "application/json", @response.content_type
410     js = ActiveSupport::JSON.decode(@response.body)
411     assert_not_nil js
412     assert_equal "Feature", js["type"]
413     assert_equal "Point", js["geometry"]["type"]
414     assert_equal notes(:open_note).lat, js["geometry"]["coordinates"][0]
415     assert_equal notes(:open_note).lon, js["geometry"]["coordinates"][1]
416     assert_equal notes(:open_note).id, js["properties"]["id"]
417     assert_equal note_url(notes(:open_note), :format => "json"), js["properties"]["url"]
418     assert_equal comment_note_url(notes(:open_note), :format => "json"), js["properties"]["comment_url"]
419     assert_equal close_note_url(notes(:open_note), :format => "json"), js["properties"]["close_url"]
420     assert_equal notes(:open_note).created_at, js["properties"]["date_created"]
421     assert_equal notes(:open_note).status, js["properties"]["status"]
422
423     get :show, {:id => notes(:open_note).id, :format => "gpx"}
424     assert_response :success
425     assert_equal "application/gpx+xml", @response.content_type
426     assert_select "gpx", :count => 1 do
427       assert_select "wpt[lat=#{notes(:open_note).lat}][lon=#{notes(:open_note).lon}]", :count => 1 do
428         assert_select "extension", :count => 1 do
429           assert_select "id", notes(:open_note).id
430           assert_select "url", note_url(notes(:open_note), :format => "gpx")
431           assert_select "comment_url", comment_note_url(notes(:open_note), :format => "gpx")
432           assert_select "close_url", close_note_url(notes(:open_note), :format => "gpx")
433         end
434       end
435     end
436   end
437
438   def test_show_hidden_comment
439     get :show, {:id => notes(:note_with_hidden_comment).id, :format => "json"}
440     assert_response :success
441     js = ActiveSupport::JSON.decode(@response.body)
442     assert_not_nil js
443     assert_equal "Feature", js["type"]
444     assert_equal notes(:note_with_hidden_comment).id, js["properties"]["id"]
445     assert_equal 2, js["properties"]["comments"].count
446     assert_equal "Valid comment for note 5", js["properties"]["comments"][0]["text"]
447     assert_equal "Another valid comment for note 5", js["properties"]["comments"][1]["text"]
448   end
449
450   def test_show_fail
451     get :show, {:id => 12345}
452     assert_response :not_found
453
454     get :show, {:id => notes(:hidden_note_with_comment).id}
455     assert_response :gone
456   end
457
458   def test_destroy_success
459     delete :destroy, {:id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"}
460     assert_response :unauthorized
461
462     basic_authorization(users(:public_user).email, "test")
463
464     delete :destroy, {:id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"}
465     assert_response :forbidden
466
467     basic_authorization(users(:moderator_user).email, "test")
468
469     delete :destroy, {:id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"}
470     assert_response :success
471     js = ActiveSupport::JSON.decode(@response.body)
472     assert_not_nil js
473     assert_equal "Feature", js["type"]
474     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
475     assert_equal "hidden", js["properties"]["status"]
476     assert_equal 3, js["properties"]["comments"].count
477     assert_equal "hidden", js["properties"]["comments"].last["action"]
478     assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
479     assert_equal "moderator", js["properties"]["comments"].last["user"]
480
481     get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
482     assert_response :gone
483   end
484
485   def test_destroy_fail
486     delete :destroy, {:id => 12345, :format => "json"}
487     assert_response :unauthorized
488
489     basic_authorization(users(:public_user).email, "test")
490
491     delete :destroy, {:id => 12345, :format => "json"}
492     assert_response :forbidden
493
494     basic_authorization(users(:moderator_user).email, "test")
495
496     delete :destroy, {:id => 12345, :format => "json"}
497     assert_response :not_found
498
499     delete :destroy, {:id => notes(:hidden_note_with_comment).id, :format => "json"}
500     assert_response :gone
501   end
502
503   def test_index_success
504     get :index, {:bbox => '1,1,1.2,1.2', :format => 'rss'}
505     assert_response :success
506     assert_equal "application/rss+xml", @response.content_type
507     assert_select "rss", :count => 1 do
508       assert_select "channel", :count => 1 do
509         assert_select "item", :count => 2
510       end
511     end
512
513     get :index, {:bbox => '1,1,1.2,1.2', :format => 'json'}
514     assert_response :success
515     assert_equal "application/json", @response.content_type
516     js = ActiveSupport::JSON.decode(@response.body)
517     assert_not_nil js
518     assert_equal "FeatureCollection", js["type"]
519     assert_equal 2, js["features"].count
520
521     get :index, {:bbox => '1,1,1.2,1.2', :format => 'xml'}
522     assert_response :success
523     assert_equal "application/xml", @response.content_type
524     assert_select "osm", :count => 1 do
525       assert_select "note", :count => 2
526     end
527
528     get :index, {:bbox => '1,1,1.2,1.2', :format => 'gpx'}
529     assert_response :success
530     assert_equal "application/gpx+xml", @response.content_type
531     assert_select "gpx", :count => 1 do
532       assert_select "wpt", :count => 2
533     end
534   end
535
536   def test_index_empty_area
537     get :index, {:bbox => '5,5,5.1,5.1', :format => 'rss'}
538     assert_response :success
539     assert_equal "application/rss+xml", @response.content_type
540     assert_select "rss", :count => 1 do
541       assert_select "channel", :count => 1 do
542         assert_select "item", :count => 0
543       end
544     end
545
546     get :index, {:bbox => '5,5,5.1,5.1', :format => 'json'}
547     assert_response :success
548     assert_equal "application/json", @response.content_type
549     js = ActiveSupport::JSON.decode(@response.body)
550     assert_not_nil js
551     assert_equal "FeatureCollection", js["type"]
552     assert_equal 0, js["features"].count
553
554     get :index, {:bbox => '5,5,5.1,5.1', :format => 'xml'}
555     assert_response :success
556     assert_equal "application/xml", @response.content_type
557     assert_select "osm", :count => 1 do
558       assert_select "note", :count => 0
559     end
560
561     get :index, {:bbox => '5,5,5.1,5.1', :format => 'gpx'}
562     assert_response :success
563     assert_equal "application/gpx+xml", @response.content_type
564     assert_select "gpx", :count => 1 do
565       assert_select "wpt", :count => 0
566     end
567   end
568
569   def test_index_large_area
570     get :index, {:bbox => '-2.5,-2.5,2.5,2.5', :format => :json}
571     assert_response :success
572     assert_equal "application/json", @response.content_type
573
574     get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5', :t => '2.5', :format => :json}
575     assert_response :success
576     assert_equal "application/json", @response.content_type
577
578     get :index, {:bbox => '-10,-10,12,12', :format => :json}
579     assert_response :bad_request
580     assert_equal "text/plain", @response.content_type
581
582     get :index, {:l => '-10', :b => '-10', :r => '12', :t => '12', :format => :json}
583     assert_response :bad_request
584     assert_equal "text/plain", @response.content_type
585   end
586
587   def test_index_closed
588     get :index, {:bbox => '1,1,1.7,1.7', :closed => '7', :format => 'json'}
589     assert_response :success
590     assert_equal "application/json", @response.content_type
591     js = ActiveSupport::JSON.decode(@response.body)
592     assert_not_nil js
593     assert_equal "FeatureCollection", js["type"]
594     assert_equal 4, js["features"].count
595
596     get :index, {:bbox => '1,1,1.7,1.7', :closed => '0', :format => 'json'}
597     assert_response :success
598     assert_equal "application/json", @response.content_type
599     js = ActiveSupport::JSON.decode(@response.body)
600     assert_not_nil js
601     assert_equal "FeatureCollection", js["type"]
602     assert_equal 4, js["features"].count
603
604     get :index, {:bbox => '1,1,1.7,1.7', :closed => '-1', :format => 'json'}
605     assert_response :success
606     assert_equal "application/json", @response.content_type
607     js = ActiveSupport::JSON.decode(@response.body)
608     assert_not_nil js
609     assert_equal "FeatureCollection", js["type"]
610     assert_equal 6, js["features"].count
611   end
612
613   def test_index_bad_params
614     get :index, {:bbox => '-2.5,-2.5,2.5'}
615     assert_response :bad_request
616
617     get :index, {:bbox => '-2.5,-2.5,2.5,2.5,2.5'}
618     assert_response :bad_request
619
620     get :index, {:b => '-2.5', :r => '2.5', :t => '2.5'}
621     assert_response :bad_request
622
623     get :index, {:l => '-2.5', :r => '2.5', :t => '2.5'}
624     assert_response :bad_request
625
626     get :index, {:l => '-2.5', :b => '-2.5', :t => '2.5'}
627     assert_response :bad_request
628
629     get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5'}
630     assert_response :bad_request
631
632     get :index, {:bbox => '1,1,1.7,1.7', :limit => '0', :format => 'json'}
633     assert_response :bad_request
634
635     get :index, {:bbox => '1,1,1.7,1.7', :limit => '10001', :format => 'json'}
636     assert_response :bad_request
637   end
638
639   def test_search_success
640     get :search, {:q => 'note 1', :format => 'xml'}
641     assert_response :success
642     assert_equal "application/xml", @response.content_type
643     assert_select "osm", :count => 1 do
644       assert_select "note", :count => 1
645     end
646
647     get :search, {:q => 'note 1', :format => 'json'}
648     assert_response :success
649     assert_equal "application/json", @response.content_type
650     js = ActiveSupport::JSON.decode(@response.body)
651     assert_not_nil js
652     assert_equal "FeatureCollection", js["type"]
653     assert_equal 1, js["features"].count
654
655     get :search, {:q => 'note 1', :format => 'rss'}
656     assert_response :success
657     assert_equal "application/rss+xml", @response.content_type
658     assert_select "rss", :count => 1 do
659       assert_select "channel", :count => 1 do
660         assert_select "item", :count => 1
661       end
662     end
663
664     get :search, {:q => 'note 1', :format => 'gpx'}
665     assert_response :success
666     assert_equal "application/gpx+xml", @response.content_type
667     assert_select "gpx", :count => 1 do
668       assert_select "wpt", :count => 1
669     end
670   end
671
672   def test_search_no_match
673     get :search, {:q => 'no match', :format => 'xml'}
674     assert_response :success
675     assert_equal "application/xml", @response.content_type
676     assert_select "osm", :count => 1 do
677       assert_select "note", :count => 0
678     end
679
680     get :search, {:q => 'no match', :format => 'json'}
681     assert_response :success
682     assert_equal "application/json", @response.content_type
683     js = ActiveSupport::JSON.decode(@response.body)
684     assert_not_nil js
685     assert_equal "FeatureCollection", js["type"]
686     assert_equal 0, js["features"].count
687
688     get :search, {:q => 'no match', :format => 'rss'}
689     assert_response :success
690     assert_equal "application/rss+xml", @response.content_type
691     assert_select "rss", :count => 1 do
692       assert_select "channel", :count => 1 do
693         assert_select "item", :count => 0
694       end
695     end
696
697     get :search, {:q => 'no match', :format => 'gpx'}
698     assert_response :success
699     assert_equal "application/gpx+xml", @response.content_type
700     assert_select "gpx", :count => 1 do
701       assert_select "wpt", :count => 0
702     end
703   end
704
705   def test_search_bad_params
706     get :search
707     assert_response :bad_request
708
709     get :search, {:q => 'no match', :limit => '0', :format => 'json'}
710     assert_response :bad_request
711
712     get :search, {:q => 'no match', :limit => '10001', :format => 'json'}
713     assert_response :bad_request
714   end
715
716   def test_feed_success
717     get :feed, {:format => "rss"}
718     assert_response :success
719     assert_equal "application/rss+xml", @response.content_type
720     assert_select "rss", :count => 1 do
721       assert_select "channel", :count => 1 do
722         assert_select "item", :count => 8
723       end
724     end
725
726     get :feed, {:bbox => "1,1,1.2,1.2", :format => "rss"}
727     assert_response :success    
728     assert_equal "application/rss+xml", @response.content_type
729     assert_select "rss", :count => 1 do
730       assert_select "channel", :count => 1 do
731         assert_select "item", :count => 3
732       end
733     end
734   end
735
736   def test_feed_fail
737     get :feed, {:bbox => "1,1,1.2", :format => "rss"}
738     assert_response :bad_request
739
740     get :feed, {:bbox => "1,1,1.2,1.2,1.2", :format => "rss"}
741     assert_response :bad_request
742
743     get :feed, {:bbox => "1,1,1.2,1.2", :limit => '0', :format => "rss"}
744     assert_response :bad_request
745
746     get :feed, {:bbox => "1,1,1.2,1.2", :limit => '10001', :format => "rss"}
747     assert_response :bad_request
748   end
749
750   def test_mine_success
751     get :mine, {:display_name => "test"}
752     assert_response :success
753
754     get :mine, {:display_name => "pulibc_test2"}
755     assert_response :success
756
757     get :mine, {:display_name => "non-existent"}
758     assert_response :not_found  
759   end
760 end