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 => 12345, :text => "This is an additional comment"}
233 assert_response :not_found
235 assert_no_difference('NoteComment.count') do
236 post :comment, {:id => notes(:hidden_note_with_comment).id, :text => "This is an additional comment"}
238 assert_response :gone
240 assert_no_difference('NoteComment.count') do
241 post :comment, {:id => notes(:closed_note_with_comment).id, :text => "This is an additional comment"}
243 assert_response :conflict
246 def test_note_close_success
247 post :close, {:id => notes(:open_note_with_comment).id, :text => "This is a close comment", :format => "json"}
248 assert_response :success
249 js = ActiveSupport::JSON.decode(@response.body)
251 assert_equal "Feature", js["type"]
252 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
253 assert_equal "closed", js["properties"]["status"]
254 assert_equal 3, js["properties"]["comments"].count
255 assert_equal "closed", js["properties"]["comments"].last["action"]
256 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
257 assert_nil js["properties"]["comments"].last["user"]
259 get :show, {:id => notes(:open_note_with_comment).id, :format => "json"}
260 assert_response :success
261 js = ActiveSupport::JSON.decode(@response.body)
263 assert_equal "Feature", js["type"]
264 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
265 assert_equal "closed", js["properties"]["status"]
266 assert_equal 3, js["properties"]["comments"].count
267 assert_equal "closed", js["properties"]["comments"].last["action"]
268 assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
269 assert_nil js["properties"]["comments"].last["user"]
272 def test_note_close_fail
274 assert_response :bad_request
276 post :close, {:id => 12345}
277 assert_response :not_found
279 post :close, {:id => notes(:hidden_note_with_comment).id}
280 assert_response :gone
282 post :close, {:id => notes(:closed_note_with_comment).id}
283 assert_response :conflict
286 def test_note_read_success
287 get :show, {:id => notes(:open_note).id, :format => "xml"}
288 assert_response :success
289 assert_equal "application/xml", @response.content_type
291 get :show, {:id => notes(:open_note).id, :format => "rss"}
292 assert_response :success
293 assert_equal "application/rss+xml", @response.content_type
295 get :show, {:id => notes(:open_note).id, :format => "json"}
296 assert_response :success
297 assert_equal "application/json", @response.content_type
299 get :show, {:id => notes(:open_note).id, :format => "gpx"}
300 assert_response :success
301 assert_equal "application/gpx+xml", @response.content_type
304 def test_note_read_hidden_comment
305 get :show, {:id => notes(:note_with_hidden_comment).id, :format => "json"}
306 assert_response :success
307 js = ActiveSupport::JSON.decode(@response.body)
309 assert_equal notes(:note_with_hidden_comment).id, js["properties"]["id"]
310 assert_equal 2, js["properties"]["comments"].count
311 assert_equal "Valid comment for note 5", js["properties"]["comments"][0]["text"]
312 assert_equal "Another valid comment for note 5", js["properties"]["comments"][1]["text"]
315 def test_note_read_fail
316 get :show, {:id => 12345}
317 assert_response :not_found
319 get :show, {:id => notes(:hidden_note_with_comment).id}
320 assert_response :gone
323 def test_note_delete_success
324 delete :destroy, {:id => notes(:open_note_with_comment).id}
325 assert_response :success
327 get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
328 assert_response :gone
331 def test_note_delete_fail
332 delete :destroy, {:id => 12345}
333 assert_response :not_found
335 delete :destroy, {:id => notes(:hidden_note_with_comment).id}
336 assert_response :gone
339 def test_get_notes_success
340 # get :index, {:bbox => '1,1,1.2,1.2'}
341 # assert_response :success
342 # assert_equal "text/javascript", @response.content_type
344 get :index, {:bbox => '1,1,1.2,1.2', :format => 'rss'}
345 assert_response :success
346 assert_equal "application/rss+xml", @response.content_type
348 get :index, {:bbox => '1,1,1.2,1.2', :format => 'json'}
349 assert_response :success
350 assert_equal "application/json", @response.content_type
352 get :index, {:bbox => '1,1,1.2,1.2', :format => 'xml'}
353 assert_response :success
354 assert_equal "application/xml", @response.content_type
356 get :index, {:bbox => '1,1,1.2,1.2', :format => 'gpx'}
357 assert_response :success
358 assert_equal "application/gpx+xml", @response.content_type
361 def test_get_notes_large_area
362 # get :index, {:bbox => '-2.5,-2.5,2.5,2.5'}
363 # assert_response :success
365 # get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5', :t => '2.5'}
366 # assert_response :success
368 get :index, {:bbox => '-10,-10,12,12'}
369 assert_response :bad_request
371 get :index, {:l => '-10', :b => '-10', :r => '12', :t => '12'}
372 assert_response :bad_request
375 def test_get_notes_closed
376 get :index, {:bbox => '1,1,1.7,1.7', :closed => '7', :format => 'json'}
377 assert_response :success
378 assert_equal "application/json", @response.content_type
379 js = ActiveSupport::JSON.decode(@response.body)
381 assert_equal "FeatureCollection", js["type"]
382 assert_equal 4, js["features"].count
384 get :index, {:bbox => '1,1,1.7,1.7', :closed => '0', :format => 'json'}
385 assert_response :success
386 assert_equal "application/json", @response.content_type
387 js = ActiveSupport::JSON.decode(@response.body)
389 assert_equal "FeatureCollection", js["type"]
390 assert_equal 4, js["features"].count
392 get :index, {:bbox => '1,1,1.7,1.7', :closed => '-1', :format => 'json'}
393 assert_response :success
394 assert_equal "application/json", @response.content_type
395 js = ActiveSupport::JSON.decode(@response.body)
397 assert_equal "FeatureCollection", js["type"]
398 assert_equal 6, js["features"].count
401 def test_get_notes_bad_params
402 get :index, {:bbox => '-2.5,-2.5,2.5'}
403 assert_response :bad_request
405 get :index, {:bbox => '-2.5,-2.5,2.5,2.5,2.5'}
406 assert_response :bad_request
408 get :index, {:b => '-2.5', :r => '2.5', :t => '2.5'}
409 assert_response :bad_request
411 get :index, {:l => '-2.5', :r => '2.5', :t => '2.5'}
412 assert_response :bad_request
414 get :index, {:l => '-2.5', :b => '-2.5', :t => '2.5'}
415 assert_response :bad_request
417 get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5'}
418 assert_response :bad_request
421 def test_search_success
422 get :search, {:q => 'note 1', :format => 'xml'}
423 assert_response :success
424 assert_equal "application/xml", @response.content_type
426 get :search, {:q => 'note 1', :format => 'json'}
427 assert_response :success
428 assert_equal "application/json", @response.content_type
430 get :search, {:q => 'note 1', :format => 'rss'}
431 assert_response :success
432 assert_equal "application/rss+xml", @response.content_type
434 get :search, {:q => 'note 1', :format => 'gpx'}
435 assert_response :success
436 assert_equal "application/gpx+xml", @response.content_type
439 def test_search_bad_params
441 assert_response :bad_request
445 get :feed, {:format => "rss"}
446 assert_response :success
447 assert_equal "application/rss+xml", @response.content_type
449 get :feed, {:bbox => "1,1,1.2,1.2", :format => "rss"}
450 assert_response :success
451 assert_equal "application/rss+xml", @response.content_type
455 get :feed, {:bbox => "1,1,1.2"}
456 assert_response :bad_request
458 get :feed, {:bbox => "1,1,1.2,1.2,1.2"}
459 assert_response :bad_request
462 def test_user_notes_success
463 get :mine, {:display_name => "test"}
464 assert_response :success
466 get :mine, {:display_name => "pulibc_test2"}
467 assert_response :success
469 get :mine, {:display_name => "non-existent"}
470 assert_response :not_found