3 class NotesControllerTest < ActionController::TestCase
4 fixtures :users, :user_roles
7 # test all routes which lead to this controller
10 { :path => "/api/0.6/notes", :method => :post },
11 { :controller => "notes", :action => "create", :format => "xml" }
14 { :path => "/api/0.6/notes/1", :method => :get },
15 { :controller => "notes", :action => "show", :id => "1", :format => "xml" }
18 { :controller => "notes", :action => "show", :id => "1", :format => "xml" },
19 { :path => "/api/0.6/notes/1.xml", :method => :get }
22 { :path => "/api/0.6/notes/1.rss", :method => :get },
23 { :controller => "notes", :action => "show", :id => "1", :format => "rss" }
26 { :path => "/api/0.6/notes/1.json", :method => :get },
27 { :controller => "notes", :action => "show", :id => "1", :format => "json" }
30 { :path => "/api/0.6/notes/1.gpx", :method => :get },
31 { :controller => "notes", :action => "show", :id => "1", :format => "gpx" }
34 { :path => "/api/0.6/notes/1/comment", :method => :post },
35 { :controller => "notes", :action => "comment", :id => "1", :format => "xml" }
38 { :path => "/api/0.6/notes/1/close", :method => :post },
39 { :controller => "notes", :action => "close", :id => "1", :format => "xml" }
42 { :path => "/api/0.6/notes/1/reopen", :method => :post },
43 { :controller => "notes", :action => "reopen", :id => "1", :format => "xml" }
46 { :path => "/api/0.6/notes/1", :method => :delete },
47 { :controller => "notes", :action => "destroy", :id => "1", :format => "xml" }
51 { :path => "/api/0.6/notes", :method => :get },
52 { :controller => "notes", :action => "index", :format => "xml" }
55 { :controller => "notes", :action => "index", :format => "xml" },
56 { :path => "/api/0.6/notes.xml", :method => :get }
59 { :path => "/api/0.6/notes.rss", :method => :get },
60 { :controller => "notes", :action => "index", :format => "rss" }
63 { :path => "/api/0.6/notes.json", :method => :get },
64 { :controller => "notes", :action => "index", :format => "json" }
67 { :path => "/api/0.6/notes.gpx", :method => :get },
68 { :controller => "notes", :action => "index", :format => "gpx" }
72 { :path => "/api/0.6/notes/search", :method => :get },
73 { :controller => "notes", :action => "search", :format => "xml" }
76 { :controller => "notes", :action => "search", :format => "xml" },
77 { :path => "/api/0.6/notes/search.xml", :method => :get }
80 { :path => "/api/0.6/notes/search.rss", :method => :get },
81 { :controller => "notes", :action => "search", :format => "rss" }
84 { :path => "/api/0.6/notes/search.json", :method => :get },
85 { :controller => "notes", :action => "search", :format => "json" }
88 { :path => "/api/0.6/notes/search.gpx", :method => :get },
89 { :controller => "notes", :action => "search", :format => "gpx" }
93 { :path => "/api/0.6/notes/feed", :method => :get },
94 { :controller => "notes", :action => "feed", :format => "rss" }
98 { :controller => "notes", :action => "create" },
99 { :path => "/api/0.6/notes/addPOIexec", :method => :post }
102 { :controller => "notes", :action => "close" },
103 { :path => "/api/0.6/notes/closePOIexec", :method => :post }
106 { :controller => "notes", :action => "comment" },
107 { :path => "/api/0.6/notes/editPOIexec", :method => :post }
110 { :controller => "notes", :action => "index", :format => "gpx" },
111 { :path => "/api/0.6/notes/getGPX", :method => :get }
114 { :controller => "notes", :action => "feed", :format => "rss" },
115 { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
119 { :path => "/user/username/notes", :method => :get },
120 { :controller => "notes", :action => "mine", :display_name => "username" }
124 def test_create_success
125 assert_difference "Note.count", 1 do
126 assert_difference "NoteComment.count", 1 do
127 post :create, :lat => -1.0, :lon => -1.0, :text => "This is a comment", :format => "json"
130 assert_response :success
131 js = ActiveSupport::JSON.decode(@response.body)
133 assert_equal "Feature", js["type"]
134 assert_equal "Point", js["geometry"]["type"]
135 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
136 assert_equal "open", js["properties"]["status"]
137 assert_equal 1, js["properties"]["comments"].count
138 assert_equal "opened", js["properties"]["comments"].last["action"]
139 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
140 assert_nil js["properties"]["comments"].last["user"]
141 id = js["properties"]["id"]
143 get :show, :id => id, :format => "json"
144 assert_response :success
145 js = ActiveSupport::JSON.decode(@response.body)
147 assert_equal "Feature", js["type"]
148 assert_equal "Point", js["geometry"]["type"]
149 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
150 assert_equal id, js["properties"]["id"]
151 assert_equal "open", js["properties"]["status"]
152 assert_equal 1, js["properties"]["comments"].count
153 assert_equal "opened", js["properties"]["comments"].last["action"]
154 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
155 assert_nil js["properties"]["comments"].last["user"]
159 assert_no_difference "Note.count" do
160 assert_no_difference "NoteComment.count" do
161 post :create, :lon => -1.0, :text => "This is a comment"
164 assert_response :bad_request
166 assert_no_difference "Note.count" do
167 assert_no_difference "NoteComment.count" do
168 post :create, :lat => -1.0, :text => "This is a comment"
171 assert_response :bad_request
173 assert_no_difference "Note.count" do
174 assert_no_difference "NoteComment.count" do
175 post :create, :lat => -1.0, :lon => -1.0
178 assert_response :bad_request
180 assert_no_difference "Note.count" do
181 assert_no_difference "NoteComment.count" do
182 post :create, :lat => -1.0, :lon => -1.0, :text => ""
185 assert_response :bad_request
187 assert_no_difference "Note.count" do
188 assert_no_difference "NoteComment.count" do
189 post :create, :lat => -100.0, :lon => -1.0, :text => "This is a comment"
192 assert_response :bad_request
194 assert_no_difference "Note.count" do
195 assert_no_difference "NoteComment.count" do
196 post :create, :lat => -1.0, :lon => -200.0, :text => "This is a comment"
199 assert_response :bad_request
201 assert_no_difference "Note.count" do
202 assert_no_difference "NoteComment.count" do
203 post :create, :lat => "abc", :lon => -1.0, :text => "This is a comment"
206 assert_response :bad_request
208 assert_no_difference "Note.count" do
209 assert_no_difference "NoteComment.count" do
210 post :create, :lat => -1.0, :lon => "abc", :text => "This is a comment"
213 assert_response :bad_request
216 def test_comment_success
217 open_note_with_comment = create(:note)
218 create(:note_comment, :note => open_note_with_comment)
219 assert_difference "NoteComment.count", 1 do
220 assert_no_difference "ActionMailer::Base.deliveries.size" do
221 post :comment, :id => open_note_with_comment.id, :text => "This is an additional comment", :format => "json"
224 assert_response :success
225 js = ActiveSupport::JSON.decode(@response.body)
227 assert_equal "Feature", js["type"]
228 assert_equal open_note_with_comment.id, js["properties"]["id"]
229 assert_equal "open", js["properties"]["status"]
230 assert_equal 2, js["properties"]["comments"].count
231 assert_equal "commented", js["properties"]["comments"].last["action"]
232 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
233 assert_nil js["properties"]["comments"].last["user"]
235 get :show, :id => open_note_with_comment.id, :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 # Ensure that emails are sent to users
248 note_with_comments_by_users = create(:note)
249 create(:note_comment, :note => note_with_comments_by_users, :author_id => users(:normal_user).id)
250 create(:note_comment, :note => note_with_comments_by_users, :author_id => users(:second_public_user).id)
251 assert_difference "NoteComment.count", 1 do
252 assert_difference "ActionMailer::Base.deliveries.size", 2 do
253 post :comment, :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json"
256 assert_response :success
257 js = ActiveSupport::JSON.decode(@response.body)
259 assert_equal "Feature", js["type"]
260 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
261 assert_equal "open", js["properties"]["status"]
262 assert_equal 3, js["properties"]["comments"].count
263 assert_equal "commented", js["properties"]["comments"].last["action"]
264 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
265 assert_nil js["properties"]["comments"].last["user"]
267 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "test@openstreetmap.org" }
269 assert_equal 1, email.to.length
270 assert_equal "[OpenStreetMap] An anonymous user has commented on one of your notes", email.subject
272 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "public@OpenStreetMap.org" }
274 assert_equal 1, email.to.length
275 assert_equal "[OpenStreetMap] An anonymous user has commented on a note you are interested in", email.subject
277 get :show, :id => note_with_comments_by_users.id, :format => "json"
278 assert_response :success
279 js = ActiveSupport::JSON.decode(@response.body)
281 assert_equal "Feature", js["type"]
282 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
283 assert_equal "open", js["properties"]["status"]
284 assert_equal 3, js["properties"]["comments"].count
285 assert_equal "commented", js["properties"]["comments"].last["action"]
286 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
287 assert_nil js["properties"]["comments"].last["user"]
289 ActionMailer::Base.deliveries.clear
291 basic_authorization(users(:public_user).email, "test")
293 assert_difference "NoteComment.count", 1 do
294 assert_difference "ActionMailer::Base.deliveries.size", 2 do
295 post :comment, :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json"
298 assert_response :success
299 js = ActiveSupport::JSON.decode(@response.body)
301 assert_equal "Feature", js["type"]
302 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
303 assert_equal "open", js["properties"]["status"]
304 assert_equal 4, js["properties"]["comments"].count
305 assert_equal "commented", js["properties"]["comments"].last["action"]
306 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
307 assert_equal "test2", js["properties"]["comments"].last["user"]
309 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "test@openstreetmap.org" }
311 assert_equal 1, email.to.length
312 assert_equal "[OpenStreetMap] test2 has commented on one of your notes", email.subject
313 assert_equal "test@openstreetmap.org", email.to.first
315 email = ActionMailer::Base.deliveries.find { |e| e.to.first == "public@OpenStreetMap.org" }
317 assert_equal 1, email.to.length
318 assert_equal "[OpenStreetMap] test2 has commented on a note you are interested in", email.subject
320 get :show, :id => note_with_comments_by_users.id, :format => "json"
321 assert_response :success
322 js = ActiveSupport::JSON.decode(@response.body)
324 assert_equal "Feature", js["type"]
325 assert_equal note_with_comments_by_users.id, js["properties"]["id"]
326 assert_equal "open", js["properties"]["status"]
327 assert_equal 4, js["properties"]["comments"].count
328 assert_equal "commented", js["properties"]["comments"].last["action"]
329 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
330 assert_equal "test2", js["properties"]["comments"].last["user"]
332 ActionMailer::Base.deliveries.clear
335 def test_comment_fail
336 open_note_with_comment = create(:note_comment).note
337 assert_no_difference "NoteComment.count" do
338 post :comment, :text => "This is an additional comment"
340 assert_response :bad_request
342 assert_no_difference "NoteComment.count" do
343 post :comment, :id => open_note_with_comment.id
345 assert_response :bad_request
347 assert_no_difference "NoteComment.count" do
348 post :comment, :id => open_note_with_comment.id, :text => ""
350 assert_response :bad_request
352 assert_no_difference "NoteComment.count" do
353 post :comment, :id => 12345, :text => "This is an additional comment"
355 assert_response :not_found
357 hidden_note_with_comment = create(:note, :status => "hidden")
358 create(:note_comment, :note => hidden_note_with_comment)
359 assert_no_difference "NoteComment.count" do
360 post :comment, :id => hidden_note_with_comment.id, :text => "This is an additional comment"
362 assert_response :gone
364 closed_note_with_comment = create(:note, :status => "closed", :closed_at => Time.now)
365 create(:note_comment, :note => closed_note_with_comment)
366 assert_no_difference "NoteComment.count" do
367 post :comment, :id => closed_note_with_comment.id, :text => "This is an additional comment"
369 assert_response :conflict
372 def test_close_success
373 open_note_with_comment = create(:note)
374 create(:note_comment, :note => open_note_with_comment)
375 post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
376 assert_response :unauthorized
378 basic_authorization(users(:public_user).email, "test")
380 post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
381 assert_response :success
382 js = ActiveSupport::JSON.decode(@response.body)
384 assert_equal "Feature", js["type"]
385 assert_equal open_note_with_comment.id, js["properties"]["id"]
386 assert_equal "closed", js["properties"]["status"]
387 assert_equal 2, js["properties"]["comments"].count
388 assert_equal "closed", js["properties"]["comments"].last["action"]
389 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
390 assert_equal "test2", js["properties"]["comments"].last["user"]
392 get :show, :id => open_note_with_comment.id, :format => "json"
393 assert_response :success
394 js = ActiveSupport::JSON.decode(@response.body)
396 assert_equal "Feature", js["type"]
397 assert_equal open_note_with_comment.id, js["properties"]["id"]
398 assert_equal "closed", js["properties"]["status"]
399 assert_equal 2, js["properties"]["comments"].count
400 assert_equal "closed", js["properties"]["comments"].last["action"]
401 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
402 assert_equal "test2", js["properties"]["comments"].last["user"]
407 assert_response :unauthorized
409 basic_authorization(users(:public_user).email, "test")
412 assert_response :bad_request
414 post :close, :id => 12345
415 assert_response :not_found
417 hidden_note_with_comment = create(:note, :status => "hidden")
418 create(:note_comment, :note => hidden_note_with_comment)
419 post :close, :id => hidden_note_with_comment.id
420 assert_response :gone
422 closed_note_with_comment = create(:note, :status => "closed", :closed_at => Time.now)
423 post :close, :id => closed_note_with_comment.id
424 assert_response :conflict
427 def test_reopen_success
428 closed_note_with_comment = create(:note, :status => "closed", :closed_at => Time.now)
429 create(:note_comment, :note => closed_note_with_comment)
430 post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
431 assert_response :unauthorized
433 basic_authorization(users(:public_user).email, "test")
435 post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
436 assert_response :success
437 js = ActiveSupport::JSON.decode(@response.body)
439 assert_equal "Feature", js["type"]
440 assert_equal closed_note_with_comment.id, js["properties"]["id"]
441 assert_equal "open", js["properties"]["status"]
442 assert_equal 2, js["properties"]["comments"].count
443 assert_equal "reopened", js["properties"]["comments"].last["action"]
444 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
445 assert_equal "test2", js["properties"]["comments"].last["user"]
447 get :show, :id => closed_note_with_comment.id, :format => "json"
448 assert_response :success
449 js = ActiveSupport::JSON.decode(@response.body)
451 assert_equal "Feature", js["type"]
452 assert_equal closed_note_with_comment.id, js["properties"]["id"]
453 assert_equal "open", js["properties"]["status"]
454 assert_equal 2, js["properties"]["comments"].count
455 assert_equal "reopened", js["properties"]["comments"].last["action"]
456 assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
457 assert_equal "test2", js["properties"]["comments"].last["user"]
461 hidden_note_with_comment = create(:note, :status => "hidden")
462 create(:note_comment, :note => hidden_note_with_comment)
463 post :reopen, :id => hidden_note_with_comment.id
464 assert_response :unauthorized
466 basic_authorization(users(:public_user).email, "test")
468 post :reopen, :id => 12345
469 assert_response :not_found
471 post :reopen, :id => hidden_note_with_comment.id
472 assert_response :gone
474 open_note_with_comment = create(:note)
475 create(:note_comment, :note => open_note_with_comment)
476 post :reopen, :id => open_note_with_comment.id
477 assert_response :conflict
480 def test_show_success
481 open_note = create(:note)
482 create(:note_comment, :note => open_note)
483 get :show, :id => open_note.id, :format => "xml"
484 assert_response :success
485 assert_equal "application/xml", @response.content_type
486 assert_select "osm", :count => 1 do
487 assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
488 assert_select "id", open_note.id.to_s
489 assert_select "url", note_url(open_note, :format => "xml")
490 assert_select "comment_url", comment_note_url(open_note, :format => "xml")
491 assert_select "close_url", close_note_url(open_note, :format => "xml")
492 assert_select "date_created", open_note.created_at.to_s
493 assert_select "status", open_note.status
494 assert_select "comments", :count => 1 do
495 assert_select "comment", :count => 1
500 get :show, :id => open_note.id, :format => "rss"
501 assert_response :success
502 assert_equal "application/rss+xml", @response.content_type
503 assert_select "rss", :count => 1 do
504 assert_select "channel", :count => 1 do
505 assert_select "item", :count => 1 do
506 assert_select "link", browse_note_url(open_note)
507 assert_select "guid", note_url(open_note)
508 assert_select "pubDate", open_note.created_at.to_s(:rfc822)
509 # assert_select "geo:lat", open_note.lat.to_s
510 # assert_select "geo:long", open_note.lon
511 # assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
516 get :show, :id => open_note.id, :format => "json"
517 assert_response :success
518 assert_equal "application/json", @response.content_type
519 js = ActiveSupport::JSON.decode(@response.body)
521 assert_equal "Feature", js["type"]
522 assert_equal "Point", js["geometry"]["type"]
523 assert_equal open_note.lat, js["geometry"]["coordinates"][0]
524 assert_equal open_note.lon, js["geometry"]["coordinates"][1]
525 assert_equal open_note.id, js["properties"]["id"]
526 assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
527 assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
528 assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
529 assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
530 assert_equal open_note.status, js["properties"]["status"]
532 get :show, :id => open_note.id, :format => "gpx"
533 assert_response :success
534 assert_equal "application/gpx+xml", @response.content_type
535 assert_select "gpx", :count => 1 do
536 assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
537 assert_select "time", :count => 1
538 assert_select "name", "Note: #{open_note.id}"
539 assert_select "desc", :count => 1
540 assert_select "link[href='http://www.openstreetmap.org/note/#{open_note.id}']", :count => 1
541 assert_select "extensions", :count => 1 do
542 assert_select "id", open_note.id.to_s
543 assert_select "url", note_url(open_note, :format => "gpx")
544 assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
545 assert_select "close_url", close_note_url(open_note, :format => "gpx")
551 def test_show_hidden_comment
552 note_with_hidden_comment = create(:note)
553 create(:note_comment, :note => note_with_hidden_comment, :body => "Valid comment for hidden note")
554 create(:note_comment, :note => note_with_hidden_comment, :visible => false)
555 create(:note_comment, :note => note_with_hidden_comment, :body => "Another valid comment for hidden note")
557 get :show, :id => note_with_hidden_comment.id, :format => "json"
558 assert_response :success
559 js = ActiveSupport::JSON.decode(@response.body)
561 assert_equal "Feature", js["type"]
562 assert_equal note_with_hidden_comment.id, js["properties"]["id"]
563 assert_equal 2, js["properties"]["comments"].count
564 assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
565 assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
569 get :show, :id => 12345
570 assert_response :not_found
572 get :show, :id => create(:note, :status => "hidden").id
573 assert_response :gone
576 def test_destroy_success
577 open_note_with_comment = create(:note)
578 create(:note_comment, :note => open_note_with_comment)
579 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
580 assert_response :unauthorized
582 basic_authorization(users(:public_user).email, "test")
584 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
585 assert_response :forbidden
587 basic_authorization(users(:moderator_user).email, "test")
589 delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
590 assert_response :success
591 js = ActiveSupport::JSON.decode(@response.body)
593 assert_equal "Feature", js["type"]
594 assert_equal open_note_with_comment.id, js["properties"]["id"]
595 assert_equal "hidden", js["properties"]["status"]
596 assert_equal 2, js["properties"]["comments"].count
597 assert_equal "hidden", js["properties"]["comments"].last["action"]
598 assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
599 assert_equal "moderator", js["properties"]["comments"].last["user"]
601 get :show, :id => open_note_with_comment.id, :format => "json"
602 assert_response :gone
605 def test_destroy_fail
606 delete :destroy, :id => 12345, :format => "json"
607 assert_response :unauthorized
609 basic_authorization(users(:public_user).email, "test")
611 delete :destroy, :id => 12345, :format => "json"
612 assert_response :forbidden
614 basic_authorization(users(:moderator_user).email, "test")
616 delete :destroy, :id => 12345, :format => "json"
617 assert_response :not_found
619 hidden_note_with_comment = create(:note, :status => "hidden")
620 create(:note_comment, :note => hidden_note_with_comment)
621 delete :destroy, :id => hidden_note_with_comment.id, :format => "json"
622 assert_response :gone
625 def test_index_success
626 position = (1.1 * GeoRecord::SCALE).to_i
627 note = create(:note, :latitude => position, :longitude => position)
628 note2 = create(:note, :latitude => position, :longitude => position)
629 create(:note_comment, :note => note)
630 create(:note_comment, :note => note2)
632 get :index, :bbox => "1,1,1.2,1.2", :format => "rss"
633 assert_response :success
634 assert_equal "application/rss+xml", @response.content_type
635 assert_select "rss", :count => 1 do
636 assert_select "channel", :count => 1 do
637 assert_select "item", :count => 2
641 get :index, :bbox => "1,1,1.2,1.2", :format => "json"
642 assert_response :success
643 assert_equal "application/json", @response.content_type
644 js = ActiveSupport::JSON.decode(@response.body)
646 assert_equal "FeatureCollection", js["type"]
647 assert_equal 2, js["features"].count
649 get :index, :bbox => "1,1,1.2,1.2", :format => "xml"
650 assert_response :success
651 assert_equal "application/xml", @response.content_type
652 assert_select "osm", :count => 1 do
653 assert_select "note", :count => 2
656 get :index, :bbox => "1,1,1.2,1.2", :format => "gpx"
657 assert_response :success
658 assert_equal "application/gpx+xml", @response.content_type
659 assert_select "gpx", :count => 1 do
660 assert_select "wpt", :count => 2
665 position = (1.1 * GeoRecord::SCALE).to_i
666 note = create(:note, :latitude => position, :longitude => position)
667 note2 = create(:note, :latitude => position, :longitude => position)
668 create(:note_comment, :note => note)
669 create(:note_comment, :note => note2)
671 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss"
672 assert_response :success
673 assert_equal "application/rss+xml", @response.content_type
674 assert_select "rss", :count => 1 do
675 assert_select "channel", :count => 1 do
676 assert_select "item", :count => 1
680 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json"
681 assert_response :success
682 assert_equal "application/json", @response.content_type
683 js = ActiveSupport::JSON.decode(@response.body)
685 assert_equal "FeatureCollection", js["type"]
686 assert_equal 1, js["features"].count
688 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml"
689 assert_response :success
690 assert_equal "application/xml", @response.content_type
691 assert_select "osm", :count => 1 do
692 assert_select "note", :count => 1
695 get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx"
696 assert_response :success
697 assert_equal "application/gpx+xml", @response.content_type
698 assert_select "gpx", :count => 1 do
699 assert_select "wpt", :count => 1
703 def test_index_empty_area
704 get :index, :bbox => "5,5,5.1,5.1", :format => "rss"
705 assert_response :success
706 assert_equal "application/rss+xml", @response.content_type
707 assert_select "rss", :count => 1 do
708 assert_select "channel", :count => 1 do
709 assert_select "item", :count => 0
713 get :index, :bbox => "5,5,5.1,5.1", :format => "json"
714 assert_response :success
715 assert_equal "application/json", @response.content_type
716 js = ActiveSupport::JSON.decode(@response.body)
718 assert_equal "FeatureCollection", js["type"]
719 assert_equal 0, js["features"].count
721 get :index, :bbox => "5,5,5.1,5.1", :format => "xml"
722 assert_response :success
723 assert_equal "application/xml", @response.content_type
724 assert_select "osm", :count => 1 do
725 assert_select "note", :count => 0
728 get :index, :bbox => "5,5,5.1,5.1", :format => "gpx"
729 assert_response :success
730 assert_equal "application/gpx+xml", @response.content_type
731 assert_select "gpx", :count => 1 do
732 assert_select "wpt", :count => 0
736 def test_index_large_area
737 get :index, :bbox => "-2.5,-2.5,2.5,2.5", :format => :json
738 assert_response :success
739 assert_equal "application/json", @response.content_type
741 get :index, :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json
742 assert_response :success
743 assert_equal "application/json", @response.content_type
745 get :index, :bbox => "-10,-10,12,12", :format => :json
746 assert_response :bad_request
747 assert_equal "text/plain", @response.content_type
749 get :index, :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json
750 assert_response :bad_request
751 assert_equal "text/plain", @response.content_type
754 def test_index_closed
755 old_closed_note = create(:note, :status => "closed", :closed_at => Time.now - 5.days)
756 very_old_closed_note = create(:note, :status => "closed", :closed_at => Time.now - 100.days)
757 hidden_note = create(:note, :status => "hidden")
758 open_note = create(:note)
759 create(:note_comment, :note => old_closed_note)
760 create(:note_comment, :note => very_old_closed_note)
761 create(:note_comment, :note => hidden_note)
762 create(:note_comment, :note => open_note)
764 # Open notes + closed in last 7 days
765 get :index, :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json"
766 assert_response :success
767 assert_equal "application/json", @response.content_type
768 js = ActiveSupport::JSON.decode(@response.body)
770 assert_equal "FeatureCollection", js["type"]
771 assert_equal 2, js["features"].count
774 get :index, :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json"
775 assert_response :success
776 assert_equal "application/json", @response.content_type
777 js = ActiveSupport::JSON.decode(@response.body)
779 assert_equal "FeatureCollection", js["type"]
780 assert_equal 1, js["features"].count
782 # Open notes + all closed notes
783 get :index, :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json"
784 assert_response :success
785 assert_equal "application/json", @response.content_type
786 js = ActiveSupport::JSON.decode(@response.body)
788 assert_equal "FeatureCollection", js["type"]
789 assert_equal 3, js["features"].count
792 def test_index_bad_params
793 get :index, :bbox => "-2.5,-2.5,2.5"
794 assert_response :bad_request
796 get :index, :bbox => "-2.5,-2.5,2.5,2.5,2.5"
797 assert_response :bad_request
799 get :index, :b => "-2.5", :r => "2.5", :t => "2.5"
800 assert_response :bad_request
802 get :index, :l => "-2.5", :r => "2.5", :t => "2.5"
803 assert_response :bad_request
805 get :index, :l => "-2.5", :b => "-2.5", :t => "2.5"
806 assert_response :bad_request
808 get :index, :l => "-2.5", :b => "-2.5", :r => "2.5"
809 assert_response :bad_request
811 get :index, :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json"
812 assert_response :bad_request
814 get :index, :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json"
815 assert_response :bad_request
818 def test_search_success
820 create(:note_comment, :note => note, :body => "comment for note 1")
822 get :search, :q => "note 1", :format => "xml"
823 assert_response :success
824 assert_equal "application/xml", @response.content_type
825 assert_select "osm", :count => 1 do
826 assert_select "note", :count => 1
829 get :search, :q => "note 1", :format => "json"
830 assert_response :success
831 assert_equal "application/json", @response.content_type
832 js = ActiveSupport::JSON.decode(@response.body)
834 assert_equal "FeatureCollection", js["type"]
835 assert_equal 1, js["features"].count
837 get :search, :q => "note 1", :format => "rss"
838 assert_response :success
839 assert_equal "application/rss+xml", @response.content_type
840 assert_select "rss", :count => 1 do
841 assert_select "channel", :count => 1 do
842 assert_select "item", :count => 1
846 get :search, :q => "note 1", :format => "gpx"
847 assert_response :success
848 assert_equal "application/gpx+xml", @response.content_type
849 assert_select "gpx", :count => 1 do
850 assert_select "wpt", :count => 1
854 def test_search_no_match
856 create(:note_comment, :note => note, :body => "comment for node 1")
858 get :search, :q => "no match", :format => "xml"
859 assert_response :success
860 assert_equal "application/xml", @response.content_type
861 assert_select "osm", :count => 1 do
862 assert_select "note", :count => 0
865 get :search, :q => "no match", :format => "json"
866 assert_response :success
867 assert_equal "application/json", @response.content_type
868 js = ActiveSupport::JSON.decode(@response.body)
870 assert_equal "FeatureCollection", js["type"]
871 assert_equal 0, js["features"].count
873 get :search, :q => "no match", :format => "rss"
874 assert_response :success
875 assert_equal "application/rss+xml", @response.content_type
876 assert_select "rss", :count => 1 do
877 assert_select "channel", :count => 1 do
878 assert_select "item", :count => 0
882 get :search, :q => "no match", :format => "gpx"
883 assert_response :success
884 assert_equal "application/gpx+xml", @response.content_type
885 assert_select "gpx", :count => 1 do
886 assert_select "wpt", :count => 0
890 def test_search_bad_params
892 assert_response :bad_request
894 get :search, :q => "no match", :limit => "0", :format => "json"
895 assert_response :bad_request
897 get :search, :q => "no match", :limit => "10001", :format => "json"
898 assert_response :bad_request
901 def test_feed_success
902 position = (1.1 * GeoRecord::SCALE).to_i
903 note = create(:note, :latitude => position, :longitude => position)
904 note2 = create(:note, :latitude => position, :longitude => position)
905 create(:note_comment, :note => note)
906 create(:note_comment, :note => note2)
907 position = (1.5 * GeoRecord::SCALE).to_i
908 note3 = create(:note, :latitude => position, :longitude => position)
909 note4 = create(:note, :latitude => position, :longitude => position)
910 create(:note_comment, :note => note3)
911 create(:note_comment, :note => note4)
913 get :feed, :format => "rss"
914 assert_response :success
915 assert_equal "application/rss+xml", @response.content_type
916 assert_select "rss", :count => 1 do
917 assert_select "channel", :count => 1 do
918 assert_select "item", :count => 4
922 get :feed, :bbox => "1,1,1.2,1.2", :format => "rss"
923 assert_response :success
924 assert_equal "application/rss+xml", @response.content_type
925 assert_select "rss", :count => 1 do
926 assert_select "channel", :count => 1 do
927 assert_select "item", :count => 2
933 get :feed, :bbox => "1,1,1.2", :format => "rss"
934 assert_response :bad_request
936 get :feed, :bbox => "1,1,1.2,1.2,1.2", :format => "rss"
937 assert_response :bad_request
939 get :feed, :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss"
940 assert_response :bad_request
942 get :feed, :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss"
943 assert_response :bad_request
946 def test_mine_success
948 create(:note_comment, :note => note, :author_id => users(:normal_user).id)
949 note2 = create(:note)
950 create(:note_comment, :note => note2, :author_id => users(:second_public_user).id)
951 hidden_note = create(:note, :status => "hidden")
952 create(:note_comment, :note => hidden_note, :author_id => users(:second_public_user).id)
954 # Note that the table rows include a header row
955 get :mine, :display_name => "test"
956 assert_response :success
957 assert_select "table.note_list tr", :count => 2
959 get :mine, :display_name => "pulibc_test2"
960 assert_response :success
961 assert_select "table.note_list tr", :count => 2
963 get :mine, :display_name => "non-existent"
964 assert_response :not_found
966 session[:user] = users(:moderator_user).id
968 get :mine, :display_name => "test"
969 assert_response :success
970 assert_select "table.note_list tr", :count => 2
972 get :mine, :display_name => "pulibc_test2"
973 assert_response :success
974 assert_select "table.note_list tr", :count => 3
976 get :mine, :display_name => "non-existent"
977 assert_response :not_found