3 class NotesControllerTest < ActionController::TestCase
5 # Stub nominatim response for note locations
6 stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
7 .to_return(:status => 404)
11 # test all routes which lead to this controller
14 { :path => "/api/0.6/notes", :method => :post },
15 { :controller => "notes", :action => "create", :format => "xml" }
18 { :path => "/api/0.6/notes/1", :method => :get },
19 { :controller => "notes", :action => "show", :id => "1", :format => "xml" }
22 { :controller => "notes", :action => "show", :id => "1", :format => "xml" },
23 { :path => "/api/0.6/notes/1.xml", :method => :get }
26 { :path => "/api/0.6/notes/1.rss", :method => :get },
27 { :controller => "notes", :action => "show", :id => "1", :format => "rss" }
30 { :path => "/api/0.6/notes/1.json", :method => :get },
31 { :controller => "notes", :action => "show", :id => "1", :format => "json" }
34 { :path => "/api/0.6/notes/1.gpx", :method => :get },
35 { :controller => "notes", :action => "show", :id => "1", :format => "gpx" }
38 { :path => "/api/0.6/notes/1/comment", :method => :post },
39 { :controller => "notes", :action => "comment", :id => "1", :format => "xml" }
42 { :path => "/api/0.6/notes/1/close", :method => :post },
43 { :controller => "notes", :action => "close", :id => "1", :format => "xml" }
46 { :path => "/api/0.6/notes/1/reopen", :method => :post },
47 { :controller => "notes", :action => "reopen", :id => "1", :format => "xml" }
50 { :path => "/api/0.6/notes/1", :method => :delete },
51 { :controller => "notes", :action => "destroy", :id => "1", :format => "xml" }
55 { :path => "/api/0.6/notes", :method => :get },
56 { :controller => "notes", :action => "index", :format => "xml" }
59 { :controller => "notes", :action => "index", :format => "xml" },
60 { :path => "/api/0.6/notes.xml", :method => :get }
63 { :path => "/api/0.6/notes.rss", :method => :get },
64 { :controller => "notes", :action => "index", :format => "rss" }
67 { :path => "/api/0.6/notes.json", :method => :get },
68 { :controller => "notes", :action => "index", :format => "json" }
71 { :path => "/api/0.6/notes.gpx", :method => :get },
72 { :controller => "notes", :action => "index", :format => "gpx" }
76 { :path => "/api/0.6/notes/search", :method => :get },
77 { :controller => "notes", :action => "search", :format => "xml" }
80 { :controller => "notes", :action => "search", :format => "xml" },
81 { :path => "/api/0.6/notes/search.xml", :method => :get }
84 { :path => "/api/0.6/notes/search.rss", :method => :get },
85 { :controller => "notes", :action => "search", :format => "rss" }
88 { :path => "/api/0.6/notes/search.json", :method => :get },
89 { :controller => "notes", :action => "search", :format => "json" }
92 { :path => "/api/0.6/notes/search.gpx", :method => :get },
93 { :controller => "notes", :action => "search", :format => "gpx" }
97 { :path => "/api/0.6/notes/feed", :method => :get },
98 { :controller => "notes", :action => "feed", :format => "rss" }
102 { :controller => "notes", :action => "create" },
103 { :path => "/api/0.6/notes/addPOIexec", :method => :post }
106 { :controller => "notes", :action => "close" },
107 { :path => "/api/0.6/notes/closePOIexec", :method => :post }
110 { :controller => "notes", :action => "comment" },
111 { :path => "/api/0.6/notes/editPOIexec", :method => :post }
114 { :controller => "notes", :action => "index", :format => "gpx" },
115 { :path => "/api/0.6/notes/getGPX", :method => :get }
118 { :controller => "notes", :action => "feed", :format => "rss" },
119 { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
123 { :path => "/user/username/notes", :method => :get },
124 { :controller => "notes", :action => "mine", :display_name => "username" }
128 def test_create_success
129 assert_difference "Note.count", 1 do
130 assert_difference "NoteComment.count", 1 do
131 post :create, :params => { :lat => -1.0, :lon => -1.0, :text => "This is a comment", :format => "json" }
134 assert_response :success
135 js = ActiveSupport::JSON.decode(@response.body)
137 assert_equal "Feature", js["type"]
138 assert_equal "Point", js["geometry"]["type"]
139 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
140 assert_equal "open", js["properties"]["status"]
141 assert_equal 1, js["properties"]["comments"].count
142 assert_equal "opened", js["properties"]["comments"].last["action"]
143 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
144 assert_nil js["properties"]["comments"].last["user"]
145 id = js["properties"]["id"]
147 get :show, :params => { :id => id, :format => "json" }
148 assert_response :success
149 js = ActiveSupport::JSON.decode(@response.body)
151 assert_equal "Feature", js["type"]
152 assert_equal "Point", js["geometry"]["type"]
153 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
154 assert_equal id, js["properties"]["id"]
155 assert_equal "open", js["properties"]["status"]
156 assert_equal 1, js["properties"]["comments"].count
157 assert_equal "opened", js["properties"]["comments"].last["action"]
158 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
159 assert_nil js["properties"]["comments"].last["user"]
163 assert_no_difference "Note.count" do
164 assert_no_difference "NoteComment.count" do
165 post :create, :params => { :lon => -1.0, :text => "This is a comment" }
168 assert_response :bad_request
170 assert_no_difference "Note.count" do
171 assert_no_difference "NoteComment.count" do
172 post :create, :params => { :lat => -1.0, :text => "This is a comment" }
175 assert_response :bad_request
177 assert_no_difference "Note.count" do
178 assert_no_difference "NoteComment.count" do
179 post :create, :params => { :lat => -1.0, :lon => -1.0 }
182 assert_response :bad_request
184 assert_no_difference "Note.count" do
185 assert_no_difference "NoteComment.count" do
186 post :create, :params => { :lat => -1.0, :lon => -1.0, :text => "" }
189 assert_response :bad_request
191 assert_no_difference "Note.count" do
192 assert_no_difference "NoteComment.count" do
193 post :create, :params => { :lat => -100.0, :lon => -1.0, :text => "This is a comment" }
196 assert_response :bad_request
198 assert_no_difference "Note.count" do
199 assert_no_difference "NoteComment.count" do
200 post :create, :params => { :lat => -1.0, :lon => -200.0, :text => "This is a comment" }
203 assert_response :bad_request
205 assert_no_difference "Note.count" do
206 assert_no_difference "NoteComment.count" do
207 post :create, :params => { :lat => "abc", :lon => -1.0, :text => "This is a comment" }
210 assert_response :bad_request
212 assert_no_difference "Note.count" do
213 assert_no_difference "NoteComment.count" do
214 post :create, :params => { :lat => -1.0, :lon => "abc", :text => "This is a comment" }
217 assert_response :bad_request
219 assert_no_difference "Note.count" do
220 assert_no_difference "NoteComment.count" do
221 post :create, :params => { :lat => -1.0, :lon => -1.0, :text => "x\u0000y" }
224 assert_response :bad_request
227 def test_comment_success
228 open_note_with_comment = create(:note_with_comments)
229 assert_difference "NoteComment.count", 1 do
230 assert_no_difference "ActionMailer::Base.deliveries.size" do
231 perform_enqueued_jobs do
232 post :comment, :params => { :id => open_note_with_comment.id, :text => "This is an additional comment", :format => "json" }
236 assert_response :success
237 js = ActiveSupport::JSON.decode(@response.body)
239 assert_equal "Feature", js["type"]
240 assert_equal open_note_with_comment.id, js["properties"]["id"]
241 assert_equal "open", js["properties"]["status"]
242 assert_equal 2, js["properties"]["comments"].count
243 assert_equal "commented", js["properties"]["comments"].last["action"]
244 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
245 assert_nil js["properties"]["comments"].last["user"]
247 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
248 assert_response :success
249 js = ActiveSupport::JSON.decode(@response.body)
251 assert_equal "Feature", js["type"]
252 assert_equal open_note_with_comment.id, js["properties"]["id"]
253 assert_equal "open", js["properties"]["status"]
254 assert_equal 2, js["properties"]["comments"].count
255 assert_equal "commented", js["properties"]["comments"].last["action"]
256 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
257 assert_nil js["properties"]["comments"].last["user"]
259 # Ensure that emails are sent to users
260 first_user = create(:user)
261 second_user = create(:user)
262 third_user = create(:user)
264 note_with_comments_by_users = create(:note) do |note|
265 create(:note_comment, :note => note, :author => first_user)
266 create(:note_comment, :note => note, :author => second_user)
268 assert_difference "NoteComment.count", 1 do
269 assert_difference "ActionMailer::Base.deliveries.size", 2 do
270 perform_enqueued_jobs do
271 post :comment, :params => { :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json" }
275 assert_response :success
276 js = ActiveSupport::JSON.decode(@response.body)
278 assert_equal "Feature", js["type"]
279 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
280 assert_equal "open", js["properties"]["status"]
281 assert_equal 3, js["properties"]["comments"].count
282 assert_equal "commented", js["properties"]["comments"].last["action"]
283 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
284 assert_nil js["properties"]["comments"].last["user"]
286 email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
288 assert_equal 1, email.to.length
289 assert_equal "[OpenStreetMap] An anonymous user has commented on one of your notes", email.subject
291 email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
293 assert_equal 1, email.to.length
294 assert_equal "[OpenStreetMap] An anonymous user has commented on a note you are interested in", email.subject
296 get :show, :params => { :id => note_with_comments_by_users.id, :format => "json" }
297 assert_response :success
298 js = ActiveSupport::JSON.decode(@response.body)
300 assert_equal "Feature", js["type"]
301 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
302 assert_equal "open", js["properties"]["status"]
303 assert_equal 3, js["properties"]["comments"].count
304 assert_equal "commented", js["properties"]["comments"].last["action"]
305 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
306 assert_nil js["properties"]["comments"].last["user"]
308 ActionMailer::Base.deliveries.clear
310 basic_authorization third_user.email, "test"
312 assert_difference "NoteComment.count", 1 do
313 assert_difference "ActionMailer::Base.deliveries.size", 2 do
314 perform_enqueued_jobs do
315 post :comment, :params => { :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json" }
319 assert_response :success
320 js = ActiveSupport::JSON.decode(@response.body)
322 assert_equal "Feature", js["type"]
323 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
324 assert_equal "open", js["properties"]["status"]
325 assert_equal 4, js["properties"]["comments"].count
326 assert_equal "commented", js["properties"]["comments"].last["action"]
327 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
328 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
330 email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
332 assert_equal 1, email.to.length
333 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on one of your notes", email.subject
334 assert_equal first_user.email, email.to.first
336 email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
338 assert_equal 1, email.to.length
339 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on a note you are interested in", email.subject
341 get :show, :params => { :id => note_with_comments_by_users.id, :format => "json" }
342 assert_response :success
343 js = ActiveSupport::JSON.decode(@response.body)
345 assert_equal "Feature", js["type"]
346 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
347 assert_equal "open", js["properties"]["status"]
348 assert_equal 4, js["properties"]["comments"].count
349 assert_equal "commented", js["properties"]["comments"].last["action"]
350 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
351 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
353 ActionMailer::Base.deliveries.clear
356 def test_comment_fail
357 open_note_with_comment = create(:note_with_comments)
359 assert_no_difference "NoteComment.count" do
360 post :comment, :params => { :text => "This is an additional comment" }
362 assert_response :bad_request
364 assert_no_difference "NoteComment.count" do
365 post :comment, :params => { :id => open_note_with_comment.id }
367 assert_response :bad_request
369 assert_no_difference "NoteComment.count" do
370 post :comment, :params => { :id => open_note_with_comment.id, :text => "" }
372 assert_response :bad_request
374 assert_no_difference "NoteComment.count" do
375 post :comment, :params => { :id => 12345, :text => "This is an additional comment" }
377 assert_response :not_found
379 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
381 assert_no_difference "NoteComment.count" do
382 post :comment, :params => { :id => hidden_note_with_comment.id, :text => "This is an additional comment" }
384 assert_response :gone
386 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
388 assert_no_difference "NoteComment.count" do
389 post :comment, :params => { :id => closed_note_with_comment.id, :text => "This is an additional comment" }
391 assert_response :conflict
393 assert_no_difference "NoteComment.count" do
394 post :comment, :params => { :id => open_note_with_comment.id, :text => "x\u0000y" }
396 assert_response :bad_request
399 def test_close_success
400 open_note_with_comment = create(:note_with_comments)
403 post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
404 assert_response :unauthorized
406 basic_authorization user.email, "test"
408 post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
409 assert_response :success
410 js = ActiveSupport::JSON.decode(@response.body)
412 assert_equal "Feature", js["type"]
413 assert_equal open_note_with_comment.id, js["properties"]["id"]
414 assert_equal "closed", js["properties"]["status"]
415 assert_equal 2, js["properties"]["comments"].count
416 assert_equal "closed", js["properties"]["comments"].last["action"]
417 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
418 assert_equal user.display_name, js["properties"]["comments"].last["user"]
420 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
421 assert_response :success
422 js = ActiveSupport::JSON.decode(@response.body)
424 assert_equal "Feature", js["type"]
425 assert_equal open_note_with_comment.id, js["properties"]["id"]
426 assert_equal "closed", js["properties"]["status"]
427 assert_equal 2, js["properties"]["comments"].count
428 assert_equal "closed", js["properties"]["comments"].last["action"]
429 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
430 assert_equal user.display_name, js["properties"]["comments"].last["user"]
435 assert_response :unauthorized
437 basic_authorization create(:user).email, "test"
440 assert_response :bad_request
442 post :close, :params => { :id => 12345 }
443 assert_response :not_found
445 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
447 post :close, :params => { :id => hidden_note_with_comment.id }
448 assert_response :gone
450 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
452 post :close, :params => { :id => closed_note_with_comment.id }
453 assert_response :conflict
456 def test_reopen_success
457 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
460 post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
461 assert_response :unauthorized
463 basic_authorization user.email, "test"
465 post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
466 assert_response :success
467 js = ActiveSupport::JSON.decode(@response.body)
469 assert_equal "Feature", js["type"]
470 assert_equal closed_note_with_comment.id, js["properties"]["id"]
471 assert_equal "open", js["properties"]["status"]
472 assert_equal 2, js["properties"]["comments"].count
473 assert_equal "reopened", js["properties"]["comments"].last["action"]
474 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
475 assert_equal user.display_name, js["properties"]["comments"].last["user"]
477 get :show, :params => { :id => closed_note_with_comment.id, :format => "json" }
478 assert_response :success
479 js = ActiveSupport::JSON.decode(@response.body)
481 assert_equal "Feature", js["type"]
482 assert_equal closed_note_with_comment.id, js["properties"]["id"]
483 assert_equal "open", js["properties"]["status"]
484 assert_equal 2, js["properties"]["comments"].count
485 assert_equal "reopened", js["properties"]["comments"].last["action"]
486 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
487 assert_equal user.display_name, js["properties"]["comments"].last["user"]
491 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
493 post :reopen, :params => { :id => hidden_note_with_comment.id }
494 assert_response :unauthorized
496 basic_authorization create(:user).email, "test"
498 post :reopen, :params => { :id => 12345 }
499 assert_response :not_found
501 post :reopen, :params => { :id => hidden_note_with_comment.id }
502 assert_response :gone
504 open_note_with_comment = create(:note_with_comments)
506 post :reopen, :params => { :id => open_note_with_comment.id }
507 assert_response :conflict
510 def test_show_success
511 open_note = create(:note_with_comments)
513 get :show, :params => { :id => open_note.id, :format => "xml" }
514 assert_response :success
515 assert_equal "application/xml", @response.content_type
516 assert_select "osm", :count => 1 do
517 assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
518 assert_select "id", open_note.id.to_s
519 assert_select "url", note_url(open_note, :format => "xml")
520 assert_select "comment_url", comment_note_url(open_note, :format => "xml")
521 assert_select "close_url", close_note_url(open_note, :format => "xml")
522 assert_select "date_created", open_note.created_at.to_s
523 assert_select "status", open_note.status
524 assert_select "comments", :count => 1 do
525 assert_select "comment", :count => 1
530 get :show, :params => { :id => open_note.id, :format => "rss" }
531 assert_response :success
532 assert_equal "application/rss+xml", @response.content_type
533 assert_select "rss", :count => 1 do
534 assert_select "channel", :count => 1 do
535 assert_select "item", :count => 1 do
536 assert_select "link", browse_note_url(open_note)
537 assert_select "guid", note_url(open_note)
538 assert_select "pubDate", open_note.created_at.to_s(:rfc822)
539 # assert_select "geo:lat", open_note.lat.to_s
540 # assert_select "geo:long", open_note.lon
541 # assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
546 get :show, :params => { :id => open_note.id, :format => "json" }
547 assert_response :success
548 assert_equal "application/json", @response.content_type
549 js = ActiveSupport::JSON.decode(@response.body)
551 assert_equal "Feature", js["type"]
552 assert_equal "Point", js["geometry"]["type"]
553 assert_equal open_note.lat, js["geometry"]["coordinates"][0]
554 assert_equal open_note.lon, js["geometry"]["coordinates"][1]
555 assert_equal open_note.id, js["properties"]["id"]
556 assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
557 assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
558 assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
559 assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
560 assert_equal open_note.status, js["properties"]["status"]
562 get :show, :params => { :id => open_note.id, :format => "gpx" }
563 assert_response :success
564 assert_equal "application/gpx+xml", @response.content_type
565 assert_select "gpx", :count => 1 do
566 assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
567 assert_select "time", :count => 1
568 assert_select "name", "Note: #{open_note.id}"
569 assert_select "desc", :count => 1
570 assert_select "link[href='http://test.host/note/#{open_note.id}']", :count => 1
571 assert_select "extensions", :count => 1 do
572 assert_select "id", open_note.id.to_s
573 assert_select "url", note_url(open_note, :format => "gpx")
574 assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
575 assert_select "close_url", close_note_url(open_note, :format => "gpx")
581 def test_show_hidden_comment
582 note_with_hidden_comment = create(:note) do |note|
583 create(:note_comment, :note => note, :body => "Valid comment for hidden note")
584 create(:note_comment, :note => note, :visible => false)
585 create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
588 get :show, :params => { :id => note_with_hidden_comment.id, :format => "json" }
589 assert_response :success
590 js = ActiveSupport::JSON.decode(@response.body)
592 assert_equal "Feature", js["type"]
593 assert_equal note_with_hidden_comment.id, js["properties"]["id"]
594 assert_equal 2, js["properties"]["comments"].count
595 assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
596 assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
600 get :show, :params => { :id => 12345 }
601 assert_response :not_found
603 get :show, :params => { :id => create(:note, :status => "hidden").id }
604 assert_response :gone
607 def test_destroy_success
608 open_note_with_comment = create(:note_with_comments)
610 moderator_user = create(:moderator_user)
612 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
613 assert_response :unauthorized
615 basic_authorization user.email, "test"
617 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
618 assert_response :forbidden
620 basic_authorization moderator_user.email, "test"
622 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
623 assert_response :success
624 js = ActiveSupport::JSON.decode(@response.body)
626 assert_equal "Feature", js["type"]
627 assert_equal open_note_with_comment.id, js["properties"]["id"]
628 assert_equal "hidden", js["properties"]["status"]
629 assert_equal 2, js["properties"]["comments"].count
630 assert_equal "hidden", js["properties"]["comments"].last["action"]
631 assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
632 assert_equal moderator_user.display_name, js["properties"]["comments"].last["user"]
634 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
635 assert_response :success
637 basic_authorization user.email, "test"
638 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
639 assert_response :gone
642 def test_destroy_fail
644 moderator_user = create(:moderator_user)
646 delete :destroy, :params => { :id => 12345, :format => "json" }
647 assert_response :unauthorized
649 basic_authorization user.email, "test"
651 delete :destroy, :params => { :id => 12345, :format => "json" }
652 assert_response :forbidden
654 basic_authorization moderator_user.email, "test"
656 delete :destroy, :params => { :id => 12345, :format => "json" }
657 assert_response :not_found
659 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
661 delete :destroy, :params => { :id => hidden_note_with_comment.id, :format => "json" }
662 assert_response :gone
665 def test_index_success
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, :params => { :bbox => "1,1,1.2,1.2", :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 => 2
679 get :index, :params => { :bbox => "1,1,1.2,1.2", :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 2, js["features"].count
687 get :index, :params => { :bbox => "1,1,1.2,1.2", :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 => 2
694 get :index, :params => { :bbox => "1,1,1.2,1.2", :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 => 2
703 position = (1.1 * GeoRecord::SCALE).to_i
704 create(:note_with_comments, :latitude => position, :longitude => position)
705 create(:note_with_comments, :latitude => position, :longitude => position)
707 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss" }
708 assert_response :success
709 assert_equal "application/rss+xml", @response.content_type
710 assert_select "rss", :count => 1 do
711 assert_select "channel", :count => 1 do
712 assert_select "item", :count => 1
716 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json" }
717 assert_response :success
718 assert_equal "application/json", @response.content_type
719 js = ActiveSupport::JSON.decode(@response.body)
721 assert_equal "FeatureCollection", js["type"]
722 assert_equal 1, js["features"].count
724 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml" }
725 assert_response :success
726 assert_equal "application/xml", @response.content_type
727 assert_select "osm", :count => 1 do
728 assert_select "note", :count => 1
731 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx" }
732 assert_response :success
733 assert_equal "application/gpx+xml", @response.content_type
734 assert_select "gpx", :count => 1 do
735 assert_select "wpt", :count => 1
739 def test_index_empty_area
740 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "rss" }
741 assert_response :success
742 assert_equal "application/rss+xml", @response.content_type
743 assert_select "rss", :count => 1 do
744 assert_select "channel", :count => 1 do
745 assert_select "item", :count => 0
749 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "json" }
750 assert_response :success
751 assert_equal "application/json", @response.content_type
752 js = ActiveSupport::JSON.decode(@response.body)
754 assert_equal "FeatureCollection", js["type"]
755 assert_equal 0, js["features"].count
757 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "xml" }
758 assert_response :success
759 assert_equal "application/xml", @response.content_type
760 assert_select "osm", :count => 1 do
761 assert_select "note", :count => 0
764 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "gpx" }
765 assert_response :success
766 assert_equal "application/gpx+xml", @response.content_type
767 assert_select "gpx", :count => 1 do
768 assert_select "wpt", :count => 0
772 def test_index_large_area
773 get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5", :format => :json }
774 assert_response :success
775 assert_equal "application/json", @response.content_type
777 get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json }
778 assert_response :success
779 assert_equal "application/json", @response.content_type
781 get :index, :params => { :bbox => "-10,-10,12,12", :format => :json }
782 assert_response :bad_request
783 assert_equal "application/json", @response.content_type
785 get :index, :params => { :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json }
786 assert_response :bad_request
787 assert_equal "application/json", @response.content_type
790 def test_index_closed
791 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 5.days)
792 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 100.days)
793 create(:note_with_comments, :status => "hidden")
794 create(:note_with_comments)
796 # Open notes + closed in last 7 days
797 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json" }
798 assert_response :success
799 assert_equal "application/json", @response.content_type
800 js = ActiveSupport::JSON.decode(@response.body)
802 assert_equal "FeatureCollection", js["type"]
803 assert_equal 2, js["features"].count
806 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json" }
807 assert_response :success
808 assert_equal "application/json", @response.content_type
809 js = ActiveSupport::JSON.decode(@response.body)
811 assert_equal "FeatureCollection", js["type"]
812 assert_equal 1, js["features"].count
814 # Open notes + all closed notes
815 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json" }
816 assert_response :success
817 assert_equal "application/json", @response.content_type
818 js = ActiveSupport::JSON.decode(@response.body)
820 assert_equal "FeatureCollection", js["type"]
821 assert_equal 3, js["features"].count
824 def test_index_bad_params
825 get :index, :params => { :bbox => "-2.5,-2.5,2.5" }
826 assert_response :bad_request
828 get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5,2.5" }
829 assert_response :bad_request
831 get :index, :params => { :b => "-2.5", :r => "2.5", :t => "2.5" }
832 assert_response :bad_request
834 get :index, :params => { :l => "-2.5", :r => "2.5", :t => "2.5" }
835 assert_response :bad_request
837 get :index, :params => { :l => "-2.5", :b => "-2.5", :t => "2.5" }
838 assert_response :bad_request
840 get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5" }
841 assert_response :bad_request
843 get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json" }
844 assert_response :bad_request
846 get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json" }
847 assert_response :bad_request
850 def test_search_success
851 create(:note_with_comments)
853 get :search, :params => { :q => "note comment", :format => "xml" }
854 assert_response :success
855 assert_equal "application/xml", @response.content_type
856 assert_select "osm", :count => 1 do
857 assert_select "note", :count => 1
860 get :search, :params => { :q => "note comment", :format => "json" }
861 assert_response :success
862 assert_equal "application/json", @response.content_type
863 js = ActiveSupport::JSON.decode(@response.body)
865 assert_equal "FeatureCollection", js["type"]
866 assert_equal 1, js["features"].count
868 get :search, :params => { :q => "note comment", :format => "rss" }
869 assert_response :success
870 assert_equal "application/rss+xml", @response.content_type
871 assert_select "rss", :count => 1 do
872 assert_select "channel", :count => 1 do
873 assert_select "item", :count => 1
877 get :search, :params => { :q => "note comment", :format => "gpx" }
878 assert_response :success
879 assert_equal "application/gpx+xml", @response.content_type
880 assert_select "gpx", :count => 1 do
881 assert_select "wpt", :count => 1
885 def test_search_by_display_name_success
888 create(:note) do |note|
889 create(:note_comment, :note => note, :author => user)
892 get :search, :params => { :display_name => user.display_name, :format => "xml" }
893 assert_response :success
894 assert_equal "application/xml", @response.content_type
895 assert_select "osm", :count => 1 do
896 assert_select "note", :count => 1
899 get :search, :params => { :display_name => user.display_name, :format => "json" }
900 assert_response :success
901 assert_equal "application/json", @response.content_type
902 js = ActiveSupport::JSON.decode(@response.body)
904 assert_equal "FeatureCollection", js["type"]
905 assert_equal 1, js["features"].count
907 get :search, :params => { :display_name => user.display_name, :format => "rss" }
908 assert_response :success
909 assert_equal "application/rss+xml", @response.content_type
910 assert_select "rss", :count => 1 do
911 assert_select "channel", :count => 1 do
912 assert_select "item", :count => 1
916 get :search, :params => { :display_name => user.display_name, :format => "gpx" }
917 assert_response :success
918 assert_equal "application/gpx+xml", @response.content_type
919 assert_select "gpx", :count => 1 do
920 assert_select "wpt", :count => 1
924 def test_search_by_user_success
927 create(:note) do |note|
928 create(:note_comment, :note => note, :author => user)
931 get :search, :params => { :user => user.id, :format => "xml" }
932 assert_response :success
933 assert_equal "application/xml", @response.content_type
934 assert_select "osm", :count => 1 do
935 assert_select "note", :count => 1
938 get :search, :params => { :user => user.id, :format => "json" }
939 assert_response :success
940 assert_equal "application/json", @response.content_type
941 js = ActiveSupport::JSON.decode(@response.body)
943 assert_equal "FeatureCollection", js["type"]
944 assert_equal 1, js["features"].count
946 get :search, :params => { :user => user.id, :format => "rss" }
947 assert_response :success
948 assert_equal "application/rss+xml", @response.content_type
949 assert_select "rss", :count => 1 do
950 assert_select "channel", :count => 1 do
951 assert_select "item", :count => 1
955 get :search, :params => { :user => user.id, :format => "gpx" }
956 assert_response :success
957 assert_equal "application/gpx+xml", @response.content_type
958 assert_select "gpx", :count => 1 do
959 assert_select "wpt", :count => 1
963 def test_search_no_match
964 create(:note_with_comments)
966 get :search, :params => { :q => "no match", :format => "xml" }
967 assert_response :success
968 assert_equal "application/xml", @response.content_type
969 assert_select "osm", :count => 1 do
970 assert_select "note", :count => 0
973 get :search, :params => { :q => "no match", :format => "json" }
974 assert_response :success
975 assert_equal "application/json", @response.content_type
976 js = ActiveSupport::JSON.decode(@response.body)
978 assert_equal "FeatureCollection", js["type"]
979 assert_equal 0, js["features"].count
981 get :search, :params => { :q => "no match", :format => "rss" }
982 assert_response :success
983 assert_equal "application/rss+xml", @response.content_type
984 assert_select "rss", :count => 1 do
985 assert_select "channel", :count => 1 do
986 assert_select "item", :count => 0
990 get :search, :params => { :q => "no match", :format => "gpx" }
991 assert_response :success
992 assert_equal "application/gpx+xml", @response.content_type
993 assert_select "gpx", :count => 1 do
994 assert_select "wpt", :count => 0
998 def test_search_by_time_no_match
999 create(:note_with_comments)
1001 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "xml" }
1002 assert_response :success
1003 assert_equal "application/xml", @response.content_type
1004 assert_select "osm", :count => 1 do
1005 assert_select "note", :count => 0
1008 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "json" }
1009 assert_response :success
1010 assert_equal "application/json", @response.content_type
1011 js = ActiveSupport::JSON.decode(@response.body)
1013 assert_equal "FeatureCollection", js["type"]
1014 assert_equal 0, js["features"].count
1016 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "rss" }
1017 assert_response :success
1018 assert_equal "application/rss+xml", @response.content_type
1019 assert_select "rss", :count => 1 do
1020 assert_select "channel", :count => 1 do
1021 assert_select "item", :count => 0
1025 get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "gpx" }
1026 assert_response :success
1027 assert_equal "application/gpx+xml", @response.content_type
1028 assert_select "gpx", :count => 1 do
1029 assert_select "wpt", :count => 0
1033 def test_search_bad_params
1034 get :search, :params => { :q => "no match", :limit => "0", :format => "json" }
1035 assert_response :bad_request
1037 get :search, :params => { :q => "no match", :limit => "10001", :format => "json" }
1038 assert_response :bad_request
1040 get :search, :params => { :display_name => "non-existent" }
1041 assert_response :bad_request
1043 get :search, :params => { :user => "-1" }
1044 assert_response :bad_request
1046 get :search, :params => { :from => "wrong-date", :to => "wrong-date" }
1047 assert_response :bad_request
1049 get :search, :params => { :from => "01.01.2010", :to => "2010.01.2010" }
1050 assert_response :bad_request
1053 def test_feed_success
1054 position = (1.1 * GeoRecord::SCALE).to_i
1055 create(:note_with_comments, :latitude => position, :longitude => position)
1056 create(:note_with_comments, :latitude => position, :longitude => position)
1057 position = (1.5 * GeoRecord::SCALE).to_i
1058 create(:note_with_comments, :latitude => position, :longitude => position)
1059 create(:note_with_comments, :latitude => position, :longitude => position)
1061 get :feed, :params => { :format => "rss" }
1062 assert_response :success
1063 assert_equal "application/rss+xml", @response.content_type
1064 assert_select "rss", :count => 1 do
1065 assert_select "channel", :count => 1 do
1066 assert_select "item", :count => 4
1070 get :feed, :params => { :bbox => "1,1,1.2,1.2", :format => "rss" }
1071 assert_response :success
1072 assert_equal "application/rss+xml", @response.content_type
1073 assert_select "rss", :count => 1 do
1074 assert_select "channel", :count => 1 do
1075 assert_select "item", :count => 2
1081 get :feed, :params => { :bbox => "1,1,1.2", :format => "rss" }
1082 assert_response :bad_request
1084 get :feed, :params => { :bbox => "1,1,1.2,1.2,1.2", :format => "rss" }
1085 assert_response :bad_request
1087 get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss" }
1088 assert_response :bad_request
1090 get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss" }
1091 assert_response :bad_request
1094 def test_mine_success
1095 first_user = create(:user)
1096 second_user = create(:user)
1097 moderator_user = create(:moderator_user)
1099 create(:note) do |note|
1100 create(:note_comment, :note => note, :author => first_user)
1102 create(:note) do |note|
1103 create(:note_comment, :note => note, :author => second_user)
1105 create(:note, :status => "hidden") do |note|
1106 create(:note_comment, :note => note, :author => second_user)
1109 # Note that the table rows include a header row
1110 get :mine, :params => { :display_name => first_user.display_name }
1111 assert_response :success
1112 assert_select "table.note_list tr", :count => 2
1114 get :mine, :params => { :display_name => second_user.display_name }
1115 assert_response :success
1116 assert_select "table.note_list tr", :count => 2
1118 get :mine, :params => { :display_name => "non-existent" }
1119 assert_response :not_found
1121 session[:user] = moderator_user.id
1123 get :mine, :params => { :display_name => first_user.display_name }
1124 assert_response :success
1125 assert_select "table.note_list tr", :count => 2
1127 get :mine, :params => { :display_name => second_user.display_name }
1128 assert_response :success
1129 assert_select "table.note_list tr", :count => 3
1131 get :mine, :params => { :display_name => "non-existent" }
1132 assert_response :not_found
1136 user = create(:user)
1138 create_list(:note, 50) do |note|
1139 create(:note_comment, :note => note, :author => user)
1142 get :mine, :params => { :display_name => user.display_name }
1143 assert_response :success
1144 assert_select "table.note_list tr", :count => 11
1146 get :mine, :params => { :display_name => user.display_name, :page => 2 }
1147 assert_response :success
1148 assert_select "table.note_list tr", :count => 11