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 => -100.0, :lon => -1.0, :text => "This is a comment"}
181 assert_response :bad_request
183 assert_no_difference('Note.count') do
184 assert_no_difference('NoteComment.count') do
185 post :create, {:lat => -1.0, :lon => -200.0, :text => "This is a comment"}
188 assert_response :bad_request
191 def test_note_comment_create_success
192 assert_difference('NoteComment.count') do
193 post :comment, {:id => notes(:open_note_with_comment).id, :text => "This is an additional comment", :format => "json"}
195 assert_response :success
196 js = ActiveSupport::JSON.decode(@response.body)
198 assert_equal "Feature", js["type"]
199 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
200 assert_equal "open", js["properties"]["status"]
201 assert_equal 3, js["properties"]["comments"].count
202 assert_equal "commented", js["properties"]["comments"].last["action"]
203 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
204 assert_nil js["properties"]["comments"].last["user"]
206 get :show, {:id => notes(:open_note_with_comment).id, :format => "json"}
207 assert_response :success
208 js = ActiveSupport::JSON.decode(@response.body)
210 assert_equal "Feature", js["type"]
211 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
212 assert_equal "open", js["properties"]["status"]
213 assert_equal 3, js["properties"]["comments"].count
214 assert_equal "commented", js["properties"]["comments"].last["action"]
215 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
216 assert_nil js["properties"]["comments"].last["user"]
219 def test_note_comment_create_fail
220 assert_no_difference('NoteComment.count') do
221 post :comment, {:text => "This is an additional comment"}
223 assert_response :bad_request
225 assert_no_difference('NoteComment.count') do
226 post :comment, {:id => notes(:open_note_with_comment).id}
228 assert_response :bad_request
230 assert_no_difference('NoteComment.count') do
231 post :comment, {:id => notes(:open_note_with_comment).id, :text => ""}
233 assert_response :bad_request
235 assert_no_difference('NoteComment.count') do
236 post :comment, {:id => 12345, :text => "This is an additional comment"}
238 assert_response :not_found
240 assert_no_difference('NoteComment.count') do
241 post :comment, {:id => notes(:hidden_note_with_comment).id, :text => "This is an additional comment"}
243 assert_response :gone
245 assert_no_difference('NoteComment.count') do
246 post :comment, {:id => notes(:closed_note_with_comment).id, :text => "This is an additional comment"}
248 assert_response :conflict
251 def test_note_close_success
252 post :close, {:id => notes(:open_note_with_comment).id, :text => "This is a close comment", :format => "json"}
253 assert_response :success
254 js = ActiveSupport::JSON.decode(@response.body)
256 assert_equal "Feature", js["type"]
257 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
258 assert_equal "closed", js["properties"]["status"]
259 assert_equal 3, js["properties"]["comments"].count
260 assert_equal "closed", js["properties"]["comments"].last["action"]
261 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
262 assert_nil js["properties"]["comments"].last["user"]
264 get :show, {:id => notes(:open_note_with_comment).id, :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_nil js["properties"]["comments"].last["user"]
277 def test_note_close_fail
279 assert_response :bad_request
281 post :close, {:id => 12345}
282 assert_response :not_found
284 post :close, {:id => notes(:hidden_note_with_comment).id}
285 assert_response :gone
287 post :close, {:id => notes(:closed_note_with_comment).id}
288 assert_response :conflict
291 def test_note_read_success
292 get :show, {:id => notes(:open_note).id, :format => "xml"}
293 assert_response :success
294 assert_equal "application/xml", @response.content_type
296 get :show, {:id => notes(:open_note).id, :format => "rss"}
297 assert_response :success
298 assert_equal "application/rss+xml", @response.content_type
300 get :show, {:id => notes(:open_note).id, :format => "json"}
301 assert_response :success
302 assert_equal "application/json", @response.content_type
304 get :show, {:id => notes(:open_note).id, :format => "gpx"}
305 assert_response :success
306 assert_equal "application/gpx+xml", @response.content_type
309 def test_note_read_hidden_comment
310 get :show, {:id => notes(:note_with_hidden_comment).id, :format => "json"}
311 assert_response :success
312 js = ActiveSupport::JSON.decode(@response.body)
314 assert_equal notes(:note_with_hidden_comment).id, js["properties"]["id"]
315 assert_equal 2, js["properties"]["comments"].count
316 assert_equal "Valid comment for note 5", js["properties"]["comments"][0]["text"]
317 assert_equal "Another valid comment for note 5", js["properties"]["comments"][1]["text"]
320 def test_note_read_fail
321 get :show, {:id => 12345}
322 assert_response :not_found
324 get :show, {:id => notes(:hidden_note_with_comment).id}
325 assert_response :gone
328 def test_note_delete_success
329 delete :destroy, {:id => notes(:open_note_with_comment).id}
330 assert_response :success
332 get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
333 assert_response :gone
336 def test_note_delete_fail
337 delete :destroy, {:id => 12345}
338 assert_response :not_found
340 delete :destroy, {:id => notes(:hidden_note_with_comment).id}
341 assert_response :gone
344 def test_get_notes_success
345 # get :index, {:bbox => '1,1,1.2,1.2'}
346 # assert_response :success
347 # assert_equal "text/javascript", @response.content_type
349 get :index, {:bbox => '1,1,1.2,1.2', :format => 'rss'}
350 assert_response :success
351 assert_equal "application/rss+xml", @response.content_type
353 get :index, {:bbox => '1,1,1.2,1.2', :format => 'json'}
354 assert_response :success
355 assert_equal "application/json", @response.content_type
357 get :index, {:bbox => '1,1,1.2,1.2', :format => 'xml'}
358 assert_response :success
359 assert_equal "application/xml", @response.content_type
361 get :index, {:bbox => '1,1,1.2,1.2', :format => 'gpx'}
362 assert_response :success
363 assert_equal "application/gpx+xml", @response.content_type
366 def test_get_notes_large_area
367 # get :index, {:bbox => '-2.5,-2.5,2.5,2.5'}
368 # assert_response :success
370 # get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5', :t => '2.5'}
371 # assert_response :success
373 get :index, {:bbox => '-10,-10,12,12'}
374 assert_response :bad_request
376 get :index, {:l => '-10', :b => '-10', :r => '12', :t => '12'}
377 assert_response :bad_request
380 def test_get_notes_closed
381 get :index, {:bbox => '1,1,1.7,1.7', :closed => '7', :format => 'json'}
382 assert_response :success
383 assert_equal "application/json", @response.content_type
384 js = ActiveSupport::JSON.decode(@response.body)
386 assert_equal "FeatureCollection", js["type"]
387 assert_equal 4, js["features"].count
389 get :index, {:bbox => '1,1,1.7,1.7', :closed => '0', :format => 'json'}
390 assert_response :success
391 assert_equal "application/json", @response.content_type
392 js = ActiveSupport::JSON.decode(@response.body)
394 assert_equal "FeatureCollection", js["type"]
395 assert_equal 4, js["features"].count
397 get :index, {:bbox => '1,1,1.7,1.7', :closed => '-1', :format => 'json'}
398 assert_response :success
399 assert_equal "application/json", @response.content_type
400 js = ActiveSupport::JSON.decode(@response.body)
402 assert_equal "FeatureCollection", js["type"]
403 assert_equal 6, js["features"].count
406 def test_get_notes_bad_params
407 get :index, {:bbox => '-2.5,-2.5,2.5'}
408 assert_response :bad_request
410 get :index, {:bbox => '-2.5,-2.5,2.5,2.5,2.5'}
411 assert_response :bad_request
413 get :index, {:b => '-2.5', :r => '2.5', :t => '2.5'}
414 assert_response :bad_request
416 get :index, {:l => '-2.5', :r => '2.5', :t => '2.5'}
417 assert_response :bad_request
419 get :index, {:l => '-2.5', :b => '-2.5', :t => '2.5'}
420 assert_response :bad_request
422 get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5'}
423 assert_response :bad_request
426 def test_search_success
427 get :search, {:q => 'note 1', :format => 'xml'}
428 assert_response :success
429 assert_equal "application/xml", @response.content_type
431 get :search, {:q => 'note 1', :format => 'json'}
432 assert_response :success
433 assert_equal "application/json", @response.content_type
435 get :search, {:q => 'note 1', :format => 'rss'}
436 assert_response :success
437 assert_equal "application/rss+xml", @response.content_type
439 get :search, {:q => 'note 1', :format => 'gpx'}
440 assert_response :success
441 assert_equal "application/gpx+xml", @response.content_type
444 def test_search_bad_params
446 assert_response :bad_request
450 get :feed, {:format => "rss"}
451 assert_response :success
452 assert_equal "application/rss+xml", @response.content_type
454 get :feed, {:bbox => "1,1,1.2,1.2", :format => "rss"}
455 assert_response :success
456 assert_equal "application/rss+xml", @response.content_type
460 get :feed, {:bbox => "1,1,1.2"}
461 assert_response :bad_request
463 get :feed, {:bbox => "1,1,1.2,1.2,1.2"}
464 assert_response :bad_request
467 def test_user_notes_success
468 get :mine, {:display_name => "test"}
469 assert_response :success
471 get :mine, {:display_name => "pulibc_test2"}
472 assert_response :success
474 get :mine, {:display_name => "non-existent"}
475 assert_response :not_found