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