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 post :comment, :params => { :id => open_note_with_comment.id, :text => "This is an additional comment", :format => "json" }
234 assert_response :success
235 js = ActiveSupport::JSON.decode(@response.body)
237 assert_equal "Feature", js["type"]
238 assert_equal open_note_with_comment.id, js["properties"]["id"]
239 assert_equal "open", js["properties"]["status"]
240 assert_equal 2, js["properties"]["comments"].count
241 assert_equal "commented", js["properties"]["comments"].last["action"]
242 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
243 assert_nil js["properties"]["comments"].last["user"]
245 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
246 assert_response :success
247 js = ActiveSupport::JSON.decode(@response.body)
249 assert_equal "Feature", js["type"]
250 assert_equal open_note_with_comment.id, js["properties"]["id"]
251 assert_equal "open", js["properties"]["status"]
252 assert_equal 2, js["properties"]["comments"].count
253 assert_equal "commented", js["properties"]["comments"].last["action"]
254 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
255 assert_nil js["properties"]["comments"].last["user"]
257 # Ensure that emails are sent to users
258 first_user = create(:user)
259 second_user = create(:user)
260 third_user = create(:user)
262 note_with_comments_by_users = create(:note) do |note|
263 create(:note_comment, :note => note, :author => first_user)
264 create(:note_comment, :note => note, :author => second_user)
266 assert_difference "NoteComment.count", 1 do
267 assert_difference "ActionMailer::Base.deliveries.size", 2 do
268 post :comment, :params => { :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json" }
271 assert_response :success
272 js = ActiveSupport::JSON.decode(@response.body)
274 assert_equal "Feature", js["type"]
275 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
276 assert_equal "open", js["properties"]["status"]
277 assert_equal 3, js["properties"]["comments"].count
278 assert_equal "commented", js["properties"]["comments"].last["action"]
279 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
280 assert_nil js["properties"]["comments"].last["user"]
282 email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
284 assert_equal 1, email.to.length
285 assert_equal "[OpenStreetMap] An anonymous user has commented on one of your notes", email.subject
287 email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
289 assert_equal 1, email.to.length
290 assert_equal "[OpenStreetMap] An anonymous user has commented on a note you are interested in", email.subject
292 get :show, :params => { :id => note_with_comments_by_users.id, :format => "json" }
293 assert_response :success
294 js = ActiveSupport::JSON.decode(@response.body)
296 assert_equal "Feature", js["type"]
297 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
298 assert_equal "open", js["properties"]["status"]
299 assert_equal 3, js["properties"]["comments"].count
300 assert_equal "commented", js["properties"]["comments"].last["action"]
301 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
302 assert_nil js["properties"]["comments"].last["user"]
304 ActionMailer::Base.deliveries.clear
306 basic_authorization third_user.email, "test"
308 assert_difference "NoteComment.count", 1 do
309 assert_difference "ActionMailer::Base.deliveries.size", 2 do
310 post :comment, :params => { :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json" }
313 assert_response :success
314 js = ActiveSupport::JSON.decode(@response.body)
316 assert_equal "Feature", js["type"]
317 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
318 assert_equal "open", js["properties"]["status"]
319 assert_equal 4, js["properties"]["comments"].count
320 assert_equal "commented", js["properties"]["comments"].last["action"]
321 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
322 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
324 email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
326 assert_equal 1, email.to.length
327 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on one of your notes", email.subject
328 assert_equal first_user.email, email.to.first
330 email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
332 assert_equal 1, email.to.length
333 assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on a note you are interested in", email.subject
335 get :show, :params => { :id => note_with_comments_by_users.id, :format => "json" }
336 assert_response :success
337 js = ActiveSupport::JSON.decode(@response.body)
339 assert_equal "Feature", js["type"]
340 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
341 assert_equal "open", js["properties"]["status"]
342 assert_equal 4, js["properties"]["comments"].count
343 assert_equal "commented", js["properties"]["comments"].last["action"]
344 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
345 assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
347 ActionMailer::Base.deliveries.clear
350 def test_comment_fail
351 open_note_with_comment = create(:note_with_comments)
353 assert_no_difference "NoteComment.count" do
354 post :comment, :params => { :text => "This is an additional comment" }
356 assert_response :bad_request
358 assert_no_difference "NoteComment.count" do
359 post :comment, :params => { :id => open_note_with_comment.id }
361 assert_response :bad_request
363 assert_no_difference "NoteComment.count" do
364 post :comment, :params => { :id => open_note_with_comment.id, :text => "" }
366 assert_response :bad_request
368 assert_no_difference "NoteComment.count" do
369 post :comment, :params => { :id => 12345, :text => "This is an additional comment" }
371 assert_response :not_found
373 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
375 assert_no_difference "NoteComment.count" do
376 post :comment, :params => { :id => hidden_note_with_comment.id, :text => "This is an additional comment" }
378 assert_response :gone
380 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
382 assert_no_difference "NoteComment.count" do
383 post :comment, :params => { :id => closed_note_with_comment.id, :text => "This is an additional comment" }
385 assert_response :conflict
387 assert_no_difference "NoteComment.count" do
388 post :comment, :params => { :id => open_note_with_comment.id, :text => "x\u0000y" }
390 assert_response :bad_request
393 def test_close_success
394 open_note_with_comment = create(:note_with_comments)
397 post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
398 assert_response :unauthorized
400 basic_authorization user.email, "test"
402 post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
403 assert_response :success
404 js = ActiveSupport::JSON.decode(@response.body)
406 assert_equal "Feature", js["type"]
407 assert_equal open_note_with_comment.id, js["properties"]["id"]
408 assert_equal "closed", js["properties"]["status"]
409 assert_equal 2, js["properties"]["comments"].count
410 assert_equal "closed", js["properties"]["comments"].last["action"]
411 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
412 assert_equal user.display_name, js["properties"]["comments"].last["user"]
414 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
415 assert_response :success
416 js = ActiveSupport::JSON.decode(@response.body)
418 assert_equal "Feature", js["type"]
419 assert_equal open_note_with_comment.id, js["properties"]["id"]
420 assert_equal "closed", js["properties"]["status"]
421 assert_equal 2, js["properties"]["comments"].count
422 assert_equal "closed", js["properties"]["comments"].last["action"]
423 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
424 assert_equal user.display_name, js["properties"]["comments"].last["user"]
429 assert_response :unauthorized
431 basic_authorization create(:user).email, "test"
434 assert_response :bad_request
436 post :close, :params => { :id => 12345 }
437 assert_response :not_found
439 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
441 post :close, :params => { :id => hidden_note_with_comment.id }
442 assert_response :gone
444 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
446 post :close, :params => { :id => closed_note_with_comment.id }
447 assert_response :conflict
450 def test_reopen_success
451 closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
454 post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
455 assert_response :unauthorized
457 basic_authorization user.email, "test"
459 post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
460 assert_response :success
461 js = ActiveSupport::JSON.decode(@response.body)
463 assert_equal "Feature", js["type"]
464 assert_equal closed_note_with_comment.id, js["properties"]["id"]
465 assert_equal "open", js["properties"]["status"]
466 assert_equal 2, js["properties"]["comments"].count
467 assert_equal "reopened", js["properties"]["comments"].last["action"]
468 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
469 assert_equal user.display_name, js["properties"]["comments"].last["user"]
471 get :show, :params => { :id => closed_note_with_comment.id, :format => "json" }
472 assert_response :success
473 js = ActiveSupport::JSON.decode(@response.body)
475 assert_equal "Feature", js["type"]
476 assert_equal closed_note_with_comment.id, js["properties"]["id"]
477 assert_equal "open", js["properties"]["status"]
478 assert_equal 2, js["properties"]["comments"].count
479 assert_equal "reopened", js["properties"]["comments"].last["action"]
480 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
481 assert_equal user.display_name, js["properties"]["comments"].last["user"]
485 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
487 post :reopen, :params => { :id => hidden_note_with_comment.id }
488 assert_response :unauthorized
490 basic_authorization create(:user).email, "test"
492 post :reopen, :params => { :id => 12345 }
493 assert_response :not_found
495 post :reopen, :params => { :id => hidden_note_with_comment.id }
496 assert_response :gone
498 open_note_with_comment = create(:note_with_comments)
500 post :reopen, :params => { :id => open_note_with_comment.id }
501 assert_response :conflict
504 def test_show_success
505 open_note = create(:note_with_comments)
507 get :show, :params => { :id => open_note.id, :format => "xml" }
508 assert_response :success
509 assert_equal "application/xml", @response.content_type
510 assert_select "osm", :count => 1 do
511 assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
512 assert_select "id", open_note.id.to_s
513 assert_select "url", note_url(open_note, :format => "xml")
514 assert_select "comment_url", comment_note_url(open_note, :format => "xml")
515 assert_select "close_url", close_note_url(open_note, :format => "xml")
516 assert_select "date_created", open_note.created_at.to_s
517 assert_select "status", open_note.status
518 assert_select "comments", :count => 1 do
519 assert_select "comment", :count => 1
524 get :show, :params => { :id => open_note.id, :format => "rss" }
525 assert_response :success
526 assert_equal "application/rss+xml", @response.content_type
527 assert_select "rss", :count => 1 do
528 assert_select "channel", :count => 1 do
529 assert_select "item", :count => 1 do
530 assert_select "link", browse_note_url(open_note)
531 assert_select "guid", note_url(open_note)
532 assert_select "pubDate", open_note.created_at.to_s(:rfc822)
533 # assert_select "geo:lat", open_note.lat.to_s
534 # assert_select "geo:long", open_note.lon
535 # assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
540 get :show, :params => { :id => open_note.id, :format => "json" }
541 assert_response :success
542 assert_equal "application/json", @response.content_type
543 js = ActiveSupport::JSON.decode(@response.body)
545 assert_equal "Feature", js["type"]
546 assert_equal "Point", js["geometry"]["type"]
547 assert_equal open_note.lat, js["geometry"]["coordinates"][0]
548 assert_equal open_note.lon, js["geometry"]["coordinates"][1]
549 assert_equal open_note.id, js["properties"]["id"]
550 assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
551 assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
552 assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
553 assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
554 assert_equal open_note.status, js["properties"]["status"]
556 get :show, :params => { :id => open_note.id, :format => "gpx" }
557 assert_response :success
558 assert_equal "application/gpx+xml", @response.content_type
559 assert_select "gpx", :count => 1 do
560 assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
561 assert_select "time", :count => 1
562 assert_select "name", "Note: #{open_note.id}"
563 assert_select "desc", :count => 1
564 assert_select "link[href='http://test.host/note/#{open_note.id}']", :count => 1
565 assert_select "extensions", :count => 1 do
566 assert_select "id", open_note.id.to_s
567 assert_select "url", note_url(open_note, :format => "gpx")
568 assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
569 assert_select "close_url", close_note_url(open_note, :format => "gpx")
575 def test_show_hidden_comment
576 note_with_hidden_comment = create(:note) do |note|
577 create(:note_comment, :note => note, :body => "Valid comment for hidden note")
578 create(:note_comment, :note => note, :visible => false)
579 create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
582 get :show, :params => { :id => note_with_hidden_comment.id, :format => "json" }
583 assert_response :success
584 js = ActiveSupport::JSON.decode(@response.body)
586 assert_equal "Feature", js["type"]
587 assert_equal note_with_hidden_comment.id, js["properties"]["id"]
588 assert_equal 2, js["properties"]["comments"].count
589 assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
590 assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
594 get :show, :params => { :id => 12345 }
595 assert_response :not_found
597 get :show, :params => { :id => create(:note, :status => "hidden").id }
598 assert_response :gone
601 def test_destroy_success
602 open_note_with_comment = create(:note_with_comments)
604 moderator_user = create(:moderator_user)
606 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
607 assert_response :unauthorized
609 basic_authorization user.email, "test"
611 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
612 assert_response :forbidden
614 basic_authorization moderator_user.email, "test"
616 delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
617 assert_response :success
618 js = ActiveSupport::JSON.decode(@response.body)
620 assert_equal "Feature", js["type"]
621 assert_equal open_note_with_comment.id, js["properties"]["id"]
622 assert_equal "hidden", js["properties"]["status"]
623 assert_equal 2, js["properties"]["comments"].count
624 assert_equal "hidden", js["properties"]["comments"].last["action"]
625 assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
626 assert_equal moderator_user.display_name, js["properties"]["comments"].last["user"]
628 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
629 assert_response :success
631 basic_authorization user.email, "test"
632 get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
633 assert_response :gone
636 def test_destroy_fail
638 moderator_user = create(:moderator_user)
640 delete :destroy, :params => { :id => 12345, :format => "json" }
641 assert_response :unauthorized
643 basic_authorization user.email, "test"
645 delete :destroy, :params => { :id => 12345, :format => "json" }
646 assert_response :forbidden
648 basic_authorization moderator_user.email, "test"
650 delete :destroy, :params => { :id => 12345, :format => "json" }
651 assert_response :not_found
653 hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
655 delete :destroy, :params => { :id => hidden_note_with_comment.id, :format => "json" }
656 assert_response :gone
659 def test_index_success
660 position = (1.1 * GeoRecord::SCALE).to_i
661 create(:note_with_comments, :latitude => position, :longitude => position)
662 create(:note_with_comments, :latitude => position, :longitude => position)
664 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "rss" }
665 assert_response :success
666 assert_equal "application/rss+xml", @response.content_type
667 assert_select "rss", :count => 1 do
668 assert_select "channel", :count => 1 do
669 assert_select "item", :count => 2
673 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "json" }
674 assert_response :success
675 assert_equal "application/json", @response.content_type
676 js = ActiveSupport::JSON.decode(@response.body)
678 assert_equal "FeatureCollection", js["type"]
679 assert_equal 2, js["features"].count
681 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "xml" }
682 assert_response :success
683 assert_equal "application/xml", @response.content_type
684 assert_select "osm", :count => 1 do
685 assert_select "note", :count => 2
688 get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "gpx" }
689 assert_response :success
690 assert_equal "application/gpx+xml", @response.content_type
691 assert_select "gpx", :count => 1 do
692 assert_select "wpt", :count => 2
697 position = (1.1 * GeoRecord::SCALE).to_i
698 create(:note_with_comments, :latitude => position, :longitude => position)
699 create(:note_with_comments, :latitude => position, :longitude => position)
701 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss" }
702 assert_response :success
703 assert_equal "application/rss+xml", @response.content_type
704 assert_select "rss", :count => 1 do
705 assert_select "channel", :count => 1 do
706 assert_select "item", :count => 1
710 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json" }
711 assert_response :success
712 assert_equal "application/json", @response.content_type
713 js = ActiveSupport::JSON.decode(@response.body)
715 assert_equal "FeatureCollection", js["type"]
716 assert_equal 1, js["features"].count
718 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml" }
719 assert_response :success
720 assert_equal "application/xml", @response.content_type
721 assert_select "osm", :count => 1 do
722 assert_select "note", :count => 1
725 get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx" }
726 assert_response :success
727 assert_equal "application/gpx+xml", @response.content_type
728 assert_select "gpx", :count => 1 do
729 assert_select "wpt", :count => 1
733 def test_index_empty_area
734 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "rss" }
735 assert_response :success
736 assert_equal "application/rss+xml", @response.content_type
737 assert_select "rss", :count => 1 do
738 assert_select "channel", :count => 1 do
739 assert_select "item", :count => 0
743 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "json" }
744 assert_response :success
745 assert_equal "application/json", @response.content_type
746 js = ActiveSupport::JSON.decode(@response.body)
748 assert_equal "FeatureCollection", js["type"]
749 assert_equal 0, js["features"].count
751 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "xml" }
752 assert_response :success
753 assert_equal "application/xml", @response.content_type
754 assert_select "osm", :count => 1 do
755 assert_select "note", :count => 0
758 get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "gpx" }
759 assert_response :success
760 assert_equal "application/gpx+xml", @response.content_type
761 assert_select "gpx", :count => 1 do
762 assert_select "wpt", :count => 0
766 def test_index_large_area
767 get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5", :format => :json }
768 assert_response :success
769 assert_equal "application/json", @response.content_type
771 get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json }
772 assert_response :success
773 assert_equal "application/json", @response.content_type
775 get :index, :params => { :bbox => "-10,-10,12,12", :format => :json }
776 assert_response :bad_request
777 assert_equal "application/json", @response.content_type
779 get :index, :params => { :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json }
780 assert_response :bad_request
781 assert_equal "application/json", @response.content_type
784 def test_index_closed
785 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 5.days)
786 create(:note_with_comments, :status => "closed", :closed_at => Time.now - 100.days)
787 create(:note_with_comments, :status => "hidden")
788 create(:note_with_comments)
790 # Open notes + closed in last 7 days
791 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json" }
792 assert_response :success
793 assert_equal "application/json", @response.content_type
794 js = ActiveSupport::JSON.decode(@response.body)
796 assert_equal "FeatureCollection", js["type"]
797 assert_equal 2, js["features"].count
800 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json" }
801 assert_response :success
802 assert_equal "application/json", @response.content_type
803 js = ActiveSupport::JSON.decode(@response.body)
805 assert_equal "FeatureCollection", js["type"]
806 assert_equal 1, js["features"].count
808 # Open notes + all closed notes
809 get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json" }
810 assert_response :success
811 assert_equal "application/json", @response.content_type
812 js = ActiveSupport::JSON.decode(@response.body)
814 assert_equal "FeatureCollection", js["type"]
815 assert_equal 3, js["features"].count
818 def test_index_bad_params
819 get :index, :params => { :bbox => "-2.5,-2.5,2.5" }
820 assert_response :bad_request
822 get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5,2.5" }
823 assert_response :bad_request
825 get :index, :params => { :b => "-2.5", :r => "2.5", :t => "2.5" }
826 assert_response :bad_request
828 get :index, :params => { :l => "-2.5", :r => "2.5", :t => "2.5" }
829 assert_response :bad_request
831 get :index, :params => { :l => "-2.5", :b => "-2.5", :t => "2.5" }
832 assert_response :bad_request
834 get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5" }
835 assert_response :bad_request
837 get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json" }
838 assert_response :bad_request
840 get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json" }
841 assert_response :bad_request
844 def test_search_success
845 create(:note_with_comments)
847 get :search, :params => { :q => "note comment", :format => "xml" }
848 assert_response :success
849 assert_equal "application/xml", @response.content_type
850 assert_select "osm", :count => 1 do
851 assert_select "note", :count => 1
854 get :search, :params => { :q => "note comment", :format => "json" }
855 assert_response :success
856 assert_equal "application/json", @response.content_type
857 js = ActiveSupport::JSON.decode(@response.body)
859 assert_equal "FeatureCollection", js["type"]
860 assert_equal 1, js["features"].count
862 get :search, :params => { :q => "note comment", :format => "rss" }
863 assert_response :success
864 assert_equal "application/rss+xml", @response.content_type
865 assert_select "rss", :count => 1 do
866 assert_select "channel", :count => 1 do
867 assert_select "item", :count => 1
871 get :search, :params => { :q => "note comment", :format => "gpx" }
872 assert_response :success
873 assert_equal "application/gpx+xml", @response.content_type
874 assert_select "gpx", :count => 1 do
875 assert_select "wpt", :count => 1
879 def test_search_no_match
880 create(:note_with_comments)
882 get :search, :params => { :q => "no match", :format => "xml" }
883 assert_response :success
884 assert_equal "application/xml", @response.content_type
885 assert_select "osm", :count => 1 do
886 assert_select "note", :count => 0
889 get :search, :params => { :q => "no match", :format => "json" }
890 assert_response :success
891 assert_equal "application/json", @response.content_type
892 js = ActiveSupport::JSON.decode(@response.body)
894 assert_equal "FeatureCollection", js["type"]
895 assert_equal 0, js["features"].count
897 get :search, :params => { :q => "no match", :format => "rss" }
898 assert_response :success
899 assert_equal "application/rss+xml", @response.content_type
900 assert_select "rss", :count => 1 do
901 assert_select "channel", :count => 1 do
902 assert_select "item", :count => 0
906 get :search, :params => { :q => "no match", :format => "gpx" }
907 assert_response :success
908 assert_equal "application/gpx+xml", @response.content_type
909 assert_select "gpx", :count => 1 do
910 assert_select "wpt", :count => 0
914 def test_search_bad_params
916 assert_response :bad_request
918 get :search, :params => { :q => "no match", :limit => "0", :format => "json" }
919 assert_response :bad_request
921 get :search, :params => { :q => "no match", :limit => "10001", :format => "json" }
922 assert_response :bad_request
925 def test_feed_success
926 position = (1.1 * GeoRecord::SCALE).to_i
927 create(:note_with_comments, :latitude => position, :longitude => position)
928 create(:note_with_comments, :latitude => position, :longitude => position)
929 position = (1.5 * GeoRecord::SCALE).to_i
930 create(:note_with_comments, :latitude => position, :longitude => position)
931 create(:note_with_comments, :latitude => position, :longitude => position)
933 get :feed, :params => { :format => "rss" }
934 assert_response :success
935 assert_equal "application/rss+xml", @response.content_type
936 assert_select "rss", :count => 1 do
937 assert_select "channel", :count => 1 do
938 assert_select "item", :count => 4
942 get :feed, :params => { :bbox => "1,1,1.2,1.2", :format => "rss" }
943 assert_response :success
944 assert_equal "application/rss+xml", @response.content_type
945 assert_select "rss", :count => 1 do
946 assert_select "channel", :count => 1 do
947 assert_select "item", :count => 2
953 get :feed, :params => { :bbox => "1,1,1.2", :format => "rss" }
954 assert_response :bad_request
956 get :feed, :params => { :bbox => "1,1,1.2,1.2,1.2", :format => "rss" }
957 assert_response :bad_request
959 get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss" }
960 assert_response :bad_request
962 get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss" }
963 assert_response :bad_request
966 def test_mine_success
967 first_user = create(:user)
968 second_user = create(:user)
969 moderator_user = create(:moderator_user)
971 create(:note) do |note|
972 create(:note_comment, :note => note, :author => first_user)
974 create(:note) do |note|
975 create(:note_comment, :note => note, :author => second_user)
977 create(:note, :status => "hidden") do |note|
978 create(:note_comment, :note => note, :author => second_user)
981 # Note that the table rows include a header row
982 get :mine, :params => { :display_name => first_user.display_name }
983 assert_response :success
984 assert_select "table.note_list tr", :count => 2
986 get :mine, :params => { :display_name => second_user.display_name }
987 assert_response :success
988 assert_select "table.note_list tr", :count => 2
990 get :mine, :params => { :display_name => "non-existent" }
991 assert_response :not_found
993 session[:user] = moderator_user.id
995 get :mine, :params => { :display_name => first_user.display_name }
996 assert_response :success
997 assert_select "table.note_list tr", :count => 2
999 get :mine, :params => { :display_name => second_user.display_name }
1000 assert_response :success
1001 assert_select "table.note_list tr", :count => 3
1003 get :mine, :params => { :display_name => "non-existent" }
1004 assert_response :not_found
1008 user = create(:user)
1010 create_list(:note, 50) do |note|
1011 create(:note_comment, :note => note, :author => user)
1014 get :mine, :params => { :display_name => user.display_name }
1015 assert_response :success
1016 assert_select "table.note_list tr", :count => 11
1018 get :mine, :params => { :display_name => user.display_name, :page => 2 }
1019 assert_response :success
1020 assert_select "table.note_list tr", :count => 11