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
220 def test_comment_success
221 open_note_with_comment = create(:note_with_comments)
222 assert_difference "NoteComment.count", 1 do
223 assert_no_difference "ActionMailer::Base.deliveries.size" do
224 post :comment, :id => open_note_with_comment.id, :text => "This is an additional comment", :format => "json"
227 assert_response :success
228 js = ActiveSupport::JSON.decode(@response.body)
230 assert_equal "Feature", js["type"]
231 assert_equal open_note_with_comment.id, js["properties"]["id"]
232 assert_equal "open", js["properties"]["status"]
233 assert_equal 2, js["properties"]["comments"].count
234 assert_equal "commented", js["properties"]["comments"].last["action"]
235 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
236 assert_nil js["properties"]["comments"].last["user"]
238 get :show, :id => open_note_with_comment.id, :format => "json"
239 assert_response :success
240 js = ActiveSupport::JSON.decode(@response.body)
242 assert_equal "Feature", js["type"]
243 assert_equal open_note_with_comment.id, js["properties"]["id"]
244 assert_equal "open", js["properties"]["status"]
245 assert_equal 2, js["properties"]["comments"].count
246 assert_equal "commented", js["properties"]["comments"].last["action"]
247 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
248 assert_nil js["properties"]["comments"].last["user"]
250 # Ensure that emails are sent to users
251 first_user = create(:user)
252 second_user = create(:user)
253 third_user = create(:user)
255 note_with_comments_by_users = create(:note) do |note|
256 create(:note_comment, :note => note, :author => first_user)
257 create(:note_comment, :note => note, :author => second_user)
259 assert_difference "NoteComment.count", 1 do
260 assert_difference "ActionMailer::Base.deliveries.size", 2 do
261 post :comment, :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json"
264 assert_response :success
265 js = ActiveSupport::JSON.decode(@response.body)
267 assert_equal "Feature", js["type"]
268 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
269 assert_equal "open", js["properties"]["status"]
270 assert_equal 3, js["properties"]["comments"].count
271 assert_equal "commented", js["properties"]["comments"].last["action"]
272 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
273 assert_nil js["properties"]["comments"].last["user"]
275 email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
277 assert_equal 1, email.to.length
278 assert_equal "[OpenStreetMap] An anonymous user has commented on one of your notes", email.subject
280 email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
282 assert_equal 1, email.to.length
283 assert_equal "[OpenStreetMap] An anonymous user has commented on a note you are interested in", email.subject
285 get :show, :id => note_with_comments_by_users.id, :format => "json"
286 assert_response :success
287 js = ActiveSupport::JSON.decode(@response.body)
289 assert_equal "Feature", js["type"]
290 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
291 assert_equal "open", js["properties"]["status"]
292 assert_equal 3, js["properties"]["comments"].count
293 assert_equal "commented", js["properties"]["comments"].last["action"]
294 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
295 assert_nil js["properties"]["comments"].last["user"]
297 ActionMailer::Base.deliveries.clear
299 basic_authorization(third_user.email, "test")
301 assert_difference "NoteComment.count", 1 do
302 assert_difference "ActionMailer::Base.deliveries.size", 2 do
303 post :comment, :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json"
306 assert_response :success
307 js = ActiveSupport::JSON.decode(@response.body)
309 assert_equal "Feature", js["type"]
310 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
311 assert_equal "open", js["properties"]["status"]
312 assert_equal 4, js["properties"]["comments"].count
313 assert_equal "commented", js["properties"]["comments"].last["action"]
314 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
315 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
317 email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
319 assert_equal 1, email.to.length
320 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on one of your notes", email.subject
321 assert_equal first_user.email, email.to.first
323 email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
325 assert_equal 1, email.to.length
326 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on a note you are interested in", email.subject
328 get :show, :id => note_with_comments_by_users.id, :format => "json"
329 assert_response :success
330 js = ActiveSupport::JSON.decode(@response.body)
332 assert_equal "Feature", js["type"]
333 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
334 assert_equal "open", js["properties"]["status"]
335 assert_equal 4, js["properties"]["comments"].count
336 assert_equal "commented", js["properties"]["comments"].last["action"]
337 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
338 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
340 ActionMailer::Base.deliveries.clear
343 def test_comment_fail
344 open_note_with_comment = create(:note_with_comments)
346 assert_no_difference "NoteComment.count" do
347 post :comment, :text => "This is an additional comment"
349 assert_response :bad_request
351 assert_no_difference "NoteComment.count" do
352 post :comment, :id => open_note_with_comment.id
354 assert_response :bad_request
356 assert_no_difference "NoteComment.count" do
357 post :comment, :id => open_note_with_comment.id, :text => ""
359 assert_response :bad_request
361 assert_no_difference "NoteComment.count" do
362 post :comment, :id => 12345, :text => "This is an additional comment"
364 assert_response :not_found
366 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
368 assert_no_difference "NoteComment.count" do
369 post :comment, :id => hidden_note_with_comment.id, :text => "This is an additional comment"
371 assert_response :gone
373 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
375 assert_no_difference "NoteComment.count" do
376 post :comment, :id => closed_note_with_comment.id, :text => "This is an additional comment"
378 assert_response :conflict
381 def test_close_success
382 open_note_with_comment = create(:note_with_comments)
385 post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
386 assert_response :unauthorized
388 basic_authorization(user.email, "test")
390 post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
391 assert_response :success
392 js = ActiveSupport::JSON.decode(@response.body)
394 assert_equal "Feature", js["type"]
395 assert_equal open_note_with_comment.id, js["properties"]["id"]
396 assert_equal "closed", js["properties"]["status"]
397 assert_equal 2, js["properties"]["comments"].count
398 assert_equal "closed", js["properties"]["comments"].last["action"]
399 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
400 assert_equal user.display_name, js["properties"]["comments"].last["user"]
402 get :show, :id => open_note_with_comment.id, :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"]
417 assert_response :unauthorized
419 basic_authorization(create(:user).email, "test")
422 assert_response :bad_request
424 post :close, :id => 12345
425 assert_response :not_found
427 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
429 post :close, :id => hidden_note_with_comment.id
430 assert_response :gone
432 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
434 post :close, :id => closed_note_with_comment.id
435 assert_response :conflict
438 def test_reopen_success
439 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
442 post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
443 assert_response :unauthorized
445 basic_authorization(user.email, "test")
447 post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
448 assert_response :success
449 js = ActiveSupport::JSON.decode(@response.body)
451 assert_equal "Feature", js["type"]
452 assert_equal closed_note_with_comment.id, js["properties"]["id"]
453 assert_equal "open", js["properties"]["status"]
454 assert_equal 2, js["properties"]["comments"].count
455 assert_equal "reopened", js["properties"]["comments"].last["action"]
456 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
457 assert_equal user.display_name, js["properties"]["comments"].last["user"]
459 get :show, :id => closed_note_with_comment.id, :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"]
473 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
475 post :reopen, :id => hidden_note_with_comment.id
476 assert_response :unauthorized
478 basic_authorization(create(:user).email, "test")
480 post :reopen, :id => 12345
481 assert_response :not_found
483 post :reopen, :id => hidden_note_with_comment.id
484 assert_response :gone
486 open_note_with_comment = create(:note_with_comments)
488 post :reopen, :id => open_note_with_comment.id
489 assert_response :conflict
492 def test_show_success
493 open_note = create(:note_with_comments)
495 get :show, :id => open_note.id, :format => "xml"
496 assert_response :success
497 assert_equal "application/xml", @response.content_type
498 assert_select "osm", :count => 1 do
499 assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
500 assert_select "id", open_note.id.to_s
501 assert_select "url", note_url(open_note, :format => "xml")
502 assert_select "comment_url", comment_note_url(open_note, :format => "xml")
503 assert_select "close_url", close_note_url(open_note, :format => "xml")
504 assert_select "date_created", open_note.created_at.to_s
505 assert_select "status", open_note.status
506 assert_select "comments", :count => 1 do
507 assert_select "comment", :count => 1
512 get :show, :id => open_note.id, :format => "rss"
513 assert_response :success
514 assert_equal "application/rss+xml", @response.content_type
515 assert_select "rss", :count => 1 do
516 assert_select "channel", :count => 1 do
517 assert_select "item", :count => 1 do
518 assert_select "link", browse_note_url(open_note)
519 assert_select "guid", note_url(open_note)
520 assert_select "pubDate", open_note.created_at.to_s(:rfc822)
521 # assert_select "geo:lat", open_note.lat.to_s
522 # assert_select "geo:long", open_note.lon
523 # assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
528 get :show, :id => open_note.id, :format => "json"
529 assert_response :success
530 assert_equal "application/json", @response.content_type
531 js = ActiveSupport::JSON.decode(@response.body)
533 assert_equal "Feature", js["type"]
534 assert_equal "Point", js["geometry"]["type"]
535 assert_equal open_note.lat, js["geometry"]["coordinates"][0]
536 assert_equal open_note.lon, js["geometry"]["coordinates"][1]
537 assert_equal open_note.id, js["properties"]["id"]
538 assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
539 assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
540 assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
541 assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
542 assert_equal open_note.status, js["properties"]["status"]
544 get :show, :id => open_note.id, :format => "gpx"
545 assert_response :success
546 assert_equal "application/gpx+xml", @response.content_type
547 assert_select "gpx", :count => 1 do
548 assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
549 assert_select "time", :count => 1
550 assert_select "name", "Note: #{open_note.id}"
551 assert_select "desc", :count => 1
552 assert_select "link[href='http://test.host/note/#{open_note.id}']", :count => 1
553 assert_select "extensions", :count => 1 do
554 assert_select "id", open_note.id.to_s
555 assert_select "url", note_url(open_note, :format => "gpx")
556 assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
557 assert_select "close_url", close_note_url(open_note, :format => "gpx")
563 def test_show_hidden_comment
564 note_with_hidden_comment = create(:note) do |note|
565 create(:note_comment, :note => note, :body => "Valid comment for hidden note")
566 create(:note_comment, :note => note, :visible => false)
567 create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
570 get :show, :id => note_with_hidden_comment.id, :format => "json"
571 assert_response :success
572 js = ActiveSupport::JSON.decode(@response.body)
574 assert_equal "Feature", js["type"]
575 assert_equal note_with_hidden_comment.id, js["properties"]["id"]
576 assert_equal 2, js["properties"]["comments"].count
577 assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
578 assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
582 get :show, :id => 12345
583 assert_response :not_found
585 get :show, :id => create(:note, :status => "hidden").id
586 assert_response :gone
589 def test_destroy_success
590 open_note_with_comment = create(:note_with_comments)
592 moderator_user = create(:moderator_user)
594 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
595 assert_response :unauthorized
597 basic_authorization(user.email, "test")
599 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
600 assert_response :forbidden
602 basic_authorization(moderator_user.email, "test")
604 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
605 assert_response :success
606 js = ActiveSupport::JSON.decode(@response.body)
608 assert_equal "Feature", js["type"]
609 assert_equal open_note_with_comment.id, js["properties"]["id"]
610 assert_equal "hidden", js["properties"]["status"]
611 assert_equal 2, js["properties"]["comments"].count
612 assert_equal "hidden", js["properties"]["comments"].last["action"]
613 assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
614 assert_equal moderator_user.display_name, js["properties"]["comments"].last["user"]
616 get :show, :id => open_note_with_comment.id, :format => "json"
617 assert_response :gone
620 def test_destroy_fail
622 moderator_user = create(:moderator_user)
624 delete :destroy, :id => 12345, :format => "json"
625 assert_response :unauthorized
627 basic_authorization(user.email, "test")
629 delete :destroy, :id => 12345, :format => "json"
630 assert_response :forbidden
632 basic_authorization(moderator_user.email, "test")
634 delete :destroy, :id => 12345, :format => "json"
635 assert_response :not_found
637 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
639 delete :destroy, :id => hidden_note_with_comment.id, :format => "json"
640 assert_response :gone
643 def test_index_success
644 position = (1.1 * GeoRecord::SCALE).to_i
645 create(:note_with_comments, :latitude => position, :longitude => position)
646 create(:note_with_comments, :latitude => position, :longitude => position)
648 get :index, :bbox => "1,1,1.2,1.2", :format => "rss"
649 assert_response :success
650 assert_equal "application/rss+xml", @response.content_type
651 assert_select "rss", :count => 1 do
652 assert_select "channel", :count => 1 do
653 assert_select "item", :count => 2
657 get :index, :bbox => "1,1,1.2,1.2", :format => "json"
658 assert_response :success
659 assert_equal "application/json", @response.content_type
660 js = ActiveSupport::JSON.decode(@response.body)
662 assert_equal "FeatureCollection", js["type"]
663 assert_equal 2, js["features"].count
665 get :index, :bbox => "1,1,1.2,1.2", :format => "xml"
666 assert_response :success
667 assert_equal "application/xml", @response.content_type
668 assert_select "osm", :count => 1 do
669 assert_select "note", :count => 2
672 get :index, :bbox => "1,1,1.2,1.2", :format => "gpx"
673 assert_response :success
674 assert_equal "application/gpx+xml", @response.content_type
675 assert_select "gpx", :count => 1 do
676 assert_select "wpt", :count => 2
681 position = (1.1 * GeoRecord::SCALE).to_i
682 create(:note_with_comments, :latitude => position, :longitude => position)
683 create(:note_with_comments, :latitude => position, :longitude => position)
685 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss"
686 assert_response :success
687 assert_equal "application/rss+xml", @response.content_type
688 assert_select "rss", :count => 1 do
689 assert_select "channel", :count => 1 do
690 assert_select "item", :count => 1
694 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json"
695 assert_response :success
696 assert_equal "application/json", @response.content_type
697 js = ActiveSupport::JSON.decode(@response.body)
699 assert_equal "FeatureCollection", js["type"]
700 assert_equal 1, js["features"].count
702 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml"
703 assert_response :success
704 assert_equal "application/xml", @response.content_type
705 assert_select "osm", :count => 1 do
706 assert_select "note", :count => 1
709 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx"
710 assert_response :success
711 assert_equal "application/gpx+xml", @response.content_type
712 assert_select "gpx", :count => 1 do
713 assert_select "wpt", :count => 1
717 def test_index_empty_area
718 get :index, :bbox => "5,5,5.1,5.1", :format => "rss"
719 assert_response :success
720 assert_equal "application/rss+xml", @response.content_type
721 assert_select "rss", :count => 1 do
722 assert_select "channel", :count => 1 do
723 assert_select "item", :count => 0
727 get :index, :bbox => "5,5,5.1,5.1", :format => "json"
728 assert_response :success
729 assert_equal "application/json", @response.content_type
730 js = ActiveSupport::JSON.decode(@response.body)
732 assert_equal "FeatureCollection", js["type"]
733 assert_equal 0, js["features"].count
735 get :index, :bbox => "5,5,5.1,5.1", :format => "xml"
736 assert_response :success
737 assert_equal "application/xml", @response.content_type
738 assert_select "osm", :count => 1 do
739 assert_select "note", :count => 0
742 get :index, :bbox => "5,5,5.1,5.1", :format => "gpx"
743 assert_response :success
744 assert_equal "application/gpx+xml", @response.content_type
745 assert_select "gpx", :count => 1 do
746 assert_select "wpt", :count => 0
750 def test_index_large_area
751 get :index, :bbox => "-2.5,-2.5,2.5,2.5", :format => :json
752 assert_response :success
753 assert_equal "application/json", @response.content_type
755 get :index, :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json
756 assert_response :success
757 assert_equal "application/json", @response.content_type
759 get :index, :bbox => "-10,-10,12,12", :format => :json
760 assert_response :bad_request
761 assert_equal "text/plain", @response.content_type
763 get :index, :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json
764 assert_response :bad_request
765 assert_equal "text/plain", @response.content_type
768 def test_index_closed
769 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 5.days)
770 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 100.days)
771 create(:note_with_comments, :status => "hidden")
772 create(:note_with_comments)
774 # Open notes + closed in last 7 days
775 get :index, :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json"
776 assert_response :success
777 assert_equal "application/json", @response.content_type
778 js = ActiveSupport::JSON.decode(@response.body)
780 assert_equal "FeatureCollection", js["type"]
781 assert_equal 2, js["features"].count
784 get :index, :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json"
785 assert_response :success
786 assert_equal "application/json", @response.content_type
787 js = ActiveSupport::JSON.decode(@response.body)
789 assert_equal "FeatureCollection", js["type"]
790 assert_equal 1, js["features"].count
792 # Open notes + all closed notes
793 get :index, :bbox => "1,1,1.7,1.7", :closed => "-1", :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 3, js["features"].count
802 def test_index_bad_params
803 get :index, :bbox => "-2.5,-2.5,2.5"
804 assert_response :bad_request
806 get :index, :bbox => "-2.5,-2.5,2.5,2.5,2.5"
807 assert_response :bad_request
809 get :index, :b => "-2.5", :r => "2.5", :t => "2.5"
810 assert_response :bad_request
812 get :index, :l => "-2.5", :r => "2.5", :t => "2.5"
813 assert_response :bad_request
815 get :index, :l => "-2.5", :b => "-2.5", :t => "2.5"
816 assert_response :bad_request
818 get :index, :l => "-2.5", :b => "-2.5", :r => "2.5"
819 assert_response :bad_request
821 get :index, :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json"
822 assert_response :bad_request
824 get :index, :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json"
825 assert_response :bad_request
828 def test_search_success
829 create(:note_with_comments)
831 get :search, :q => "note comment", :format => "xml"
832 assert_response :success
833 assert_equal "application/xml", @response.content_type
834 assert_select "osm", :count => 1 do
835 assert_select "note", :count => 1
838 get :search, :q => "note comment", :format => "json"
839 assert_response :success
840 assert_equal "application/json", @response.content_type
841 js = ActiveSupport::JSON.decode(@response.body)
843 assert_equal "FeatureCollection", js["type"]
844 assert_equal 1, js["features"].count
846 get :search, :q => "note comment", :format => "rss"
847 assert_response :success
848 assert_equal "application/rss+xml", @response.content_type
849 assert_select "rss", :count => 1 do
850 assert_select "channel", :count => 1 do
851 assert_select "item", :count => 1
855 get :search, :q => "note comment", :format => "gpx"
856 assert_response :success
857 assert_equal "application/gpx+xml", @response.content_type
858 assert_select "gpx", :count => 1 do
859 assert_select "wpt", :count => 1
863 def test_search_no_match
864 create(:note_with_comments)
866 get :search, :q => "no match", :format => "xml"
867 assert_response :success
868 assert_equal "application/xml", @response.content_type
869 assert_select "osm", :count => 1 do
870 assert_select "note", :count => 0
873 get :search, :q => "no match", :format => "json"
874 assert_response :success
875 assert_equal "application/json", @response.content_type
876 js = ActiveSupport::JSON.decode(@response.body)
878 assert_equal "FeatureCollection", js["type"]
879 assert_equal 0, js["features"].count
881 get :search, :q => "no match", :format => "rss"
882 assert_response :success
883 assert_equal "application/rss+xml", @response.content_type
884 assert_select "rss", :count => 1 do
885 assert_select "channel", :count => 1 do
886 assert_select "item", :count => 0
890 get :search, :q => "no match", :format => "gpx"
891 assert_response :success
892 assert_equal "application/gpx+xml", @response.content_type
893 assert_select "gpx", :count => 1 do
894 assert_select "wpt", :count => 0
898 def test_search_bad_params
900 assert_response :bad_request
902 get :search, :q => "no match", :limit => "0", :format => "json"
903 assert_response :bad_request
905 get :search, :q => "no match", :limit => "10001", :format => "json"
906 assert_response :bad_request
909 def test_feed_success
910 position = (1.1 * GeoRecord::SCALE).to_i
911 create(:note_with_comments, :latitude => position, :longitude => position)
912 create(:note_with_comments, :latitude => position, :longitude => position)
913 position = (1.5 * GeoRecord::SCALE).to_i
914 create(:note_with_comments, :latitude => position, :longitude => position)
915 create(:note_with_comments, :latitude => position, :longitude => position)
917 get :feed, :format => "rss"
918 assert_response :success
919 assert_equal "application/rss+xml", @response.content_type
920 assert_select "rss", :count => 1 do
921 assert_select "channel", :count => 1 do
922 assert_select "item", :count => 4
926 get :feed, :bbox => "1,1,1.2,1.2", :format => "rss"
927 assert_response :success
928 assert_equal "application/rss+xml", @response.content_type
929 assert_select "rss", :count => 1 do
930 assert_select "channel", :count => 1 do
931 assert_select "item", :count => 2
937 get :feed, :bbox => "1,1,1.2", :format => "rss"
938 assert_response :bad_request
940 get :feed, :bbox => "1,1,1.2,1.2,1.2", :format => "rss"
941 assert_response :bad_request
943 get :feed, :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss"
944 assert_response :bad_request
946 get :feed, :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss"
947 assert_response :bad_request
950 def test_mine_success
951 first_user = create(:user)
952 second_user = create(:user)
953 moderator_user = create(:moderator_user)
955 create(:note) do |note|
956 create(:note_comment, :note => note, :author => first_user)
958 create(:note) do |note|
959 create(:note_comment, :note => note, :author => second_user)
961 create(:note, :status => "hidden") do |note|
962 create(:note_comment, :note => note, :author => second_user)
965 # Note that the table rows include a header row
966 get :mine, :display_name => first_user.display_name
967 assert_response :success
968 assert_select "table.note_list tr", :count => 2
970 get :mine, :display_name => second_user.display_name
971 assert_response :success
972 assert_select "table.note_list tr", :count => 2
974 get :mine, :display_name => "non-existent"
975 assert_response :not_found
977 session[:user] = moderator_user.id
979 get :mine, :display_name => first_user.display_name
980 assert_response :success
981 assert_select "table.note_list tr", :count => 2
983 get :mine, :display_name => second_user.display_name
984 assert_response :success
985 assert_select "table.note_list tr", :count => 3
987 get :mine, :display_name => "non-existent"
988 assert_response :not_found