3 class NotesControllerTest < ActionController::TestCase
4 fixtures :users, :user_roles
7 # test all routes which lead to this controller
10 { :path => "/api/0.6/notes", :method => :post },
11 { :controller => "notes", :action => "create", :format => "xml" }
14 { :path => "/api/0.6/notes/1", :method => :get },
15 { :controller => "notes", :action => "show", :id => "1", :format => "xml" }
18 { :controller => "notes", :action => "show", :id => "1", :format => "xml" },
19 { :path => "/api/0.6/notes/1.xml", :method => :get }
22 { :path => "/api/0.6/notes/1.rss", :method => :get },
23 { :controller => "notes", :action => "show", :id => "1", :format => "rss" }
26 { :path => "/api/0.6/notes/1.json", :method => :get },
27 { :controller => "notes", :action => "show", :id => "1", :format => "json" }
30 { :path => "/api/0.6/notes/1.gpx", :method => :get },
31 { :controller => "notes", :action => "show", :id => "1", :format => "gpx" }
34 { :path => "/api/0.6/notes/1/comment", :method => :post },
35 { :controller => "notes", :action => "comment", :id => "1", :format => "xml" }
38 { :path => "/api/0.6/notes/1/close", :method => :post },
39 { :controller => "notes", :action => "close", :id => "1", :format => "xml" }
42 { :path => "/api/0.6/notes/1/reopen", :method => :post },
43 { :controller => "notes", :action => "reopen", :id => "1", :format => "xml" }
46 { :path => "/api/0.6/notes/1", :method => :delete },
47 { :controller => "notes", :action => "destroy", :id => "1", :format => "xml" }
51 { :path => "/api/0.6/notes", :method => :get },
52 { :controller => "notes", :action => "index", :format => "xml" }
55 { :controller => "notes", :action => "index", :format => "xml" },
56 { :path => "/api/0.6/notes.xml", :method => :get }
59 { :path => "/api/0.6/notes.rss", :method => :get },
60 { :controller => "notes", :action => "index", :format => "rss" }
63 { :path => "/api/0.6/notes.json", :method => :get },
64 { :controller => "notes", :action => "index", :format => "json" }
67 { :path => "/api/0.6/notes.gpx", :method => :get },
68 { :controller => "notes", :action => "index", :format => "gpx" }
72 { :path => "/api/0.6/notes/search", :method => :get },
73 { :controller => "notes", :action => "search", :format => "xml" }
76 { :controller => "notes", :action => "search", :format => "xml" },
77 { :path => "/api/0.6/notes/search.xml", :method => :get }
80 { :path => "/api/0.6/notes/search.rss", :method => :get },
81 { :controller => "notes", :action => "search", :format => "rss" }
84 { :path => "/api/0.6/notes/search.json", :method => :get },
85 { :controller => "notes", :action => "search", :format => "json" }
88 { :path => "/api/0.6/notes/search.gpx", :method => :get },
89 { :controller => "notes", :action => "search", :format => "gpx" }
93 { :path => "/api/0.6/notes/feed", :method => :get },
94 { :controller => "notes", :action => "feed", :format => "rss" }
98 { :controller => "notes", :action => "create" },
99 { :path => "/api/0.6/notes/addPOIexec", :method => :post }
102 { :controller => "notes", :action => "close" },
103 { :path => "/api/0.6/notes/closePOIexec", :method => :post }
106 { :controller => "notes", :action => "comment" },
107 { :path => "/api/0.6/notes/editPOIexec", :method => :post }
110 { :controller => "notes", :action => "index", :format => "gpx" },
111 { :path => "/api/0.6/notes/getGPX", :method => :get }
114 { :controller => "notes", :action => "feed", :format => "rss" },
115 { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
119 { :path => "/user/username/notes", :method => :get },
120 { :controller => "notes", :action => "mine", :display_name => "username" }
124 def test_create_success
125 assert_difference "Note.count", 1 do
126 assert_difference "NoteComment.count", 1 do
127 post :create, :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, :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, :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, :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, :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, :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, :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, :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, :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, :lat => -1.0, :lon => "abc", :text => "This is a comment"
213 assert_response :bad_request
216 def test_comment_success
217 open_note_with_comment = create(:note_with_comments)
218 assert_difference "NoteComment.count", 1 do
219 assert_no_difference "ActionMailer::Base.deliveries.size" do
220 post :comment, :id => open_note_with_comment.id, :text => "This is an additional comment", :format => "json"
223 assert_response :success
224 js = ActiveSupport::JSON.decode(@response.body)
226 assert_equal "Feature", js["type"]
227 assert_equal open_note_with_comment.id, js["properties"]["id"]
228 assert_equal "open", js["properties"]["status"]
229 assert_equal 2, js["properties"]["comments"].count
230 assert_equal "commented", js["properties"]["comments"].last["action"]
231 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
232 assert_nil js["properties"]["comments"].last["user"]
234 get :show, :id => open_note_with_comment.id, :format => "json"
235 assert_response :success
236 js = ActiveSupport::JSON.decode(@response.body)
238 assert_equal "Feature", js["type"]
239 assert_equal open_note_with_comment.id, js["properties"]["id"]
240 assert_equal "open", js["properties"]["status"]
241 assert_equal 2, js["properties"]["comments"].count
242 assert_equal "commented", js["properties"]["comments"].last["action"]
243 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
244 assert_nil js["properties"]["comments"].last["user"]
246 # Ensure that emails are sent to users
247 note_with_comments_by_users = create(:note) do |note|
248 create(:note_comment, :note => note, :author_id => users(:normal_user).id)
249 create(:note_comment, :note => note, :author_id => users(:second_public_user).id)
251 assert_difference "NoteComment.count", 1 do
252 assert_difference "ActionMailer::Base.deliveries.size", 2 do
253 post :comment, :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json"
256 assert_response :success
257 js = ActiveSupport::JSON.decode(@response.body)
259 assert_equal "Feature", js["type"]
260 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
261 assert_equal "open", js["properties"]["status"]
262 assert_equal 3, js["properties"]["comments"].count
263 assert_equal "commented", js["properties"]["comments"].last["action"]
264 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
265 assert_nil js["properties"]["comments"].last["user"]
267 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "test@openstreetmap.org" }
269 assert_equal 1, email.to.length
270 assert_equal "[OpenStreetMap] An anonymous user has commented on one of your notes", email.subject
272 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "public@OpenStreetMap.org" }
274 assert_equal 1, email.to.length
275 assert_equal "[OpenStreetMap] An anonymous user has commented on a note you are interested in", email.subject
277 get :show, :id => note_with_comments_by_users.id, :format => "json"
278 assert_response :success
279 js = ActiveSupport::JSON.decode(@response.body)
281 assert_equal "Feature", js["type"]
282 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
283 assert_equal "open", js["properties"]["status"]
284 assert_equal 3, js["properties"]["comments"].count
285 assert_equal "commented", js["properties"]["comments"].last["action"]
286 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
287 assert_nil js["properties"]["comments"].last["user"]
289 ActionMailer::Base.deliveries.clear
291 basic_authorization(users(:public_user).email, "test")
293 assert_difference "NoteComment.count", 1 do
294 assert_difference "ActionMailer::Base.deliveries.size", 2 do
295 post :comment, :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json"
298 assert_response :success
299 js = ActiveSupport::JSON.decode(@response.body)
301 assert_equal "Feature", js["type"]
302 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
303 assert_equal "open", js["properties"]["status"]
304 assert_equal 4, js["properties"]["comments"].count
305 assert_equal "commented", js["properties"]["comments"].last["action"]
306 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
307 assert_equal "test2", js["properties"]["comments"].last["user"]
309 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "test@openstreetmap.org" }
311 assert_equal 1, email.to.length
312 assert_equal "[OpenStreetMap] test2 has commented on one of your notes", email.subject
313 assert_equal "test@openstreetmap.org", email.to.first
315 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "public@OpenStreetMap.org" }
317 assert_equal 1, email.to.length
318 assert_equal "[OpenStreetMap] test2 has commented on a note you are interested in", email.subject
320 get :show, :id => note_with_comments_by_users.id, :format => "json"
321 assert_response :success
322 js = ActiveSupport::JSON.decode(@response.body)
324 assert_equal "Feature", js["type"]
325 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
326 assert_equal "open", js["properties"]["status"]
327 assert_equal 4, js["properties"]["comments"].count
328 assert_equal "commented", js["properties"]["comments"].last["action"]
329 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
330 assert_equal "test2", js["properties"]["comments"].last["user"]
332 ActionMailer::Base.deliveries.clear
335 def test_comment_fail
336 open_note_with_comment = create(:note_with_comments)
338 assert_no_difference "NoteComment.count" do
339 post :comment, :text => "This is an additional comment"
341 assert_response :bad_request
343 assert_no_difference "NoteComment.count" do
344 post :comment, :id => open_note_with_comment.id
346 assert_response :bad_request
348 assert_no_difference "NoteComment.count" do
349 post :comment, :id => open_note_with_comment.id, :text => ""
351 assert_response :bad_request
353 assert_no_difference "NoteComment.count" do
354 post :comment, :id => 12345, :text => "This is an additional comment"
356 assert_response :not_found
358 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
360 assert_no_difference "NoteComment.count" do
361 post :comment, :id => hidden_note_with_comment.id, :text => "This is an additional comment"
363 assert_response :gone
365 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
367 assert_no_difference "NoteComment.count" do
368 post :comment, :id => closed_note_with_comment.id, :text => "This is an additional comment"
370 assert_response :conflict
373 def test_close_success
374 open_note_with_comment = create(:note_with_comments)
376 post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
377 assert_response :unauthorized
379 basic_authorization(users(:public_user).email, "test")
381 post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
382 assert_response :success
383 js = ActiveSupport::JSON.decode(@response.body)
385 assert_equal "Feature", js["type"]
386 assert_equal open_note_with_comment.id, js["properties"]["id"]
387 assert_equal "closed", js["properties"]["status"]
388 assert_equal 2, js["properties"]["comments"].count
389 assert_equal "closed", js["properties"]["comments"].last["action"]
390 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
391 assert_equal "test2", js["properties"]["comments"].last["user"]
393 get :show, :id => open_note_with_comment.id, :format => "json"
394 assert_response :success
395 js = ActiveSupport::JSON.decode(@response.body)
397 assert_equal "Feature", js["type"]
398 assert_equal open_note_with_comment.id, js["properties"]["id"]
399 assert_equal "closed", js["properties"]["status"]
400 assert_equal 2, js["properties"]["comments"].count
401 assert_equal "closed", js["properties"]["comments"].last["action"]
402 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
403 assert_equal "test2", js["properties"]["comments"].last["user"]
408 assert_response :unauthorized
410 basic_authorization(users(:public_user).email, "test")
413 assert_response :bad_request
415 post :close, :id => 12345
416 assert_response :not_found
418 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
420 post :close, :id => hidden_note_with_comment.id
421 assert_response :gone
423 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
425 post :close, :id => closed_note_with_comment.id
426 assert_response :conflict
429 def test_reopen_success
430 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
432 post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
433 assert_response :unauthorized
435 basic_authorization(users(:public_user).email, "test")
437 post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
438 assert_response :success
439 js = ActiveSupport::JSON.decode(@response.body)
441 assert_equal "Feature", js["type"]
442 assert_equal closed_note_with_comment.id, js["properties"]["id"]
443 assert_equal "open", js["properties"]["status"]
444 assert_equal 2, js["properties"]["comments"].count
445 assert_equal "reopened", js["properties"]["comments"].last["action"]
446 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
447 assert_equal "test2", js["properties"]["comments"].last["user"]
449 get :show, :id => closed_note_with_comment.id, :format => "json"
450 assert_response :success
451 js = ActiveSupport::JSON.decode(@response.body)
453 assert_equal "Feature", js["type"]
454 assert_equal closed_note_with_comment.id, js["properties"]["id"]
455 assert_equal "open", js["properties"]["status"]
456 assert_equal 2, js["properties"]["comments"].count
457 assert_equal "reopened", js["properties"]["comments"].last["action"]
458 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
459 assert_equal "test2", js["properties"]["comments"].last["user"]
463 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
465 post :reopen, :id => hidden_note_with_comment.id
466 assert_response :unauthorized
468 basic_authorization(users(:public_user).email, "test")
470 post :reopen, :id => 12345
471 assert_response :not_found
473 post :reopen, :id => hidden_note_with_comment.id
474 assert_response :gone
476 open_note_with_comment = create(:note_with_comments)
478 post :reopen, :id => open_note_with_comment.id
479 assert_response :conflict
482 def test_show_success
483 open_note = create(:note_with_comments)
485 get :show, :id => open_note.id, :format => "xml"
486 assert_response :success
487 assert_equal "application/xml", @response.content_type
488 assert_select "osm", :count => 1 do
489 assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
490 assert_select "id", open_note.id.to_s
491 assert_select "url", note_url(open_note, :format => "xml")
492 assert_select "comment_url", comment_note_url(open_note, :format => "xml")
493 assert_select "close_url", close_note_url(open_note, :format => "xml")
494 assert_select "date_created", open_note.created_at.to_s
495 assert_select "status", open_note.status
496 assert_select "comments", :count => 1 do
497 assert_select "comment", :count => 1
502 get :show, :id => open_note.id, :format => "rss"
503 assert_response :success
504 assert_equal "application/rss+xml", @response.content_type
505 assert_select "rss", :count => 1 do
506 assert_select "channel", :count => 1 do
507 assert_select "item", :count => 1 do
508 assert_select "link", browse_note_url(open_note)
509 assert_select "guid", note_url(open_note)
510 assert_select "pubDate", open_note.created_at.to_s(:rfc822)
511 # assert_select "geo:lat", open_note.lat.to_s
512 # assert_select "geo:long", open_note.lon
513 # assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
518 get :show, :id => open_note.id, :format => "json"
519 assert_response :success
520 assert_equal "application/json", @response.content_type
521 js = ActiveSupport::JSON.decode(@response.body)
523 assert_equal "Feature", js["type"]
524 assert_equal "Point", js["geometry"]["type"]
525 assert_equal open_note.lat, js["geometry"]["coordinates"][0]
526 assert_equal open_note.lon, js["geometry"]["coordinates"][1]
527 assert_equal open_note.id, js["properties"]["id"]
528 assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
529 assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
530 assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
531 assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
532 assert_equal open_note.status, js["properties"]["status"]
534 get :show, :id => open_note.id, :format => "gpx"
535 assert_response :success
536 assert_equal "application/gpx+xml", @response.content_type
537 assert_select "gpx", :count => 1 do
538 assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
539 assert_select "time", :count => 1
540 assert_select "name", "Note: #{open_note.id}"
541 assert_select "desc", :count => 1
542 assert_select "link[href='http://www.openstreetmap.org/note/#{open_note.id}']", :count => 1
543 assert_select "extensions", :count => 1 do
544 assert_select "id", open_note.id.to_s
545 assert_select "url", note_url(open_note, :format => "gpx")
546 assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
547 assert_select "close_url", close_note_url(open_note, :format => "gpx")
553 def test_show_hidden_comment
554 note_with_hidden_comment = create(:note) do |note|
555 create(:note_comment, :note => note, :body => "Valid comment for hidden note")
556 create(:note_comment, :note => note, :visible => false)
557 create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
560 get :show, :id => note_with_hidden_comment.id, :format => "json"
561 assert_response :success
562 js = ActiveSupport::JSON.decode(@response.body)
564 assert_equal "Feature", js["type"]
565 assert_equal note_with_hidden_comment.id, js["properties"]["id"]
566 assert_equal 2, js["properties"]["comments"].count
567 assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
568 assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
572 get :show, :id => 12345
573 assert_response :not_found
575 get :show, :id => create(:note, :status => "hidden").id
576 assert_response :gone
579 def test_destroy_success
580 open_note_with_comment = create(:note_with_comments)
582 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
583 assert_response :unauthorized
585 basic_authorization(users(:public_user).email, "test")
587 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
588 assert_response :forbidden
590 basic_authorization(users(:moderator_user).email, "test")
592 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
593 assert_response :success
594 js = ActiveSupport::JSON.decode(@response.body)
596 assert_equal "Feature", js["type"]
597 assert_equal open_note_with_comment.id, js["properties"]["id"]
598 assert_equal "hidden", js["properties"]["status"]
599 assert_equal 2, js["properties"]["comments"].count
600 assert_equal "hidden", js["properties"]["comments"].last["action"]
601 assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
602 assert_equal "moderator", js["properties"]["comments"].last["user"]
604 get :show, :id => open_note_with_comment.id, :format => "json"
605 assert_response :gone
608 def test_destroy_fail
609 delete :destroy, :id => 12345, :format => "json"
610 assert_response :unauthorized
612 basic_authorization(users(:public_user).email, "test")
614 delete :destroy, :id => 12345, :format => "json"
615 assert_response :forbidden
617 basic_authorization(users(:moderator_user).email, "test")
619 delete :destroy, :id => 12345, :format => "json"
620 assert_response :not_found
622 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
624 delete :destroy, :id => hidden_note_with_comment.id, :format => "json"
625 assert_response :gone
628 def test_index_success
629 position = (1.1 * GeoRecord::SCALE).to_i
630 create(:note_with_comments, :latitude => position, :longitude => position)
631 create(:note_with_comments, :latitude => position, :longitude => position)
633 get :index, :bbox => "1,1,1.2,1.2", :format => "rss"
634 assert_response :success
635 assert_equal "application/rss+xml", @response.content_type
636 assert_select "rss", :count => 1 do
637 assert_select "channel", :count => 1 do
638 assert_select "item", :count => 2
642 get :index, :bbox => "1,1,1.2,1.2", :format => "json"
643 assert_response :success
644 assert_equal "application/json", @response.content_type
645 js = ActiveSupport::JSON.decode(@response.body)
647 assert_equal "FeatureCollection", js["type"]
648 assert_equal 2, js["features"].count
650 get :index, :bbox => "1,1,1.2,1.2", :format => "xml"
651 assert_response :success
652 assert_equal "application/xml", @response.content_type
653 assert_select "osm", :count => 1 do
654 assert_select "note", :count => 2
657 get :index, :bbox => "1,1,1.2,1.2", :format => "gpx"
658 assert_response :success
659 assert_equal "application/gpx+xml", @response.content_type
660 assert_select "gpx", :count => 1 do
661 assert_select "wpt", :count => 2
666 position = (1.1 * GeoRecord::SCALE).to_i
667 create(:note_with_comments, :latitude => position, :longitude => position)
668 create(:note_with_comments, :latitude => position, :longitude => position)
670 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss"
671 assert_response :success
672 assert_equal "application/rss+xml", @response.content_type
673 assert_select "rss", :count => 1 do
674 assert_select "channel", :count => 1 do
675 assert_select "item", :count => 1
679 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json"
680 assert_response :success
681 assert_equal "application/json", @response.content_type
682 js = ActiveSupport::JSON.decode(@response.body)
684 assert_equal "FeatureCollection", js["type"]
685 assert_equal 1, js["features"].count
687 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml"
688 assert_response :success
689 assert_equal "application/xml", @response.content_type
690 assert_select "osm", :count => 1 do
691 assert_select "note", :count => 1
694 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx"
695 assert_response :success
696 assert_equal "application/gpx+xml", @response.content_type
697 assert_select "gpx", :count => 1 do
698 assert_select "wpt", :count => 1
702 def test_index_empty_area
703 get :index, :bbox => "5,5,5.1,5.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 => 0
712 get :index, :bbox => "5,5,5.1,5.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 0, js["features"].count
720 get :index, :bbox => "5,5,5.1,5.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 => 0
727 get :index, :bbox => "5,5,5.1,5.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 => 0
735 def test_index_large_area
736 get :index, :bbox => "-2.5,-2.5,2.5,2.5", :format => :json
737 assert_response :success
738 assert_equal "application/json", @response.content_type
740 get :index, :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json
741 assert_response :success
742 assert_equal "application/json", @response.content_type
744 get :index, :bbox => "-10,-10,12,12", :format => :json
745 assert_response :bad_request
746 assert_equal "text/plain", @response.content_type
748 get :index, :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json
749 assert_response :bad_request
750 assert_equal "text/plain", @response.content_type
753 def test_index_closed
754 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 5.days)
755 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 100.days)
756 create(:note_with_comments, :status => "hidden")
757 create(:note_with_comments)
759 # Open notes + closed in last 7 days
760 get :index, :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json"
761 assert_response :success
762 assert_equal "application/json", @response.content_type
763 js = ActiveSupport::JSON.decode(@response.body)
765 assert_equal "FeatureCollection", js["type"]
766 assert_equal 2, js["features"].count
769 get :index, :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json"
770 assert_response :success
771 assert_equal "application/json", @response.content_type
772 js = ActiveSupport::JSON.decode(@response.body)
774 assert_equal "FeatureCollection", js["type"]
775 assert_equal 1, js["features"].count
777 # Open notes + all closed notes
778 get :index, :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json"
779 assert_response :success
780 assert_equal "application/json", @response.content_type
781 js = ActiveSupport::JSON.decode(@response.body)
783 assert_equal "FeatureCollection", js["type"]
784 assert_equal 3, js["features"].count
787 def test_index_bad_params
788 get :index, :bbox => "-2.5,-2.5,2.5"
789 assert_response :bad_request
791 get :index, :bbox => "-2.5,-2.5,2.5,2.5,2.5"
792 assert_response :bad_request
794 get :index, :b => "-2.5", :r => "2.5", :t => "2.5"
795 assert_response :bad_request
797 get :index, :l => "-2.5", :r => "2.5", :t => "2.5"
798 assert_response :bad_request
800 get :index, :l => "-2.5", :b => "-2.5", :t => "2.5"
801 assert_response :bad_request
803 get :index, :l => "-2.5", :b => "-2.5", :r => "2.5"
804 assert_response :bad_request
806 get :index, :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json"
807 assert_response :bad_request
809 get :index, :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json"
810 assert_response :bad_request
813 def test_search_success
814 create(:note_with_comments)
816 get :search, :q => "note comment", :format => "xml"
817 assert_response :success
818 assert_equal "application/xml", @response.content_type
819 assert_select "osm", :count => 1 do
820 assert_select "note", :count => 1
823 get :search, :q => "note comment", :format => "json"
824 assert_response :success
825 assert_equal "application/json", @response.content_type
826 js = ActiveSupport::JSON.decode(@response.body)
828 assert_equal "FeatureCollection", js["type"]
829 assert_equal 1, js["features"].count
831 get :search, :q => "note comment", :format => "rss"
832 assert_response :success
833 assert_equal "application/rss+xml", @response.content_type
834 assert_select "rss", :count => 1 do
835 assert_select "channel", :count => 1 do
836 assert_select "item", :count => 1
840 get :search, :q => "note comment", :format => "gpx"
841 assert_response :success
842 assert_equal "application/gpx+xml", @response.content_type
843 assert_select "gpx", :count => 1 do
844 assert_select "wpt", :count => 1
848 def test_search_no_match
849 create(:note_with_comments)
851 get :search, :q => "no match", :format => "xml"
852 assert_response :success
853 assert_equal "application/xml", @response.content_type
854 assert_select "osm", :count => 1 do
855 assert_select "note", :count => 0
858 get :search, :q => "no match", :format => "json"
859 assert_response :success
860 assert_equal "application/json", @response.content_type
861 js = ActiveSupport::JSON.decode(@response.body)
863 assert_equal "FeatureCollection", js["type"]
864 assert_equal 0, js["features"].count
866 get :search, :q => "no match", :format => "rss"
867 assert_response :success
868 assert_equal "application/rss+xml", @response.content_type
869 assert_select "rss", :count => 1 do
870 assert_select "channel", :count => 1 do
871 assert_select "item", :count => 0
875 get :search, :q => "no match", :format => "gpx"
876 assert_response :success
877 assert_equal "application/gpx+xml", @response.content_type
878 assert_select "gpx", :count => 1 do
879 assert_select "wpt", :count => 0
883 def test_search_bad_params
885 assert_response :bad_request
887 get :search, :q => "no match", :limit => "0", :format => "json"
888 assert_response :bad_request
890 get :search, :q => "no match", :limit => "10001", :format => "json"
891 assert_response :bad_request
894 def test_feed_success
895 position = (1.1 * GeoRecord::SCALE).to_i
896 create(:note_with_comments, :latitude => position, :longitude => position)
897 create(:note_with_comments, :latitude => position, :longitude => position)
898 position = (1.5 * GeoRecord::SCALE).to_i
899 create(:note_with_comments, :latitude => position, :longitude => position)
900 create(:note_with_comments, :latitude => position, :longitude => position)
902 get :feed, :format => "rss"
903 assert_response :success
904 assert_equal "application/rss+xml", @response.content_type
905 assert_select "rss", :count => 1 do
906 assert_select "channel", :count => 1 do
907 assert_select "item", :count => 4
911 get :feed, :bbox => "1,1,1.2,1.2", :format => "rss"
912 assert_response :success
913 assert_equal "application/rss+xml", @response.content_type
914 assert_select "rss", :count => 1 do
915 assert_select "channel", :count => 1 do
916 assert_select "item", :count => 2
922 get :feed, :bbox => "1,1,1.2", :format => "rss"
923 assert_response :bad_request
925 get :feed, :bbox => "1,1,1.2,1.2,1.2", :format => "rss"
926 assert_response :bad_request
928 get :feed, :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss"
929 assert_response :bad_request
931 get :feed, :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss"
932 assert_response :bad_request
935 def test_mine_success
936 create(:note) do |note|
937 create(:note_comment, :note => note, :author_id => users(:normal_user).id)
939 create(:note) do |note|
940 create(:note_comment, :note => note, :author_id => users(:second_public_user).id)
942 create(:note, :status => "hidden") do |note|
943 create(:note_comment, :note => note, :author_id => users(:second_public_user).id)
946 # Note that the table rows include a header row
947 get :mine, :display_name => "test"
948 assert_response :success
949 assert_select "table.note_list tr", :count => 2
951 get :mine, :display_name => "pulibc_test2"
952 assert_response :success
953 assert_select "table.note_list tr", :count => 2
955 get :mine, :display_name => "non-existent"
956 assert_response :not_found
958 session[:user] = users(:moderator_user).id
960 get :mine, :display_name => "test"
961 assert_response :success
962 assert_select "table.note_list tr", :count => 2
964 get :mine, :display_name => "pulibc_test2"
965 assert_response :success
966 assert_select "table.note_list tr", :count => 3
968 get :mine, :display_name => "non-existent"
969 assert_response :not_found