]> git.openstreetmap.org Git - rails.git/blob - test/functional/notes_controller_test.rb
Stop newly added notes disappearing
[rails.git] / test / functional / notes_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class NotesControllerTest < ActionController::TestCase
4   fixtures :users, :notes, :note_comments
5
6   ##
7   # test all routes which lead to this controller
8   def test_routes
9     assert_routing(
10       { :path => "/api/0.6/notes", :method => :post },
11       { :controller => "notes", :action => "create", :format => "xml" }
12     )
13     assert_routing(
14       { :path => "/api/0.6/notes/1", :method => :get },
15       { :controller => "notes", :action => "show", :id => "1", :format => "xml" }
16     )
17     assert_recognizes(
18       { :controller => "notes", :action => "show", :id => "1", :format => "xml" },
19       { :path => "/api/0.6/notes/1.xml", :method => :get }
20     )
21     assert_routing(
22       { :path => "/api/0.6/notes/1.rss", :method => :get },
23       { :controller => "notes", :action => "show", :id => "1", :format => "rss" }
24     )
25     assert_routing(
26       { :path => "/api/0.6/notes/1.json", :method => :get },
27       { :controller => "notes", :action => "show", :id => "1", :format => "json" }
28     )
29     assert_routing(
30       { :path => "/api/0.6/notes/1.gpx", :method => :get },
31       { :controller => "notes", :action => "show", :id => "1", :format => "gpx" }
32     )
33     assert_routing(
34       { :path => "/api/0.6/notes/1/comment", :method => :post },
35       { :controller => "notes", :action => "comment", :id => "1", :format => "xml" }
36     )
37     assert_routing(
38       { :path => "/api/0.6/notes/1/close", :method => :post },
39       { :controller => "notes", :action => "close", :id => "1", :format => "xml" }
40     )
41     assert_routing(
42       { :path => "/api/0.6/notes/1", :method => :delete },
43       { :controller => "notes", :action => "destroy", :id => "1", :format => "xml" }
44     )
45
46     assert_routing(
47       { :path => "/api/0.6/notes", :method => :get },
48       { :controller => "notes", :action => "index", :format => "xml" }
49     )
50     assert_recognizes(
51       { :controller => "notes", :action => "index", :format => "xml" },
52       { :path => "/api/0.6/notes.xml", :method => :get }
53     )
54     assert_routing(
55       { :path => "/api/0.6/notes.rss", :method => :get },
56       { :controller => "notes", :action => "index", :format => "rss" }
57     )
58     assert_routing(
59       { :path => "/api/0.6/notes.json", :method => :get },
60       { :controller => "notes", :action => "index", :format => "json" }
61     )
62     assert_routing(
63       { :path => "/api/0.6/notes.gpx", :method => :get },
64       { :controller => "notes", :action => "index", :format => "gpx" }
65     )
66
67     assert_routing(
68       { :path => "/api/0.6/notes/search", :method => :get },
69       { :controller => "notes", :action => "search", :format => "xml" }
70     )
71     assert_recognizes(
72       { :controller => "notes", :action => "search", :format => "xml" },
73       { :path => "/api/0.6/notes/search.xml", :method => :get }
74     )
75     assert_routing(
76       { :path => "/api/0.6/notes/search.rss", :method => :get },
77       { :controller => "notes", :action => "search", :format => "rss" }
78     )
79     assert_routing(
80       { :path => "/api/0.6/notes/search.json", :method => :get },
81       { :controller => "notes", :action => "search", :format => "json" }
82     )
83     assert_routing(
84       { :path => "/api/0.6/notes/search.gpx", :method => :get },
85       { :controller => "notes", :action => "search", :format => "gpx" }
86     )
87
88     assert_routing(
89       { :path => "/api/0.6/notes/feed", :method => :get },
90       { :controller => "notes", :action => "feed", :format => "rss" }
91     )
92
93     assert_recognizes(
94       { :controller => "notes", :action => "create" },
95       { :path => "/api/0.6/notes/addPOIexec", :method => :post }
96     )
97     assert_recognizes(
98       { :controller => "notes", :action => "close" },
99       { :path => "/api/0.6/notes/closePOIexec", :method => :post }
100     )
101     assert_recognizes(
102       { :controller => "notes", :action => "comment" },
103       { :path => "/api/0.6/notes/editPOIexec", :method => :post }
104     )
105     assert_recognizes(
106       { :controller => "notes", :action => "index", :format => "gpx" },
107       { :path => "/api/0.6/notes/getGPX", :method => :get }
108     )
109     assert_recognizes(
110       { :controller => "notes", :action => "feed", :format => "rss" },
111       { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
112     )
113
114     assert_routing(
115       { :path => "/user/username/notes", :method => :get },
116       { :controller => "notes", :action => "mine", :display_name => "username" }
117     )
118   end
119
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"}
124       end
125     end
126     assert_response :success
127     js = ActiveSupport::JSON.decode(@response.body)
128     assert_not_nil js
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"]
138
139     get :show, {:id => id, :format => "json"}
140     assert_response :success
141     js = ActiveSupport::JSON.decode(@response.body)
142     assert_not_nil js
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"]
152   end
153
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"}
158       end
159     end
160     assert_response :bad_request
161
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"}
165       end
166     end
167     assert_response :bad_request
168
169     assert_no_difference('Note.count') do
170       assert_no_difference('NoteComment.count') do
171         post :create, {:lat => -1.0, :lon => -1.0}
172       end
173     end
174     assert_response :bad_request
175
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"}
179       end
180     end
181     assert_response :bad_request
182
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"}
186       end
187     end
188     assert_response :bad_request
189   end
190
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"}
194     end
195     assert_response :success
196     js = ActiveSupport::JSON.decode(@response.body)
197     assert_not_nil js
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"]
205
206     get :show, {:id => notes(:open_note_with_comment).id, :format => "json"}
207     assert_response :success
208     js = ActiveSupport::JSON.decode(@response.body)
209     assert_not_nil js
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"]
217   end
218
219   def test_note_comment_create_fail
220     assert_no_difference('NoteComment.count') do
221       post :comment, {:text => "This is an additional comment"}
222     end
223     assert_response :bad_request
224
225     assert_no_difference('NoteComment.count') do
226       post :comment, {:id => notes(:open_note_with_comment).id}
227     end
228     assert_response :bad_request
229
230     assert_no_difference('NoteComment.count') do
231       post :comment, {:id => notes(:open_note_with_comment).id, :text => ""}
232     end
233     assert_response :bad_request
234
235     assert_no_difference('NoteComment.count') do
236       post :comment, {:id => 12345, :text => "This is an additional comment"}
237     end
238     assert_response :not_found
239
240     assert_no_difference('NoteComment.count') do
241       post :comment, {:id => notes(:hidden_note_with_comment).id, :text => "This is an additional comment"}
242     end
243     assert_response :gone
244
245     assert_no_difference('NoteComment.count') do
246       post :comment, {:id => notes(:closed_note_with_comment).id, :text => "This is an additional comment"}
247     end
248     assert_response :conflict
249   end
250
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)
255     assert_not_nil js
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"]
263
264     get :show, {:id => notes(:open_note_with_comment).id, :format => "json"}
265     assert_response :success
266     js = ActiveSupport::JSON.decode(@response.body)
267     assert_not_nil js
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"]
275   end
276
277   def test_note_close_fail
278     post :close
279     assert_response :bad_request
280
281     post :close, {:id => 12345}
282     assert_response :not_found
283
284     post :close, {:id => notes(:hidden_note_with_comment).id}
285     assert_response :gone
286
287     post :close, {:id => notes(:closed_note_with_comment).id}
288     assert_response :conflict
289   end
290
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
295
296     get :show, {:id => notes(:open_note).id, :format => "rss"}
297     assert_response :success
298     assert_equal "application/rss+xml", @response.content_type
299
300     get :show, {:id => notes(:open_note).id, :format => "json"}
301     assert_response :success
302     assert_equal "application/json", @response.content_type
303
304     get :show, {:id => notes(:open_note).id, :format => "gpx"}
305     assert_response :success
306     assert_equal "application/gpx+xml", @response.content_type
307   end
308
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)
313     assert_not_nil js
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"]
318   end
319
320   def test_note_read_fail
321     get :show, {:id => 12345}
322     assert_response :not_found
323
324     get :show, {:id => notes(:hidden_note_with_comment).id}
325     assert_response :gone
326   end
327
328   def test_note_delete_success
329     delete :destroy, {:id => notes(:open_note_with_comment).id}
330     assert_response :success
331
332     get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
333     assert_response :gone
334   end
335
336   def test_note_delete_fail
337     delete :destroy, {:id => 12345}
338     assert_response :not_found
339
340     delete :destroy, {:id => notes(:hidden_note_with_comment).id}
341     assert_response :gone
342   end
343
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
348
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
352
353     get :index, {:bbox => '1,1,1.2,1.2', :format => 'json'}
354     assert_response :success
355     assert_equal "application/json", @response.content_type
356
357     get :index, {:bbox => '1,1,1.2,1.2', :format => 'xml'}
358     assert_response :success
359     assert_equal "application/xml", @response.content_type
360
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
364   end
365
366   def test_get_notes_large_area
367 #    get :index, {:bbox => '-2.5,-2.5,2.5,2.5'}
368 #    assert_response :success
369
370 #    get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5', :t => '2.5'}
371 #    assert_response :success
372
373     get :index, {:bbox => '-10,-10,12,12'}
374     assert_response :bad_request
375
376     get :index, {:l => '-10', :b => '-10', :r => '12', :t => '12'}
377     assert_response :bad_request
378   end
379
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)
385     assert_not_nil js
386     assert_equal "FeatureCollection", js["type"]
387     assert_equal 4, js["features"].count
388
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)
393     assert_not_nil js
394     assert_equal "FeatureCollection", js["type"]
395     assert_equal 4, js["features"].count
396
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)
401     assert_not_nil js
402     assert_equal "FeatureCollection", js["type"]
403     assert_equal 6, js["features"].count
404   end
405
406   def test_get_notes_bad_params
407     get :index, {:bbox => '-2.5,-2.5,2.5'}
408     assert_response :bad_request
409
410     get :index, {:bbox => '-2.5,-2.5,2.5,2.5,2.5'}
411     assert_response :bad_request
412
413     get :index, {:b => '-2.5', :r => '2.5', :t => '2.5'}
414     assert_response :bad_request
415
416     get :index, {:l => '-2.5', :r => '2.5', :t => '2.5'}
417     assert_response :bad_request
418
419     get :index, {:l => '-2.5', :b => '-2.5', :t => '2.5'}
420     assert_response :bad_request
421
422     get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5'}
423     assert_response :bad_request
424   end
425
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
430
431     get :search, {:q => 'note 1', :format => 'json'}
432     assert_response :success
433     assert_equal "application/json", @response.content_type
434
435     get :search, {:q => 'note 1', :format => 'rss'}
436     assert_response :success
437     assert_equal "application/rss+xml", @response.content_type
438
439     get :search, {:q => 'note 1', :format => 'gpx'}
440     assert_response :success
441     assert_equal "application/gpx+xml", @response.content_type
442   end
443
444   def test_search_bad_params
445     get :search
446     assert_response :bad_request
447   end
448
449   def test_rss_success
450     get :feed, {:format => "rss"}
451     assert_response :success
452     assert_equal "application/rss+xml", @response.content_type
453
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
457   end
458
459   def test_rss_fail
460     get :feed, {:bbox => "1,1,1.2"}
461     assert_response :bad_request
462
463     get :feed, {:bbox => "1,1,1.2,1.2,1.2"}
464     assert_response :bad_request
465   end
466
467   def test_user_notes_success
468     get :mine, {:display_name => "test"}
469     assert_response :success
470
471     get :mine, {:display_name => "pulibc_test2"}
472     assert_response :success
473
474     get :mine, {:display_name => "non-existent"}
475     assert_response :not_found  
476   end
477 end