4 class NotesControllerTest < ActionController::TestCase
7 # Stub nominatim response for note locations
8 stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
9 .to_return(:status => 404)
13 # test all routes which lead to this controller
16 { :path => "/api/0.6/notes", :method => :post },
17 { :controller => "api/notes", :action => "create", :format => "xml" }
20 { :path => "/api/0.6/notes/1", :method => :get },
21 { :controller => "api/notes", :action => "show", :id => "1", :format => "xml" }
24 { :controller => "api/notes", :action => "show", :id => "1", :format => "xml" },
25 { :path => "/api/0.6/notes/1.xml", :method => :get }
28 { :path => "/api/0.6/notes/1.rss", :method => :get },
29 { :controller => "api/notes", :action => "show", :id => "1", :format => "rss" }
32 { :path => "/api/0.6/notes/1.json", :method => :get },
33 { :controller => "api/notes", :action => "show", :id => "1", :format => "json" }
36 { :path => "/api/0.6/notes/1.gpx", :method => :get },
37 { :controller => "api/notes", :action => "show", :id => "1", :format => "gpx" }
40 { :path => "/api/0.6/notes/1/comment", :method => :post },
41 { :controller => "api/notes", :action => "comment", :id => "1", :format => "xml" }
44 { :path => "/api/0.6/notes/1/close", :method => :post },
45 { :controller => "api/notes", :action => "close", :id => "1", :format => "xml" }
48 { :path => "/api/0.6/notes/1/reopen", :method => :post },
49 { :controller => "api/notes", :action => "reopen", :id => "1", :format => "xml" }
52 { :path => "/api/0.6/notes/1", :method => :delete },
53 { :controller => "api/notes", :action => "destroy", :id => "1", :format => "xml" }
57 { :path => "/api/0.6/notes", :method => :get },
58 { :controller => "api/notes", :action => "index", :format => "xml" }
61 { :controller => "api/notes", :action => "index", :format => "xml" },
62 { :path => "/api/0.6/notes.xml", :method => :get }
65 { :path => "/api/0.6/notes.rss", :method => :get },
66 { :controller => "api/notes", :action => "index", :format => "rss" }
69 { :path => "/api/0.6/notes.json", :method => :get },
70 { :controller => "api/notes", :action => "index", :format => "json" }
73 { :path => "/api/0.6/notes.gpx", :method => :get },
74 { :controller => "api/notes", :action => "index", :format => "gpx" }
78 { :path => "/api/0.6/notes/search", :method => :get },
79 { :controller => "api/notes", :action => "search", :format => "xml" }
82 { :controller => "api/notes", :action => "search", :format => "xml" },
83 { :path => "/api/0.6/notes/search.xml", :method => :get }
86 { :path => "/api/0.6/notes/search.rss", :method => :get },
87 { :controller => "api/notes", :action => "search", :format => "rss" }
90 { :path => "/api/0.6/notes/search.json", :method => :get },
91 { :controller => "api/notes", :action => "search", :format => "json" }
94 { :path => "/api/0.6/notes/search.gpx", :method => :get },
95 { :controller => "api/notes", :action => "search", :format => "gpx" }
99 { :path => "/api/0.6/notes/feed", :method => :get },
100 { :controller => "api/notes", :action => "feed", :format => "rss" }
104 { :controller => "api/notes", :action => "create" },
105 { :path => "/api/0.6/notes/addPOIexec", :method => :post }
108 { :controller => "api/notes", :action => "close" },
109 { :path => "/api/0.6/notes/closePOIexec", :method => :post }
112 { :controller => "api/notes", :action => "comment" },
113 { :path => "/api/0.6/notes/editPOIexec", :method => :post }
116 { :controller => "api/notes", :action => "index", :format => "gpx" },
117 { :path => "/api/0.6/notes/getGPX", :method => :get }
120 { :controller => "api/notes", :action => "feed", :format => "rss" },
121 { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
125 def test_create_success
126 assert_difference "Note.count", 1 do
127 assert_difference "NoteComment.count", 1 do
128 post :create, :params => { :lat => -1.0, :lon => -1.0, :text => "This is a comment", :format => "json" }
131 assert_response :success
132 js = ActiveSupport::JSON.decode(@response.body)
134 assert_equal "Feature", js["type"]
135 assert_equal "Point", js["geometry"]["type"]
136 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
137 assert_equal "open", js["properties"]["status"]
138 assert_equal 1, js["properties"]["comments"].count
139 assert_equal "opened", js["properties"]["comments"].last["action"]
140 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
141 assert_nil js["properties"]["comments"].last["user"]
142 id = js["properties"]["id"]
144 get :show, :params => { :id => id, :format => "json" }
145 assert_response :success
146 js = ActiveSupport::JSON.decode(@response.body)
148 assert_equal "Feature", js["type"]
149 assert_equal "Point", js["geometry"]["type"]
150 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
151 assert_equal id, js["properties"]["id"]
152 assert_equal "open", js["properties"]["status"]
153 assert_equal 1, js["properties"]["comments"].count
154 assert_equal "opened", js["properties"]["comments"].last["action"]
155 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
156 assert_nil js["properties"]["comments"].last["user"]
160 assert_no_difference "Note.count" do
161 assert_no_difference "NoteComment.count" do
162 post :create, :params => { :lon => -1.0, :text => "This is a comment" }
165 assert_response :bad_request
167 assert_no_difference "Note.count" do
168 assert_no_difference "NoteComment.count" do
169 post :create, :params => { :lat => -1.0, :text => "This is a comment" }
172 assert_response :bad_request
174 assert_no_difference "Note.count" do
175 assert_no_difference "NoteComment.count" do
176 post :create, :params => { :lat => -1.0, :lon => -1.0 }
179 assert_response :bad_request
181 assert_no_difference "Note.count" do
182 assert_no_difference "NoteComment.count" do
183 post :create, :params => { :lat => -1.0, :lon => -1.0, :text => "" }
186 assert_response :bad_request
188 assert_no_difference "Note.count" do
189 assert_no_difference "NoteComment.count" do
190 post :create, :params => { :lat => -100.0, :lon => -1.0, :text => "This is a comment" }
193 assert_response :bad_request
195 assert_no_difference "Note.count" do
196 assert_no_difference "NoteComment.count" do
197 post :create, :params => { :lat => -1.0, :lon => -200.0, :text => "This is a comment" }
200 assert_response :bad_request
202 assert_no_difference "Note.count" do
203 assert_no_difference "NoteComment.count" do
204 post :create, :params => { :lat => "abc", :lon => -1.0, :text => "This is a comment" }
207 assert_response :bad_request
209 assert_no_difference "Note.count" do
210 assert_no_difference "NoteComment.count" do
211 post :create, :params => { :lat => -1.0, :lon => "abc", :text => "This is a comment" }
214 assert_response :bad_request
216 assert_no_difference "Note.count" do
217 assert_no_difference "NoteComment.count" do
218 post :create, :params => { :lat => -1.0, :lon => -1.0, :text => "x\u0000y" }
221 assert_response :bad_request
224 def test_comment_success
225 open_note_with_comment = create(:note_with_comments)
227 basic_authorization user.email, "test"
228 assert_difference "NoteComment.count", 1 do
229 assert_no_difference "ActionMailer::Base.deliveries.size" do
230 perform_enqueued_jobs do
231 post :comment, :params => { :id => open_note_with_comment.id, :text => "This is an additional comment", :format => "json" }
235 assert_response :success
236 js = ActiveSupport::JSON.decode(@response.body)
238 assert_equal "Feature", js["type"]
239 assert_equal open_note_with_comment.id, js["properties"]["id"]
240 assert_equal "open", js["properties"]["status"]
241 assert_equal 2, js["properties"]["comments"].count
242 assert_equal "commented", js["properties"]["comments"].last["action"]
243 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
244 assert_equal user.display_name, js["properties"]["comments"].last["user"]
246 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
247 assert_response :success
248 js = ActiveSupport::JSON.decode(@response.body)
250 assert_equal "Feature", js["type"]
251 assert_equal open_note_with_comment.id, js["properties"]["id"]
252 assert_equal "open", js["properties"]["status"]
253 assert_equal 2, js["properties"]["comments"].count
254 assert_equal "commented", js["properties"]["comments"].last["action"]
255 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
256 assert_equal user.display_name, js["properties"]["comments"].last["user"]
258 # Ensure that emails are sent to users
259 first_user = create(:user)
260 second_user = create(:user)
261 third_user = create(:user)
263 note_with_comments_by_users = create(:note) do |note|
264 create(:note_comment, :note => note, :author => first_user)
265 create(:note_comment, :note => note, :author => second_user)
268 basic_authorization third_user.email, "test"
270 assert_difference "NoteComment.count", 1 do
271 assert_difference "ActionMailer::Base.deliveries.size", 2 do
272 perform_enqueued_jobs do
273 post :comment, :params => { :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json" }
277 assert_response :success
278 js = ActiveSupport::JSON.decode(@response.body)
280 assert_equal "Feature", js["type"]
281 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
282 assert_equal "open", js["properties"]["status"]
283 assert_equal 3, js["properties"]["comments"].count
284 assert_equal "commented", js["properties"]["comments"].last["action"]
285 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
286 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
288 email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
290 assert_equal 1, email.to.length
291 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on one of your notes", email.subject
292 assert_equal first_user.email, email.to.first
294 email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
296 assert_equal 1, email.to.length
297 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on a note you are interested in", email.subject
299 get :show, :params => { :id => note_with_comments_by_users.id, :format => "json" }
300 assert_response :success
301 js = ActiveSupport::JSON.decode(@response.body)
303 assert_equal "Feature", js["type"]
304 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
305 assert_equal "open", js["properties"]["status"]
306 assert_equal 3, js["properties"]["comments"].count
307 assert_equal "commented", js["properties"]["comments"].last["action"]
308 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
309 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
311 ActionMailer::Base.deliveries.clear
314 def test_comment_fail
315 open_note_with_comment = create(:note_with_comments)
319 assert_no_difference "NoteComment.count" do
320 post :comment, :params => { :text => "This is an additional comment" }
321 assert_response :unauthorized
324 basic_authorization user.email, "test"
326 assert_no_difference "NoteComment.count" do
327 post :comment, :params => { :text => "This is an additional comment" }
329 assert_response :bad_request
331 assert_no_difference "NoteComment.count" do
332 post :comment, :params => { :id => open_note_with_comment.id }
334 assert_response :bad_request
336 assert_no_difference "NoteComment.count" do
337 post :comment, :params => { :id => open_note_with_comment.id, :text => "" }
339 assert_response :bad_request
341 assert_no_difference "NoteComment.count" do
342 post :comment, :params => { :id => 12345, :text => "This is an additional comment" }
344 assert_response :not_found
346 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
348 assert_no_difference "NoteComment.count" do
349 post :comment, :params => { :id => hidden_note_with_comment.id, :text => "This is an additional comment" }
351 assert_response :gone
353 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
355 assert_no_difference "NoteComment.count" do
356 post :comment, :params => { :id => closed_note_with_comment.id, :text => "This is an additional comment" }
358 assert_response :conflict
360 assert_no_difference "NoteComment.count" do
361 post :comment, :params => { :id => open_note_with_comment.id, :text => "x\u0000y" }
363 assert_response :bad_request
366 def test_close_success
367 open_note_with_comment = create(:note_with_comments)
370 post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
371 assert_response :unauthorized
373 basic_authorization user.email, "test"
375 post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
376 assert_response :success
377 js = ActiveSupport::JSON.decode(@response.body)
379 assert_equal "Feature", js["type"]
380 assert_equal open_note_with_comment.id, js["properties"]["id"]
381 assert_equal "closed", js["properties"]["status"]
382 assert_equal 2, js["properties"]["comments"].count
383 assert_equal "closed", js["properties"]["comments"].last["action"]
384 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
385 assert_equal user.display_name, js["properties"]["comments"].last["user"]
387 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
388 assert_response :success
389 js = ActiveSupport::JSON.decode(@response.body)
391 assert_equal "Feature", js["type"]
392 assert_equal open_note_with_comment.id, js["properties"]["id"]
393 assert_equal "closed", js["properties"]["status"]
394 assert_equal 2, js["properties"]["comments"].count
395 assert_equal "closed", js["properties"]["comments"].last["action"]
396 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
397 assert_equal user.display_name, js["properties"]["comments"].last["user"]
402 assert_response :unauthorized
404 basic_authorization create(:user).email, "test"
407 assert_response :bad_request
409 post :close, :params => { :id => 12345 }
410 assert_response :not_found
412 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
414 post :close, :params => { :id => hidden_note_with_comment.id }
415 assert_response :gone
417 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
419 post :close, :params => { :id => closed_note_with_comment.id }
420 assert_response :conflict
423 def test_reopen_success
424 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
427 post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
428 assert_response :unauthorized
430 basic_authorization user.email, "test"
432 post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
433 assert_response :success
434 js = ActiveSupport::JSON.decode(@response.body)
436 assert_equal "Feature", js["type"]
437 assert_equal closed_note_with_comment.id, js["properties"]["id"]
438 assert_equal "open", js["properties"]["status"]
439 assert_equal 2, js["properties"]["comments"].count
440 assert_equal "reopened", js["properties"]["comments"].last["action"]
441 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
442 assert_equal user.display_name, js["properties"]["comments"].last["user"]
444 get :show, :params => { :id => closed_note_with_comment.id, :format => "json" }
445 assert_response :success
446 js = ActiveSupport::JSON.decode(@response.body)
448 assert_equal "Feature", js["type"]
449 assert_equal closed_note_with_comment.id, js["properties"]["id"]
450 assert_equal "open", js["properties"]["status"]
451 assert_equal 2, js["properties"]["comments"].count
452 assert_equal "reopened", js["properties"]["comments"].last["action"]
453 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
454 assert_equal user.display_name, js["properties"]["comments"].last["user"]
458 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
460 post :reopen, :params => { :id => hidden_note_with_comment.id }
461 assert_response :unauthorized
463 basic_authorization create(:user).email, "test"
465 post :reopen, :params => { :id => 12345 }
466 assert_response :not_found
468 post :reopen, :params => { :id => hidden_note_with_comment.id }
469 assert_response :gone
471 open_note_with_comment = create(:note_with_comments)
473 post :reopen, :params => { :id => open_note_with_comment.id }
474 assert_response :conflict
477 def test_show_success
478 open_note = create(:note_with_comments)
480 get :show, :params => { :id => open_note.id, :format => "xml" }
481 assert_response :success
482 assert_equal "application/xml", @response.media_type
483 assert_select "osm", :count => 1 do
484 assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
485 assert_select "id", open_note.id.to_s
486 assert_select "url", note_url(open_note, :format => "xml")
487 assert_select "comment_url", comment_note_url(open_note, :format => "xml")
488 assert_select "close_url", close_note_url(open_note, :format => "xml")
489 assert_select "date_created", open_note.created_at.to_s
490 assert_select "status", open_note.status
491 assert_select "comments", :count => 1 do
492 assert_select "comment", :count => 1
497 get :show, :params => { :id => open_note.id, :format => "rss" }
498 assert_response :success
499 assert_equal "application/rss+xml", @response.media_type
500 assert_select "rss", :count => 1 do
501 assert_select "channel", :count => 1 do
502 assert_select "item", :count => 1 do
503 assert_select "link", browse_note_url(open_note)
504 assert_select "guid", note_url(open_note)
505 assert_select "pubDate", open_note.created_at.to_s(:rfc822)
506 # assert_select "geo:lat", open_note.lat.to_s
507 # assert_select "geo:long", open_note.lon
508 # assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
513 get :show, :params => { :id => open_note.id, :format => "json" }
514 assert_response :success
515 assert_equal "application/json", @response.media_type
516 js = ActiveSupport::JSON.decode(@response.body)
518 assert_equal "Feature", js["type"]
519 assert_equal "Point", js["geometry"]["type"]
520 assert_equal open_note.lat, js["geometry"]["coordinates"][0]
521 assert_equal open_note.lon, js["geometry"]["coordinates"][1]
522 assert_equal open_note.id, js["properties"]["id"]
523 assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
524 assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
525 assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
526 assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
527 assert_equal open_note.status, js["properties"]["status"]
529 get :show, :params => { :id => open_note.id, :format => "gpx" }
530 assert_response :success
531 assert_equal "application/gpx+xml", @response.media_type
532 assert_select "gpx", :count => 1 do
533 assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
534 assert_select "time", :count => 1
535 assert_select "name", "Note: #{open_note.id}"
536 assert_select "desc", :count => 1
537 assert_select "link[href='http://test.host/note/#{open_note.id}']", :count => 1
538 assert_select "extensions", :count => 1 do
539 assert_select "id", open_note.id.to_s
540 assert_select "url", note_url(open_note, :format => "gpx")
541 assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
542 assert_select "close_url", close_note_url(open_note, :format => "gpx")
548 def test_show_hidden_comment
549 note_with_hidden_comment = create(:note) do |note|
550 create(:note_comment, :note => note, :body => "Valid comment for hidden note")
551 create(:note_comment, :note => note, :visible => false)
552 create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
555 get :show, :params => { :id => note_with_hidden_comment.id, :format => "json" }
556 assert_response :success
557 js = ActiveSupport::JSON.decode(@response.body)
559 assert_equal "Feature", js["type"]
560 assert_equal note_with_hidden_comment.id, js["properties"]["id"]
561 assert_equal 2, js["properties"]["comments"].count
562 assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
563 assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
567 get :show, :params => { :id => 12345 }
568 assert_response :not_found
570 get :show, :params => { :id => create(:note, :status => "hidden").id }
571 assert_response :gone
574 def test_destroy_success
575 open_note_with_comment = create(:note_with_comments)
577 moderator_user = create(:moderator_user)
579 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
580 assert_response :unauthorized
582 basic_authorization user.email, "test"
584 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
585 assert_response :forbidden
587 basic_authorization moderator_user.email, "test"
589 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
590 assert_response :success
591 js = ActiveSupport::JSON.decode(@response.body)
593 assert_equal "Feature", js["type"]
594 assert_equal open_note_with_comment.id, js["properties"]["id"]
595 assert_equal "hidden", js["properties"]["status"]
596 assert_equal 2, js["properties"]["comments"].count
597 assert_equal "hidden", js["properties"]["comments"].last["action"]
598 assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
599 assert_equal moderator_user.display_name, js["properties"]["comments"].last["user"]
601 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
602 assert_response :success
604 basic_authorization user.email, "test"
605 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
606 assert_response :gone
609 def test_destroy_fail
611 moderator_user = create(:moderator_user)
613 delete :destroy, :params => { :id => 12345, :format => "json" }
614 assert_response :unauthorized
616 basic_authorization user.email, "test"
618 delete :destroy, :params => { :id => 12345, :format => "json" }
619 assert_response :forbidden
621 basic_authorization moderator_user.email, "test"
623 delete :destroy, :params => { :id => 12345, :format => "json" }
624 assert_response :not_found
626 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
628 delete :destroy, :params => { :id => hidden_note_with_comment.id, :format => "json" }
629 assert_response :gone
632 def test_index_success
633 position = (1.1 * GeoRecord::SCALE).to_i
634 create(:note_with_comments, :latitude => position, :longitude => position)
635 create(:note_with_comments, :latitude => position, :longitude => position)
637 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "rss" }
638 assert_response :success
639 assert_equal "application/rss+xml", @response.media_type
640 assert_select "rss", :count => 1 do
641 assert_select "channel", :count => 1 do
642 assert_select "item", :count => 2
646 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "json" }
647 assert_response :success
648 assert_equal "application/json", @response.media_type
649 js = ActiveSupport::JSON.decode(@response.body)
651 assert_equal "FeatureCollection", js["type"]
652 assert_equal 2, js["features"].count
654 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "xml" }
655 assert_response :success
656 assert_equal "application/xml", @response.media_type
657 assert_select "osm", :count => 1 do
658 assert_select "note", :count => 2
661 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "gpx" }
662 assert_response :success
663 assert_equal "application/gpx+xml", @response.media_type
664 assert_select "gpx", :count => 1 do
665 assert_select "wpt", :count => 2
670 position = (1.1 * GeoRecord::SCALE).to_i
671 create(:note_with_comments, :latitude => position, :longitude => position)
672 create(:note_with_comments, :latitude => position, :longitude => position)
674 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss" }
675 assert_response :success
676 assert_equal "application/rss+xml", @response.media_type
677 assert_select "rss", :count => 1 do
678 assert_select "channel", :count => 1 do
679 assert_select "item", :count => 1
683 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json" }
684 assert_response :success
685 assert_equal "application/json", @response.media_type
686 js = ActiveSupport::JSON.decode(@response.body)
688 assert_equal "FeatureCollection", js["type"]
689 assert_equal 1, js["features"].count
691 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml" }
692 assert_response :success
693 assert_equal "application/xml", @response.media_type
694 assert_select "osm", :count => 1 do
695 assert_select "note", :count => 1
698 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx" }
699 assert_response :success
700 assert_equal "application/gpx+xml", @response.media_type
701 assert_select "gpx", :count => 1 do
702 assert_select "wpt", :count => 1
706 def test_index_empty_area
707 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "rss" }
708 assert_response :success
709 assert_equal "application/rss+xml", @response.media_type
710 assert_select "rss", :count => 1 do
711 assert_select "channel", :count => 1 do
712 assert_select "item", :count => 0
716 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "json" }
717 assert_response :success
718 assert_equal "application/json", @response.media_type
719 js = ActiveSupport::JSON.decode(@response.body)
721 assert_equal "FeatureCollection", js["type"]
722 assert_equal 0, js["features"].count
724 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "xml" }
725 assert_response :success
726 assert_equal "application/xml", @response.media_type
727 assert_select "osm", :count => 1 do
728 assert_select "note", :count => 0
731 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "gpx" }
732 assert_response :success
733 assert_equal "application/gpx+xml", @response.media_type
734 assert_select "gpx", :count => 1 do
735 assert_select "wpt", :count => 0
739 def test_index_large_area
740 get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5", :format => :json }
741 assert_response :success
742 assert_equal "application/json", @response.media_type
744 get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json }
745 assert_response :success
746 assert_equal "application/json", @response.media_type
748 get :index, :params => { :bbox => "-10,-10,12,12", :format => :json }
749 assert_response :bad_request
750 assert_equal "application/json", @response.media_type
752 get :index, :params => { :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json }
753 assert_response :bad_request
754 assert_equal "application/json", @response.media_type
757 def test_index_closed
758 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 5.days)
759 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 100.days)
760 create(:note_with_comments, :status => "hidden")
761 create(:note_with_comments)
763 # Open notes + closed in last 7 days
764 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json" }
765 assert_response :success
766 assert_equal "application/json", @response.media_type
767 js = ActiveSupport::JSON.decode(@response.body)
769 assert_equal "FeatureCollection", js["type"]
770 assert_equal 2, js["features"].count
773 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json" }
774 assert_response :success
775 assert_equal "application/json", @response.media_type
776 js = ActiveSupport::JSON.decode(@response.body)
778 assert_equal "FeatureCollection", js["type"]
779 assert_equal 1, js["features"].count
781 # Open notes + all closed notes
782 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json" }
783 assert_response :success
784 assert_equal "application/json", @response.media_type
785 js = ActiveSupport::JSON.decode(@response.body)
787 assert_equal "FeatureCollection", js["type"]
788 assert_equal 3, js["features"].count
791 def test_index_bad_params
792 get :index, :params => { :bbox => "-2.5,-2.5,2.5" }
793 assert_response :bad_request
795 get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5,2.5" }
796 assert_response :bad_request
798 get :index, :params => { :b => "-2.5", :r => "2.5", :t => "2.5" }
799 assert_response :bad_request
801 get :index, :params => { :l => "-2.5", :r => "2.5", :t => "2.5" }
802 assert_response :bad_request
804 get :index, :params => { :l => "-2.5", :b => "-2.5", :t => "2.5" }
805 assert_response :bad_request
807 get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5" }
808 assert_response :bad_request
810 get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json" }
811 assert_response :bad_request
813 get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json" }
814 assert_response :bad_request
817 def test_search_success
818 create(:note_with_comments)
820 get :search, :params => { :q => "note comment", :format => "xml" }
821 assert_response :success
822 assert_equal "application/xml", @response.media_type
823 assert_select "osm", :count => 1 do
824 assert_select "note", :count => 1
827 get :search, :params => { :q => "note comment", :format => "json" }
828 assert_response :success
829 assert_equal "application/json", @response.media_type
830 js = ActiveSupport::JSON.decode(@response.body)
832 assert_equal "FeatureCollection", js["type"]
833 assert_equal 1, js["features"].count
835 get :search, :params => { :q => "note comment", :format => "rss" }
836 assert_response :success
837 assert_equal "application/rss+xml", @response.media_type
838 assert_select "rss", :count => 1 do
839 assert_select "channel", :count => 1 do
840 assert_select "item", :count => 1
844 get :search, :params => { :q => "note comment", :format => "gpx" }
845 assert_response :success
846 assert_equal "application/gpx+xml", @response.media_type
847 assert_select "gpx", :count => 1 do
848 assert_select "wpt", :count => 1
852 def test_search_by_display_name_success
855 create(:note) do |note|
856 create(:note_comment, :note => note, :author => user)
859 get :search, :params => { :display_name => user.display_name, :format => "xml" }
860 assert_response :success
861 assert_equal "application/xml", @response.media_type
862 assert_select "osm", :count => 1 do
863 assert_select "note", :count => 1
866 get :search, :params => { :display_name => user.display_name, :format => "json" }
867 assert_response :success
868 assert_equal "application/json", @response.media_type
869 js = ActiveSupport::JSON.decode(@response.body)
871 assert_equal "FeatureCollection", js["type"]
872 assert_equal 1, js["features"].count
874 get :search, :params => { :display_name => user.display_name, :format => "rss" }
875 assert_response :success
876 assert_equal "application/rss+xml", @response.media_type
877 assert_select "rss", :count => 1 do
878 assert_select "channel", :count => 1 do
879 assert_select "item", :count => 1
883 get :search, :params => { :display_name => user.display_name, :format => "gpx" }
884 assert_response :success
885 assert_equal "application/gpx+xml", @response.media_type
886 assert_select "gpx", :count => 1 do
887 assert_select "wpt", :count => 1
891 def test_search_by_user_success
894 create(:note) do |note|
895 create(:note_comment, :note => note, :author => user)
898 get :search, :params => { :user => user.id, :format => "xml" }
899 assert_response :success
900 assert_equal "application/xml", @response.media_type
901 assert_select "osm", :count => 1 do
902 assert_select "note", :count => 1
905 get :search, :params => { :user => user.id, :format => "json" }
906 assert_response :success
907 assert_equal "application/json", @response.media_type
908 js = ActiveSupport::JSON.decode(@response.body)
910 assert_equal "FeatureCollection", js["type"]
911 assert_equal 1, js["features"].count
913 get :search, :params => { :user => user.id, :format => "rss" }
914 assert_response :success
915 assert_equal "application/rss+xml", @response.media_type
916 assert_select "rss", :count => 1 do
917 assert_select "channel", :count => 1 do
918 assert_select "item", :count => 1
922 get :search, :params => { :user => user.id, :format => "gpx" }
923 assert_response :success
924 assert_equal "application/gpx+xml", @response.media_type
925 assert_select "gpx", :count => 1 do
926 assert_select "wpt", :count => 1
930 def test_search_no_match
931 create(:note_with_comments)
933 get :search, :params => { :q => "no match", :format => "xml" }
934 assert_response :success
935 assert_equal "application/xml", @response.media_type
936 assert_select "osm", :count => 1 do
937 assert_select "note", :count => 0
940 get :search, :params => { :q => "no match", :format => "json" }
941 assert_response :success
942 assert_equal "application/json", @response.media_type
943 js = ActiveSupport::JSON.decode(@response.body)
945 assert_equal "FeatureCollection", js["type"]
946 assert_equal 0, js["features"].count
948 get :search, :params => { :q => "no match", :format => "rss" }
949 assert_response :success
950 assert_equal "application/rss+xml", @response.media_type
951 assert_select "rss", :count => 1 do
952 assert_select "channel", :count => 1 do
953 assert_select "item", :count => 0
957 get :search, :params => { :q => "no match", :format => "gpx" }
958 assert_response :success
959 assert_equal "application/gpx+xml", @response.media_type
960 assert_select "gpx", :count => 1 do
961 assert_select "wpt", :count => 0
965 def test_search_by_time_no_match
966 create(:note_with_comments)
968 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "xml" }
969 assert_response :success
970 assert_equal "application/xml", @response.media_type
971 assert_select "osm", :count => 1 do
972 assert_select "note", :count => 0
975 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "json" }
976 assert_response :success
977 assert_equal "application/json", @response.media_type
978 js = ActiveSupport::JSON.decode(@response.body)
980 assert_equal "FeatureCollection", js["type"]
981 assert_equal 0, js["features"].count
983 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "rss" }
984 assert_response :success
985 assert_equal "application/rss+xml", @response.media_type
986 assert_select "rss", :count => 1 do
987 assert_select "channel", :count => 1 do
988 assert_select "item", :count => 0
992 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "gpx" }
993 assert_response :success
994 assert_equal "application/gpx+xml", @response.media_type
995 assert_select "gpx", :count => 1 do
996 assert_select "wpt", :count => 0
1000 def test_search_bad_params
1001 get :search, :params => { :q => "no match", :limit => "0", :format => "json" }
1002 assert_response :bad_request
1004 get :search, :params => { :q => "no match", :limit => "10001", :format => "json" }
1005 assert_response :bad_request
1007 get :search, :params => { :display_name => "non-existent" }
1008 assert_response :bad_request
1010 get :search, :params => { :user => "-1" }
1011 assert_response :bad_request
1013 get :search, :params => { :from => "wrong-date", :to => "wrong-date" }
1014 assert_response :bad_request
1016 get :search, :params => { :from => "01.01.2010", :to => "2010.01.2010" }
1017 assert_response :bad_request
1020 def test_feed_success
1021 position = (1.1 * GeoRecord::SCALE).to_i
1022 create(:note_with_comments, :latitude => position, :longitude => position)
1023 create(:note_with_comments, :latitude => position, :longitude => position)
1024 position = (1.5 * GeoRecord::SCALE).to_i
1025 create(:note_with_comments, :latitude => position, :longitude => position)
1026 create(:note_with_comments, :latitude => position, :longitude => position)
1028 get :feed, :params => { :format => "rss" }
1029 assert_response :success
1030 assert_equal "application/rss+xml", @response.media_type
1031 assert_select "rss", :count => 1 do
1032 assert_select "channel", :count => 1 do
1033 assert_select "item", :count => 4
1037 get :feed, :params => { :bbox => "1,1,1.2,1.2", :format => "rss" }
1038 assert_response :success
1039 assert_equal "application/rss+xml", @response.media_type
1040 assert_select "rss", :count => 1 do
1041 assert_select "channel", :count => 1 do
1042 assert_select "item", :count => 2
1048 get :feed, :params => { :bbox => "1,1,1.2", :format => "rss" }
1049 assert_response :bad_request
1051 get :feed, :params => { :bbox => "1,1,1.2,1.2,1.2", :format => "rss" }
1052 assert_response :bad_request
1054 get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss" }
1055 assert_response :bad_request
1057 get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss" }
1058 assert_response :bad_request