4   class NotesControllerTest < ActionDispatch::IntegrationTest
 
   7       # Stub nominatim response for note locations
 
   8       stub_request(:get, %r{^https://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 => "api/notes", :action => "create" }
 
  20         { :path => "/api/0.6/notes/1", :method => :get },
 
  21         { :controller => "api/notes", :action => "show", :id => "1" }
 
  24         { :controller => "api/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 => "api/notes", :action => "show", :id => "1", :format => "rss" }
 
  32         { :path => "/api/0.6/notes/1.json", :method => :get },
 
  33         { :controller => "api/notes", :action => "show", :id => "1", :format => "json" }
 
  36         { :path => "/api/0.6/notes/1.gpx", :method => :get },
 
  37         { :controller => "api/notes", :action => "show", :id => "1", :format => "gpx" }
 
  40         { :path => "/api/0.6/notes/1/comment", :method => :post },
 
  41         { :controller => "api/notes", :action => "comment", :id => "1" }
 
  44         { :path => "/api/0.6/notes/1/close", :method => :post },
 
  45         { :controller => "api/notes", :action => "close", :id => "1" }
 
  48         { :path => "/api/0.6/notes/1/reopen", :method => :post },
 
  49         { :controller => "api/notes", :action => "reopen", :id => "1" }
 
  52         { :path => "/api/0.6/notes/1", :method => :delete },
 
  53         { :controller => "api/notes", :action => "destroy", :id => "1" }
 
  57         { :path => "/api/0.6/notes", :method => :get },
 
  58         { :controller => "api/notes", :action => "index" }
 
  61         { :controller => "api/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 => "api/notes", :action => "index", :format => "rss" }
 
  69         { :path => "/api/0.6/notes.json", :method => :get },
 
  70         { :controller => "api/notes", :action => "index", :format => "json" }
 
  73         { :path => "/api/0.6/notes.gpx", :method => :get },
 
  74         { :controller => "api/notes", :action => "index", :format => "gpx" }
 
  78         { :path => "/api/0.6/notes/search", :method => :get },
 
  79         { :controller => "api/notes", :action => "search" }
 
  82         { :controller => "api/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 => "api/notes", :action => "search", :format => "rss" }
 
  90         { :path => "/api/0.6/notes/search.json", :method => :get },
 
  91         { :controller => "api/notes", :action => "search", :format => "json" }
 
  94         { :path => "/api/0.6/notes/search.gpx", :method => :get },
 
  95         { :controller => "api/notes", :action => "search", :format => "gpx" }
 
  99         { :path => "/api/0.6/notes/feed", :method => :get },
 
 100         { :controller => "api/notes", :action => "feed", :format => "rss" }
 
 104         { :controller => "api/notes", :action => "create" },
 
 105         { :path => "/api/0.6/notes/addPOIexec", :method => :post }
 
 108         { :controller => "api/notes", :action => "close" },
 
 109         { :path => "/api/0.6/notes/closePOIexec", :method => :post }
 
 112         { :controller => "api/notes", :action => "comment" },
 
 113         { :path => "/api/0.6/notes/editPOIexec", :method => :post }
 
 116         { :controller => "api/notes", :action => "index", :format => "gpx" },
 
 117         { :path => "/api/0.6/notes/getGPX", :method => :get }
 
 120         { :controller => "api/notes", :action => "feed", :format => "rss" },
 
 121         { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
 
 125     def test_create_success
 
 126       assert_difference "Note.count", 1 do
 
 127         assert_difference "NoteComment.count", 1 do
 
 128           post api_notes_path(:lat => -1.0, :lon => -1.0, :text => "This is a comment", :format => "json")
 
 131       assert_response :success
 
 132       js = ActiveSupport::JSON.decode(@response.body)
 
 134       assert_equal "Feature", js["type"]
 
 135       assert_equal "Point", js["geometry"]["type"]
 
 136       assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
 
 137       assert_equal "open", js["properties"]["status"]
 
 138       assert_equal 1, js["properties"]["comments"].count
 
 139       assert_equal "opened", js["properties"]["comments"].last["action"]
 
 140       assert_equal "This is a comment", js["properties"]["comments"].last["text"]
 
 141       assert_nil js["properties"]["comments"].last["user"]
 
 142       id = js["properties"]["id"]
 
 144       get api_note_path(id, :format => "json")
 
 145       assert_response :success
 
 146       js = ActiveSupport::JSON.decode(@response.body)
 
 148       assert_equal "Feature", js["type"]
 
 149       assert_equal "Point", js["geometry"]["type"]
 
 150       assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
 
 151       assert_equal id, js["properties"]["id"]
 
 152       assert_equal "open", js["properties"]["status"]
 
 153       assert_equal 1, js["properties"]["comments"].count
 
 154       assert_equal "opened", js["properties"]["comments"].last["action"]
 
 155       assert_equal "This is a comment", js["properties"]["comments"].last["text"]
 
 156       assert_nil js["properties"]["comments"].last["user"]
 
 160       assert_no_difference "Note.count" do
 
 161         assert_no_difference "NoteComment.count" do
 
 162           post api_notes_path(:lon => -1.0, :text => "This is a comment")
 
 165       assert_response :bad_request
 
 167       assert_no_difference "Note.count" do
 
 168         assert_no_difference "NoteComment.count" do
 
 169           post api_notes_path(:lat => -1.0, :text => "This is a comment")
 
 172       assert_response :bad_request
 
 174       assert_no_difference "Note.count" do
 
 175         assert_no_difference "NoteComment.count" do
 
 176           post api_notes_path(:lat => -1.0, :lon => -1.0)
 
 179       assert_response :bad_request
 
 181       assert_no_difference "Note.count" do
 
 182         assert_no_difference "NoteComment.count" do
 
 183           post api_notes_path(:lat => -1.0, :lon => -1.0, :text => "")
 
 186       assert_response :bad_request
 
 188       assert_no_difference "Note.count" do
 
 189         assert_no_difference "NoteComment.count" do
 
 190           post api_notes_path(:lat => -100.0, :lon => -1.0, :text => "This is a comment")
 
 193       assert_response :bad_request
 
 195       assert_no_difference "Note.count" do
 
 196         assert_no_difference "NoteComment.count" do
 
 197           post api_notes_path(:lat => -1.0, :lon => -200.0, :text => "This is a comment")
 
 200       assert_response :bad_request
 
 202       assert_no_difference "Note.count" do
 
 203         assert_no_difference "NoteComment.count" do
 
 204           post api_notes_path(:lat => "abc", :lon => -1.0, :text => "This is a comment")
 
 207       assert_response :bad_request
 
 209       assert_no_difference "Note.count" do
 
 210         assert_no_difference "NoteComment.count" do
 
 211           post api_notes_path(:lat => -1.0, :lon => "abc", :text => "This is a comment")
 
 214       assert_response :bad_request
 
 216       assert_no_difference "Note.count" do
 
 217         assert_no_difference "NoteComment.count" do
 
 218           post api_notes_path(:lat => -1.0, :lon => -1.0, :text => "x\u0000y")
 
 221       assert_response :bad_request
 
 224     def test_comment_success
 
 225       open_note_with_comment = create(:note_with_comments)
 
 227       auth_header = basic_authorization_header user.email, "test"
 
 228       assert_difference "NoteComment.count", 1 do
 
 229         assert_no_difference "ActionMailer::Base.deliveries.size" do
 
 230           perform_enqueued_jobs do
 
 231             post comment_api_note_path(open_note_with_comment, :text => "This is an additional comment", :format => "json"), :headers => auth_header
 
 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_equal user.display_name, js["properties"]["comments"].last["user"]
 
 246       get api_note_path(open_note_with_comment, :format => "json")
 
 247       assert_response :success
 
 248       js = ActiveSupport::JSON.decode(@response.body)
 
 250       assert_equal "Feature", js["type"]
 
 251       assert_equal open_note_with_comment.id, js["properties"]["id"]
 
 252       assert_equal "open", js["properties"]["status"]
 
 253       assert_equal 2, js["properties"]["comments"].count
 
 254       assert_equal "commented", js["properties"]["comments"].last["action"]
 
 255       assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
 
 256       assert_equal user.display_name, js["properties"]["comments"].last["user"]
 
 258       # Ensure that emails are sent to users
 
 259       first_user = create(:user)
 
 260       second_user = create(:user)
 
 261       third_user = create(:user)
 
 263       note_with_comments_by_users = create(:note) do |note|
 
 264         create(:note_comment, :note => note, :author => first_user)
 
 265         create(:note_comment, :note => note, :author => second_user)
 
 268       auth_header = basic_authorization_header third_user.email, "test"
 
 270       assert_difference "NoteComment.count", 1 do
 
 271         assert_difference "ActionMailer::Base.deliveries.size", 2 do
 
 272           perform_enqueued_jobs do
 
 273             post comment_api_note_path(note_with_comments_by_users, :text => "This is an additional comment", :format => "json"), :headers => auth_header
 
 277       assert_response :success
 
 278       js = ActiveSupport::JSON.decode(@response.body)
 
 280       assert_equal "Feature", js["type"]
 
 281       assert_equal note_with_comments_by_users.id, js["properties"]["id"]
 
 282       assert_equal "open", js["properties"]["status"]
 
 283       assert_equal 3, js["properties"]["comments"].count
 
 284       assert_equal "commented", js["properties"]["comments"].last["action"]
 
 285       assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
 
 286       assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
 
 288       email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
 
 290       assert_equal 1, email.to.length
 
 291       assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on one of your notes", email.subject
 
 292       assert_equal first_user.email, email.to.first
 
 294       email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
 
 296       assert_equal 1, email.to.length
 
 297       assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on a note you are interested in", email.subject
 
 299       get api_note_path(note_with_comments_by_users, :format => "json")
 
 300       assert_response :success
 
 301       js = ActiveSupport::JSON.decode(@response.body)
 
 303       assert_equal "Feature", js["type"]
 
 304       assert_equal note_with_comments_by_users.id, js["properties"]["id"]
 
 305       assert_equal "open", js["properties"]["status"]
 
 306       assert_equal 3, js["properties"]["comments"].count
 
 307       assert_equal "commented", js["properties"]["comments"].last["action"]
 
 308       assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
 
 309       assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
 
 311       ActionMailer::Base.deliveries.clear
 
 314     def test_comment_fail
 
 315       open_note_with_comment = create(:note_with_comments)
 
 319       assert_no_difference "NoteComment.count" do
 
 320         post comment_api_note_path(open_note_with_comment)
 
 321         assert_response :unauthorized
 
 324       auth_header = basic_authorization_header user.email, "test"
 
 326       assert_no_difference "NoteComment.count" do
 
 327         post comment_api_note_path(open_note_with_comment), :headers => auth_header
 
 329       assert_response :bad_request
 
 331       assert_no_difference "NoteComment.count" do
 
 332         post comment_api_note_path(open_note_with_comment, :text => ""), :headers => auth_header
 
 334       assert_response :bad_request
 
 336       assert_no_difference "NoteComment.count" do
 
 337         post comment_api_note_path(12345, :text => "This is an additional comment"), :headers => auth_header
 
 339       assert_response :not_found
 
 341       hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
 
 343       assert_no_difference "NoteComment.count" do
 
 344         post comment_api_note_path(hidden_note_with_comment, :text => "This is an additional comment"), :headers => auth_header
 
 346       assert_response :gone
 
 348       closed_note_with_comment = create(:note_with_comments, :closed)
 
 350       assert_no_difference "NoteComment.count" do
 
 351         post comment_api_note_path(closed_note_with_comment, :text => "This is an additional comment"), :headers => auth_header
 
 353       assert_response :conflict
 
 355       assert_no_difference "NoteComment.count" do
 
 356         post comment_api_note_path(open_note_with_comment, :text => "x\u0000y"), :headers => auth_header
 
 358       assert_response :bad_request
 
 361     def test_close_success
 
 362       open_note_with_comment = create(:note_with_comments)
 
 365       post close_api_note_path(open_note_with_comment, :text => "This is a close comment", :format => "json")
 
 366       assert_response :unauthorized
 
 368       auth_header = basic_authorization_header user.email, "test"
 
 370       post close_api_note_path(open_note_with_comment, :text => "This is a close comment", :format => "json"), :headers => auth_header
 
 371       assert_response :success
 
 372       js = ActiveSupport::JSON.decode(@response.body)
 
 374       assert_equal "Feature", js["type"]
 
 375       assert_equal open_note_with_comment.id, js["properties"]["id"]
 
 376       assert_equal "closed", js["properties"]["status"]
 
 377       assert_equal 2, js["properties"]["comments"].count
 
 378       assert_equal "closed", js["properties"]["comments"].last["action"]
 
 379       assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
 
 380       assert_equal user.display_name, js["properties"]["comments"].last["user"]
 
 382       get api_note_path(open_note_with_comment.id, :format => "json")
 
 383       assert_response :success
 
 384       js = ActiveSupport::JSON.decode(@response.body)
 
 386       assert_equal "Feature", js["type"]
 
 387       assert_equal open_note_with_comment.id, js["properties"]["id"]
 
 388       assert_equal "closed", js["properties"]["status"]
 
 389       assert_equal 2, js["properties"]["comments"].count
 
 390       assert_equal "closed", js["properties"]["comments"].last["action"]
 
 391       assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
 
 392       assert_equal user.display_name, js["properties"]["comments"].last["user"]
 
 396       post close_api_note_path(12345)
 
 397       assert_response :unauthorized
 
 399       auth_header = basic_authorization_header create(:user).email, "test"
 
 401       post close_api_note_path(12345), :headers => auth_header
 
 402       assert_response :not_found
 
 404       hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
 
 406       post close_api_note_path(hidden_note_with_comment), :headers => auth_header
 
 407       assert_response :gone
 
 409       closed_note_with_comment = create(:note_with_comments, :closed)
 
 411       post close_api_note_path(closed_note_with_comment), :headers => auth_header
 
 412       assert_response :conflict
 
 415     def test_reopen_success
 
 416       closed_note_with_comment = create(:note_with_comments, :closed)
 
 419       post reopen_api_note_path(closed_note_with_comment, :text => "This is a reopen comment", :format => "json")
 
 420       assert_response :unauthorized
 
 422       auth_header = basic_authorization_header user.email, "test"
 
 424       post reopen_api_note_path(closed_note_with_comment, :text => "This is a reopen comment", :format => "json"), :headers => auth_header
 
 425       assert_response :success
 
 426       js = ActiveSupport::JSON.decode(@response.body)
 
 428       assert_equal "Feature", js["type"]
 
 429       assert_equal closed_note_with_comment.id, js["properties"]["id"]
 
 430       assert_equal "open", js["properties"]["status"]
 
 431       assert_equal 3, js["properties"]["comments"].count
 
 432       assert_equal "reopened", js["properties"]["comments"].last["action"]
 
 433       assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
 
 434       assert_equal user.display_name, js["properties"]["comments"].last["user"]
 
 436       get api_note_path(closed_note_with_comment, :format => "json")
 
 437       assert_response :success
 
 438       js = ActiveSupport::JSON.decode(@response.body)
 
 440       assert_equal "Feature", js["type"]
 
 441       assert_equal closed_note_with_comment.id, js["properties"]["id"]
 
 442       assert_equal "open", js["properties"]["status"]
 
 443       assert_equal 3, js["properties"]["comments"].count
 
 444       assert_equal "reopened", js["properties"]["comments"].last["action"]
 
 445       assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
 
 446       assert_equal user.display_name, js["properties"]["comments"].last["user"]
 
 450       hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
 
 452       post reopen_api_note_path(hidden_note_with_comment)
 
 453       assert_response :unauthorized
 
 455       auth_header = basic_authorization_header create(:user).email, "test"
 
 457       post reopen_api_note_path(12345), :headers => auth_header
 
 458       assert_response :not_found
 
 460       post reopen_api_note_path(hidden_note_with_comment), :headers => auth_header
 
 461       assert_response :gone
 
 463       open_note_with_comment = create(:note_with_comments)
 
 465       post reopen_api_note_path(open_note_with_comment), :headers => auth_header
 
 466       assert_response :conflict
 
 469     def test_show_success
 
 470       open_note = create(:note_with_comments)
 
 472       get api_note_path(open_note, :format => "xml")
 
 473       assert_response :success
 
 474       assert_equal "application/xml", @response.media_type
 
 475       assert_select "osm", :count => 1 do
 
 476         assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
 
 477           assert_select "id", open_note.id.to_s
 
 478           assert_select "url", api_note_url(open_note, :format => "xml")
 
 479           assert_select "comment_url", comment_api_note_url(open_note, :format => "xml")
 
 480           assert_select "close_url", close_api_note_url(open_note, :format => "xml")
 
 481           assert_select "date_created", open_note.created_at.to_s
 
 482           assert_select "status", open_note.status
 
 483           assert_select "comments", :count => 1 do
 
 484             assert_select "comment", :count => 1
 
 489       get api_note_path(open_note, :format => "rss")
 
 490       assert_response :success
 
 491       assert_equal "application/rss+xml", @response.media_type
 
 492       assert_select "rss", :count => 1 do
 
 493         assert_select "channel", :count => 1 do
 
 494           assert_select "item", :count => 1 do
 
 495             assert_select "link", note_url(open_note)
 
 496             assert_select "guid", api_note_url(open_note)
 
 497             assert_select "pubDate", open_note.created_at.to_fs(:rfc822)
 
 498             assert_select "geo|lat", open_note.lat.to_s
 
 499             assert_select "geo|long", open_note.lon.to_s
 
 500             assert_select "georss|point", "#{open_note.lon} #{open_note.lon}"
 
 505       get api_note_path(open_note, :format => "json")
 
 506       assert_response :success
 
 507       assert_equal "application/json", @response.media_type
 
 508       js = ActiveSupport::JSON.decode(@response.body)
 
 510       assert_equal "Feature", js["type"]
 
 511       assert_equal "Point", js["geometry"]["type"]
 
 512       assert_equal open_note.lat, js["geometry"]["coordinates"][0]
 
 513       assert_equal open_note.lon, js["geometry"]["coordinates"][1]
 
 514       assert_equal open_note.id, js["properties"]["id"]
 
 515       assert_equal api_note_url(open_note, :format => "json"), js["properties"]["url"]
 
 516       assert_equal comment_api_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
 
 517       assert_equal close_api_note_url(open_note, :format => "json"), js["properties"]["close_url"]
 
 518       assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
 
 519       assert_equal open_note.status, js["properties"]["status"]
 
 521       get api_note_path(open_note, :format => "gpx")
 
 522       assert_response :success
 
 523       assert_equal "application/gpx+xml", @response.media_type
 
 524       assert_select "gpx", :count => 1 do
 
 525         assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
 
 526           assert_select "time", :count => 1
 
 527           assert_select "name", "Note: #{open_note.id}"
 
 528           assert_select "desc", :count => 1
 
 529           assert_select "link[href='http://www.example.com/note/#{open_note.id}']", :count => 1
 
 530           assert_select "extensions", :count => 1 do
 
 531             assert_select "id", open_note.id.to_s
 
 532             assert_select "url", api_note_url(open_note, :format => "gpx")
 
 533             assert_select "comment_url", comment_api_note_url(open_note, :format => "gpx")
 
 534             assert_select "close_url", close_api_note_url(open_note, :format => "gpx")
 
 540     def test_show_hidden_comment
 
 541       note_with_hidden_comment = create(:note) do |note|
 
 542         create(:note_comment, :note => note, :body => "Valid comment for hidden note")
 
 543         create(:note_comment, :note => note, :visible => false)
 
 544         create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
 
 547       get api_note_path(note_with_hidden_comment, :format => "json")
 
 548       assert_response :success
 
 549       js = ActiveSupport::JSON.decode(@response.body)
 
 551       assert_equal "Feature", js["type"]
 
 552       assert_equal note_with_hidden_comment.id, js["properties"]["id"]
 
 553       assert_equal 2, js["properties"]["comments"].count
 
 554       assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
 
 555       assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
 
 559       get api_note_path(12345)
 
 560       assert_response :not_found
 
 562       get api_note_path(create(:note, :status => "hidden"))
 
 563       assert_response :gone
 
 566     def test_destroy_success
 
 567       open_note_with_comment = create(:note_with_comments)
 
 569       moderator_user = create(:moderator_user)
 
 571       delete api_note_path(open_note_with_comment, :text => "This is a hide comment", :format => "json")
 
 572       assert_response :unauthorized
 
 574       auth_header = basic_authorization_header user.email, "test"
 
 576       delete api_note_path(open_note_with_comment, :text => "This is a hide comment", :format => "json"), :headers => auth_header
 
 577       assert_response :forbidden
 
 579       auth_header = basic_authorization_header moderator_user.email, "test"
 
 581       delete api_note_path(open_note_with_comment, :text => "This is a hide comment", :format => "json"), :headers => auth_header
 
 582       assert_response :success
 
 583       js = ActiveSupport::JSON.decode(@response.body)
 
 585       assert_equal "Feature", js["type"]
 
 586       assert_equal open_note_with_comment.id, js["properties"]["id"]
 
 587       assert_equal "hidden", js["properties"]["status"]
 
 588       assert_equal 2, js["properties"]["comments"].count
 
 589       assert_equal "hidden", js["properties"]["comments"].last["action"]
 
 590       assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
 
 591       assert_equal moderator_user.display_name, js["properties"]["comments"].last["user"]
 
 593       get api_note_path(open_note_with_comment, :format => "json"), :headers => auth_header
 
 594       assert_response :success
 
 596       auth_header = basic_authorization_header user.email, "test"
 
 598       get api_note_path(open_note_with_comment, :format => "json"), :headers => auth_header
 
 599       assert_response :gone
 
 602     def test_destroy_fail
 
 604       moderator_user = create(:moderator_user)
 
 606       delete api_note_path(12345, :format => "json")
 
 607       assert_response :unauthorized
 
 609       auth_header = basic_authorization_header user.email, "test"
 
 611       delete api_note_path(12345, :format => "json"), :headers => auth_header
 
 612       assert_response :forbidden
 
 614       auth_header = basic_authorization_header moderator_user.email, "test"
 
 616       delete api_note_path(12345, :format => "json"), :headers => auth_header
 
 617       assert_response :not_found
 
 619       hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
 
 621       delete api_note_path(hidden_note_with_comment, :format => "json"), :headers => auth_header
 
 622       assert_response :gone
 
 625     def test_index_success
 
 626       position = (1.1 * GeoRecord::SCALE).to_i
 
 627       create(:note_with_comments, :latitude => position, :longitude => position)
 
 628       create(:note_with_comments, :latitude => position, :longitude => position)
 
 630       get api_notes_path(:bbox => "1,1,1.2,1.2", :format => "rss")
 
 631       assert_response :success
 
 632       assert_equal "application/rss+xml", @response.media_type
 
 633       assert_select "rss", :count => 1 do
 
 634         assert_select "channel", :count => 1 do
 
 635           assert_select "description", :text => /1\.2/, :count => 1
 
 636           assert_select "item", :count => 2
 
 640       get api_notes_path(:bbox => "1,1,1.2,1.2", :format => "json")
 
 641       assert_response :success
 
 642       assert_equal "application/json", @response.media_type
 
 643       js = ActiveSupport::JSON.decode(@response.body)
 
 645       assert_equal "FeatureCollection", js["type"]
 
 646       assert_equal 2, js["features"].count
 
 648       get api_notes_path(:bbox => "1,1,1.2,1.2", :format => "xml")
 
 649       assert_response :success
 
 650       assert_equal "application/xml", @response.media_type
 
 651       assert_select "osm", :count => 1 do
 
 652         assert_select "note", :count => 2
 
 655       get api_notes_path(:bbox => "1,1,1.2,1.2", :format => "gpx")
 
 656       assert_response :success
 
 657       assert_equal "application/gpx+xml", @response.media_type
 
 658       assert_select "gpx", :count => 1 do
 
 659         assert_select "wpt", :count => 2
 
 664       position = (1.1 * GeoRecord::SCALE).to_i
 
 665       create(:note_with_comments, :latitude => position, :longitude => position)
 
 666       create(:note_with_comments, :latitude => position, :longitude => position)
 
 668       get api_notes_path(:bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss")
 
 669       assert_response :success
 
 670       assert_equal "application/rss+xml", @response.media_type
 
 671       assert_select "rss", :count => 1 do
 
 672         assert_select "channel", :count => 1 do
 
 673           assert_select "item", :count => 1
 
 677       get api_notes_path(:bbox => "1,1,1.2,1.2", :limit => 1, :format => "json")
 
 678       assert_response :success
 
 679       assert_equal "application/json", @response.media_type
 
 680       js = ActiveSupport::JSON.decode(@response.body)
 
 682       assert_equal "FeatureCollection", js["type"]
 
 683       assert_equal 1, js["features"].count
 
 685       get api_notes_path(:bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml")
 
 686       assert_response :success
 
 687       assert_equal "application/xml", @response.media_type
 
 688       assert_select "osm", :count => 1 do
 
 689         assert_select "note", :count => 1
 
 692       get api_notes_path(:bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx")
 
 693       assert_response :success
 
 694       assert_equal "application/gpx+xml", @response.media_type
 
 695       assert_select "gpx", :count => 1 do
 
 696         assert_select "wpt", :count => 1
 
 699       get api_notes_path(:bbox => "1,1,1.2,1.2", :limit => Settings.max_note_query_limit, :format => "rss")
 
 700       assert_response :success
 
 703     def test_index_empty_area
 
 704       get api_notes_path(:bbox => "5,5,5.1,5.1", :format => "rss")
 
 705       assert_response :success
 
 706       assert_equal "application/rss+xml", @response.media_type
 
 707       assert_select "rss", :count => 1 do
 
 708         assert_select "channel", :count => 1 do
 
 709           assert_select "item", :count => 0
 
 713       get api_notes_path(:bbox => "5,5,5.1,5.1", :format => "json")
 
 714       assert_response :success
 
 715       assert_equal "application/json", @response.media_type
 
 716       js = ActiveSupport::JSON.decode(@response.body)
 
 718       assert_equal "FeatureCollection", js["type"]
 
 719       assert_equal 0, js["features"].count
 
 721       get api_notes_path(:bbox => "5,5,5.1,5.1", :format => "xml")
 
 722       assert_response :success
 
 723       assert_equal "application/xml", @response.media_type
 
 724       assert_select "osm", :count => 1 do
 
 725         assert_select "note", :count => 0
 
 728       get api_notes_path(:bbox => "5,5,5.1,5.1", :format => "gpx")
 
 729       assert_response :success
 
 730       assert_equal "application/gpx+xml", @response.media_type
 
 731       assert_select "gpx", :count => 1 do
 
 732         assert_select "wpt", :count => 0
 
 736     def test_index_large_area
 
 737       get api_notes_path(:bbox => "-2.5,-2.5,2.5,2.5", :format => :json)
 
 738       assert_response :success
 
 739       assert_equal "application/json", @response.media_type
 
 741       get api_notes_path(:l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json)
 
 742       assert_response :success
 
 743       assert_equal "application/json", @response.media_type
 
 745       get api_notes_path(:bbox => "-10,-10,12,12", :format => :json)
 
 746       assert_response :bad_request
 
 747       assert_equal "text/plain", @response.media_type
 
 749       get api_notes_path(:l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json)
 
 750       assert_response :bad_request
 
 751       assert_equal "text/plain", @response.media_type
 
 754     def test_index_closed
 
 755       create(:note_with_comments, :closed, :closed_at => Time.now.utc - 5.days)
 
 756       create(:note_with_comments, :closed, :closed_at => Time.now.utc - 100.days)
 
 757       create(:note_with_comments, :status => "hidden")
 
 758       create(:note_with_comments)
 
 760       # Open notes + closed in last 7 days
 
 761       get api_notes_path(:bbox => "1,1,1.7,1.7", :closed => "7", :format => "json")
 
 762       assert_response :success
 
 763       assert_equal "application/json", @response.media_type
 
 764       js = ActiveSupport::JSON.decode(@response.body)
 
 766       assert_equal "FeatureCollection", js["type"]
 
 767       assert_equal 2, js["features"].count
 
 770       get api_notes_path(:bbox => "1,1,1.7,1.7", :closed => "0", :format => "json")
 
 771       assert_response :success
 
 772       assert_equal "application/json", @response.media_type
 
 773       js = ActiveSupport::JSON.decode(@response.body)
 
 775       assert_equal "FeatureCollection", js["type"]
 
 776       assert_equal 1, js["features"].count
 
 778       # Open notes + all closed notes
 
 779       get api_notes_path(:bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json")
 
 780       assert_response :success
 
 781       assert_equal "application/json", @response.media_type
 
 782       js = ActiveSupport::JSON.decode(@response.body)
 
 784       assert_equal "FeatureCollection", js["type"]
 
 785       assert_equal 3, js["features"].count
 
 788     def test_index_bad_params
 
 790       assert_response :bad_request
 
 791       assert_equal "The parameter bbox is required", @response.body
 
 793       get api_notes_path(:bbox => "-2.5,-2.5,2.5")
 
 794       assert_response :bad_request
 
 796       get api_notes_path(:bbox => "-2.5,-2.5,2.5,2.5,2.5")
 
 797       assert_response :bad_request
 
 799       get api_notes_path(:b => "-2.5", :r => "2.5", :t => "2.5")
 
 800       assert_response :bad_request
 
 802       get api_notes_path(:l => "-2.5", :r => "2.5", :t => "2.5")
 
 803       assert_response :bad_request
 
 805       get api_notes_path(:l => "-2.5", :b => "-2.5", :t => "2.5")
 
 806       assert_response :bad_request
 
 808       get api_notes_path(:l => "-2.5", :b => "-2.5", :r => "2.5")
 
 809       assert_response :bad_request
 
 811       get api_notes_path(:bbox => "1,1,1.7,1.7", :limit => "0", :format => "json")
 
 812       assert_response :bad_request
 
 814       get api_notes_path(:bbox => "1,1,1.7,1.7", :limit => Settings.max_note_query_limit + 1, :format => "json")
 
 815       assert_response :bad_request
 
 818     def test_search_success
 
 819       create(:note_with_comments)
 
 821       get search_api_notes_path(:q => "note comment", :format => "xml")
 
 822       assert_response :success
 
 823       assert_equal "application/xml", @response.media_type
 
 824       assert_select "osm", :count => 1 do
 
 825         assert_select "note", :count => 1
 
 828       get search_api_notes_path(:q => "note comment", :format => "json")
 
 829       assert_response :success
 
 830       assert_equal "application/json", @response.media_type
 
 831       js = ActiveSupport::JSON.decode(@response.body)
 
 833       assert_equal "FeatureCollection", js["type"]
 
 834       assert_equal 1, js["features"].count
 
 836       get search_api_notes_path(:q => "note comment", :format => "rss")
 
 837       assert_response :success
 
 838       assert_equal "application/rss+xml", @response.media_type
 
 839       assert_select "rss", :count => 1 do
 
 840         assert_select "channel", :count => 1 do
 
 841           assert_select "item", :count => 1
 
 845       get search_api_notes_path(:q => "note comment", :format => "gpx")
 
 846       assert_response :success
 
 847       assert_equal "application/gpx+xml", @response.media_type
 
 848       assert_select "gpx", :count => 1 do
 
 849         assert_select "wpt", :count => 1
 
 852       get search_api_notes_path(:q => "note comment", :limit => Settings.max_note_query_limit, :format => "xml")
 
 853       assert_response :success
 
 856     def test_search_by_display_name_success
 
 859       create(:note) do |note|
 
 860         create(:note_comment, :note => note, :author => user)
 
 863       get search_api_notes_path(:display_name => user.display_name, :format => "xml")
 
 864       assert_response :success
 
 865       assert_equal "application/xml", @response.media_type
 
 866       assert_select "osm", :count => 1 do
 
 867         assert_select "note", :count => 1
 
 870       get search_api_notes_path(:display_name => user.display_name, :format => "json")
 
 871       assert_response :success
 
 872       assert_equal "application/json", @response.media_type
 
 873       js = ActiveSupport::JSON.decode(@response.body)
 
 875       assert_equal "FeatureCollection", js["type"]
 
 876       assert_equal 1, js["features"].count
 
 878       get search_api_notes_path(:display_name => user.display_name, :format => "rss")
 
 879       assert_response :success
 
 880       assert_equal "application/rss+xml", @response.media_type
 
 881       assert_select "rss", :count => 1 do
 
 882         assert_select "channel", :count => 1 do
 
 883           assert_select "item", :count => 1
 
 887       get search_api_notes_path(:display_name => user.display_name, :format => "gpx")
 
 888       assert_response :success
 
 889       assert_equal "application/gpx+xml", @response.media_type
 
 890       assert_select "gpx", :count => 1 do
 
 891         assert_select "wpt", :count => 1
 
 895     def test_search_by_user_success
 
 898       create(:note) do |note|
 
 899         create(:note_comment, :note => note, :author => user)
 
 902       get search_api_notes_path(:user => user.id, :format => "xml")
 
 903       assert_response :success
 
 904       assert_equal "application/xml", @response.media_type
 
 905       assert_select "osm", :count => 1 do
 
 906         assert_select "note", :count => 1
 
 909       get search_api_notes_path(:user => user.id, :format => "json")
 
 910       assert_response :success
 
 911       assert_equal "application/json", @response.media_type
 
 912       js = ActiveSupport::JSON.decode(@response.body)
 
 914       assert_equal "FeatureCollection", js["type"]
 
 915       assert_equal 1, js["features"].count
 
 917       get search_api_notes_path(:user => user.id, :format => "rss")
 
 918       assert_response :success
 
 919       assert_equal "application/rss+xml", @response.media_type
 
 920       assert_select "rss", :count => 1 do
 
 921         assert_select "channel", :count => 1 do
 
 922           assert_select "item", :count => 1
 
 926       get search_api_notes_path(:user => user.id, :format => "gpx")
 
 927       assert_response :success
 
 928       assert_equal "application/gpx+xml", @response.media_type
 
 929       assert_select "gpx", :count => 1 do
 
 930         assert_select "wpt", :count => 1
 
 934     def test_search_by_bbox_success
 
 935       notes = Array.new(5) do |i|
 
 936         position = ((1.0 + (i * 0.1)) * GeoRecord::SCALE).to_i
 
 937         create(:note_with_comments, :created_at => Time.parse("2020-01-01T00:00:00Z") + i.day, :latitude => position, :longitude => position)
 
 940       get search_api_notes_path(:bbox => "1.0,1.0,1.6,1.6", :sort => "created_at", :order => "oldest", :format => "xml")
 
 941       assert_response :success
 
 942       assert_equal "application/xml", @response.media_type
 
 943       assert_notes_in_order notes
 
 945       get search_api_notes_path(:bbox => "1.25,1.25,1.45,1.45", :sort => "created_at", :order => "oldest", :format => "xml")
 
 946       assert_response :success
 
 947       assert_equal "application/xml", @response.media_type
 
 948       assert_notes_in_order [notes[3], notes[4]]
 
 950       get search_api_notes_path(:bbox => "2.0,2.0,2.5,2.5", :sort => "created_at", :order => "oldest", :format => "xml")
 
 951       assert_response :success
 
 952       assert_equal "application/xml", @response.media_type
 
 953       assert_notes_in_order []
 
 956     def test_search_no_match
 
 957       create(:note_with_comments)
 
 959       get search_api_notes_path(:q => "no match", :format => "xml")
 
 960       assert_response :success
 
 961       assert_equal "application/xml", @response.media_type
 
 962       assert_select "osm", :count => 1 do
 
 963         assert_select "note", :count => 0
 
 966       get search_api_notes_path(:q => "no match", :format => "json")
 
 967       assert_response :success
 
 968       assert_equal "application/json", @response.media_type
 
 969       js = ActiveSupport::JSON.decode(@response.body)
 
 971       assert_equal "FeatureCollection", js["type"]
 
 972       assert_equal 0, js["features"].count
 
 974       get search_api_notes_path(:q => "no match", :format => "rss")
 
 975       assert_response :success
 
 976       assert_equal "application/rss+xml", @response.media_type
 
 977       assert_select "rss", :count => 1 do
 
 978         assert_select "channel", :count => 1 do
 
 979           assert_select "item", :count => 0
 
 983       get search_api_notes_path(:q => "no match", :format => "gpx")
 
 984       assert_response :success
 
 985       assert_equal "application/gpx+xml", @response.media_type
 
 986       assert_select "gpx", :count => 1 do
 
 987         assert_select "wpt", :count => 0
 
 991     def test_search_by_time_no_match
 
 992       create(:note_with_comments)
 
 994       get search_api_notes_path(:from => "01.01.2010", :to => "01.10.2010", :format => "xml")
 
 995       assert_response :success
 
 996       assert_equal "application/xml", @response.media_type
 
 997       assert_select "osm", :count => 1 do
 
 998         assert_select "note", :count => 0
 
1001       get search_api_notes_path(:from => "01.01.2010", :to => "01.10.2010", :format => "json")
 
1002       assert_response :success
 
1003       assert_equal "application/json", @response.media_type
 
1004       js = ActiveSupport::JSON.decode(@response.body)
 
1006       assert_equal "FeatureCollection", js["type"]
 
1007       assert_equal 0, js["features"].count
 
1009       get search_api_notes_path(:from => "01.01.2010", :to => "01.10.2010", :format => "rss")
 
1010       assert_response :success
 
1011       assert_equal "application/rss+xml", @response.media_type
 
1012       assert_select "rss", :count => 1 do
 
1013         assert_select "channel", :count => 1 do
 
1014           assert_select "item", :count => 0
 
1018       get search_api_notes_path(:from => "01.01.2010", :to => "01.10.2010", :format => "gpx")
 
1019       assert_response :success
 
1020       assert_equal "application/gpx+xml", @response.media_type
 
1021       assert_select "gpx", :count => 1 do
 
1022         assert_select "wpt", :count => 0
 
1026     def test_search_bad_params
 
1027       get search_api_notes_path(:q => "no match", :limit => "0", :format => "json")
 
1028       assert_response :bad_request
 
1030       get search_api_notes_path(:q => "no match", :limit => Settings.max_note_query_limit + 1, :format => "json")
 
1031       assert_response :bad_request
 
1033       get search_api_notes_path(:display_name => "non-existent")
 
1034       assert_response :bad_request
 
1036       get search_api_notes_path(:user => "-1")
 
1037       assert_response :bad_request
 
1039       get search_api_notes_path(:from => "wrong-date", :to => "wrong-date")
 
1040       assert_response :bad_request
 
1042       get search_api_notes_path(:from => "01.01.2010", :to => "2010.01.2010")
 
1043       assert_response :bad_request
 
1046     def test_feed_success
 
1047       position = (1.1 * GeoRecord::SCALE).to_i
 
1048       create(:note_with_comments, :latitude => position, :longitude => position)
 
1049       create(:note_with_comments, :latitude => position, :longitude => position)
 
1050       position = (1.5 * GeoRecord::SCALE).to_i
 
1051       create(:note_with_comments, :latitude => position, :longitude => position)
 
1052       create(:note_with_comments, :latitude => position, :longitude => position)
 
1054       get feed_api_notes_path(:format => "rss")
 
1055       assert_response :success
 
1056       assert_equal "application/rss+xml", @response.media_type
 
1057       assert_select "rss", :count => 1 do
 
1058         assert_select "channel", :count => 1 do
 
1059           assert_select "item", :count => 4
 
1063       get feed_api_notes_path(:bbox => "1,1,1.2,1.2", :format => "rss")
 
1064       assert_response :success
 
1065       assert_equal "application/rss+xml", @response.media_type
 
1066       assert_select "rss", :count => 1 do
 
1067         assert_select "channel", :count => 1 do
 
1068           assert_select "description", :text => /1\.2/, :count => 1
 
1069           assert_select "item", :count => 2
 
1073       get feed_api_notes_path(:bbox => "1,1,1.2,1.2", :limit => Settings.max_note_query_limit, :format => "rss")
 
1074       assert_response :success
 
1078       get feed_api_notes_path(:bbox => "1,1,1.2", :format => "rss")
 
1079       assert_response :bad_request
 
1081       get feed_api_notes_path(:bbox => "1,1,1.2,1.2,1.2", :format => "rss")
 
1082       assert_response :bad_request
 
1084       get feed_api_notes_path(:bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss")
 
1085       assert_response :bad_request
 
1087       get feed_api_notes_path(:bbox => "1,1,1.2,1.2", :limit => Settings.max_note_query_limit + 1, :format => "rss")
 
1088       assert_response :bad_request
 
1093     def assert_notes_in_order(notes)
 
1094       assert_select "osm>note", notes.size
 
1095       notes.each_with_index do |note, index|
 
1096         assert_select "osm>note:nth-child(#{index + 1})>id", :text => note.id.to_s, :count => 1