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)
225 assert_difference "NoteComment.count", 1 do
226 assert_no_difference "ActionMailer::Base.deliveries.size" do
227 perform_enqueued_jobs do
228 post :comment, :params => { :id => open_note_with_comment.id, :text => "This is an additional comment", :format => "json" }
232 assert_response :success
233 js = ActiveSupport::JSON.decode(@response.body)
235 assert_equal "Feature", js["type"]
236 assert_equal open_note_with_comment.id, js["properties"]["id"]
237 assert_equal "open", js["properties"]["status"]
238 assert_equal 2, js["properties"]["comments"].count
239 assert_equal "commented", js["properties"]["comments"].last["action"]
240 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
241 assert_nil js["properties"]["comments"].last["user"]
243 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
244 assert_response :success
245 js = ActiveSupport::JSON.decode(@response.body)
247 assert_equal "Feature", js["type"]
248 assert_equal open_note_with_comment.id, js["properties"]["id"]
249 assert_equal "open", js["properties"]["status"]
250 assert_equal 2, js["properties"]["comments"].count
251 assert_equal "commented", js["properties"]["comments"].last["action"]
252 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
253 assert_nil js["properties"]["comments"].last["user"]
255 # Ensure that emails are sent to users
256 first_user = create(:user)
257 second_user = create(:user)
258 third_user = create(:user)
260 note_with_comments_by_users = create(:note) do |note|
261 create(:note_comment, :note => note, :author => first_user)
262 create(:note_comment, :note => note, :author => second_user)
264 assert_difference "NoteComment.count", 1 do
265 assert_difference "ActionMailer::Base.deliveries.size", 2 do
266 perform_enqueued_jobs do
267 post :comment, :params => { :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json" }
271 assert_response :success
272 js = ActiveSupport::JSON.decode(@response.body)
274 assert_equal "Feature", js["type"]
275 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
276 assert_equal "open", js["properties"]["status"]
277 assert_equal 3, js["properties"]["comments"].count
278 assert_equal "commented", js["properties"]["comments"].last["action"]
279 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
280 assert_nil js["properties"]["comments"].last["user"]
282 email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
284 assert_equal 1, email.to.length
285 assert_equal "[OpenStreetMap] An anonymous user has commented on one of your notes", email.subject
287 email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
289 assert_equal 1, email.to.length
290 assert_equal "[OpenStreetMap] An anonymous user has commented on a note you are interested in", email.subject
292 get :show, :params => { :id => note_with_comments_by_users.id, :format => "json" }
293 assert_response :success
294 js = ActiveSupport::JSON.decode(@response.body)
296 assert_equal "Feature", js["type"]
297 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
298 assert_equal "open", js["properties"]["status"]
299 assert_equal 3, js["properties"]["comments"].count
300 assert_equal "commented", js["properties"]["comments"].last["action"]
301 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
302 assert_nil js["properties"]["comments"].last["user"]
304 ActionMailer::Base.deliveries.clear
306 basic_authorization third_user.email, "test"
308 assert_difference "NoteComment.count", 1 do
309 assert_difference "ActionMailer::Base.deliveries.size", 2 do
310 perform_enqueued_jobs do
311 post :comment, :params => { :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json" }
315 assert_response :success
316 js = ActiveSupport::JSON.decode(@response.body)
318 assert_equal "Feature", js["type"]
319 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
320 assert_equal "open", js["properties"]["status"]
321 assert_equal 4, js["properties"]["comments"].count
322 assert_equal "commented", js["properties"]["comments"].last["action"]
323 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
324 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
326 email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
328 assert_equal 1, email.to.length
329 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on one of your notes", email.subject
330 assert_equal first_user.email, email.to.first
332 email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
334 assert_equal 1, email.to.length
335 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on a note you are interested in", email.subject
337 get :show, :params => { :id => note_with_comments_by_users.id, :format => "json" }
338 assert_response :success
339 js = ActiveSupport::JSON.decode(@response.body)
341 assert_equal "Feature", js["type"]
342 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
343 assert_equal "open", js["properties"]["status"]
344 assert_equal 4, js["properties"]["comments"].count
345 assert_equal "commented", js["properties"]["comments"].last["action"]
346 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
347 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
349 ActionMailer::Base.deliveries.clear
352 def test_comment_fail
353 open_note_with_comment = create(:note_with_comments)
355 assert_no_difference "NoteComment.count" do
356 post :comment, :params => { :text => "This is an additional comment" }
358 assert_response :bad_request
360 assert_no_difference "NoteComment.count" do
361 post :comment, :params => { :id => open_note_with_comment.id }
363 assert_response :bad_request
365 assert_no_difference "NoteComment.count" do
366 post :comment, :params => { :id => open_note_with_comment.id, :text => "" }
368 assert_response :bad_request
370 assert_no_difference "NoteComment.count" do
371 post :comment, :params => { :id => 12345, :text => "This is an additional comment" }
373 assert_response :not_found
375 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
377 assert_no_difference "NoteComment.count" do
378 post :comment, :params => { :id => hidden_note_with_comment.id, :text => "This is an additional comment" }
380 assert_response :gone
382 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
384 assert_no_difference "NoteComment.count" do
385 post :comment, :params => { :id => closed_note_with_comment.id, :text => "This is an additional comment" }
387 assert_response :conflict
389 assert_no_difference "NoteComment.count" do
390 post :comment, :params => { :id => open_note_with_comment.id, :text => "x\u0000y" }
392 assert_response :bad_request
395 def test_close_success
396 open_note_with_comment = create(:note_with_comments)
399 post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
400 assert_response :unauthorized
402 basic_authorization user.email, "test"
404 post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
405 assert_response :success
406 js = ActiveSupport::JSON.decode(@response.body)
408 assert_equal "Feature", js["type"]
409 assert_equal open_note_with_comment.id, js["properties"]["id"]
410 assert_equal "closed", js["properties"]["status"]
411 assert_equal 2, js["properties"]["comments"].count
412 assert_equal "closed", js["properties"]["comments"].last["action"]
413 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
414 assert_equal user.display_name, js["properties"]["comments"].last["user"]
416 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
417 assert_response :success
418 js = ActiveSupport::JSON.decode(@response.body)
420 assert_equal "Feature", js["type"]
421 assert_equal open_note_with_comment.id, js["properties"]["id"]
422 assert_equal "closed", js["properties"]["status"]
423 assert_equal 2, js["properties"]["comments"].count
424 assert_equal "closed", js["properties"]["comments"].last["action"]
425 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
426 assert_equal user.display_name, js["properties"]["comments"].last["user"]
431 assert_response :unauthorized
433 basic_authorization create(:user).email, "test"
436 assert_response :bad_request
438 post :close, :params => { :id => 12345 }
439 assert_response :not_found
441 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
443 post :close, :params => { :id => hidden_note_with_comment.id }
444 assert_response :gone
446 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
448 post :close, :params => { :id => closed_note_with_comment.id }
449 assert_response :conflict
452 def test_reopen_success
453 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
456 post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
457 assert_response :unauthorized
459 basic_authorization user.email, "test"
461 post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
462 assert_response :success
463 js = ActiveSupport::JSON.decode(@response.body)
465 assert_equal "Feature", js["type"]
466 assert_equal closed_note_with_comment.id, js["properties"]["id"]
467 assert_equal "open", js["properties"]["status"]
468 assert_equal 2, js["properties"]["comments"].count
469 assert_equal "reopened", js["properties"]["comments"].last["action"]
470 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
471 assert_equal user.display_name, js["properties"]["comments"].last["user"]
473 get :show, :params => { :id => closed_note_with_comment.id, :format => "json" }
474 assert_response :success
475 js = ActiveSupport::JSON.decode(@response.body)
477 assert_equal "Feature", js["type"]
478 assert_equal closed_note_with_comment.id, js["properties"]["id"]
479 assert_equal "open", js["properties"]["status"]
480 assert_equal 2, js["properties"]["comments"].count
481 assert_equal "reopened", js["properties"]["comments"].last["action"]
482 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
483 assert_equal user.display_name, js["properties"]["comments"].last["user"]
487 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
489 post :reopen, :params => { :id => hidden_note_with_comment.id }
490 assert_response :unauthorized
492 basic_authorization create(:user).email, "test"
494 post :reopen, :params => { :id => 12345 }
495 assert_response :not_found
497 post :reopen, :params => { :id => hidden_note_with_comment.id }
498 assert_response :gone
500 open_note_with_comment = create(:note_with_comments)
502 post :reopen, :params => { :id => open_note_with_comment.id }
503 assert_response :conflict
506 def test_show_success
507 open_note = create(:note_with_comments)
509 get :show, :params => { :id => open_note.id, :format => "xml" }
510 assert_response :success
511 assert_equal "application/xml", @response.content_type
512 assert_select "osm", :count => 1 do
513 assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
514 assert_select "id", open_note.id.to_s
515 assert_select "url", note_url(open_note, :format => "xml")
516 assert_select "comment_url", comment_note_url(open_note, :format => "xml")
517 assert_select "close_url", close_note_url(open_note, :format => "xml")
518 assert_select "date_created", open_note.created_at.to_s
519 assert_select "status", open_note.status
520 assert_select "comments", :count => 1 do
521 assert_select "comment", :count => 1
526 get :show, :params => { :id => open_note.id, :format => "rss" }
527 assert_response :success
528 assert_equal "application/rss+xml", @response.content_type
529 assert_select "rss", :count => 1 do
530 assert_select "channel", :count => 1 do
531 assert_select "item", :count => 1 do
532 assert_select "link", browse_note_url(open_note)
533 assert_select "guid", note_url(open_note)
534 assert_select "pubDate", open_note.created_at.to_s(:rfc822)
535 # assert_select "geo:lat", open_note.lat.to_s
536 # assert_select "geo:long", open_note.lon
537 # assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
542 get :show, :params => { :id => open_note.id, :format => "json" }
543 assert_response :success
544 assert_equal "application/json", @response.content_type
545 js = ActiveSupport::JSON.decode(@response.body)
547 assert_equal "Feature", js["type"]
548 assert_equal "Point", js["geometry"]["type"]
549 assert_equal open_note.lat, js["geometry"]["coordinates"][0]
550 assert_equal open_note.lon, js["geometry"]["coordinates"][1]
551 assert_equal open_note.id, js["properties"]["id"]
552 assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
553 assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
554 assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
555 assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
556 assert_equal open_note.status, js["properties"]["status"]
558 get :show, :params => { :id => open_note.id, :format => "gpx" }
559 assert_response :success
560 assert_equal "application/gpx+xml", @response.content_type
561 assert_select "gpx", :count => 1 do
562 assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
563 assert_select "time", :count => 1
564 assert_select "name", "Note: #{open_note.id}"
565 assert_select "desc", :count => 1
566 assert_select "link[href='http://test.host/note/#{open_note.id}']", :count => 1
567 assert_select "extensions", :count => 1 do
568 assert_select "id", open_note.id.to_s
569 assert_select "url", note_url(open_note, :format => "gpx")
570 assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
571 assert_select "close_url", close_note_url(open_note, :format => "gpx")
577 def test_show_hidden_comment
578 note_with_hidden_comment = create(:note) do |note|
579 create(:note_comment, :note => note, :body => "Valid comment for hidden note")
580 create(:note_comment, :note => note, :visible => false)
581 create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
584 get :show, :params => { :id => note_with_hidden_comment.id, :format => "json" }
585 assert_response :success
586 js = ActiveSupport::JSON.decode(@response.body)
588 assert_equal "Feature", js["type"]
589 assert_equal note_with_hidden_comment.id, js["properties"]["id"]
590 assert_equal 2, js["properties"]["comments"].count
591 assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
592 assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
596 get :show, :params => { :id => 12345 }
597 assert_response :not_found
599 get :show, :params => { :id => create(:note, :status => "hidden").id }
600 assert_response :gone
603 def test_destroy_success
604 open_note_with_comment = create(:note_with_comments)
606 moderator_user = create(:moderator_user)
608 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
609 assert_response :unauthorized
611 basic_authorization user.email, "test"
613 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
614 assert_response :forbidden
616 basic_authorization moderator_user.email, "test"
618 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
619 assert_response :success
620 js = ActiveSupport::JSON.decode(@response.body)
622 assert_equal "Feature", js["type"]
623 assert_equal open_note_with_comment.id, js["properties"]["id"]
624 assert_equal "hidden", js["properties"]["status"]
625 assert_equal 2, js["properties"]["comments"].count
626 assert_equal "hidden", js["properties"]["comments"].last["action"]
627 assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
628 assert_equal moderator_user.display_name, js["properties"]["comments"].last["user"]
630 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
631 assert_response :success
633 basic_authorization user.email, "test"
634 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
635 assert_response :gone
638 def test_destroy_fail
640 moderator_user = create(:moderator_user)
642 delete :destroy, :params => { :id => 12345, :format => "json" }
643 assert_response :unauthorized
645 basic_authorization user.email, "test"
647 delete :destroy, :params => { :id => 12345, :format => "json" }
648 assert_response :forbidden
650 basic_authorization moderator_user.email, "test"
652 delete :destroy, :params => { :id => 12345, :format => "json" }
653 assert_response :not_found
655 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
657 delete :destroy, :params => { :id => hidden_note_with_comment.id, :format => "json" }
658 assert_response :gone
661 def test_index_success
662 position = (1.1 * GeoRecord::SCALE).to_i
663 create(:note_with_comments, :latitude => position, :longitude => position)
664 create(:note_with_comments, :latitude => position, :longitude => position)
666 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "rss" }
667 assert_response :success
668 assert_equal "application/rss+xml", @response.content_type
669 assert_select "rss", :count => 1 do
670 assert_select "channel", :count => 1 do
671 assert_select "item", :count => 2
675 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "json" }
676 assert_response :success
677 assert_equal "application/json", @response.content_type
678 js = ActiveSupport::JSON.decode(@response.body)
680 assert_equal "FeatureCollection", js["type"]
681 assert_equal 2, js["features"].count
683 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "xml" }
684 assert_response :success
685 assert_equal "application/xml", @response.content_type
686 assert_select "osm", :count => 1 do
687 assert_select "note", :count => 2
690 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "gpx" }
691 assert_response :success
692 assert_equal "application/gpx+xml", @response.content_type
693 assert_select "gpx", :count => 1 do
694 assert_select "wpt", :count => 2
699 position = (1.1 * GeoRecord::SCALE).to_i
700 create(:note_with_comments, :latitude => position, :longitude => position)
701 create(:note_with_comments, :latitude => position, :longitude => position)
703 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss" }
704 assert_response :success
705 assert_equal "application/rss+xml", @response.content_type
706 assert_select "rss", :count => 1 do
707 assert_select "channel", :count => 1 do
708 assert_select "item", :count => 1
712 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json" }
713 assert_response :success
714 assert_equal "application/json", @response.content_type
715 js = ActiveSupport::JSON.decode(@response.body)
717 assert_equal "FeatureCollection", js["type"]
718 assert_equal 1, js["features"].count
720 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml" }
721 assert_response :success
722 assert_equal "application/xml", @response.content_type
723 assert_select "osm", :count => 1 do
724 assert_select "note", :count => 1
727 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx" }
728 assert_response :success
729 assert_equal "application/gpx+xml", @response.content_type
730 assert_select "gpx", :count => 1 do
731 assert_select "wpt", :count => 1
735 def test_index_empty_area
736 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "rss" }
737 assert_response :success
738 assert_equal "application/rss+xml", @response.content_type
739 assert_select "rss", :count => 1 do
740 assert_select "channel", :count => 1 do
741 assert_select "item", :count => 0
745 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "json" }
746 assert_response :success
747 assert_equal "application/json", @response.content_type
748 js = ActiveSupport::JSON.decode(@response.body)
750 assert_equal "FeatureCollection", js["type"]
751 assert_equal 0, js["features"].count
753 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "xml" }
754 assert_response :success
755 assert_equal "application/xml", @response.content_type
756 assert_select "osm", :count => 1 do
757 assert_select "note", :count => 0
760 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "gpx" }
761 assert_response :success
762 assert_equal "application/gpx+xml", @response.content_type
763 assert_select "gpx", :count => 1 do
764 assert_select "wpt", :count => 0
768 def test_index_large_area
769 get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5", :format => :json }
770 assert_response :success
771 assert_equal "application/json", @response.content_type
773 get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json }
774 assert_response :success
775 assert_equal "application/json", @response.content_type
777 get :index, :params => { :bbox => "-10,-10,12,12", :format => :json }
778 assert_response :bad_request
779 assert_equal "application/json", @response.content_type
781 get :index, :params => { :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json }
782 assert_response :bad_request
783 assert_equal "application/json", @response.content_type
786 def test_index_closed
787 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 5.days)
788 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 100.days)
789 create(:note_with_comments, :status => "hidden")
790 create(:note_with_comments)
792 # Open notes + closed in last 7 days
793 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json" }
794 assert_response :success
795 assert_equal "application/json", @response.content_type
796 js = ActiveSupport::JSON.decode(@response.body)
798 assert_equal "FeatureCollection", js["type"]
799 assert_equal 2, js["features"].count
802 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json" }
803 assert_response :success
804 assert_equal "application/json", @response.content_type
805 js = ActiveSupport::JSON.decode(@response.body)
807 assert_equal "FeatureCollection", js["type"]
808 assert_equal 1, js["features"].count
810 # Open notes + all closed notes
811 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json" }
812 assert_response :success
813 assert_equal "application/json", @response.content_type
814 js = ActiveSupport::JSON.decode(@response.body)
816 assert_equal "FeatureCollection", js["type"]
817 assert_equal 3, js["features"].count
820 def test_index_bad_params
821 get :index, :params => { :bbox => "-2.5,-2.5,2.5" }
822 assert_response :bad_request
824 get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5,2.5" }
825 assert_response :bad_request
827 get :index, :params => { :b => "-2.5", :r => "2.5", :t => "2.5" }
828 assert_response :bad_request
830 get :index, :params => { :l => "-2.5", :r => "2.5", :t => "2.5" }
831 assert_response :bad_request
833 get :index, :params => { :l => "-2.5", :b => "-2.5", :t => "2.5" }
834 assert_response :bad_request
836 get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5" }
837 assert_response :bad_request
839 get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json" }
840 assert_response :bad_request
842 get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json" }
843 assert_response :bad_request
846 def test_search_success
847 create(:note_with_comments)
849 get :search, :params => { :q => "note comment", :format => "xml" }
850 assert_response :success
851 assert_equal "application/xml", @response.content_type
852 assert_select "osm", :count => 1 do
853 assert_select "note", :count => 1
856 get :search, :params => { :q => "note comment", :format => "json" }
857 assert_response :success
858 assert_equal "application/json", @response.content_type
859 js = ActiveSupport::JSON.decode(@response.body)
861 assert_equal "FeatureCollection", js["type"]
862 assert_equal 1, js["features"].count
864 get :search, :params => { :q => "note comment", :format => "rss" }
865 assert_response :success
866 assert_equal "application/rss+xml", @response.content_type
867 assert_select "rss", :count => 1 do
868 assert_select "channel", :count => 1 do
869 assert_select "item", :count => 1
873 get :search, :params => { :q => "note comment", :format => "gpx" }
874 assert_response :success
875 assert_equal "application/gpx+xml", @response.content_type
876 assert_select "gpx", :count => 1 do
877 assert_select "wpt", :count => 1
881 def test_search_by_display_name_success
884 create(:note) do |note|
885 create(:note_comment, :note => note, :author => user)
888 get :search, :params => { :display_name => user.display_name, :format => "xml" }
889 assert_response :success
890 assert_equal "application/xml", @response.content_type
891 assert_select "osm", :count => 1 do
892 assert_select "note", :count => 1
895 get :search, :params => { :display_name => user.display_name, :format => "json" }
896 assert_response :success
897 assert_equal "application/json", @response.content_type
898 js = ActiveSupport::JSON.decode(@response.body)
900 assert_equal "FeatureCollection", js["type"]
901 assert_equal 1, js["features"].count
903 get :search, :params => { :display_name => user.display_name, :format => "rss" }
904 assert_response :success
905 assert_equal "application/rss+xml", @response.content_type
906 assert_select "rss", :count => 1 do
907 assert_select "channel", :count => 1 do
908 assert_select "item", :count => 1
912 get :search, :params => { :display_name => user.display_name, :format => "gpx" }
913 assert_response :success
914 assert_equal "application/gpx+xml", @response.content_type
915 assert_select "gpx", :count => 1 do
916 assert_select "wpt", :count => 1
920 def test_search_by_user_success
923 create(:note) do |note|
924 create(:note_comment, :note => note, :author => user)
927 get :search, :params => { :user => user.id, :format => "xml" }
928 assert_response :success
929 assert_equal "application/xml", @response.content_type
930 assert_select "osm", :count => 1 do
931 assert_select "note", :count => 1
934 get :search, :params => { :user => user.id, :format => "json" }
935 assert_response :success
936 assert_equal "application/json", @response.content_type
937 js = ActiveSupport::JSON.decode(@response.body)
939 assert_equal "FeatureCollection", js["type"]
940 assert_equal 1, js["features"].count
942 get :search, :params => { :user => user.id, :format => "rss" }
943 assert_response :success
944 assert_equal "application/rss+xml", @response.content_type
945 assert_select "rss", :count => 1 do
946 assert_select "channel", :count => 1 do
947 assert_select "item", :count => 1
951 get :search, :params => { :user => user.id, :format => "gpx" }
952 assert_response :success
953 assert_equal "application/gpx+xml", @response.content_type
954 assert_select "gpx", :count => 1 do
955 assert_select "wpt", :count => 1
959 def test_search_no_match
960 create(:note_with_comments)
962 get :search, :params => { :q => "no match", :format => "xml" }
963 assert_response :success
964 assert_equal "application/xml", @response.content_type
965 assert_select "osm", :count => 1 do
966 assert_select "note", :count => 0
969 get :search, :params => { :q => "no match", :format => "json" }
970 assert_response :success
971 assert_equal "application/json", @response.content_type
972 js = ActiveSupport::JSON.decode(@response.body)
974 assert_equal "FeatureCollection", js["type"]
975 assert_equal 0, js["features"].count
977 get :search, :params => { :q => "no match", :format => "rss" }
978 assert_response :success
979 assert_equal "application/rss+xml", @response.content_type
980 assert_select "rss", :count => 1 do
981 assert_select "channel", :count => 1 do
982 assert_select "item", :count => 0
986 get :search, :params => { :q => "no match", :format => "gpx" }
987 assert_response :success
988 assert_equal "application/gpx+xml", @response.content_type
989 assert_select "gpx", :count => 1 do
990 assert_select "wpt", :count => 0
994 def test_search_by_time_no_match
995 create(:note_with_comments)
997 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "xml" }
998 assert_response :success
999 assert_equal "application/xml", @response.content_type
1000 assert_select "osm", :count => 1 do
1001 assert_select "note", :count => 0
1004 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "json" }
1005 assert_response :success
1006 assert_equal "application/json", @response.content_type
1007 js = ActiveSupport::JSON.decode(@response.body)
1009 assert_equal "FeatureCollection", js["type"]
1010 assert_equal 0, js["features"].count
1012 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "rss" }
1013 assert_response :success
1014 assert_equal "application/rss+xml", @response.content_type
1015 assert_select "rss", :count => 1 do
1016 assert_select "channel", :count => 1 do
1017 assert_select "item", :count => 0
1021 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "gpx" }
1022 assert_response :success
1023 assert_equal "application/gpx+xml", @response.content_type
1024 assert_select "gpx", :count => 1 do
1025 assert_select "wpt", :count => 0
1029 def test_search_bad_params
1030 get :search, :params => { :q => "no match", :limit => "0", :format => "json" }
1031 assert_response :bad_request
1033 get :search, :params => { :q => "no match", :limit => "10001", :format => "json" }
1034 assert_response :bad_request
1036 get :search, :params => { :display_name => "non-existent" }
1037 assert_response :bad_request
1039 get :search, :params => { :user => "-1" }
1040 assert_response :bad_request
1042 get :search, :params => { :from => "wrong-date", :to => "wrong-date" }
1043 assert_response :bad_request
1045 get :search, :params => { :from => "01.01.2010", :to => "2010.01.2010" }
1046 assert_response :bad_request
1049 def test_feed_success
1050 position = (1.1 * GeoRecord::SCALE).to_i
1051 create(:note_with_comments, :latitude => position, :longitude => position)
1052 create(:note_with_comments, :latitude => position, :longitude => position)
1053 position = (1.5 * GeoRecord::SCALE).to_i
1054 create(:note_with_comments, :latitude => position, :longitude => position)
1055 create(:note_with_comments, :latitude => position, :longitude => position)
1057 get :feed, :params => { :format => "rss" }
1058 assert_response :success
1059 assert_equal "application/rss+xml", @response.content_type
1060 assert_select "rss", :count => 1 do
1061 assert_select "channel", :count => 1 do
1062 assert_select "item", :count => 4
1066 get :feed, :params => { :bbox => "1,1,1.2,1.2", :format => "rss" }
1067 assert_response :success
1068 assert_equal "application/rss+xml", @response.content_type
1069 assert_select "rss", :count => 1 do
1070 assert_select "channel", :count => 1 do
1071 assert_select "item", :count => 2
1077 get :feed, :params => { :bbox => "1,1,1.2", :format => "rss" }
1078 assert_response :bad_request
1080 get :feed, :params => { :bbox => "1,1,1.2,1.2,1.2", :format => "rss" }
1081 assert_response :bad_request
1083 get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss" }
1084 assert_response :bad_request
1086 get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss" }
1087 assert_response :bad_request