3 class NotesControllerTest < ActionController::TestCase
5 # Stub nominatim response for note locations
6 stub_request(:get, %r{^http://nominatim\.openstreetmap\.org/reverse\?})
7 .to_return(:status => 404)
11 # test all routes which lead to this controller
14 { :path => "/api/0.6/notes", :method => :post },
15 { :controller => "notes", :action => "create", :format => "xml" }
18 { :path => "/api/0.6/notes/1", :method => :get },
19 { :controller => "notes", :action => "show", :id => "1", :format => "xml" }
22 { :controller => "notes", :action => "show", :id => "1", :format => "xml" },
23 { :path => "/api/0.6/notes/1.xml", :method => :get }
26 { :path => "/api/0.6/notes/1.rss", :method => :get },
27 { :controller => "notes", :action => "show", :id => "1", :format => "rss" }
30 { :path => "/api/0.6/notes/1.json", :method => :get },
31 { :controller => "notes", :action => "show", :id => "1", :format => "json" }
34 { :path => "/api/0.6/notes/1.gpx", :method => :get },
35 { :controller => "notes", :action => "show", :id => "1", :format => "gpx" }
38 { :path => "/api/0.6/notes/1/comment", :method => :post },
39 { :controller => "notes", :action => "comment", :id => "1", :format => "xml" }
42 { :path => "/api/0.6/notes/1/close", :method => :post },
43 { :controller => "notes", :action => "close", :id => "1", :format => "xml" }
46 { :path => "/api/0.6/notes/1/reopen", :method => :post },
47 { :controller => "notes", :action => "reopen", :id => "1", :format => "xml" }
50 { :path => "/api/0.6/notes/1", :method => :delete },
51 { :controller => "notes", :action => "destroy", :id => "1", :format => "xml" }
55 { :path => "/api/0.6/notes", :method => :get },
56 { :controller => "notes", :action => "index", :format => "xml" }
59 { :controller => "notes", :action => "index", :format => "xml" },
60 { :path => "/api/0.6/notes.xml", :method => :get }
63 { :path => "/api/0.6/notes.rss", :method => :get },
64 { :controller => "notes", :action => "index", :format => "rss" }
67 { :path => "/api/0.6/notes.json", :method => :get },
68 { :controller => "notes", :action => "index", :format => "json" }
71 { :path => "/api/0.6/notes.gpx", :method => :get },
72 { :controller => "notes", :action => "index", :format => "gpx" }
76 { :path => "/api/0.6/notes/search", :method => :get },
77 { :controller => "notes", :action => "search", :format => "xml" }
80 { :controller => "notes", :action => "search", :format => "xml" },
81 { :path => "/api/0.6/notes/search.xml", :method => :get }
84 { :path => "/api/0.6/notes/search.rss", :method => :get },
85 { :controller => "notes", :action => "search", :format => "rss" }
88 { :path => "/api/0.6/notes/search.json", :method => :get },
89 { :controller => "notes", :action => "search", :format => "json" }
92 { :path => "/api/0.6/notes/search.gpx", :method => :get },
93 { :controller => "notes", :action => "search", :format => "gpx" }
97 { :path => "/api/0.6/notes/feed", :method => :get },
98 { :controller => "notes", :action => "feed", :format => "rss" }
102 { :controller => "notes", :action => "create" },
103 { :path => "/api/0.6/notes/addPOIexec", :method => :post }
106 { :controller => "notes", :action => "close" },
107 { :path => "/api/0.6/notes/closePOIexec", :method => :post }
110 { :controller => "notes", :action => "comment" },
111 { :path => "/api/0.6/notes/editPOIexec", :method => :post }
114 { :controller => "notes", :action => "index", :format => "gpx" },
115 { :path => "/api/0.6/notes/getGPX", :method => :get }
118 { :controller => "notes", :action => "feed", :format => "rss" },
119 { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
123 { :path => "/user/username/notes", :method => :get },
124 { :controller => "notes", :action => "mine", :display_name => "username" }
128 def test_create_success
129 assert_difference "Note.count", 1 do
130 assert_difference "NoteComment.count", 1 do
131 post :create, :lat => -1.0, :lon => -1.0, :text => "This is a comment", :format => "json"
134 assert_response :success
135 js = ActiveSupport::JSON.decode(@response.body)
137 assert_equal "Feature", js["type"]
138 assert_equal "Point", js["geometry"]["type"]
139 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
140 assert_equal "open", js["properties"]["status"]
141 assert_equal 1, js["properties"]["comments"].count
142 assert_equal "opened", js["properties"]["comments"].last["action"]
143 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
144 assert_nil js["properties"]["comments"].last["user"]
145 id = js["properties"]["id"]
147 get :show, :id => id, :format => "json"
148 assert_response :success
149 js = ActiveSupport::JSON.decode(@response.body)
151 assert_equal "Feature", js["type"]
152 assert_equal "Point", js["geometry"]["type"]
153 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
154 assert_equal id, js["properties"]["id"]
155 assert_equal "open", js["properties"]["status"]
156 assert_equal 1, js["properties"]["comments"].count
157 assert_equal "opened", js["properties"]["comments"].last["action"]
158 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
159 assert_nil js["properties"]["comments"].last["user"]
163 assert_no_difference "Note.count" do
164 assert_no_difference "NoteComment.count" do
165 post :create, :lon => -1.0, :text => "This is a comment"
168 assert_response :bad_request
170 assert_no_difference "Note.count" do
171 assert_no_difference "NoteComment.count" do
172 post :create, :lat => -1.0, :text => "This is a comment"
175 assert_response :bad_request
177 assert_no_difference "Note.count" do
178 assert_no_difference "NoteComment.count" do
179 post :create, :lat => -1.0, :lon => -1.0
182 assert_response :bad_request
184 assert_no_difference "Note.count" do
185 assert_no_difference "NoteComment.count" do
186 post :create, :lat => -1.0, :lon => -1.0, :text => ""
189 assert_response :bad_request
191 assert_no_difference "Note.count" do
192 assert_no_difference "NoteComment.count" do
193 post :create, :lat => -100.0, :lon => -1.0, :text => "This is a comment"
196 assert_response :bad_request
198 assert_no_difference "Note.count" do
199 assert_no_difference "NoteComment.count" do
200 post :create, :lat => -1.0, :lon => -200.0, :text => "This is a comment"
203 assert_response :bad_request
205 assert_no_difference "Note.count" do
206 assert_no_difference "NoteComment.count" do
207 post :create, :lat => "abc", :lon => -1.0, :text => "This is a comment"
210 assert_response :bad_request
212 assert_no_difference "Note.count" do
213 assert_no_difference "NoteComment.count" do
214 post :create, :lat => -1.0, :lon => "abc", :text => "This is a comment"
217 assert_response :bad_request
219 assert_no_difference "Note.count" do
220 assert_no_difference "NoteComment.count" do
221 post :create, :lat => -1.0, :lon => -1.0, :text => "x\u0000y"
224 assert_response :bad_request
227 def test_comment_success
228 open_note_with_comment = create(:note_with_comments)
229 assert_difference "NoteComment.count", 1 do
230 assert_no_difference "ActionMailer::Base.deliveries.size" do
231 post :comment, :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_nil js["properties"]["comments"].last["user"]
245 get :show, :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_nil 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)
266 assert_difference "NoteComment.count", 1 do
267 assert_difference "ActionMailer::Base.deliveries.size", 2 do
268 post :comment, :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, :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 post :comment, :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json"
313 assert_response :success
314 js = ActiveSupport::JSON.decode(@response.body)
316 assert_equal "Feature", js["type"]
317 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
318 assert_equal "open", js["properties"]["status"]
319 assert_equal 4, js["properties"]["comments"].count
320 assert_equal "commented", js["properties"]["comments"].last["action"]
321 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
322 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
324 email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
326 assert_equal 1, email.to.length
327 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on one of your notes", email.subject
328 assert_equal first_user.email, email.to.first
330 email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
332 assert_equal 1, email.to.length
333 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on a note you are interested in", email.subject
335 get :show, :id => note_with_comments_by_users.id, :format => "json"
336 assert_response :success
337 js = ActiveSupport::JSON.decode(@response.body)
339 assert_equal "Feature", js["type"]
340 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
341 assert_equal "open", js["properties"]["status"]
342 assert_equal 4, js["properties"]["comments"].count
343 assert_equal "commented", js["properties"]["comments"].last["action"]
344 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
345 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
347 ActionMailer::Base.deliveries.clear
350 def test_comment_fail
351 open_note_with_comment = create(:note_with_comments)
353 assert_no_difference "NoteComment.count" do
354 post :comment, :text => "This is an additional comment"
356 assert_response :bad_request
358 assert_no_difference "NoteComment.count" do
359 post :comment, :id => open_note_with_comment.id
361 assert_response :bad_request
363 assert_no_difference "NoteComment.count" do
364 post :comment, :id => open_note_with_comment.id, :text => ""
366 assert_response :bad_request
368 assert_no_difference "NoteComment.count" do
369 post :comment, :id => 12345, :text => "This is an additional comment"
371 assert_response :not_found
373 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
375 assert_no_difference "NoteComment.count" do
376 post :comment, :id => hidden_note_with_comment.id, :text => "This is an additional comment"
378 assert_response :gone
380 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
382 assert_no_difference "NoteComment.count" do
383 post :comment, :id => closed_note_with_comment.id, :text => "This is an additional comment"
385 assert_response :conflict
387 assert_no_difference "NoteComment.count" do
388 post :comment, :id => open_note_with_comment.id, :text => "x\u0000y"
390 assert_response :bad_request
393 def test_close_success
394 open_note_with_comment = create(:note_with_comments)
397 post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
398 assert_response :unauthorized
400 basic_authorization(user.email, "test")
402 post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
403 assert_response :success
404 js = ActiveSupport::JSON.decode(@response.body)
406 assert_equal "Feature", js["type"]
407 assert_equal open_note_with_comment.id, js["properties"]["id"]
408 assert_equal "closed", js["properties"]["status"]
409 assert_equal 2, js["properties"]["comments"].count
410 assert_equal "closed", js["properties"]["comments"].last["action"]
411 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
412 assert_equal user.display_name, js["properties"]["comments"].last["user"]
414 get :show, :id => open_note_with_comment.id, :format => "json"
415 assert_response :success
416 js = ActiveSupport::JSON.decode(@response.body)
418 assert_equal "Feature", js["type"]
419 assert_equal open_note_with_comment.id, js["properties"]["id"]
420 assert_equal "closed", js["properties"]["status"]
421 assert_equal 2, js["properties"]["comments"].count
422 assert_equal "closed", js["properties"]["comments"].last["action"]
423 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
424 assert_equal user.display_name, js["properties"]["comments"].last["user"]
429 assert_response :unauthorized
431 basic_authorization(create(:user).email, "test")
434 assert_response :bad_request
436 post :close, :id => 12345
437 assert_response :not_found
439 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
441 post :close, :id => hidden_note_with_comment.id
442 assert_response :gone
444 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
446 post :close, :id => closed_note_with_comment.id
447 assert_response :conflict
450 def test_reopen_success
451 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
454 post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
455 assert_response :unauthorized
457 basic_authorization(user.email, "test")
459 post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
460 assert_response :success
461 js = ActiveSupport::JSON.decode(@response.body)
463 assert_equal "Feature", js["type"]
464 assert_equal closed_note_with_comment.id, js["properties"]["id"]
465 assert_equal "open", js["properties"]["status"]
466 assert_equal 2, js["properties"]["comments"].count
467 assert_equal "reopened", js["properties"]["comments"].last["action"]
468 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
469 assert_equal user.display_name, js["properties"]["comments"].last["user"]
471 get :show, :id => closed_note_with_comment.id, :format => "json"
472 assert_response :success
473 js = ActiveSupport::JSON.decode(@response.body)
475 assert_equal "Feature", js["type"]
476 assert_equal closed_note_with_comment.id, js["properties"]["id"]
477 assert_equal "open", js["properties"]["status"]
478 assert_equal 2, js["properties"]["comments"].count
479 assert_equal "reopened", js["properties"]["comments"].last["action"]
480 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
481 assert_equal user.display_name, js["properties"]["comments"].last["user"]
485 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
487 post :reopen, :id => hidden_note_with_comment.id
488 assert_response :unauthorized
490 basic_authorization(create(:user).email, "test")
492 post :reopen, :id => 12345
493 assert_response :not_found
495 post :reopen, :id => hidden_note_with_comment.id
496 assert_response :gone
498 open_note_with_comment = create(:note_with_comments)
500 post :reopen, :id => open_note_with_comment.id
501 assert_response :conflict
504 def test_show_success
505 open_note = create(:note_with_comments)
507 get :show, :id => open_note.id, :format => "xml"
508 assert_response :success
509 assert_equal "application/xml", @response.content_type
510 assert_select "osm", :count => 1 do
511 assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
512 assert_select "id", open_note.id.to_s
513 assert_select "url", note_url(open_note, :format => "xml")
514 assert_select "comment_url", comment_note_url(open_note, :format => "xml")
515 assert_select "close_url", close_note_url(open_note, :format => "xml")
516 assert_select "date_created", open_note.created_at.to_s
517 assert_select "status", open_note.status
518 assert_select "comments", :count => 1 do
519 assert_select "comment", :count => 1
524 get :show, :id => open_note.id, :format => "rss"
525 assert_response :success
526 assert_equal "application/rss+xml", @response.content_type
527 assert_select "rss", :count => 1 do
528 assert_select "channel", :count => 1 do
529 assert_select "item", :count => 1 do
530 assert_select "link", browse_note_url(open_note)
531 assert_select "guid", note_url(open_note)
532 assert_select "pubDate", open_note.created_at.to_s(:rfc822)
533 # assert_select "geo:lat", open_note.lat.to_s
534 # assert_select "geo:long", open_note.lon
535 # assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
540 get :show, :id => open_note.id, :format => "json"
541 assert_response :success
542 assert_equal "application/json", @response.content_type
543 js = ActiveSupport::JSON.decode(@response.body)
545 assert_equal "Feature", js["type"]
546 assert_equal "Point", js["geometry"]["type"]
547 assert_equal open_note.lat, js["geometry"]["coordinates"][0]
548 assert_equal open_note.lon, js["geometry"]["coordinates"][1]
549 assert_equal open_note.id, js["properties"]["id"]
550 assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
551 assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
552 assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
553 assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
554 assert_equal open_note.status, js["properties"]["status"]
556 get :show, :id => open_note.id, :format => "gpx"
557 assert_response :success
558 assert_equal "application/gpx+xml", @response.content_type
559 assert_select "gpx", :count => 1 do
560 assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
561 assert_select "time", :count => 1
562 assert_select "name", "Note: #{open_note.id}"
563 assert_select "desc", :count => 1
564 assert_select "link[href='http://test.host/note/#{open_note.id}']", :count => 1
565 assert_select "extensions", :count => 1 do
566 assert_select "id", open_note.id.to_s
567 assert_select "url", note_url(open_note, :format => "gpx")
568 assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
569 assert_select "close_url", close_note_url(open_note, :format => "gpx")
575 def test_show_hidden_comment
576 note_with_hidden_comment = create(:note) do |note|
577 create(:note_comment, :note => note, :body => "Valid comment for hidden note")
578 create(:note_comment, :note => note, :visible => false)
579 create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
582 get :show, :id => note_with_hidden_comment.id, :format => "json"
583 assert_response :success
584 js = ActiveSupport::JSON.decode(@response.body)
586 assert_equal "Feature", js["type"]
587 assert_equal note_with_hidden_comment.id, js["properties"]["id"]
588 assert_equal 2, js["properties"]["comments"].count
589 assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
590 assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
594 get :show, :id => 12345
595 assert_response :not_found
597 get :show, :id => create(:note, :status => "hidden").id
598 assert_response :gone
601 def test_destroy_success
602 open_note_with_comment = create(:note_with_comments)
604 moderator_user = create(:moderator_user)
606 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
607 assert_response :unauthorized
609 basic_authorization(user.email, "test")
611 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
612 assert_response :forbidden
614 basic_authorization(moderator_user.email, "test")
616 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
617 assert_response :success
618 js = ActiveSupport::JSON.decode(@response.body)
620 assert_equal "Feature", js["type"]
621 assert_equal open_note_with_comment.id, js["properties"]["id"]
622 assert_equal "hidden", js["properties"]["status"]
623 assert_equal 2, js["properties"]["comments"].count
624 assert_equal "hidden", js["properties"]["comments"].last["action"]
625 assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
626 assert_equal moderator_user.display_name, js["properties"]["comments"].last["user"]
628 get :show, :id => open_note_with_comment.id, :format => "json"
629 assert_response :gone
632 def test_destroy_fail
634 moderator_user = create(:moderator_user)
636 delete :destroy, :id => 12345, :format => "json"
637 assert_response :unauthorized
639 basic_authorization(user.email, "test")
641 delete :destroy, :id => 12345, :format => "json"
642 assert_response :forbidden
644 basic_authorization(moderator_user.email, "test")
646 delete :destroy, :id => 12345, :format => "json"
647 assert_response :not_found
649 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
651 delete :destroy, :id => hidden_note_with_comment.id, :format => "json"
652 assert_response :gone
655 def test_index_success
656 position = (1.1 * GeoRecord::SCALE).to_i
657 create(:note_with_comments, :latitude => position, :longitude => position)
658 create(:note_with_comments, :latitude => position, :longitude => position)
660 get :index, :bbox => "1,1,1.2,1.2", :format => "rss"
661 assert_response :success
662 assert_equal "application/rss+xml", @response.content_type
663 assert_select "rss", :count => 1 do
664 assert_select "channel", :count => 1 do
665 assert_select "item", :count => 2
669 get :index, :bbox => "1,1,1.2,1.2", :format => "json"
670 assert_response :success
671 assert_equal "application/json", @response.content_type
672 js = ActiveSupport::JSON.decode(@response.body)
674 assert_equal "FeatureCollection", js["type"]
675 assert_equal 2, js["features"].count
677 get :index, :bbox => "1,1,1.2,1.2", :format => "xml"
678 assert_response :success
679 assert_equal "application/xml", @response.content_type
680 assert_select "osm", :count => 1 do
681 assert_select "note", :count => 2
684 get :index, :bbox => "1,1,1.2,1.2", :format => "gpx"
685 assert_response :success
686 assert_equal "application/gpx+xml", @response.content_type
687 assert_select "gpx", :count => 1 do
688 assert_select "wpt", :count => 2
693 position = (1.1 * GeoRecord::SCALE).to_i
694 create(:note_with_comments, :latitude => position, :longitude => position)
695 create(:note_with_comments, :latitude => position, :longitude => position)
697 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss"
698 assert_response :success
699 assert_equal "application/rss+xml", @response.content_type
700 assert_select "rss", :count => 1 do
701 assert_select "channel", :count => 1 do
702 assert_select "item", :count => 1
706 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json"
707 assert_response :success
708 assert_equal "application/json", @response.content_type
709 js = ActiveSupport::JSON.decode(@response.body)
711 assert_equal "FeatureCollection", js["type"]
712 assert_equal 1, js["features"].count
714 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml"
715 assert_response :success
716 assert_equal "application/xml", @response.content_type
717 assert_select "osm", :count => 1 do
718 assert_select "note", :count => 1
721 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx"
722 assert_response :success
723 assert_equal "application/gpx+xml", @response.content_type
724 assert_select "gpx", :count => 1 do
725 assert_select "wpt", :count => 1
729 def test_index_empty_area
730 get :index, :bbox => "5,5,5.1,5.1", :format => "rss"
731 assert_response :success
732 assert_equal "application/rss+xml", @response.content_type
733 assert_select "rss", :count => 1 do
734 assert_select "channel", :count => 1 do
735 assert_select "item", :count => 0
739 get :index, :bbox => "5,5,5.1,5.1", :format => "json"
740 assert_response :success
741 assert_equal "application/json", @response.content_type
742 js = ActiveSupport::JSON.decode(@response.body)
744 assert_equal "FeatureCollection", js["type"]
745 assert_equal 0, js["features"].count
747 get :index, :bbox => "5,5,5.1,5.1", :format => "xml"
748 assert_response :success
749 assert_equal "application/xml", @response.content_type
750 assert_select "osm", :count => 1 do
751 assert_select "note", :count => 0
754 get :index, :bbox => "5,5,5.1,5.1", :format => "gpx"
755 assert_response :success
756 assert_equal "application/gpx+xml", @response.content_type
757 assert_select "gpx", :count => 1 do
758 assert_select "wpt", :count => 0
762 def test_index_large_area
763 get :index, :bbox => "-2.5,-2.5,2.5,2.5", :format => :json
764 assert_response :success
765 assert_equal "application/json", @response.content_type
767 get :index, :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json
768 assert_response :success
769 assert_equal "application/json", @response.content_type
771 get :index, :bbox => "-10,-10,12,12", :format => :json
772 assert_response :bad_request
773 assert_equal "text/plain", @response.content_type
775 get :index, :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json
776 assert_response :bad_request
777 assert_equal "text/plain", @response.content_type
780 def test_index_closed
781 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 5.days)
782 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 100.days)
783 create(:note_with_comments, :status => "hidden")
784 create(:note_with_comments)
786 # Open notes + closed in last 7 days
787 get :index, :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json"
788 assert_response :success
789 assert_equal "application/json", @response.content_type
790 js = ActiveSupport::JSON.decode(@response.body)
792 assert_equal "FeatureCollection", js["type"]
793 assert_equal 2, js["features"].count
796 get :index, :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json"
797 assert_response :success
798 assert_equal "application/json", @response.content_type
799 js = ActiveSupport::JSON.decode(@response.body)
801 assert_equal "FeatureCollection", js["type"]
802 assert_equal 1, js["features"].count
804 # Open notes + all closed notes
805 get :index, :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json"
806 assert_response :success
807 assert_equal "application/json", @response.content_type
808 js = ActiveSupport::JSON.decode(@response.body)
810 assert_equal "FeatureCollection", js["type"]
811 assert_equal 3, js["features"].count
814 def test_index_bad_params
815 get :index, :bbox => "-2.5,-2.5,2.5"
816 assert_response :bad_request
818 get :index, :bbox => "-2.5,-2.5,2.5,2.5,2.5"
819 assert_response :bad_request
821 get :index, :b => "-2.5", :r => "2.5", :t => "2.5"
822 assert_response :bad_request
824 get :index, :l => "-2.5", :r => "2.5", :t => "2.5"
825 assert_response :bad_request
827 get :index, :l => "-2.5", :b => "-2.5", :t => "2.5"
828 assert_response :bad_request
830 get :index, :l => "-2.5", :b => "-2.5", :r => "2.5"
831 assert_response :bad_request
833 get :index, :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json"
834 assert_response :bad_request
836 get :index, :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json"
837 assert_response :bad_request
840 def test_search_success
841 create(:note_with_comments)
843 get :search, :q => "note comment", :format => "xml"
844 assert_response :success
845 assert_equal "application/xml", @response.content_type
846 assert_select "osm", :count => 1 do
847 assert_select "note", :count => 1
850 get :search, :q => "note comment", :format => "json"
851 assert_response :success
852 assert_equal "application/json", @response.content_type
853 js = ActiveSupport::JSON.decode(@response.body)
855 assert_equal "FeatureCollection", js["type"]
856 assert_equal 1, js["features"].count
858 get :search, :q => "note comment", :format => "rss"
859 assert_response :success
860 assert_equal "application/rss+xml", @response.content_type
861 assert_select "rss", :count => 1 do
862 assert_select "channel", :count => 1 do
863 assert_select "item", :count => 1
867 get :search, :q => "note comment", :format => "gpx"
868 assert_response :success
869 assert_equal "application/gpx+xml", @response.content_type
870 assert_select "gpx", :count => 1 do
871 assert_select "wpt", :count => 1
875 def test_search_no_match
876 create(:note_with_comments)
878 get :search, :q => "no match", :format => "xml"
879 assert_response :success
880 assert_equal "application/xml", @response.content_type
881 assert_select "osm", :count => 1 do
882 assert_select "note", :count => 0
885 get :search, :q => "no match", :format => "json"
886 assert_response :success
887 assert_equal "application/json", @response.content_type
888 js = ActiveSupport::JSON.decode(@response.body)
890 assert_equal "FeatureCollection", js["type"]
891 assert_equal 0, js["features"].count
893 get :search, :q => "no match", :format => "rss"
894 assert_response :success
895 assert_equal "application/rss+xml", @response.content_type
896 assert_select "rss", :count => 1 do
897 assert_select "channel", :count => 1 do
898 assert_select "item", :count => 0
902 get :search, :q => "no match", :format => "gpx"
903 assert_response :success
904 assert_equal "application/gpx+xml", @response.content_type
905 assert_select "gpx", :count => 1 do
906 assert_select "wpt", :count => 0
910 def test_search_bad_params
912 assert_response :bad_request
914 get :search, :q => "no match", :limit => "0", :format => "json"
915 assert_response :bad_request
917 get :search, :q => "no match", :limit => "10001", :format => "json"
918 assert_response :bad_request
921 def test_feed_success
922 position = (1.1 * GeoRecord::SCALE).to_i
923 create(:note_with_comments, :latitude => position, :longitude => position)
924 create(:note_with_comments, :latitude => position, :longitude => position)
925 position = (1.5 * GeoRecord::SCALE).to_i
926 create(:note_with_comments, :latitude => position, :longitude => position)
927 create(:note_with_comments, :latitude => position, :longitude => position)
929 get :feed, :format => "rss"
930 assert_response :success
931 assert_equal "application/rss+xml", @response.content_type
932 assert_select "rss", :count => 1 do
933 assert_select "channel", :count => 1 do
934 assert_select "item", :count => 4
938 get :feed, :bbox => "1,1,1.2,1.2", :format => "rss"
939 assert_response :success
940 assert_equal "application/rss+xml", @response.content_type
941 assert_select "rss", :count => 1 do
942 assert_select "channel", :count => 1 do
943 assert_select "item", :count => 2
949 get :feed, :bbox => "1,1,1.2", :format => "rss"
950 assert_response :bad_request
952 get :feed, :bbox => "1,1,1.2,1.2,1.2", :format => "rss"
953 assert_response :bad_request
955 get :feed, :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss"
956 assert_response :bad_request
958 get :feed, :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss"
959 assert_response :bad_request
962 def test_mine_success
963 first_user = create(:user)
964 second_user = create(:user)
965 moderator_user = create(:moderator_user)
967 create(:note) do |note|
968 create(:note_comment, :note => note, :author => first_user)
970 create(:note) do |note|
971 create(:note_comment, :note => note, :author => second_user)
973 create(:note, :status => "hidden") do |note|
974 create(:note_comment, :note => note, :author => second_user)
977 # Note that the table rows include a header row
978 get :mine, :display_name => first_user.display_name
979 assert_response :success
980 assert_select "table.note_list tr", :count => 2
982 get :mine, :display_name => second_user.display_name
983 assert_response :success
984 assert_select "table.note_list tr", :count => 2
986 get :mine, :display_name => "non-existent"
987 assert_response :not_found
989 session[:user] = moderator_user.id
991 get :mine, :display_name => first_user.display_name
992 assert_response :success
993 assert_select "table.note_list tr", :count => 2
995 get :mine, :display_name => second_user.display_name
996 assert_response :success
997 assert_select "table.note_list tr", :count => 3
999 get :mine, :display_name => "non-existent"
1000 assert_response :not_found