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, :name => "new_tester", :text => "This is a comment"}
126 assert_response :success
127 id = @response.body.sub(/ok/,"").to_i
129 get :show, {:id => id, :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 id, js["properties"]["id"]
137 assert_equal "open", js["properties"]["status"]
138 assert_equal 1, js["properties"]["comments"].count
139 assert_equal "opened", js["properties"]["comments"].last["action"]
140 assert_equal "This is a comment", js["properties"]["comments"].last["text"]
141 assert_equal "new_tester (a)", js["properties"]["comments"].last["user"]
144 def test_note_create_fail
145 assert_no_difference('Note.count') do
146 assert_no_difference('NoteComment.count') do
147 post :create, {:lon => -1.0, :name => "new_tester", :text => "This is a comment"}
150 assert_response :bad_request
152 assert_no_difference('Note.count') do
153 assert_no_difference('NoteComment.count') do
154 post :create, {:lat => -1.0, :name => "new_tester", :text => "This is a comment"}
157 assert_response :bad_request
159 assert_no_difference('Note.count') do
160 assert_no_difference('NoteComment.count') do
161 post :create, {:lat => -1.0, :lon => -1.0, :name => "new_tester"}
164 assert_response :bad_request
166 assert_no_difference('Note.count') do
167 assert_no_difference('NoteComment.count') do
168 post :create, {:lat => -100.0, :lon => -1.0, :name => "new_tester", :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 => -200.0, :name => "new_tester", :text => "This is a comment"}
178 assert_response :bad_request
181 def test_note_comment_create_success
182 assert_difference('NoteComment.count') do
183 post :comment, {:id => notes(:open_note_with_comment).id, :name => "new_tester2", :text => "This is an additional comment"}
185 assert_response :success
187 get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
188 assert_response :success
189 js = ActiveSupport::JSON.decode(@response.body)
191 assert_equal "Feature", js["type"]
192 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
193 assert_equal "open", js["properties"]["status"]
194 assert_equal 3, js["properties"]["comments"].count
195 assert_equal "commented", js["properties"]["comments"].last["action"]
196 assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
197 assert_equal "new_tester2 (a)", js["properties"]["comments"].last["user"]
200 def test_note_comment_create_fail
201 assert_no_difference('NoteComment.count') do
202 post :comment, {:name => "new_tester2", :text => "This is an additional comment"}
204 assert_response :bad_request
206 assert_no_difference('NoteComment.count') do
207 post :comment, {:id => notes(:open_note_with_comment).id, :name => "new_tester2"}
209 assert_response :bad_request
211 assert_no_difference('NoteComment.count') do
212 post :comment, {:id => 12345, :name => "new_tester2", :text => "This is an additional comment"}
214 assert_response :not_found
216 assert_no_difference('NoteComment.count') do
217 post :comment, {:id => notes(:hidden_note_with_comment).id, :name => "new_tester2", :text => "This is an additional comment"}
219 assert_response :gone
222 def test_note_close_success
223 post :close, {:id => notes(:open_note_with_comment).id}
224 assert_response :success
226 get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
227 assert_response :success
228 js = ActiveSupport::JSON.decode(@response.body)
230 assert_equal "Feature", js["type"]
231 assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
232 assert_equal "closed", js["properties"]["status"]
233 assert_equal 3, js["properties"]["comments"].count
234 assert_equal "closed", js["properties"]["comments"].last["action"]
235 assert_equal nil, js["properties"]["comments"].last["text"]
236 assert_equal "NoName (a)", js["properties"]["comments"].last["user"]
239 def test_note_close_fail
241 assert_response :bad_request
243 post :close, {:id => 12345}
244 assert_response :not_found
246 post :close, {:id => notes(:hidden_note_with_comment).id}
247 assert_response :gone
250 def test_note_read_success
251 get :show, {:id => notes(:open_note).id, :format => "xml"}
252 assert_response :success
253 assert_equal "application/xml", @response.content_type
255 get :show, {:id => notes(:open_note).id, :format => "rss"}
256 assert_response :success
257 assert_equal "application/rss+xml", @response.content_type
259 get :show, {:id => notes(:open_note).id, :format => "json"}
260 assert_response :success
261 assert_equal "application/json", @response.content_type
263 get :show, {:id => notes(:open_note).id, :format => "gpx"}
264 assert_response :success
265 assert_equal "application/gpx+xml", @response.content_type
268 def test_note_read_hidden_comment
269 get :show, {:id => notes(:note_with_hidden_comment).id, :format => 'json'}
270 assert_response :success
271 js = ActiveSupport::JSON.decode(@response.body)
273 assert_equal notes(:note_with_hidden_comment).id, js["properties"]["id"]
274 assert_equal 2, js["properties"]["comments"].count
275 assert_equal "Valid comment for note 5", js["properties"]["comments"][0]["text"]
276 assert_equal "Another valid comment for note 5", js["properties"]["comments"][1]["text"]
279 def test_note_read_fail
281 assert_response :bad_request
283 get :show, {:id => 12345}
284 assert_response :not_found
286 get :show, {:id => notes(:hidden_note_with_comment).id}
287 assert_response :gone
290 def test_note_delete_success
291 delete :destroy, {:id => notes(:open_note_with_comment).id}
292 assert_response :success
294 get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
295 assert_response :gone
298 def test_note_delete_fail
300 assert_response :bad_request
302 delete :destroy, {:id => 12345}
303 assert_response :not_found
305 delete :destroy, {:id => notes(:hidden_note_with_comment).id}
306 assert_response :gone
309 def test_get_notes_success
310 # get :index, {:bbox => '1,1,1.2,1.2'}
311 # assert_response :success
312 # assert_equal "text/javascript", @response.content_type
314 get :index, {:bbox => '1,1,1.2,1.2', :format => 'rss'}
315 assert_response :success
316 assert_equal "application/rss+xml", @response.content_type
318 get :index, {:bbox => '1,1,1.2,1.2', :format => 'json'}
319 assert_response :success
320 assert_equal "application/json", @response.content_type
322 get :index, {:bbox => '1,1,1.2,1.2', :format => 'xml'}
323 assert_response :success
324 assert_equal "application/xml", @response.content_type
326 get :index, {:bbox => '1,1,1.2,1.2', :format => 'gpx'}
327 assert_response :success
328 assert_equal "application/gpx+xml", @response.content_type
331 def test_get_notes_large_area
332 # get :index, {:bbox => '-2.5,-2.5,2.5,2.5'}
333 # assert_response :success
335 # get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5', :t => '2.5'}
336 # assert_response :success
338 get :index, {:bbox => '-10,-10,12,12'}
339 assert_response :bad_request
341 get :index, {:l => '-10', :b => '-10', :r => '12', :t => '12'}
342 assert_response :bad_request
345 def test_get_notes_closed
346 get :index, {:bbox => '1,1,1.7,1.7', :closed => '7', :format => 'json'}
347 assert_response :success
348 assert_equal "application/json", @response.content_type
349 js = ActiveSupport::JSON.decode(@response.body)
351 assert_equal "FeatureCollection", js["type"]
352 assert_equal 4, js["features"].count
354 get :index, {:bbox => '1,1,1.7,1.7', :closed => '0', :format => 'json'}
355 assert_response :success
356 assert_equal "application/json", @response.content_type
357 js = ActiveSupport::JSON.decode(@response.body)
359 assert_equal "FeatureCollection", js["type"]
360 assert_equal 4, js["features"].count
362 get :index, {:bbox => '1,1,1.7,1.7', :closed => '-1', :format => 'json'}
363 assert_response :success
364 assert_equal "application/json", @response.content_type
365 js = ActiveSupport::JSON.decode(@response.body)
367 assert_equal "FeatureCollection", js["type"]
368 assert_equal 6, js["features"].count
371 def test_get_notes_bad_params
372 get :index, {:bbox => '-2.5,-2.5,2.5'}
373 assert_response :bad_request
375 get :index, {:bbox => '-2.5,-2.5,2.5,2.5,2.5'}
376 assert_response :bad_request
378 get :index, {:b => '-2.5', :r => '2.5', :t => '2.5'}
379 assert_response :bad_request
381 get :index, {:l => '-2.5', :r => '2.5', :t => '2.5'}
382 assert_response :bad_request
384 get :index, {:l => '-2.5', :b => '-2.5', :t => '2.5'}
385 assert_response :bad_request
387 get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5'}
388 assert_response :bad_request
391 def test_search_success
392 get :search, {:q => 'note 1', :format => 'xml'}
393 assert_response :success
394 assert_equal "application/xml", @response.content_type
396 get :search, {:q => 'note 1', :format => 'json'}
397 assert_response :success
398 assert_equal "application/json", @response.content_type
400 get :search, {:q => 'note 1', :format => 'rss'}
401 assert_response :success
402 assert_equal "application/rss+xml", @response.content_type
404 get :search, {:q => 'note 1', :format => 'gpx'}
405 assert_response :success
406 assert_equal "application/gpx+xml", @response.content_type
409 def test_search_bad_params
411 assert_response :bad_request
415 get :feed, {:format => 'rss'}
416 assert_response :success
417 assert_equal "application/rss+xml", @response.content_type
419 get :feed, {:bbox=>'1,1,1.2,1.2', :format => 'rss'}
420 assert_response :success
421 assert_equal "application/rss+xml", @response.content_type
425 get :feed, {:bbox=>'1,1,1.2'}
426 assert_response :bad_request
428 get :feed, {:bbox=>'1,1,1.2,1.2,1.2'}
429 assert_response :bad_request
432 def test_user_notes_success
433 get :mine, {:display_name=>'test'}
434 assert_response :success
436 get :mine, {:display_name=>'pulibc_test2'}
437 assert_response :success
439 get :mine, {:display_name=>'non-existent'}
440 assert_response :not_found