3 class NotesControllerTest < ActionController::TestCase
4 fixtures :users, :user_roles
7 # Stub nominatim response for note locations
8 stub_request(:get, %r{^http://nominatim\.openstreetmap\.org/reverse\?})
9 .to_return(:status => 404)
13 # test all routes which lead to this controller
16 { :path => "/api/0.6/notes", :method => :post },
17 { :controller => "notes", :action => "create", :format => "xml" }
20 { :path => "/api/0.6/notes/1", :method => :get },
21 { :controller => "notes", :action => "show", :id => "1", :format => "xml" }
24 { :controller => "notes", :action => "show", :id => "1", :format => "xml" },
25 { :path => "/api/0.6/notes/1.xml", :method => :get }
28 { :path => "/api/0.6/notes/1.rss", :method => :get },
29 { :controller => "notes", :action => "show", :id => "1", :format => "rss" }
32 { :path => "/api/0.6/notes/1.json", :method => :get },
33 { :controller => "notes", :action => "show", :id => "1", :format => "json" }
36 { :path => "/api/0.6/notes/1.gpx", :method => :get },
37 { :controller => "notes", :action => "show", :id => "1", :format => "gpx" }
40 { :path => "/api/0.6/notes/1/comment", :method => :post },
41 { :controller => "notes", :action => "comment", :id => "1", :format => "xml" }
44 { :path => "/api/0.6/notes/1/close", :method => :post },
45 { :controller => "notes", :action => "close", :id => "1", :format => "xml" }
48 { :path => "/api/0.6/notes/1/reopen", :method => :post },
49 { :controller => "notes", :action => "reopen", :id => "1", :format => "xml" }
52 { :path => "/api/0.6/notes/1", :method => :delete },
53 { :controller => "notes", :action => "destroy", :id => "1", :format => "xml" }
57 { :path => "/api/0.6/notes", :method => :get },
58 { :controller => "notes", :action => "index", :format => "xml" }
61 { :controller => "notes", :action => "index", :format => "xml" },
62 { :path => "/api/0.6/notes.xml", :method => :get }
65 { :path => "/api/0.6/notes.rss", :method => :get },
66 { :controller => "notes", :action => "index", :format => "rss" }
69 { :path => "/api/0.6/notes.json", :method => :get },
70 { :controller => "notes", :action => "index", :format => "json" }
73 { :path => "/api/0.6/notes.gpx", :method => :get },
74 { :controller => "notes", :action => "index", :format => "gpx" }
78 { :path => "/api/0.6/notes/search", :method => :get },
79 { :controller => "notes", :action => "search", :format => "xml" }
82 { :controller => "notes", :action => "search", :format => "xml" },
83 { :path => "/api/0.6/notes/search.xml", :method => :get }
86 { :path => "/api/0.6/notes/search.rss", :method => :get },
87 { :controller => "notes", :action => "search", :format => "rss" }
90 { :path => "/api/0.6/notes/search.json", :method => :get },
91 { :controller => "notes", :action => "search", :format => "json" }
94 { :path => "/api/0.6/notes/search.gpx", :method => :get },
95 { :controller => "notes", :action => "search", :format => "gpx" }
99 { :path => "/api/0.6/notes/feed", :method => :get },
100 { :controller => "notes", :action => "feed", :format => "rss" }
104 { :controller => "notes", :action => "create" },
105 { :path => "/api/0.6/notes/addPOIexec", :method => :post }
108 { :controller => "notes", :action => "close" },
109 { :path => "/api/0.6/notes/closePOIexec", :method => :post }
112 { :controller => "notes", :action => "comment" },
113 { :path => "/api/0.6/notes/editPOIexec", :method => :post }
116 { :controller => "notes", :action => "index", :format => "gpx" },
117 { :path => "/api/0.6/notes/getGPX", :method => :get }
120 { :controller => "notes", :action => "feed", :format => "rss" },
121 { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
125 { :path => "/user/username/notes", :method => :get },
126 { :controller => "notes", :action => "mine", :display_name => "username" }
130 def test_create_success
131 assert_difference "Note.count", 1 do
132 assert_difference "NoteComment.count", 1 do
133 post :create, :lat => -1.0, :lon => -1.0, :text => "This is a comment", :format => "json"
136 assert_response :success
137 js = ActiveSupport::JSON.decode(@response.body)
139 assert_equal "Feature", js["type"]
140 assert_equal "Point", js["geometry"]["type"]
141 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
142 assert_equal "open", js["properties"]["status"]
143 assert_equal 1, js["properties"]["comments"].count
144 assert_equal "opened", js["properties"]["comments"].last["action"]
145 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
146 assert_nil js["properties"]["comments"].last["user"]
147 id = js["properties"]["id"]
149 get :show, :id => id, :format => "json"
150 assert_response :success
151 js = ActiveSupport::JSON.decode(@response.body)
153 assert_equal "Feature", js["type"]
154 assert_equal "Point", js["geometry"]["type"]
155 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
156 assert_equal id, js["properties"]["id"]
157 assert_equal "open", js["properties"]["status"]
158 assert_equal 1, js["properties"]["comments"].count
159 assert_equal "opened", js["properties"]["comments"].last["action"]
160 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
161 assert_nil js["properties"]["comments"].last["user"]
165 assert_no_difference "Note.count" do
166 assert_no_difference "NoteComment.count" do
167 post :create, :lon => -1.0, :text => "This is a comment"
170 assert_response :bad_request
172 assert_no_difference "Note.count" do
173 assert_no_difference "NoteComment.count" do
174 post :create, :lat => -1.0, :text => "This is a comment"
177 assert_response :bad_request
179 assert_no_difference "Note.count" do
180 assert_no_difference "NoteComment.count" do
181 post :create, :lat => -1.0, :lon => -1.0
184 assert_response :bad_request
186 assert_no_difference "Note.count" do
187 assert_no_difference "NoteComment.count" do
188 post :create, :lat => -1.0, :lon => -1.0, :text => ""
191 assert_response :bad_request
193 assert_no_difference "Note.count" do
194 assert_no_difference "NoteComment.count" do
195 post :create, :lat => -100.0, :lon => -1.0, :text => "This is a comment"
198 assert_response :bad_request
200 assert_no_difference "Note.count" do
201 assert_no_difference "NoteComment.count" do
202 post :create, :lat => -1.0, :lon => -200.0, :text => "This is a comment"
205 assert_response :bad_request
207 assert_no_difference "Note.count" do
208 assert_no_difference "NoteComment.count" do
209 post :create, :lat => "abc", :lon => -1.0, :text => "This is a comment"
212 assert_response :bad_request
214 assert_no_difference "Note.count" do
215 assert_no_difference "NoteComment.count" do
216 post :create, :lat => -1.0, :lon => "abc", :text => "This is a comment"
219 assert_response :bad_request
222 def test_comment_success
223 open_note_with_comment = create(:note_with_comments)
224 assert_difference "NoteComment.count", 1 do
225 assert_no_difference "ActionMailer::Base.deliveries.size" do
226 post :comment, :id => open_note_with_comment.id, :text => "This is an additional comment", :format => "json"
229 assert_response :success
230 js = ActiveSupport::JSON.decode(@response.body)
232 assert_equal "Feature", js["type"]
233 assert_equal open_note_with_comment.id, js["properties"]["id"]
234 assert_equal "open", js["properties"]["status"]
235 assert_equal 2, js["properties"]["comments"].count
236 assert_equal "commented", js["properties"]["comments"].last["action"]
237 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
238 assert_nil js["properties"]["comments"].last["user"]
240 get :show, :id => open_note_with_comment.id, :format => "json"
241 assert_response :success
242 js = ActiveSupport::JSON.decode(@response.body)
244 assert_equal "Feature", js["type"]
245 assert_equal open_note_with_comment.id, js["properties"]["id"]
246 assert_equal "open", js["properties"]["status"]
247 assert_equal 2, js["properties"]["comments"].count
248 assert_equal "commented", js["properties"]["comments"].last["action"]
249 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
250 assert_nil js["properties"]["comments"].last["user"]
252 # Ensure that emails are sent to users
253 note_with_comments_by_users = create(:note) do |note|
254 create(:note_comment, :note => note, :author => users(:normal_user))
255 create(:note_comment, :note => note, :author => users(:second_public_user))
257 assert_difference "NoteComment.count", 1 do
258 assert_difference "ActionMailer::Base.deliveries.size", 2 do
259 post :comment, :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json"
262 assert_response :success
263 js = ActiveSupport::JSON.decode(@response.body)
265 assert_equal "Feature", js["type"]
266 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
267 assert_equal "open", js["properties"]["status"]
268 assert_equal 3, js["properties"]["comments"].count
269 assert_equal "commented", js["properties"]["comments"].last["action"]
270 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
271 assert_nil js["properties"]["comments"].last["user"]
273 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "test@openstreetmap.org" }
275 assert_equal 1, email.to.length
276 assert_equal "[OpenStreetMap] An anonymous user has commented on one of your notes", email.subject
278 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "public@OpenStreetMap.org" }
280 assert_equal 1, email.to.length
281 assert_equal "[OpenStreetMap] An anonymous user has commented on a note you are interested in", email.subject
283 get :show, :id => note_with_comments_by_users.id, :format => "json"
284 assert_response :success
285 js = ActiveSupport::JSON.decode(@response.body)
287 assert_equal "Feature", js["type"]
288 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
289 assert_equal "open", js["properties"]["status"]
290 assert_equal 3, js["properties"]["comments"].count
291 assert_equal "commented", js["properties"]["comments"].last["action"]
292 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
293 assert_nil js["properties"]["comments"].last["user"]
295 ActionMailer::Base.deliveries.clear
297 basic_authorization(users(:public_user).email, "test")
299 assert_difference "NoteComment.count", 1 do
300 assert_difference "ActionMailer::Base.deliveries.size", 2 do
301 post :comment, :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json"
304 assert_response :success
305 js = ActiveSupport::JSON.decode(@response.body)
307 assert_equal "Feature", js["type"]
308 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
309 assert_equal "open", js["properties"]["status"]
310 assert_equal 4, js["properties"]["comments"].count
311 assert_equal "commented", js["properties"]["comments"].last["action"]
312 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
313 assert_equal "test2", js["properties"]["comments"].last["user"]
315 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "test@openstreetmap.org" }
317 assert_equal 1, email.to.length
318 assert_equal "[OpenStreetMap] test2 has commented on one of your notes", email.subject
319 assert_equal "test@openstreetmap.org", email.to.first
321 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "public@OpenStreetMap.org" }
323 assert_equal 1, email.to.length
324 assert_equal "[OpenStreetMap] test2 has commented on a note you are interested in", email.subject
326 get :show, :id => note_with_comments_by_users.id, :format => "json"
327 assert_response :success
328 js = ActiveSupport::JSON.decode(@response.body)
330 assert_equal "Feature", js["type"]
331 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
332 assert_equal "open", js["properties"]["status"]
333 assert_equal 4, js["properties"]["comments"].count
334 assert_equal "commented", js["properties"]["comments"].last["action"]
335 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
336 assert_equal "test2", js["properties"]["comments"].last["user"]
338 ActionMailer::Base.deliveries.clear
341 def test_comment_fail
342 open_note_with_comment = create(:note_with_comments)
344 assert_no_difference "NoteComment.count" do
345 post :comment, :text => "This is an additional comment"
347 assert_response :bad_request
349 assert_no_difference "NoteComment.count" do
350 post :comment, :id => open_note_with_comment.id
352 assert_response :bad_request
354 assert_no_difference "NoteComment.count" do
355 post :comment, :id => open_note_with_comment.id, :text => ""
357 assert_response :bad_request
359 assert_no_difference "NoteComment.count" do
360 post :comment, :id => 12345, :text => "This is an additional comment"
362 assert_response :not_found
364 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
366 assert_no_difference "NoteComment.count" do
367 post :comment, :id => hidden_note_with_comment.id, :text => "This is an additional comment"
369 assert_response :gone
371 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
373 assert_no_difference "NoteComment.count" do
374 post :comment, :id => closed_note_with_comment.id, :text => "This is an additional comment"
376 assert_response :conflict
379 def test_close_success
380 open_note_with_comment = create(:note_with_comments)
382 post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
383 assert_response :unauthorized
385 basic_authorization(users(:public_user).email, "test")
387 post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
388 assert_response :success
389 js = ActiveSupport::JSON.decode(@response.body)
391 assert_equal "Feature", js["type"]
392 assert_equal open_note_with_comment.id, js["properties"]["id"]
393 assert_equal "closed", js["properties"]["status"]
394 assert_equal 2, js["properties"]["comments"].count
395 assert_equal "closed", js["properties"]["comments"].last["action"]
396 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
397 assert_equal "test2", js["properties"]["comments"].last["user"]
399 get :show, :id => open_note_with_comment.id, :format => "json"
400 assert_response :success
401 js = ActiveSupport::JSON.decode(@response.body)
403 assert_equal "Feature", js["type"]
404 assert_equal open_note_with_comment.id, js["properties"]["id"]
405 assert_equal "closed", js["properties"]["status"]
406 assert_equal 2, js["properties"]["comments"].count
407 assert_equal "closed", js["properties"]["comments"].last["action"]
408 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
409 assert_equal "test2", js["properties"]["comments"].last["user"]
414 assert_response :unauthorized
416 basic_authorization(users(:public_user).email, "test")
419 assert_response :bad_request
421 post :close, :id => 12345
422 assert_response :not_found
424 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
426 post :close, :id => hidden_note_with_comment.id
427 assert_response :gone
429 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
431 post :close, :id => closed_note_with_comment.id
432 assert_response :conflict
435 def test_reopen_success
436 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
438 post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
439 assert_response :unauthorized
441 basic_authorization(users(:public_user).email, "test")
443 post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
444 assert_response :success
445 js = ActiveSupport::JSON.decode(@response.body)
447 assert_equal "Feature", js["type"]
448 assert_equal closed_note_with_comment.id, js["properties"]["id"]
449 assert_equal "open", js["properties"]["status"]
450 assert_equal 2, js["properties"]["comments"].count
451 assert_equal "reopened", js["properties"]["comments"].last["action"]
452 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
453 assert_equal "test2", js["properties"]["comments"].last["user"]
455 get :show, :id => closed_note_with_comment.id, :format => "json"
456 assert_response :success
457 js = ActiveSupport::JSON.decode(@response.body)
459 assert_equal "Feature", js["type"]
460 assert_equal closed_note_with_comment.id, js["properties"]["id"]
461 assert_equal "open", js["properties"]["status"]
462 assert_equal 2, js["properties"]["comments"].count
463 assert_equal "reopened", js["properties"]["comments"].last["action"]
464 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
465 assert_equal "test2", js["properties"]["comments"].last["user"]
469 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
471 post :reopen, :id => hidden_note_with_comment.id
472 assert_response :unauthorized
474 basic_authorization(users(:public_user).email, "test")
476 post :reopen, :id => 12345
477 assert_response :not_found
479 post :reopen, :id => hidden_note_with_comment.id
480 assert_response :gone
482 open_note_with_comment = create(:note_with_comments)
484 post :reopen, :id => open_note_with_comment.id
485 assert_response :conflict
488 def test_show_success
489 open_note = create(:note_with_comments)
491 get :show, :id => open_note.id, :format => "xml"
492 assert_response :success
493 assert_equal "application/xml", @response.content_type
494 assert_select "osm", :count => 1 do
495 assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
496 assert_select "id", open_note.id.to_s
497 assert_select "url", note_url(open_note, :format => "xml")
498 assert_select "comment_url", comment_note_url(open_note, :format => "xml")
499 assert_select "close_url", close_note_url(open_note, :format => "xml")
500 assert_select "date_created", open_note.created_at.to_s
501 assert_select "status", open_note.status
502 assert_select "comments", :count => 1 do
503 assert_select "comment", :count => 1
508 get :show, :id => open_note.id, :format => "rss"
509 assert_response :success
510 assert_equal "application/rss+xml", @response.content_type
511 assert_select "rss", :count => 1 do
512 assert_select "channel", :count => 1 do
513 assert_select "item", :count => 1 do
514 assert_select "link", browse_note_url(open_note)
515 assert_select "guid", note_url(open_note)
516 assert_select "pubDate", open_note.created_at.to_s(:rfc822)
517 # assert_select "geo:lat", open_note.lat.to_s
518 # assert_select "geo:long", open_note.lon
519 # assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
524 get :show, :id => open_note.id, :format => "json"
525 assert_response :success
526 assert_equal "application/json", @response.content_type
527 js = ActiveSupport::JSON.decode(@response.body)
529 assert_equal "Feature", js["type"]
530 assert_equal "Point", js["geometry"]["type"]
531 assert_equal open_note.lat, js["geometry"]["coordinates"][0]
532 assert_equal open_note.lon, js["geometry"]["coordinates"][1]
533 assert_equal open_note.id, js["properties"]["id"]
534 assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
535 assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
536 assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
537 assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
538 assert_equal open_note.status, js["properties"]["status"]
540 get :show, :id => open_note.id, :format => "gpx"
541 assert_response :success
542 assert_equal "application/gpx+xml", @response.content_type
543 assert_select "gpx", :count => 1 do
544 assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
545 assert_select "time", :count => 1
546 assert_select "name", "Note: #{open_note.id}"
547 assert_select "desc", :count => 1
548 assert_select "link[href='http://test.host/note/#{open_note.id}']", :count => 1
549 assert_select "extensions", :count => 1 do
550 assert_select "id", open_note.id.to_s
551 assert_select "url", note_url(open_note, :format => "gpx")
552 assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
553 assert_select "close_url", close_note_url(open_note, :format => "gpx")
559 def test_show_hidden_comment
560 note_with_hidden_comment = create(:note) do |note|
561 create(:note_comment, :note => note, :body => "Valid comment for hidden note")
562 create(:note_comment, :note => note, :visible => false)
563 create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
566 get :show, :id => note_with_hidden_comment.id, :format => "json"
567 assert_response :success
568 js = ActiveSupport::JSON.decode(@response.body)
570 assert_equal "Feature", js["type"]
571 assert_equal note_with_hidden_comment.id, js["properties"]["id"]
572 assert_equal 2, js["properties"]["comments"].count
573 assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
574 assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
578 get :show, :id => 12345
579 assert_response :not_found
581 get :show, :id => create(:note, :status => "hidden").id
582 assert_response :gone
585 def test_destroy_success
586 open_note_with_comment = create(:note_with_comments)
588 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
589 assert_response :unauthorized
591 basic_authorization(users(:public_user).email, "test")
593 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
594 assert_response :forbidden
596 basic_authorization(users(:moderator_user).email, "test")
598 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
599 assert_response :success
600 js = ActiveSupport::JSON.decode(@response.body)
602 assert_equal "Feature", js["type"]
603 assert_equal open_note_with_comment.id, js["properties"]["id"]
604 assert_equal "hidden", js["properties"]["status"]
605 assert_equal 2, js["properties"]["comments"].count
606 assert_equal "hidden", js["properties"]["comments"].last["action"]
607 assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
608 assert_equal "moderator", js["properties"]["comments"].last["user"]
610 get :show, :id => open_note_with_comment.id, :format => "json"
611 assert_response :gone
614 def test_destroy_fail
615 delete :destroy, :id => 12345, :format => "json"
616 assert_response :unauthorized
618 basic_authorization(users(:public_user).email, "test")
620 delete :destroy, :id => 12345, :format => "json"
621 assert_response :forbidden
623 basic_authorization(users(:moderator_user).email, "test")
625 delete :destroy, :id => 12345, :format => "json"
626 assert_response :not_found
628 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
630 delete :destroy, :id => hidden_note_with_comment.id, :format => "json"
631 assert_response :gone
634 def test_index_success
635 position = (1.1 * GeoRecord::SCALE).to_i
636 create(:note_with_comments, :latitude => position, :longitude => position)
637 create(:note_with_comments, :latitude => position, :longitude => position)
639 get :index, :bbox => "1,1,1.2,1.2", :format => "rss"
640 assert_response :success
641 assert_equal "application/rss+xml", @response.content_type
642 assert_select "rss", :count => 1 do
643 assert_select "channel", :count => 1 do
644 assert_select "item", :count => 2
648 get :index, :bbox => "1,1,1.2,1.2", :format => "json"
649 assert_response :success
650 assert_equal "application/json", @response.content_type
651 js = ActiveSupport::JSON.decode(@response.body)
653 assert_equal "FeatureCollection", js["type"]
654 assert_equal 2, js["features"].count
656 get :index, :bbox => "1,1,1.2,1.2", :format => "xml"
657 assert_response :success
658 assert_equal "application/xml", @response.content_type
659 assert_select "osm", :count => 1 do
660 assert_select "note", :count => 2
663 get :index, :bbox => "1,1,1.2,1.2", :format => "gpx"
664 assert_response :success
665 assert_equal "application/gpx+xml", @response.content_type
666 assert_select "gpx", :count => 1 do
667 assert_select "wpt", :count => 2
672 position = (1.1 * GeoRecord::SCALE).to_i
673 create(:note_with_comments, :latitude => position, :longitude => position)
674 create(:note_with_comments, :latitude => position, :longitude => position)
676 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss"
677 assert_response :success
678 assert_equal "application/rss+xml", @response.content_type
679 assert_select "rss", :count => 1 do
680 assert_select "channel", :count => 1 do
681 assert_select "item", :count => 1
685 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json"
686 assert_response :success
687 assert_equal "application/json", @response.content_type
688 js = ActiveSupport::JSON.decode(@response.body)
690 assert_equal "FeatureCollection", js["type"]
691 assert_equal 1, js["features"].count
693 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml"
694 assert_response :success
695 assert_equal "application/xml", @response.content_type
696 assert_select "osm", :count => 1 do
697 assert_select "note", :count => 1
700 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx"
701 assert_response :success
702 assert_equal "application/gpx+xml", @response.content_type
703 assert_select "gpx", :count => 1 do
704 assert_select "wpt", :count => 1
708 def test_index_empty_area
709 get :index, :bbox => "5,5,5.1,5.1", :format => "rss"
710 assert_response :success
711 assert_equal "application/rss+xml", @response.content_type
712 assert_select "rss", :count => 1 do
713 assert_select "channel", :count => 1 do
714 assert_select "item", :count => 0
718 get :index, :bbox => "5,5,5.1,5.1", :format => "json"
719 assert_response :success
720 assert_equal "application/json", @response.content_type
721 js = ActiveSupport::JSON.decode(@response.body)
723 assert_equal "FeatureCollection", js["type"]
724 assert_equal 0, js["features"].count
726 get :index, :bbox => "5,5,5.1,5.1", :format => "xml"
727 assert_response :success
728 assert_equal "application/xml", @response.content_type
729 assert_select "osm", :count => 1 do
730 assert_select "note", :count => 0
733 get :index, :bbox => "5,5,5.1,5.1", :format => "gpx"
734 assert_response :success
735 assert_equal "application/gpx+xml", @response.content_type
736 assert_select "gpx", :count => 1 do
737 assert_select "wpt", :count => 0
741 def test_index_large_area
742 get :index, :bbox => "-2.5,-2.5,2.5,2.5", :format => :json
743 assert_response :success
744 assert_equal "application/json", @response.content_type
746 get :index, :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json
747 assert_response :success
748 assert_equal "application/json", @response.content_type
750 get :index, :bbox => "-10,-10,12,12", :format => :json
751 assert_response :bad_request
752 assert_equal "text/plain", @response.content_type
754 get :index, :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json
755 assert_response :bad_request
756 assert_equal "text/plain", @response.content_type
759 def test_index_closed
760 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 5.days)
761 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 100.days)
762 create(:note_with_comments, :status => "hidden")
763 create(:note_with_comments)
765 # Open notes + closed in last 7 days
766 get :index, :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json"
767 assert_response :success
768 assert_equal "application/json", @response.content_type
769 js = ActiveSupport::JSON.decode(@response.body)
771 assert_equal "FeatureCollection", js["type"]
772 assert_equal 2, js["features"].count
775 get :index, :bbox => "1,1,1.7,1.7", :closed => "0", :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 1, js["features"].count
783 # Open notes + all closed notes
784 get :index, :bbox => "1,1,1.7,1.7", :closed => "-1", :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 3, js["features"].count
793 def test_index_bad_params
794 get :index, :bbox => "-2.5,-2.5,2.5"
795 assert_response :bad_request
797 get :index, :bbox => "-2.5,-2.5,2.5,2.5,2.5"
798 assert_response :bad_request
800 get :index, :b => "-2.5", :r => "2.5", :t => "2.5"
801 assert_response :bad_request
803 get :index, :l => "-2.5", :r => "2.5", :t => "2.5"
804 assert_response :bad_request
806 get :index, :l => "-2.5", :b => "-2.5", :t => "2.5"
807 assert_response :bad_request
809 get :index, :l => "-2.5", :b => "-2.5", :r => "2.5"
810 assert_response :bad_request
812 get :index, :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json"
813 assert_response :bad_request
815 get :index, :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json"
816 assert_response :bad_request
819 def test_search_success
820 create(:note_with_comments)
822 get :search, :q => "note comment", :format => "xml"
823 assert_response :success
824 assert_equal "application/xml", @response.content_type
825 assert_select "osm", :count => 1 do
826 assert_select "note", :count => 1
829 get :search, :q => "note comment", :format => "json"
830 assert_response :success
831 assert_equal "application/json", @response.content_type
832 js = ActiveSupport::JSON.decode(@response.body)
834 assert_equal "FeatureCollection", js["type"]
835 assert_equal 1, js["features"].count
837 get :search, :q => "note comment", :format => "rss"
838 assert_response :success
839 assert_equal "application/rss+xml", @response.content_type
840 assert_select "rss", :count => 1 do
841 assert_select "channel", :count => 1 do
842 assert_select "item", :count => 1
846 get :search, :q => "note comment", :format => "gpx"
847 assert_response :success
848 assert_equal "application/gpx+xml", @response.content_type
849 assert_select "gpx", :count => 1 do
850 assert_select "wpt", :count => 1
854 def test_search_no_match
855 create(:note_with_comments)
857 get :search, :q => "no match", :format => "xml"
858 assert_response :success
859 assert_equal "application/xml", @response.content_type
860 assert_select "osm", :count => 1 do
861 assert_select "note", :count => 0
864 get :search, :q => "no match", :format => "json"
865 assert_response :success
866 assert_equal "application/json", @response.content_type
867 js = ActiveSupport::JSON.decode(@response.body)
869 assert_equal "FeatureCollection", js["type"]
870 assert_equal 0, js["features"].count
872 get :search, :q => "no match", :format => "rss"
873 assert_response :success
874 assert_equal "application/rss+xml", @response.content_type
875 assert_select "rss", :count => 1 do
876 assert_select "channel", :count => 1 do
877 assert_select "item", :count => 0
881 get :search, :q => "no match", :format => "gpx"
882 assert_response :success
883 assert_equal "application/gpx+xml", @response.content_type
884 assert_select "gpx", :count => 1 do
885 assert_select "wpt", :count => 0
889 def test_search_bad_params
891 assert_response :bad_request
893 get :search, :q => "no match", :limit => "0", :format => "json"
894 assert_response :bad_request
896 get :search, :q => "no match", :limit => "10001", :format => "json"
897 assert_response :bad_request
900 def test_feed_success
901 position = (1.1 * GeoRecord::SCALE).to_i
902 create(:note_with_comments, :latitude => position, :longitude => position)
903 create(:note_with_comments, :latitude => position, :longitude => position)
904 position = (1.5 * GeoRecord::SCALE).to_i
905 create(:note_with_comments, :latitude => position, :longitude => position)
906 create(:note_with_comments, :latitude => position, :longitude => position)
908 get :feed, :format => "rss"
909 assert_response :success
910 assert_equal "application/rss+xml", @response.content_type
911 assert_select "rss", :count => 1 do
912 assert_select "channel", :count => 1 do
913 assert_select "item", :count => 4
917 get :feed, :bbox => "1,1,1.2,1.2", :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 => 2
928 get :feed, :bbox => "1,1,1.2", :format => "rss"
929 assert_response :bad_request
931 get :feed, :bbox => "1,1,1.2,1.2,1.2", :format => "rss"
932 assert_response :bad_request
934 get :feed, :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss"
935 assert_response :bad_request
937 get :feed, :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss"
938 assert_response :bad_request
941 def test_mine_success
942 create(:note) do |note|
943 create(:note_comment, :note => note, :author => users(:normal_user))
945 create(:note) do |note|
946 create(:note_comment, :note => note, :author => users(:second_public_user))
948 create(:note, :status => "hidden") do |note|
949 create(:note_comment, :note => note, :author => users(:second_public_user))
952 # Note that the table rows include a header row
953 get :mine, :display_name => "test"
954 assert_response :success
955 assert_select "table.note_list tr", :count => 2
957 get :mine, :display_name => "pulibc_test2"
958 assert_response :success
959 assert_select "table.note_list tr", :count => 2
961 get :mine, :display_name => "non-existent"
962 assert_response :not_found
964 session[:user] = users(:moderator_user).id
966 get :mine, :display_name => "test"
967 assert_response :success
968 assert_select "table.note_list tr", :count => 2
970 get :mine, :display_name => "pulibc_test2"
971 assert_response :success
972 assert_select "table.note_list tr", :count => 3
974 get :mine, :display_name => "non-existent"
975 assert_response :not_found