1 require File.dirname(__FILE__) + '/../test_helper'
3 class NotesControllerTest < ActionController::TestCase
4 fixtures :users, :notes, :note_comments
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", :method => :delete },
43 { :controller => "notes", :action => "destroy", :id => "1", :format => "xml" }
47 { :path => "/api/0.6/notes", :method => :get },
48 { :controller => "notes", :action => "index", :format => "xml" }
51 { :controller => "notes", :action => "index", :format => "xml" },
52 { :path => "/api/0.6/notes.xml", :method => :get }
55 { :path => "/api/0.6/notes.rss", :method => :get },
56 { :controller => "notes", :action => "index", :format => "rss" }
59 { :path => "/api/0.6/notes.json", :method => :get },
60 { :controller => "notes", :action => "index", :format => "json" }
63 { :path => "/api/0.6/notes.gpx", :method => :get },
64 { :controller => "notes", :action => "index", :format => "gpx" }
68 { :path => "/api/0.6/notes/search", :method => :get },
69 { :controller => "notes", :action => "search", :format => "xml" }
72 { :controller => "notes", :action => "search", :format => "xml" },
73 { :path => "/api/0.6/notes/search.xml", :method => :get }
76 { :path => "/api/0.6/notes/search.rss", :method => :get },
77 { :controller => "notes", :action => "search", :format => "rss" }
80 { :path => "/api/0.6/notes/search.json", :method => :get },
81 { :controller => "notes", :action => "search", :format => "json" }
84 { :path => "/api/0.6/notes/search.gpx", :method => :get },
85 { :controller => "notes", :action => "search", :format => "gpx" }
89 { :path => "/api/0.6/notes/feed", :method => :get },
90 { :controller => "notes", :action => "feed", :format => "rss" }
94 { :controller => "notes", :action => "create" },
95 { :path => "/api/0.6/notes/addPOIexec", :method => :post }
98 { :controller => "notes", :action => "close" },
99 { :path => "/api/0.6/notes/closePOIexec", :method => :post }
102 { :controller => "notes", :action => "comment" },
103 { :path => "/api/0.6/notes/editPOIexec", :method => :post }
106 { :controller => "notes", :action => "index", :format => "gpx" },
107 { :path => "/api/0.6/notes/getGPX", :method => :get }
110 { :controller => "notes", :action => "feed", :format => "rss" },
111 { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
115 { :path => "/user/username/notes", :method => :get },
116 { :controller => "notes", :action => "mine", :display_name => "username" }
120 def test_note_create_success
121 assert_difference('Note.count') do
122 assert_difference('NoteComment.count') do
123 post :create, {:lat => -1.0, :lon => -1.0, :text => "This is a comment", :format => "json"}
126 assert_response :success
127 js = ActiveSupport::JSON.decode(@response.body)
129 assert_equal "Feature", js["type"]
130 assert_equal "Point", js["geometry"]["type"]
131 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
132 assert_equal "open", js["properties"]["status"]
133 assert_equal 1, js["properties"]["comments"].count
134 assert_equal "opened", js["properties"]["comments"].last["action"]
135 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
136 assert_nil js["properties"]["comments"].last["user"]
137 id = js["properties"]["id"]
139 get :show, {:id => id, :format => "json"}
140 assert_response :success
141 js = ActiveSupport::JSON.decode(@response.body)
143 assert_equal "Feature", js["type"]
144 assert_equal "Point", js["geometry"]["type"]
145 assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
146 assert_equal id, js["properties"]["id"]
147 assert_equal "open", js["properties"]["status"]
148 assert_equal 1, js["properties"]["comments"].count
149 assert_equal "opened", js["properties"]["comments"].last["action"]
150 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
151 assert_nil js["properties"]["comments"].last["user"]
154 def test_note_create_fail
155 assert_no_difference('Note.count') do
156 assert_no_difference('NoteComment.count') do
157 post :create, {:lon => -1.0, :text => "This is a comment"}
160 assert_response :bad_request
162 assert_no_difference('Note.count') do
163 assert_no_difference('NoteComment.count') do
164 post :create, {:lat => -1.0, :text => "This is a comment"}
167 assert_response :bad_request
169 assert_no_difference('Note.count') do
170 assert_no_difference('NoteComment.count') do
171 post :create, {:lat => -1.0, :lon => -1.0}
174 assert_response :bad_request
176 assert_no_difference('Note.count') do
177 assert_no_difference('NoteComment.count') do
178 post :create, {:lat => -1.0, :lon => -1.0, :text => ""}
181 assert_response :bad_request
183 assert_no_difference('Note.count') do
184 assert_no_difference('NoteComment.count') do
185 post :create, {:lat => -100.0, :lon => -1.0, :text => "This is a comment"}
188 assert_response :bad_request
190 assert_no_difference('Note.count') do
191 assert_no_difference('NoteComment.count') do
192 post :create, {:lat => -1.0, :lon => -200.0, :text => "This is a comment"}
195 assert_response :bad_request
198 def test_note_comment_create_success
199 assert_difference('NoteComment.count') do
200 post :comment, {:id => notes(:open_note_with_comment).id, :text => "This is an additional comment", :format => "json"}
202 assert_response :success
203 js = ActiveSupport::JSON.decode(@response.body)
205 assert_equal "Feature", js["type"]
206 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
207 assert_equal "open", js["properties"]["status"]
208 assert_equal 3, js["properties"]["comments"].count
209 assert_equal "commented", js["properties"]["comments"].last["action"]
210 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
211 assert_nil js["properties"]["comments"].last["user"]
213 get :show, {:id => notes(:open_note_with_comment).id, :format => "json"}
214 assert_response :success
215 js = ActiveSupport::JSON.decode(@response.body)
217 assert_equal "Feature", js["type"]
218 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
219 assert_equal "open", js["properties"]["status"]
220 assert_equal 3, js["properties"]["comments"].count
221 assert_equal "commented", js["properties"]["comments"].last["action"]
222 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
223 assert_nil js["properties"]["comments"].last["user"]
226 def test_note_comment_create_fail
227 assert_no_difference('NoteComment.count') do
228 post :comment, {:text => "This is an additional comment"}
230 assert_response :bad_request
232 assert_no_difference('NoteComment.count') do
233 post :comment, {:id => notes(:open_note_with_comment).id}
235 assert_response :bad_request
237 assert_no_difference('NoteComment.count') do
238 post :comment, {:id => notes(:open_note_with_comment).id, :text => ""}
240 assert_response :bad_request
242 assert_no_difference('NoteComment.count') do
243 post :comment, {:id => 12345, :text => "This is an additional comment"}
245 assert_response :not_found
247 assert_no_difference('NoteComment.count') do
248 post :comment, {:id => notes(:hidden_note_with_comment).id, :text => "This is an additional comment"}
250 assert_response :gone
252 assert_no_difference('NoteComment.count') do
253 post :comment, {:id => notes(:closed_note_with_comment).id, :text => "This is an additional comment"}
255 assert_response :conflict
258 def test_note_close_success
259 post :close, {:id => notes(:open_note_with_comment).id, :text => "This is a close comment", :format => "json"}
260 assert_response :unauthorized
262 basic_authorization(users(:public_user).email, "test")
264 post :close, {:id => notes(:open_note_with_comment).id, :text => "This is a close comment", :format => "json"}
265 assert_response :success
266 js = ActiveSupport::JSON.decode(@response.body)
268 assert_equal "Feature", js["type"]
269 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
270 assert_equal "closed", js["properties"]["status"]
271 assert_equal 3, js["properties"]["comments"].count
272 assert_equal "closed", js["properties"]["comments"].last["action"]
273 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
274 assert_equal "test2", js["properties"]["comments"].last["user"]
276 get :show, {:id => notes(:open_note_with_comment).id, :format => "json"}
277 assert_response :success
278 js = ActiveSupport::JSON.decode(@response.body)
280 assert_equal "Feature", js["type"]
281 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
282 assert_equal "closed", js["properties"]["status"]
283 assert_equal 3, js["properties"]["comments"].count
284 assert_equal "closed", js["properties"]["comments"].last["action"]
285 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
286 assert_equal "test2", js["properties"]["comments"].last["user"]
289 def test_note_close_fail
291 assert_response :unauthorized
293 basic_authorization(users(:public_user).email, "test")
296 assert_response :bad_request
298 post :close, {:id => 12345}
299 assert_response :not_found
301 post :close, {:id => notes(:hidden_note_with_comment).id}
302 assert_response :gone
304 post :close, {:id => notes(:closed_note_with_comment).id}
305 assert_response :conflict
308 def test_note_read_success
309 get :show, {:id => notes(:open_note).id, :format => "xml"}
310 assert_response :success
311 assert_equal "application/xml", @response.content_type
313 get :show, {:id => notes(:open_note).id, :format => "rss"}
314 assert_response :success
315 assert_equal "application/rss+xml", @response.content_type
317 get :show, {:id => notes(:open_note).id, :format => "json"}
318 assert_response :success
319 assert_equal "application/json", @response.content_type
321 get :show, {:id => notes(:open_note).id, :format => "gpx"}
322 assert_response :success
323 assert_equal "application/gpx+xml", @response.content_type
326 def test_note_read_hidden_comment
327 get :show, {:id => notes(:note_with_hidden_comment).id, :format => "json"}
328 assert_response :success
329 js = ActiveSupport::JSON.decode(@response.body)
331 assert_equal notes(:note_with_hidden_comment).id, js["properties"]["id"]
332 assert_equal 2, js["properties"]["comments"].count
333 assert_equal "Valid comment for note 5", js["properties"]["comments"][0]["text"]
334 assert_equal "Another valid comment for note 5", js["properties"]["comments"][1]["text"]
337 def test_note_read_fail
338 get :show, {:id => 12345}
339 assert_response :not_found
341 get :show, {:id => notes(:hidden_note_with_comment).id}
342 assert_response :gone
345 def test_note_delete_success
346 delete :destroy, {:id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"}
347 assert_response :unauthorized
349 basic_authorization(users(:public_user).email, "test")
351 delete :destroy, {:id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"}
352 assert_response :forbidden
354 basic_authorization(users(:moderator_user).email, "test")
356 delete :destroy, {:id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"}
357 assert_response :success
358 js = ActiveSupport::JSON.decode(@response.body)
360 assert_equal "Feature", js["type"]
361 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
362 assert_equal "hidden", js["properties"]["status"]
363 assert_equal 3, js["properties"]["comments"].count
364 assert_equal "hidden", js["properties"]["comments"].last["action"]
365 assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
366 assert_equal "moderator", js["properties"]["comments"].last["user"]
368 get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
369 assert_response :gone
372 def test_note_delete_fail
373 delete :destroy, {:id => 12345, :format => "json"}
374 assert_response :unauthorized
376 basic_authorization(users(:public_user).email, "test")
378 delete :destroy, {:id => 12345, :format => "json"}
379 assert_response :forbidden
381 basic_authorization(users(:moderator_user).email, "test")
383 delete :destroy, {:id => 12345, :format => "json"}
384 assert_response :not_found
386 delete :destroy, {:id => notes(:hidden_note_with_comment).id, :format => "json"}
387 assert_response :gone
390 def test_get_notes_success
391 get :index, {:bbox => '1,1,1.2,1.2', :format => 'rss'}
392 assert_response :success
393 assert_equal "application/rss+xml", @response.content_type
394 assert_select "rss", :count => 1 do
395 assert_select "channel", :count => 1 do
396 assert_select "item", :count => 2
400 get :index, {:bbox => '1,1,1.2,1.2', :format => 'json'}
401 assert_response :success
402 assert_equal "application/json", @response.content_type
403 js = ActiveSupport::JSON.decode(@response.body)
405 assert_equal "FeatureCollection", js["type"]
406 assert_equal 2, js["features"].count
408 get :index, {:bbox => '1,1,1.2,1.2', :format => 'xml'}
409 assert_response :success
410 assert_equal "application/xml", @response.content_type
411 assert_select "osm", :count => 1 do
412 assert_select "note", :count => 2
415 get :index, {:bbox => '1,1,1.2,1.2', :format => 'gpx'}
416 assert_response :success
417 assert_equal "application/gpx+xml", @response.content_type
418 assert_select "gpx", :count => 1 do
419 assert_select "wpt", :count => 2
423 def test_get_notes_empty_area
424 get :index, {:bbox => '5,5,5.1,5.1', :format => 'rss'}
425 assert_response :success
426 assert_equal "application/rss+xml", @response.content_type
427 assert_select "rss", :count => 1 do
428 assert_select "channel", :count => 1 do
429 assert_select "item", :count => 0
433 get :index, {:bbox => '5,5,5.1,5.1', :format => 'json'}
434 assert_response :success
435 assert_equal "application/json", @response.content_type
436 js = ActiveSupport::JSON.decode(@response.body)
438 assert_equal "FeatureCollection", js["type"]
439 assert_equal 0, js["features"].count
441 get :index, {:bbox => '5,5,5.1,5.1', :format => 'xml'}
442 assert_response :success
443 assert_equal "application/xml", @response.content_type
444 assert_select "osm", :count => 1 do
445 assert_select "note", :count => 0
448 get :index, {:bbox => '5,5,5.1,5.1', :format => 'gpx'}
449 assert_response :success
450 assert_equal "application/gpx+xml", @response.content_type
451 assert_select "gpx", :count => 1 do
452 assert_select "wpt", :count => 0
456 def test_get_notes_large_area
457 get :index, {:bbox => '-2.5,-2.5,2.5,2.5', :format => :json}
458 assert_response :success
459 assert_equal "application/json", @response.content_type
461 get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5', :t => '2.5', :format => :json}
462 assert_response :success
463 assert_equal "application/json", @response.content_type
465 get :index, {:bbox => '-10,-10,12,12', :format => :json}
466 assert_response :bad_request
467 assert_equal "text/plain", @response.content_type
469 get :index, {:l => '-10', :b => '-10', :r => '12', :t => '12', :format => :json}
470 assert_response :bad_request
471 assert_equal "text/plain", @response.content_type
474 def test_get_notes_closed
475 get :index, {:bbox => '1,1,1.7,1.7', :closed => '7', :format => 'json'}
476 assert_response :success
477 assert_equal "application/json", @response.content_type
478 js = ActiveSupport::JSON.decode(@response.body)
480 assert_equal "FeatureCollection", js["type"]
481 assert_equal 4, js["features"].count
483 get :index, {:bbox => '1,1,1.7,1.7', :closed => '0', :format => 'json'}
484 assert_response :success
485 assert_equal "application/json", @response.content_type
486 js = ActiveSupport::JSON.decode(@response.body)
488 assert_equal "FeatureCollection", js["type"]
489 assert_equal 4, js["features"].count
491 get :index, {:bbox => '1,1,1.7,1.7', :closed => '-1', :format => 'json'}
492 assert_response :success
493 assert_equal "application/json", @response.content_type
494 js = ActiveSupport::JSON.decode(@response.body)
496 assert_equal "FeatureCollection", js["type"]
497 assert_equal 6, js["features"].count
500 def test_get_notes_bad_params
501 get :index, {:bbox => '-2.5,-2.5,2.5'}
502 assert_response :bad_request
504 get :index, {:bbox => '-2.5,-2.5,2.5,2.5,2.5'}
505 assert_response :bad_request
507 get :index, {:b => '-2.5', :r => '2.5', :t => '2.5'}
508 assert_response :bad_request
510 get :index, {:l => '-2.5', :r => '2.5', :t => '2.5'}
511 assert_response :bad_request
513 get :index, {:l => '-2.5', :b => '-2.5', :t => '2.5'}
514 assert_response :bad_request
516 get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5'}
517 assert_response :bad_request
520 def test_search_success
521 get :search, {:q => 'note 1', :format => 'xml'}
522 assert_response :success
523 assert_equal "application/xml", @response.content_type
524 assert_select "osm", :count => 1 do
525 assert_select "note", :count => 1
528 get :search, {:q => 'note 1', :format => 'json'}
529 assert_response :success
530 assert_equal "application/json", @response.content_type
531 js = ActiveSupport::JSON.decode(@response.body)
533 assert_equal "FeatureCollection", js["type"]
534 assert_equal 1, js["features"].count
536 get :search, {:q => 'note 1', :format => 'rss'}
537 assert_response :success
538 assert_equal "application/rss+xml", @response.content_type
539 assert_select "rss", :count => 1 do
540 assert_select "channel", :count => 1 do
541 assert_select "item", :count => 1
545 get :search, {:q => 'note 1', :format => 'gpx'}
546 assert_response :success
547 assert_equal "application/gpx+xml", @response.content_type
548 assert_select "gpx", :count => 1 do
549 assert_select "wpt", :count => 1
553 def test_search_no_match
554 get :search, {:q => 'no match', :format => 'xml'}
555 assert_response :success
556 assert_equal "application/xml", @response.content_type
557 assert_select "osm", :count => 1 do
558 assert_select "note", :count => 0
561 get :search, {:q => 'no match', :format => 'json'}
562 assert_response :success
563 assert_equal "application/json", @response.content_type
564 js = ActiveSupport::JSON.decode(@response.body)
566 assert_equal "FeatureCollection", js["type"]
567 assert_equal 0, js["features"].count
569 get :search, {:q => 'no match', :format => 'rss'}
570 assert_response :success
571 assert_equal "application/rss+xml", @response.content_type
572 assert_select "rss", :count => 1 do
573 assert_select "channel", :count => 1 do
574 assert_select "item", :count => 0
578 get :search, {:q => 'no match', :format => 'gpx'}
579 assert_response :success
580 assert_equal "application/gpx+xml", @response.content_type
581 assert_select "gpx", :count => 1 do
582 assert_select "wpt", :count => 0
586 def test_search_bad_params
588 assert_response :bad_request
592 get :feed, {:format => "rss"}
593 assert_response :success
594 assert_equal "application/rss+xml", @response.content_type
596 get :feed, {:bbox => "1,1,1.2,1.2", :format => "rss"}
597 assert_response :success
598 assert_equal "application/rss+xml", @response.content_type
602 get :feed, {:bbox => "1,1,1.2"}
603 assert_response :bad_request
605 get :feed, {:bbox => "1,1,1.2,1.2,1.2"}
606 assert_response :bad_request
609 def test_user_notes_success
610 get :mine, {:display_name => "test"}
611 assert_response :success
613 get :mine, {:display_name => "pulibc_test2"}
614 assert_response :success
616 get :mine, {:display_name => "non-existent"}
617 assert_response :not_found