]> git.openstreetmap.org Git - rails.git/blob - test/controllers/notes_controller_test.rb
Add extra tests for API and redaction controllers
[rails.git] / test / controllers / notes_controller_test.rb
1 require "test_helper"
2
3 class NotesControllerTest < ActionController::TestCase
4   fixtures :users, :user_roles, :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", 1 do
126       assert_difference "NoteComment.count", 1 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", 1 do
218       assert_no_difference "ActionMailer::Base.deliveries.size" do
219         post :comment, :id => notes(:open_note_with_comment).id, :text => "This is an additional comment", :format => "json"
220       end
221     end
222     assert_response :success
223     js = ActiveSupport::JSON.decode(@response.body)
224     assert_not_nil js
225     assert_equal "Feature", js["type"]
226     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
227     assert_equal "open", js["properties"]["status"]
228     assert_equal 3, js["properties"]["comments"].count
229     assert_equal "commented", js["properties"]["comments"].last["action"]
230     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
231     assert_nil js["properties"]["comments"].last["user"]
232
233     get :show, :id => notes(:open_note_with_comment).id, :format => "json"
234     assert_response :success
235     js = ActiveSupport::JSON.decode(@response.body)
236     assert_not_nil js
237     assert_equal "Feature", js["type"]
238     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
239     assert_equal "open", js["properties"]["status"]
240     assert_equal 3, js["properties"]["comments"].count
241     assert_equal "commented", js["properties"]["comments"].last["action"]
242     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
243     assert_nil js["properties"]["comments"].last["user"]
244
245     assert_difference "NoteComment.count", 1 do
246       assert_difference "ActionMailer::Base.deliveries.size", 2 do
247         post :comment, :id => notes(:note_with_comments_by_users).id, :text => "This is an additional comment", :format => "json"
248       end
249     end
250     assert_response :success
251     js = ActiveSupport::JSON.decode(@response.body)
252     assert_not_nil js
253     assert_equal "Feature", js["type"]
254     assert_equal notes(:note_with_comments_by_users).id, js["properties"]["id"]
255     assert_equal "open", js["properties"]["status"]
256     assert_equal 3, js["properties"]["comments"].count
257     assert_equal "commented", js["properties"]["comments"].last["action"]
258     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
259     assert_nil js["properties"]["comments"].last["user"]
260
261     email = ActionMailer::Base.deliveries.first
262     assert_equal 1, email.to.length
263     assert_equal "[OpenStreetMap] An anonymous user has commented on one of your notes", email.subject
264     assert_equal "test@openstreetmap.org", email.to.first
265
266     email = ActionMailer::Base.deliveries.second
267     assert_equal 1, email.to.length
268     assert_equal "[OpenStreetMap] An anonymous user has commented on a note you are interested in", email.subject
269     assert_equal "public@OpenStreetMap.org", email.to.first
270
271     get :show, :id => notes(:note_with_comments_by_users).id, :format => "json"
272     assert_response :success
273     js = ActiveSupport::JSON.decode(@response.body)
274     assert_not_nil js
275     assert_equal "Feature", js["type"]
276     assert_equal notes(:note_with_comments_by_users).id, js["properties"]["id"]
277     assert_equal "open", js["properties"]["status"]
278     assert_equal 3, js["properties"]["comments"].count
279     assert_equal "commented", js["properties"]["comments"].last["action"]
280     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
281     assert_nil js["properties"]["comments"].last["user"]
282
283     ActionMailer::Base.deliveries.clear
284   end
285
286   def test_comment_fail
287     assert_no_difference "NoteComment.count" do
288       post :comment, :text => "This is an additional comment"
289     end
290     assert_response :bad_request
291
292     assert_no_difference "NoteComment.count" do
293       post :comment, :id => notes(:open_note_with_comment).id
294     end
295     assert_response :bad_request
296
297     assert_no_difference "NoteComment.count" do
298       post :comment, :id => notes(:open_note_with_comment).id, :text => ""
299     end
300     assert_response :bad_request
301
302     assert_no_difference "NoteComment.count" do
303       post :comment, :id => 12345, :text => "This is an additional comment"
304     end
305     assert_response :not_found
306
307     assert_no_difference "NoteComment.count" do
308       post :comment, :id => notes(:hidden_note_with_comment).id, :text => "This is an additional comment"
309     end
310     assert_response :gone
311
312     assert_no_difference "NoteComment.count" do
313       post :comment, :id => notes(:closed_note_with_comment).id, :text => "This is an additional comment"
314     end
315     assert_response :conflict
316   end
317
318   def test_close_success
319     post :close, :id => notes(:open_note_with_comment).id, :text => "This is a close comment", :format => "json"
320     assert_response :unauthorized
321
322     basic_authorization(users(:public_user).email, "test")
323
324     post :close, :id => notes(:open_note_with_comment).id, :text => "This is a close comment", :format => "json"
325     assert_response :success
326     js = ActiveSupport::JSON.decode(@response.body)
327     assert_not_nil js
328     assert_equal "Feature", js["type"]
329     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
330     assert_equal "closed", js["properties"]["status"]
331     assert_equal 3, js["properties"]["comments"].count
332     assert_equal "closed", js["properties"]["comments"].last["action"]
333     assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
334     assert_equal "test2", js["properties"]["comments"].last["user"]
335
336     get :show, :id => notes(:open_note_with_comment).id, :format => "json"
337     assert_response :success
338     js = ActiveSupport::JSON.decode(@response.body)
339     assert_not_nil js
340     assert_equal "Feature", js["type"]
341     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
342     assert_equal "closed", js["properties"]["status"]
343     assert_equal 3, js["properties"]["comments"].count
344     assert_equal "closed", js["properties"]["comments"].last["action"]
345     assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
346     assert_equal "test2", js["properties"]["comments"].last["user"]
347   end
348
349   def test_close_fail
350     post :close
351     assert_response :unauthorized
352
353     basic_authorization(users(:public_user).email, "test")
354
355     post :close
356     assert_response :bad_request
357
358     post :close, :id => 12345
359     assert_response :not_found
360
361     post :close, :id => notes(:hidden_note_with_comment).id
362     assert_response :gone
363
364     post :close, :id => notes(:closed_note_with_comment).id
365     assert_response :conflict
366   end
367
368   def test_reopen_success
369     post :reopen, :id => notes(:closed_note_with_comment).id, :text => "This is a reopen comment", :format => "json"
370     assert_response :unauthorized
371
372     basic_authorization(users(:public_user).email, "test")
373
374     post :reopen, :id => notes(:closed_note_with_comment).id, :text => "This is a reopen comment", :format => "json"
375     assert_response :success
376     js = ActiveSupport::JSON.decode(@response.body)
377     assert_not_nil js
378     assert_equal "Feature", js["type"]
379     assert_equal notes(:closed_note_with_comment).id, js["properties"]["id"]
380     assert_equal "open", js["properties"]["status"]
381     assert_equal 2, js["properties"]["comments"].count
382     assert_equal "reopened", js["properties"]["comments"].last["action"]
383     assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
384     assert_equal "test2", js["properties"]["comments"].last["user"]
385
386     get :show, :id => notes(:closed_note_with_comment).id, :format => "json"
387     assert_response :success
388     js = ActiveSupport::JSON.decode(@response.body)
389     assert_not_nil js
390     assert_equal "Feature", js["type"]
391     assert_equal notes(:closed_note_with_comment).id, js["properties"]["id"]
392     assert_equal "open", js["properties"]["status"]
393     assert_equal 2, js["properties"]["comments"].count
394     assert_equal "reopened", js["properties"]["comments"].last["action"]
395     assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
396     assert_equal "test2", js["properties"]["comments"].last["user"]
397   end
398
399   def test_reopen_fail
400     post :reopen, :id => notes(:hidden_note_with_comment).id
401     assert_response :unauthorized
402
403     basic_authorization(users(:public_user).email, "test")
404
405     post :reopen, :id => 12345
406     assert_response :not_found
407
408     post :reopen, :id => notes(:hidden_note_with_comment).id
409     assert_response :gone
410
411     post :reopen, :id => notes(:open_note_with_comment).id
412     assert_response :conflict
413   end
414
415   def test_show_success
416     get :show, :id => notes(:open_note).id, :format => "xml"
417     assert_response :success
418     assert_equal "application/xml", @response.content_type
419     assert_select "osm", :count => 1 do
420       assert_select "note[lat='#{notes(:open_note).lat}'][lon='#{notes(:open_note).lon}']", :count => 1 do
421         assert_select "id", notes(:open_note).id
422         assert_select "url", note_url(notes(:open_note), :format => "xml")
423         assert_select "comment_url", comment_note_url(notes(:open_note), :format => "xml")
424         assert_select "close_url", close_note_url(notes(:open_note), :format => "xml")
425         assert_select "date_created", notes(:open_note).created_at.to_s
426         assert_select "status", notes(:open_note).status
427         assert_select "comments", :count => 1 do
428           assert_select "comment", :count => 1
429         end
430       end
431     end
432
433     get :show, :id => notes(:open_note).id, :format => "rss"
434     assert_response :success
435     assert_equal "application/rss+xml", @response.content_type
436     assert_select "rss", :count => 1 do
437       assert_select "channel", :count => 1 do
438         assert_select "item", :count => 1 do
439           assert_select "link", browse_note_url(notes(:open_note))
440           assert_select "guid", note_url(notes(:open_note))
441           assert_select "pubDate", notes(:open_note).created_at.to_s(:rfc822)
442           #          assert_select "geo:lat", notes(:open_note).lat.to_s
443           #          assert_select "geo:long", notes(:open_note).lon
444           #          assert_select "georss:point", "#{notes(:open_note).lon} #{notes(:open_note).lon}"
445         end
446       end
447     end
448
449     get :show, :id => notes(:open_note).id, :format => "json"
450     assert_response :success
451     assert_equal "application/json", @response.content_type
452     js = ActiveSupport::JSON.decode(@response.body)
453     assert_not_nil js
454     assert_equal "Feature", js["type"]
455     assert_equal "Point", js["geometry"]["type"]
456     assert_equal notes(:open_note).lat, js["geometry"]["coordinates"][0]
457     assert_equal notes(:open_note).lon, js["geometry"]["coordinates"][1]
458     assert_equal notes(:open_note).id, js["properties"]["id"]
459     assert_equal note_url(notes(:open_note), :format => "json"), js["properties"]["url"]
460     assert_equal comment_note_url(notes(:open_note), :format => "json"), js["properties"]["comment_url"]
461     assert_equal close_note_url(notes(:open_note), :format => "json"), js["properties"]["close_url"]
462     assert_equal notes(:open_note).created_at, js["properties"]["date_created"]
463     assert_equal notes(:open_note).status, js["properties"]["status"]
464
465     get :show, :id => notes(:open_note).id, :format => "gpx"
466     assert_response :success
467     assert_equal "application/gpx+xml", @response.content_type
468     assert_select "gpx", :count => 1 do
469       assert_select "wpt[lat='#{notes(:open_note).lat}'][lon='#{notes(:open_note).lon}']", :count => 1 do
470         assert_select "extension", :count => 1 do
471           assert_select "id", notes(:open_note).id
472           assert_select "url", note_url(notes(:open_note), :format => "gpx")
473           assert_select "comment_url", comment_note_url(notes(:open_note), :format => "gpx")
474           assert_select "close_url", close_note_url(notes(:open_note), :format => "gpx")
475         end
476       end
477     end
478   end
479
480   def test_show_hidden_comment
481     get :show, :id => notes(:note_with_hidden_comment).id, :format => "json"
482     assert_response :success
483     js = ActiveSupport::JSON.decode(@response.body)
484     assert_not_nil js
485     assert_equal "Feature", js["type"]
486     assert_equal notes(:note_with_hidden_comment).id, js["properties"]["id"]
487     assert_equal 2, js["properties"]["comments"].count
488     assert_equal "Valid comment for note 5", js["properties"]["comments"][0]["text"]
489     assert_equal "Another valid comment for note 5", js["properties"]["comments"][1]["text"]
490   end
491
492   def test_show_fail
493     get :show, :id => 12345
494     assert_response :not_found
495
496     get :show, :id => notes(:hidden_note_with_comment).id
497     assert_response :gone
498   end
499
500   def test_destroy_success
501     delete :destroy, :id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"
502     assert_response :unauthorized
503
504     basic_authorization(users(:public_user).email, "test")
505
506     delete :destroy, :id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"
507     assert_response :forbidden
508
509     basic_authorization(users(:moderator_user).email, "test")
510
511     delete :destroy, :id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"
512     assert_response :success
513     js = ActiveSupport::JSON.decode(@response.body)
514     assert_not_nil js
515     assert_equal "Feature", js["type"]
516     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
517     assert_equal "hidden", js["properties"]["status"]
518     assert_equal 3, js["properties"]["comments"].count
519     assert_equal "hidden", js["properties"]["comments"].last["action"]
520     assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
521     assert_equal "moderator", js["properties"]["comments"].last["user"]
522
523     get :show, :id => notes(:open_note_with_comment).id, :format => "json"
524     assert_response :gone
525   end
526
527   def test_destroy_fail
528     delete :destroy, :id => 12345, :format => "json"
529     assert_response :unauthorized
530
531     basic_authorization(users(:public_user).email, "test")
532
533     delete :destroy, :id => 12345, :format => "json"
534     assert_response :forbidden
535
536     basic_authorization(users(:moderator_user).email, "test")
537
538     delete :destroy, :id => 12345, :format => "json"
539     assert_response :not_found
540
541     delete :destroy, :id => notes(:hidden_note_with_comment).id, :format => "json"
542     assert_response :gone
543   end
544
545   def test_index_success
546     get :index, :bbox => "1,1,1.2,1.2", :format => "rss"
547     assert_response :success
548     assert_equal "application/rss+xml", @response.content_type
549     assert_select "rss", :count => 1 do
550       assert_select "channel", :count => 1 do
551         assert_select "item", :count => 2
552       end
553     end
554
555     get :index, :bbox => "1,1,1.2,1.2", :format => "json"
556     assert_response :success
557     assert_equal "application/json", @response.content_type
558     js = ActiveSupport::JSON.decode(@response.body)
559     assert_not_nil js
560     assert_equal "FeatureCollection", js["type"]
561     assert_equal 2, js["features"].count
562
563     get :index, :bbox => "1,1,1.2,1.2", :format => "xml"
564     assert_response :success
565     assert_equal "application/xml", @response.content_type
566     assert_select "osm", :count => 1 do
567       assert_select "note", :count => 2
568     end
569
570     get :index, :bbox => "1,1,1.2,1.2", :format => "gpx"
571     assert_response :success
572     assert_equal "application/gpx+xml", @response.content_type
573     assert_select "gpx", :count => 1 do
574       assert_select "wpt", :count => 2
575     end
576   end
577
578   def test_index_empty_area
579     get :index, :bbox => "5,5,5.1,5.1", :format => "rss"
580     assert_response :success
581     assert_equal "application/rss+xml", @response.content_type
582     assert_select "rss", :count => 1 do
583       assert_select "channel", :count => 1 do
584         assert_select "item", :count => 0
585       end
586     end
587
588     get :index, :bbox => "5,5,5.1,5.1", :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 0, js["features"].count
595
596     get :index, :bbox => "5,5,5.1,5.1", :format => "xml"
597     assert_response :success
598     assert_equal "application/xml", @response.content_type
599     assert_select "osm", :count => 1 do
600       assert_select "note", :count => 0
601     end
602
603     get :index, :bbox => "5,5,5.1,5.1", :format => "gpx"
604     assert_response :success
605     assert_equal "application/gpx+xml", @response.content_type
606     assert_select "gpx", :count => 1 do
607       assert_select "wpt", :count => 0
608     end
609   end
610
611   def test_index_large_area
612     get :index, :bbox => "-2.5,-2.5,2.5,2.5", :format => :json
613     assert_response :success
614     assert_equal "application/json", @response.content_type
615
616     get :index, :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json
617     assert_response :success
618     assert_equal "application/json", @response.content_type
619
620     get :index, :bbox => "-10,-10,12,12", :format => :json
621     assert_response :bad_request
622     assert_equal "text/plain", @response.content_type
623
624     get :index, :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json
625     assert_response :bad_request
626     assert_equal "text/plain", @response.content_type
627   end
628
629   def test_index_closed
630     get :index, :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json"
631     assert_response :success
632     assert_equal "application/json", @response.content_type
633     js = ActiveSupport::JSON.decode(@response.body)
634     assert_not_nil js
635     assert_equal "FeatureCollection", js["type"]
636     assert_equal 4, js["features"].count
637
638     get :index, :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json"
639     assert_response :success
640     assert_equal "application/json", @response.content_type
641     js = ActiveSupport::JSON.decode(@response.body)
642     assert_not_nil js
643     assert_equal "FeatureCollection", js["type"]
644     assert_equal 4, js["features"].count
645
646     get :index, :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json"
647     assert_response :success
648     assert_equal "application/json", @response.content_type
649     js = ActiveSupport::JSON.decode(@response.body)
650     assert_not_nil js
651     assert_equal "FeatureCollection", js["type"]
652     assert_equal 6, js["features"].count
653   end
654
655   def test_index_bad_params
656     get :index, :bbox => "-2.5,-2.5,2.5"
657     assert_response :bad_request
658
659     get :index, :bbox => "-2.5,-2.5,2.5,2.5,2.5"
660     assert_response :bad_request
661
662     get :index, :b => "-2.5", :r => "2.5", :t => "2.5"
663     assert_response :bad_request
664
665     get :index, :l => "-2.5", :r => "2.5", :t => "2.5"
666     assert_response :bad_request
667
668     get :index, :l => "-2.5", :b => "-2.5", :t => "2.5"
669     assert_response :bad_request
670
671     get :index, :l => "-2.5", :b => "-2.5", :r => "2.5"
672     assert_response :bad_request
673
674     get :index, :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json"
675     assert_response :bad_request
676
677     get :index, :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json"
678     assert_response :bad_request
679   end
680
681   def test_search_success
682     get :search, :q => "note 1", :format => "xml"
683     assert_response :success
684     assert_equal "application/xml", @response.content_type
685     assert_select "osm", :count => 1 do
686       assert_select "note", :count => 1
687     end
688
689     get :search, :q => "note 1", :format => "json"
690     assert_response :success
691     assert_equal "application/json", @response.content_type
692     js = ActiveSupport::JSON.decode(@response.body)
693     assert_not_nil js
694     assert_equal "FeatureCollection", js["type"]
695     assert_equal 1, js["features"].count
696
697     get :search, :q => "note 1", :format => "rss"
698     assert_response :success
699     assert_equal "application/rss+xml", @response.content_type
700     assert_select "rss", :count => 1 do
701       assert_select "channel", :count => 1 do
702         assert_select "item", :count => 1
703       end
704     end
705
706     get :search, :q => "note 1", :format => "gpx"
707     assert_response :success
708     assert_equal "application/gpx+xml", @response.content_type
709     assert_select "gpx", :count => 1 do
710       assert_select "wpt", :count => 1
711     end
712   end
713
714   def test_search_no_match
715     get :search, :q => "no match", :format => "xml"
716     assert_response :success
717     assert_equal "application/xml", @response.content_type
718     assert_select "osm", :count => 1 do
719       assert_select "note", :count => 0
720     end
721
722     get :search, :q => "no match", :format => "json"
723     assert_response :success
724     assert_equal "application/json", @response.content_type
725     js = ActiveSupport::JSON.decode(@response.body)
726     assert_not_nil js
727     assert_equal "FeatureCollection", js["type"]
728     assert_equal 0, js["features"].count
729
730     get :search, :q => "no match", :format => "rss"
731     assert_response :success
732     assert_equal "application/rss+xml", @response.content_type
733     assert_select "rss", :count => 1 do
734       assert_select "channel", :count => 1 do
735         assert_select "item", :count => 0
736       end
737     end
738
739     get :search, :q => "no match", :format => "gpx"
740     assert_response :success
741     assert_equal "application/gpx+xml", @response.content_type
742     assert_select "gpx", :count => 1 do
743       assert_select "wpt", :count => 0
744     end
745   end
746
747   def test_search_bad_params
748     get :search
749     assert_response :bad_request
750
751     get :search, :q => "no match", :limit => "0", :format => "json"
752     assert_response :bad_request
753
754     get :search, :q => "no match", :limit => "10001", :format => "json"
755     assert_response :bad_request
756   end
757
758   def test_feed_success
759     get :feed, :format => "rss"
760     assert_response :success
761     assert_equal "application/rss+xml", @response.content_type
762     assert_select "rss", :count => 1 do
763       assert_select "channel", :count => 1 do
764         assert_select "item", :count => 8
765       end
766     end
767
768     get :feed, :bbox => "1,1,1.2,1.2", :format => "rss"
769     assert_response :success
770     assert_equal "application/rss+xml", @response.content_type
771     assert_select "rss", :count => 1 do
772       assert_select "channel", :count => 1 do
773         assert_select "item", :count => 3
774       end
775     end
776   end
777
778   def test_feed_fail
779     get :feed, :bbox => "1,1,1.2", :format => "rss"
780     assert_response :bad_request
781
782     get :feed, :bbox => "1,1,1.2,1.2,1.2", :format => "rss"
783     assert_response :bad_request
784
785     get :feed, :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss"
786     assert_response :bad_request
787
788     get :feed, :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss"
789     assert_response :bad_request
790   end
791
792   def test_mine_success
793     get :mine, :display_name => "test"
794     assert_response :success
795
796     get :mine, :display_name => "pulibc_test2"
797     assert_response :success
798
799     get :mine, :display_name => "non-existent"
800     assert_response :not_found
801   end
802 end