4 class NotesControllerTest < ActionController::TestCase
6 # Stub nominatim response for note locations
7 stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
8 .to_return(:status => 404)
12 # test all routes which lead to this controller
15 { :path => "/api/0.6/notes", :method => :post },
16 { :controller => "api/notes", :action => "create", :format => "xml" }
19 { :path => "/api/0.6/notes/1", :method => :get },
20 { :controller => "api/notes", :action => "show", :id => "1", :format => "xml" }
23 { :controller => "api/notes", :action => "show", :id => "1", :format => "xml" },
24 { :path => "/api/0.6/notes/1.xml", :method => :get }
27 { :path => "/api/0.6/notes/1.rss", :method => :get },
28 { :controller => "api/notes", :action => "show", :id => "1", :format => "rss" }
31 { :path => "/api/0.6/notes/1.json", :method => :get },
32 { :controller => "api/notes", :action => "show", :id => "1", :format => "json" }
35 { :path => "/api/0.6/notes/1.gpx", :method => :get },
36 { :controller => "api/notes", :action => "show", :id => "1", :format => "gpx" }
39 { :path => "/api/0.6/notes/1/comment", :method => :post },
40 { :controller => "api/notes", :action => "comment", :id => "1", :format => "xml" }
43 { :path => "/api/0.6/notes/1/close", :method => :post },
44 { :controller => "api/notes", :action => "close", :id => "1", :format => "xml" }
47 { :path => "/api/0.6/notes/1/reopen", :method => :post },
48 { :controller => "api/notes", :action => "reopen", :id => "1", :format => "xml" }
51 { :path => "/api/0.6/notes/1", :method => :delete },
52 { :controller => "api/notes", :action => "destroy", :id => "1", :format => "xml" }
56 { :path => "/api/0.6/notes", :method => :get },
57 { :controller => "api/notes", :action => "index", :format => "xml" }
60 { :controller => "api/notes", :action => "index", :format => "xml" },
61 { :path => "/api/0.6/notes.xml", :method => :get }
64 { :path => "/api/0.6/notes.rss", :method => :get },
65 { :controller => "api/notes", :action => "index", :format => "rss" }
68 { :path => "/api/0.6/notes.json", :method => :get },
69 { :controller => "api/notes", :action => "index", :format => "json" }
72 { :path => "/api/0.6/notes.gpx", :method => :get },
73 { :controller => "api/notes", :action => "index", :format => "gpx" }
77 { :path => "/api/0.6/notes/search", :method => :get },
78 { :controller => "api/notes", :action => "search", :format => "xml" }
81 { :controller => "api/notes", :action => "search", :format => "xml" },
82 { :path => "/api/0.6/notes/search.xml", :method => :get }
85 { :path => "/api/0.6/notes/search.rss", :method => :get },
86 { :controller => "api/notes", :action => "search", :format => "rss" }
89 { :path => "/api/0.6/notes/search.json", :method => :get },
90 { :controller => "api/notes", :action => "search", :format => "json" }
93 { :path => "/api/0.6/notes/search.gpx", :method => :get },
94 { :controller => "api/notes", :action => "search", :format => "gpx" }
98 { :path => "/api/0.6/notes/feed", :method => :get },
99 { :controller => "api/notes", :action => "feed", :format => "rss" }
103 { :controller => "api/notes", :action => "create" },
104 { :path => "/api/0.6/notes/addPOIexec", :method => :post }
107 { :controller => "api/notes", :action => "close" },
108 { :path => "/api/0.6/notes/closePOIexec", :method => :post }
111 { :controller => "api/notes", :action => "comment" },
112 { :path => "/api/0.6/notes/editPOIexec", :method => :post }
115 { :controller => "api/notes", :action => "index", :format => "gpx" },
116 { :path => "/api/0.6/notes/getGPX", :method => :get }
119 { :controller => "api/notes", :action => "feed", :format => "rss" },
120 { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
124 def test_create_success
125 assert_difference "Note.count", 1 do
126 assert_difference "NoteComment.count", 1 do
127 post :create, :params => { :lat => -1.0, :lon => -1.0, :text => "This is a comment", :format => "json" }
130 assert_response :success
131 js = ActiveSupport::JSON.decode(@response.body)
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"]
143 get :show, :params => { :id => id, :format => "json" }
144 assert_response :success
145 js = ActiveSupport::JSON.decode(@response.body)
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"]
159 assert_no_difference "Note.count" do
160 assert_no_difference "NoteComment.count" do
161 post :create, :params => { :lon => -1.0, :text => "This is a comment" }
164 assert_response :bad_request
166 assert_no_difference "Note.count" do
167 assert_no_difference "NoteComment.count" do
168 post :create, :params => { :lat => -1.0, :text => "This is a comment" }
171 assert_response :bad_request
173 assert_no_difference "Note.count" do
174 assert_no_difference "NoteComment.count" do
175 post :create, :params => { :lat => -1.0, :lon => -1.0 }
178 assert_response :bad_request
180 assert_no_difference "Note.count" do
181 assert_no_difference "NoteComment.count" do
182 post :create, :params => { :lat => -1.0, :lon => -1.0, :text => "" }
185 assert_response :bad_request
187 assert_no_difference "Note.count" do
188 assert_no_difference "NoteComment.count" do
189 post :create, :params => { :lat => -100.0, :lon => -1.0, :text => "This is a comment" }
192 assert_response :bad_request
194 assert_no_difference "Note.count" do
195 assert_no_difference "NoteComment.count" do
196 post :create, :params => { :lat => -1.0, :lon => -200.0, :text => "This is a comment" }
199 assert_response :bad_request
201 assert_no_difference "Note.count" do
202 assert_no_difference "NoteComment.count" do
203 post :create, :params => { :lat => "abc", :lon => -1.0, :text => "This is a comment" }
206 assert_response :bad_request
208 assert_no_difference "Note.count" do
209 assert_no_difference "NoteComment.count" do
210 post :create, :params => { :lat => -1.0, :lon => "abc", :text => "This is a comment" }
213 assert_response :bad_request
215 assert_no_difference "Note.count" do
216 assert_no_difference "NoteComment.count" do
217 post :create, :params => { :lat => -1.0, :lon => -1.0, :text => "x\u0000y" }
220 assert_response :bad_request
223 def test_comment_success
224 open_note_with_comment = create(:note_with_comments)
226 basic_authorization user.email, "test"
227 assert_difference "NoteComment.count", 1 do
228 assert_no_difference "ActionMailer::Base.deliveries.size" do
229 perform_enqueued_jobs do
230 post :comment, :params => { :id => open_note_with_comment.id, :text => "This is an additional comment", :format => "json" }
234 assert_response :success
235 js = ActiveSupport::JSON.decode(@response.body)
237 assert_equal "Feature", js["type"]
238 assert_equal open_note_with_comment.id, js["properties"]["id"]
239 assert_equal "open", js["properties"]["status"]
240 assert_equal 2, 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_equal user.display_name, js["properties"]["comments"].last["user"]
245 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
246 assert_response :success
247 js = ActiveSupport::JSON.decode(@response.body)
249 assert_equal "Feature", js["type"]
250 assert_equal open_note_with_comment.id, js["properties"]["id"]
251 assert_equal "open", js["properties"]["status"]
252 assert_equal 2, js["properties"]["comments"].count
253 assert_equal "commented", js["properties"]["comments"].last["action"]
254 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
255 assert_equal user.display_name, js["properties"]["comments"].last["user"]
257 # Ensure that emails are sent to users
258 first_user = create(:user)
259 second_user = create(:user)
260 third_user = create(:user)
262 note_with_comments_by_users = create(:note) do |note|
263 create(:note_comment, :note => note, :author => first_user)
264 create(:note_comment, :note => note, :author => second_user)
267 basic_authorization third_user.email, "test"
269 assert_difference "NoteComment.count", 1 do
270 assert_difference "ActionMailer::Base.deliveries.size", 2 do
271 perform_enqueued_jobs do
272 post :comment, :params => { :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json" }
276 assert_response :success
277 js = ActiveSupport::JSON.decode(@response.body)
279 assert_equal "Feature", js["type"]
280 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
281 assert_equal "open", js["properties"]["status"]
282 assert_equal 3, js["properties"]["comments"].count
283 assert_equal "commented", js["properties"]["comments"].last["action"]
284 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
285 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
287 email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
289 assert_equal 1, email.to.length
290 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on one of your notes", email.subject
291 assert_equal first_user.email, email.to.first
293 email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
295 assert_equal 1, email.to.length
296 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on a note you are interested in", email.subject
298 get :show, :params => { :id => note_with_comments_by_users.id, :format => "json" }
299 assert_response :success
300 js = ActiveSupport::JSON.decode(@response.body)
302 assert_equal "Feature", js["type"]
303 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
304 assert_equal "open", js["properties"]["status"]
305 assert_equal 3, js["properties"]["comments"].count
306 assert_equal "commented", js["properties"]["comments"].last["action"]
307 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
308 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
310 ActionMailer::Base.deliveries.clear
313 def test_comment_fail
314 open_note_with_comment = create(:note_with_comments)
318 assert_no_difference "NoteComment.count" do
319 post :comment, :params => { :text => "This is an additional comment" }
320 assert_response :unauthorized
323 basic_authorization user.email, "test"
325 assert_no_difference "NoteComment.count" do
326 post :comment, :params => { :text => "This is an additional comment" }
328 assert_response :bad_request
330 assert_no_difference "NoteComment.count" do
331 post :comment, :params => { :id => open_note_with_comment.id }
333 assert_response :bad_request
335 assert_no_difference "NoteComment.count" do
336 post :comment, :params => { :id => open_note_with_comment.id, :text => "" }
338 assert_response :bad_request
340 assert_no_difference "NoteComment.count" do
341 post :comment, :params => { :id => 12345, :text => "This is an additional comment" }
343 assert_response :not_found
345 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
347 assert_no_difference "NoteComment.count" do
348 post :comment, :params => { :id => hidden_note_with_comment.id, :text => "This is an additional comment" }
350 assert_response :gone
352 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
354 assert_no_difference "NoteComment.count" do
355 post :comment, :params => { :id => closed_note_with_comment.id, :text => "This is an additional comment" }
357 assert_response :conflict
359 assert_no_difference "NoteComment.count" do
360 post :comment, :params => { :id => open_note_with_comment.id, :text => "x\u0000y" }
362 assert_response :bad_request
365 def test_close_success
366 open_note_with_comment = create(:note_with_comments)
369 post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
370 assert_response :unauthorized
372 basic_authorization user.email, "test"
374 post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
375 assert_response :success
376 js = ActiveSupport::JSON.decode(@response.body)
378 assert_equal "Feature", js["type"]
379 assert_equal open_note_with_comment.id, js["properties"]["id"]
380 assert_equal "closed", js["properties"]["status"]
381 assert_equal 2, js["properties"]["comments"].count
382 assert_equal "closed", js["properties"]["comments"].last["action"]
383 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
384 assert_equal user.display_name, js["properties"]["comments"].last["user"]
386 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
387 assert_response :success
388 js = ActiveSupport::JSON.decode(@response.body)
390 assert_equal "Feature", js["type"]
391 assert_equal open_note_with_comment.id, js["properties"]["id"]
392 assert_equal "closed", js["properties"]["status"]
393 assert_equal 2, js["properties"]["comments"].count
394 assert_equal "closed", js["properties"]["comments"].last["action"]
395 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
396 assert_equal user.display_name, js["properties"]["comments"].last["user"]
401 assert_response :unauthorized
403 basic_authorization create(:user).email, "test"
406 assert_response :bad_request
408 post :close, :params => { :id => 12345 }
409 assert_response :not_found
411 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
413 post :close, :params => { :id => hidden_note_with_comment.id }
414 assert_response :gone
416 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
418 post :close, :params => { :id => closed_note_with_comment.id }
419 assert_response :conflict
422 def test_reopen_success
423 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
426 post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
427 assert_response :unauthorized
429 basic_authorization user.email, "test"
431 post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
432 assert_response :success
433 js = ActiveSupport::JSON.decode(@response.body)
435 assert_equal "Feature", js["type"]
436 assert_equal closed_note_with_comment.id, js["properties"]["id"]
437 assert_equal "open", js["properties"]["status"]
438 assert_equal 2, js["properties"]["comments"].count
439 assert_equal "reopened", js["properties"]["comments"].last["action"]
440 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
441 assert_equal user.display_name, js["properties"]["comments"].last["user"]
443 get :show, :params => { :id => closed_note_with_comment.id, :format => "json" }
444 assert_response :success
445 js = ActiveSupport::JSON.decode(@response.body)
447 assert_equal "Feature", js["type"]
448 assert_equal closed_note_with_comment.id, js["properties"]["id"]
449 assert_equal "open", js["properties"]["status"]
450 assert_equal 2, js["properties"]["comments"].count
451 assert_equal "reopened", js["properties"]["comments"].last["action"]
452 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
453 assert_equal user.display_name, js["properties"]["comments"].last["user"]
457 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
459 post :reopen, :params => { :id => hidden_note_with_comment.id }
460 assert_response :unauthorized
462 basic_authorization create(:user).email, "test"
464 post :reopen, :params => { :id => 12345 }
465 assert_response :not_found
467 post :reopen, :params => { :id => hidden_note_with_comment.id }
468 assert_response :gone
470 open_note_with_comment = create(:note_with_comments)
472 post :reopen, :params => { :id => open_note_with_comment.id }
473 assert_response :conflict
476 def test_show_success
477 open_note = create(:note_with_comments)
479 get :show, :params => { :id => open_note.id, :format => "xml" }
480 assert_response :success
481 assert_equal "application/xml", @response.content_type
482 assert_select "osm", :count => 1 do
483 assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
484 assert_select "id", open_note.id.to_s
485 assert_select "url", note_url(open_note, :format => "xml")
486 assert_select "comment_url", comment_note_url(open_note, :format => "xml")
487 assert_select "close_url", close_note_url(open_note, :format => "xml")
488 assert_select "date_created", open_note.created_at.to_s
489 assert_select "status", open_note.status
490 assert_select "comments", :count => 1 do
491 assert_select "comment", :count => 1
496 get :show, :params => { :id => open_note.id, :format => "rss" }
497 assert_response :success
498 assert_equal "application/rss+xml", @response.content_type
499 assert_select "rss", :count => 1 do
500 assert_select "channel", :count => 1 do
501 assert_select "item", :count => 1 do
502 assert_select "link", browse_note_url(open_note)
503 assert_select "guid", note_url(open_note)
504 assert_select "pubDate", open_note.created_at.to_s(:rfc822)
505 # assert_select "geo:lat", open_note.lat.to_s
506 # assert_select "geo:long", open_note.lon
507 # assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
512 get :show, :params => { :id => open_note.id, :format => "json" }
513 assert_response :success
514 assert_equal "application/json", @response.content_type
515 js = ActiveSupport::JSON.decode(@response.body)
517 assert_equal "Feature", js["type"]
518 assert_equal "Point", js["geometry"]["type"]
519 assert_equal open_note.lat, js["geometry"]["coordinates"][0]
520 assert_equal open_note.lon, js["geometry"]["coordinates"][1]
521 assert_equal open_note.id, js["properties"]["id"]
522 assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
523 assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
524 assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
525 assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
526 assert_equal open_note.status, js["properties"]["status"]
528 get :show, :params => { :id => open_note.id, :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[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
533 assert_select "time", :count => 1
534 assert_select "name", "Note: #{open_note.id}"
535 assert_select "desc", :count => 1
536 assert_select "link[href='http://test.host/note/#{open_note.id}']", :count => 1
537 assert_select "extensions", :count => 1 do
538 assert_select "id", open_note.id.to_s
539 assert_select "url", note_url(open_note, :format => "gpx")
540 assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
541 assert_select "close_url", close_note_url(open_note, :format => "gpx")
547 def test_show_hidden_comment
548 note_with_hidden_comment = create(:note) do |note|
549 create(:note_comment, :note => note, :body => "Valid comment for hidden note")
550 create(:note_comment, :note => note, :visible => false)
551 create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
554 get :show, :params => { :id => note_with_hidden_comment.id, :format => "json" }
555 assert_response :success
556 js = ActiveSupport::JSON.decode(@response.body)
558 assert_equal "Feature", js["type"]
559 assert_equal note_with_hidden_comment.id, js["properties"]["id"]
560 assert_equal 2, js["properties"]["comments"].count
561 assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
562 assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
566 get :show, :params => { :id => 12345 }
567 assert_response :not_found
569 get :show, :params => { :id => create(:note, :status => "hidden").id }
570 assert_response :gone
573 def test_destroy_success
574 open_note_with_comment = create(:note_with_comments)
576 moderator_user = create(:moderator_user)
578 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
579 assert_response :unauthorized
581 basic_authorization user.email, "test"
583 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
584 assert_response :forbidden
586 basic_authorization moderator_user.email, "test"
588 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
589 assert_response :success
590 js = ActiveSupport::JSON.decode(@response.body)
592 assert_equal "Feature", js["type"]
593 assert_equal open_note_with_comment.id, js["properties"]["id"]
594 assert_equal "hidden", js["properties"]["status"]
595 assert_equal 2, js["properties"]["comments"].count
596 assert_equal "hidden", js["properties"]["comments"].last["action"]
597 assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
598 assert_equal moderator_user.display_name, js["properties"]["comments"].last["user"]
600 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
601 assert_response :success
603 basic_authorization user.email, "test"
604 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
605 assert_response :gone
608 def test_destroy_fail
610 moderator_user = create(:moderator_user)
612 delete :destroy, :params => { :id => 12345, :format => "json" }
613 assert_response :unauthorized
615 basic_authorization user.email, "test"
617 delete :destroy, :params => { :id => 12345, :format => "json" }
618 assert_response :forbidden
620 basic_authorization moderator_user.email, "test"
622 delete :destroy, :params => { :id => 12345, :format => "json" }
623 assert_response :not_found
625 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
627 delete :destroy, :params => { :id => hidden_note_with_comment.id, :format => "json" }
628 assert_response :gone
631 def test_index_success
632 position = (1.1 * GeoRecord::SCALE).to_i
633 create(:note_with_comments, :latitude => position, :longitude => position)
634 create(:note_with_comments, :latitude => position, :longitude => position)
636 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "rss" }
637 assert_response :success
638 assert_equal "application/rss+xml", @response.content_type
639 assert_select "rss", :count => 1 do
640 assert_select "channel", :count => 1 do
641 assert_select "item", :count => 2
645 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "json" }
646 assert_response :success
647 assert_equal "application/json", @response.content_type
648 js = ActiveSupport::JSON.decode(@response.body)
650 assert_equal "FeatureCollection", js["type"]
651 assert_equal 2, js["features"].count
653 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "xml" }
654 assert_response :success
655 assert_equal "application/xml", @response.content_type
656 assert_select "osm", :count => 1 do
657 assert_select "note", :count => 2
660 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "gpx" }
661 assert_response :success
662 assert_equal "application/gpx+xml", @response.content_type
663 assert_select "gpx", :count => 1 do
664 assert_select "wpt", :count => 2
669 position = (1.1 * GeoRecord::SCALE).to_i
670 create(:note_with_comments, :latitude => position, :longitude => position)
671 create(:note_with_comments, :latitude => position, :longitude => position)
673 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss" }
674 assert_response :success
675 assert_equal "application/rss+xml", @response.content_type
676 assert_select "rss", :count => 1 do
677 assert_select "channel", :count => 1 do
678 assert_select "item", :count => 1
682 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json" }
683 assert_response :success
684 assert_equal "application/json", @response.content_type
685 js = ActiveSupport::JSON.decode(@response.body)
687 assert_equal "FeatureCollection", js["type"]
688 assert_equal 1, js["features"].count
690 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml" }
691 assert_response :success
692 assert_equal "application/xml", @response.content_type
693 assert_select "osm", :count => 1 do
694 assert_select "note", :count => 1
697 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :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 => 1
705 def test_index_empty_area
706 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "rss" }
707 assert_response :success
708 assert_equal "application/rss+xml", @response.content_type
709 assert_select "rss", :count => 1 do
710 assert_select "channel", :count => 1 do
711 assert_select "item", :count => 0
715 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "json" }
716 assert_response :success
717 assert_equal "application/json", @response.content_type
718 js = ActiveSupport::JSON.decode(@response.body)
720 assert_equal "FeatureCollection", js["type"]
721 assert_equal 0, js["features"].count
723 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "xml" }
724 assert_response :success
725 assert_equal "application/xml", @response.content_type
726 assert_select "osm", :count => 1 do
727 assert_select "note", :count => 0
730 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "gpx" }
731 assert_response :success
732 assert_equal "application/gpx+xml", @response.content_type
733 assert_select "gpx", :count => 1 do
734 assert_select "wpt", :count => 0
738 def test_index_large_area
739 get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5", :format => :json }
740 assert_response :success
741 assert_equal "application/json", @response.content_type
743 get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json }
744 assert_response :success
745 assert_equal "application/json", @response.content_type
747 get :index, :params => { :bbox => "-10,-10,12,12", :format => :json }
748 assert_response :bad_request
749 assert_equal "application/json", @response.content_type
751 get :index, :params => { :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json }
752 assert_response :bad_request
753 assert_equal "application/json", @response.content_type
756 def test_index_closed
757 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 5.days)
758 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 100.days)
759 create(:note_with_comments, :status => "hidden")
760 create(:note_with_comments)
762 # Open notes + closed in last 7 days
763 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json" }
764 assert_response :success
765 assert_equal "application/json", @response.content_type
766 js = ActiveSupport::JSON.decode(@response.body)
768 assert_equal "FeatureCollection", js["type"]
769 assert_equal 2, js["features"].count
772 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json" }
773 assert_response :success
774 assert_equal "application/json", @response.content_type
775 js = ActiveSupport::JSON.decode(@response.body)
777 assert_equal "FeatureCollection", js["type"]
778 assert_equal 1, js["features"].count
780 # Open notes + all closed notes
781 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json" }
782 assert_response :success
783 assert_equal "application/json", @response.content_type
784 js = ActiveSupport::JSON.decode(@response.body)
786 assert_equal "FeatureCollection", js["type"]
787 assert_equal 3, js["features"].count
790 def test_index_bad_params
791 get :index, :params => { :bbox => "-2.5,-2.5,2.5" }
792 assert_response :bad_request
794 get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5,2.5" }
795 assert_response :bad_request
797 get :index, :params => { :b => "-2.5", :r => "2.5", :t => "2.5" }
798 assert_response :bad_request
800 get :index, :params => { :l => "-2.5", :r => "2.5", :t => "2.5" }
801 assert_response :bad_request
803 get :index, :params => { :l => "-2.5", :b => "-2.5", :t => "2.5" }
804 assert_response :bad_request
806 get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5" }
807 assert_response :bad_request
809 get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json" }
810 assert_response :bad_request
812 get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json" }
813 assert_response :bad_request
816 def test_search_success
817 create(:note_with_comments)
819 get :search, :params => { :q => "note comment", :format => "xml" }
820 assert_response :success
821 assert_equal "application/xml", @response.content_type
822 assert_select "osm", :count => 1 do
823 assert_select "note", :count => 1
826 get :search, :params => { :q => "note comment", :format => "json" }
827 assert_response :success
828 assert_equal "application/json", @response.content_type
829 js = ActiveSupport::JSON.decode(@response.body)
831 assert_equal "FeatureCollection", js["type"]
832 assert_equal 1, js["features"].count
834 get :search, :params => { :q => "note comment", :format => "rss" }
835 assert_response :success
836 assert_equal "application/rss+xml", @response.content_type
837 assert_select "rss", :count => 1 do
838 assert_select "channel", :count => 1 do
839 assert_select "item", :count => 1
843 get :search, :params => { :q => "note comment", :format => "gpx" }
844 assert_response :success
845 assert_equal "application/gpx+xml", @response.content_type
846 assert_select "gpx", :count => 1 do
847 assert_select "wpt", :count => 1
851 def test_search_by_display_name_success
854 create(:note) do |note|
855 create(:note_comment, :note => note, :author => user)
858 get :search, :params => { :display_name => user.display_name, :format => "xml" }
859 assert_response :success
860 assert_equal "application/xml", @response.content_type
861 assert_select "osm", :count => 1 do
862 assert_select "note", :count => 1
865 get :search, :params => { :display_name => user.display_name, :format => "json" }
866 assert_response :success
867 assert_equal "application/json", @response.content_type
868 js = ActiveSupport::JSON.decode(@response.body)
870 assert_equal "FeatureCollection", js["type"]
871 assert_equal 1, js["features"].count
873 get :search, :params => { :display_name => user.display_name, :format => "rss" }
874 assert_response :success
875 assert_equal "application/rss+xml", @response.content_type
876 assert_select "rss", :count => 1 do
877 assert_select "channel", :count => 1 do
878 assert_select "item", :count => 1
882 get :search, :params => { :display_name => user.display_name, :format => "gpx" }
883 assert_response :success
884 assert_equal "application/gpx+xml", @response.content_type
885 assert_select "gpx", :count => 1 do
886 assert_select "wpt", :count => 1
890 def test_search_by_user_success
893 create(:note) do |note|
894 create(:note_comment, :note => note, :author => user)
897 get :search, :params => { :user => user.id, :format => "xml" }
898 assert_response :success
899 assert_equal "application/xml", @response.content_type
900 assert_select "osm", :count => 1 do
901 assert_select "note", :count => 1
904 get :search, :params => { :user => user.id, :format => "json" }
905 assert_response :success
906 assert_equal "application/json", @response.content_type
907 js = ActiveSupport::JSON.decode(@response.body)
909 assert_equal "FeatureCollection", js["type"]
910 assert_equal 1, js["features"].count
912 get :search, :params => { :user => user.id, :format => "rss" }
913 assert_response :success
914 assert_equal "application/rss+xml", @response.content_type
915 assert_select "rss", :count => 1 do
916 assert_select "channel", :count => 1 do
917 assert_select "item", :count => 1
921 get :search, :params => { :user => user.id, :format => "gpx" }
922 assert_response :success
923 assert_equal "application/gpx+xml", @response.content_type
924 assert_select "gpx", :count => 1 do
925 assert_select "wpt", :count => 1
929 def test_search_no_match
930 create(:note_with_comments)
932 get :search, :params => { :q => "no match", :format => "xml" }
933 assert_response :success
934 assert_equal "application/xml", @response.content_type
935 assert_select "osm", :count => 1 do
936 assert_select "note", :count => 0
939 get :search, :params => { :q => "no match", :format => "json" }
940 assert_response :success
941 assert_equal "application/json", @response.content_type
942 js = ActiveSupport::JSON.decode(@response.body)
944 assert_equal "FeatureCollection", js["type"]
945 assert_equal 0, js["features"].count
947 get :search, :params => { :q => "no match", :format => "rss" }
948 assert_response :success
949 assert_equal "application/rss+xml", @response.content_type
950 assert_select "rss", :count => 1 do
951 assert_select "channel", :count => 1 do
952 assert_select "item", :count => 0
956 get :search, :params => { :q => "no match", :format => "gpx" }
957 assert_response :success
958 assert_equal "application/gpx+xml", @response.content_type
959 assert_select "gpx", :count => 1 do
960 assert_select "wpt", :count => 0
964 def test_search_by_time_no_match
965 create(:note_with_comments)
967 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "xml" }
968 assert_response :success
969 assert_equal "application/xml", @response.content_type
970 assert_select "osm", :count => 1 do
971 assert_select "note", :count => 0
974 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "json" }
975 assert_response :success
976 assert_equal "application/json", @response.content_type
977 js = ActiveSupport::JSON.decode(@response.body)
979 assert_equal "FeatureCollection", js["type"]
980 assert_equal 0, js["features"].count
982 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "rss" }
983 assert_response :success
984 assert_equal "application/rss+xml", @response.content_type
985 assert_select "rss", :count => 1 do
986 assert_select "channel", :count => 1 do
987 assert_select "item", :count => 0
991 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "gpx" }
992 assert_response :success
993 assert_equal "application/gpx+xml", @response.content_type
994 assert_select "gpx", :count => 1 do
995 assert_select "wpt", :count => 0
999 def test_search_bad_params
1000 get :search, :params => { :q => "no match", :limit => "0", :format => "json" }
1001 assert_response :bad_request
1003 get :search, :params => { :q => "no match", :limit => "10001", :format => "json" }
1004 assert_response :bad_request
1006 get :search, :params => { :display_name => "non-existent" }
1007 assert_response :bad_request
1009 get :search, :params => { :user => "-1" }
1010 assert_response :bad_request
1012 get :search, :params => { :from => "wrong-date", :to => "wrong-date" }
1013 assert_response :bad_request
1015 get :search, :params => { :from => "01.01.2010", :to => "2010.01.2010" }
1016 assert_response :bad_request
1019 def test_feed_success
1020 position = (1.1 * GeoRecord::SCALE).to_i
1021 create(:note_with_comments, :latitude => position, :longitude => position)
1022 create(:note_with_comments, :latitude => position, :longitude => position)
1023 position = (1.5 * GeoRecord::SCALE).to_i
1024 create(:note_with_comments, :latitude => position, :longitude => position)
1025 create(:note_with_comments, :latitude => position, :longitude => position)
1027 get :feed, :params => { :format => "rss" }
1028 assert_response :success
1029 assert_equal "application/rss+xml", @response.content_type
1030 assert_select "rss", :count => 1 do
1031 assert_select "channel", :count => 1 do
1032 assert_select "item", :count => 4
1036 get :feed, :params => { :bbox => "1,1,1.2,1.2", :format => "rss" }
1037 assert_response :success
1038 assert_equal "application/rss+xml", @response.content_type
1039 assert_select "rss", :count => 1 do
1040 assert_select "channel", :count => 1 do
1041 assert_select "item", :count => 2
1047 get :feed, :params => { :bbox => "1,1,1.2", :format => "rss" }
1048 assert_response :bad_request
1050 get :feed, :params => { :bbox => "1,1,1.2,1.2,1.2", :format => "rss" }
1051 assert_response :bad_request
1053 get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss" }
1054 assert_response :bad_request
1056 get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss" }
1057 assert_response :bad_request