]> git.openstreetmap.org Git - rails.git/blob - test/functional/notes_controller_test.rb
Reject attempts to create notes with no comment text
[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 => -1.0, :lon => -1.0, :text => ""}
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 => -100.0, :lon => -1.0, :text => "This is a comment"}
186       end
187     end
188     assert_response :bad_request
189
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"}
193       end
194     end
195     assert_response :bad_request
196   end
197
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"}
201     end
202     assert_response :success
203     js = ActiveSupport::JSON.decode(@response.body)
204     assert_not_nil js
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"]
212
213     get :show, {:id => notes(:open_note_with_comment).id, :format => "json"}
214     assert_response :success
215     js = ActiveSupport::JSON.decode(@response.body)
216     assert_not_nil js
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"]
224   end
225
226   def test_note_comment_create_fail
227     assert_no_difference('NoteComment.count') do
228       post :comment, {:text => "This is an additional comment"}
229     end
230     assert_response :bad_request
231
232     assert_no_difference('NoteComment.count') do
233       post :comment, {:id => notes(:open_note_with_comment).id}
234     end
235     assert_response :bad_request
236
237     assert_no_difference('NoteComment.count') do
238       post :comment, {:id => notes(:open_note_with_comment).id, :text => ""}
239     end
240     assert_response :bad_request
241
242     assert_no_difference('NoteComment.count') do
243       post :comment, {:id => 12345, :text => "This is an additional comment"}
244     end
245     assert_response :not_found
246
247     assert_no_difference('NoteComment.count') do
248       post :comment, {:id => notes(:hidden_note_with_comment).id, :text => "This is an additional comment"}
249     end
250     assert_response :gone
251
252     assert_no_difference('NoteComment.count') do
253       post :comment, {:id => notes(:closed_note_with_comment).id, :text => "This is an additional comment"}
254     end
255     assert_response :conflict
256   end
257
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 :success
261     js = ActiveSupport::JSON.decode(@response.body)
262     assert_not_nil js
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"]
270
271     get :show, {:id => notes(:open_note_with_comment).id, :format => "json"}
272     assert_response :success
273     js = ActiveSupport::JSON.decode(@response.body)
274     assert_not_nil js
275     assert_equal "Feature", js["type"]
276     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
277     assert_equal "closed", js["properties"]["status"]
278     assert_equal 3, js["properties"]["comments"].count
279     assert_equal "closed", js["properties"]["comments"].last["action"]
280     assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
281     assert_nil js["properties"]["comments"].last["user"]
282   end
283
284   def test_note_close_fail
285     post :close
286     assert_response :bad_request
287
288     post :close, {:id => 12345}
289     assert_response :not_found
290
291     post :close, {:id => notes(:hidden_note_with_comment).id}
292     assert_response :gone
293
294     post :close, {:id => notes(:closed_note_with_comment).id}
295     assert_response :conflict
296   end
297
298   def test_note_read_success
299     get :show, {:id => notes(:open_note).id, :format => "xml"}
300     assert_response :success
301     assert_equal "application/xml", @response.content_type
302
303     get :show, {:id => notes(:open_note).id, :format => "rss"}
304     assert_response :success
305     assert_equal "application/rss+xml", @response.content_type
306
307     get :show, {:id => notes(:open_note).id, :format => "json"}
308     assert_response :success
309     assert_equal "application/json", @response.content_type
310
311     get :show, {:id => notes(:open_note).id, :format => "gpx"}
312     assert_response :success
313     assert_equal "application/gpx+xml", @response.content_type
314   end
315
316   def test_note_read_hidden_comment
317     get :show, {:id => notes(:note_with_hidden_comment).id, :format => "json"}
318     assert_response :success
319     js = ActiveSupport::JSON.decode(@response.body)
320     assert_not_nil js
321     assert_equal notes(:note_with_hidden_comment).id, js["properties"]["id"]
322     assert_equal 2, js["properties"]["comments"].count
323     assert_equal "Valid comment for note 5", js["properties"]["comments"][0]["text"]
324     assert_equal "Another valid comment for note 5", js["properties"]["comments"][1]["text"]
325   end
326
327   def test_note_read_fail
328     get :show, {:id => 12345}
329     assert_response :not_found
330
331     get :show, {:id => notes(:hidden_note_with_comment).id}
332     assert_response :gone
333   end
334
335   def test_note_delete_success
336     delete :destroy, {:id => notes(:open_note_with_comment).id}
337     assert_response :success
338
339     get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
340     assert_response :gone
341   end
342
343   def test_note_delete_fail
344     delete :destroy, {:id => 12345}
345     assert_response :not_found
346
347     delete :destroy, {:id => notes(:hidden_note_with_comment).id}
348     assert_response :gone
349   end
350
351   def test_get_notes_success
352 #    get :index, {:bbox => '1,1,1.2,1.2'}
353 #    assert_response :success
354 #    assert_equal "text/javascript", @response.content_type
355
356     get :index, {:bbox => '1,1,1.2,1.2', :format => 'rss'}
357     assert_response :success
358     assert_equal "application/rss+xml", @response.content_type
359
360     get :index, {:bbox => '1,1,1.2,1.2', :format => 'json'}
361     assert_response :success
362     assert_equal "application/json", @response.content_type
363
364     get :index, {:bbox => '1,1,1.2,1.2', :format => 'xml'}
365     assert_response :success
366     assert_equal "application/xml", @response.content_type
367
368     get :index, {:bbox => '1,1,1.2,1.2', :format => 'gpx'}
369     assert_response :success
370     assert_equal "application/gpx+xml", @response.content_type
371   end
372
373   def test_get_notes_large_area
374 #    get :index, {:bbox => '-2.5,-2.5,2.5,2.5'}
375 #    assert_response :success
376
377 #    get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5', :t => '2.5'}
378 #    assert_response :success
379
380     get :index, {:bbox => '-10,-10,12,12'}
381     assert_response :bad_request
382
383     get :index, {:l => '-10', :b => '-10', :r => '12', :t => '12'}
384     assert_response :bad_request
385   end
386
387   def test_get_notes_closed
388     get :index, {:bbox => '1,1,1.7,1.7', :closed => '7', :format => 'json'}
389     assert_response :success
390     assert_equal "application/json", @response.content_type
391     js = ActiveSupport::JSON.decode(@response.body)
392     assert_not_nil js
393     assert_equal "FeatureCollection", js["type"]
394     assert_equal 4, js["features"].count
395
396     get :index, {:bbox => '1,1,1.7,1.7', :closed => '0', :format => 'json'}
397     assert_response :success
398     assert_equal "application/json", @response.content_type
399     js = ActiveSupport::JSON.decode(@response.body)
400     assert_not_nil js
401     assert_equal "FeatureCollection", js["type"]
402     assert_equal 4, js["features"].count
403
404     get :index, {:bbox => '1,1,1.7,1.7', :closed => '-1', :format => 'json'}
405     assert_response :success
406     assert_equal "application/json", @response.content_type
407     js = ActiveSupport::JSON.decode(@response.body)
408     assert_not_nil js
409     assert_equal "FeatureCollection", js["type"]
410     assert_equal 6, js["features"].count
411   end
412
413   def test_get_notes_bad_params
414     get :index, {:bbox => '-2.5,-2.5,2.5'}
415     assert_response :bad_request
416
417     get :index, {:bbox => '-2.5,-2.5,2.5,2.5,2.5'}
418     assert_response :bad_request
419
420     get :index, {:b => '-2.5', :r => '2.5', :t => '2.5'}
421     assert_response :bad_request
422
423     get :index, {:l => '-2.5', :r => '2.5', :t => '2.5'}
424     assert_response :bad_request
425
426     get :index, {:l => '-2.5', :b => '-2.5', :t => '2.5'}
427     assert_response :bad_request
428
429     get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5'}
430     assert_response :bad_request
431   end
432
433   def test_search_success
434     get :search, {:q => 'note 1', :format => 'xml'}
435     assert_response :success
436     assert_equal "application/xml", @response.content_type
437
438     get :search, {:q => 'note 1', :format => 'json'}
439     assert_response :success
440     assert_equal "application/json", @response.content_type
441
442     get :search, {:q => 'note 1', :format => 'rss'}
443     assert_response :success
444     assert_equal "application/rss+xml", @response.content_type
445
446     get :search, {:q => 'note 1', :format => 'gpx'}
447     assert_response :success
448     assert_equal "application/gpx+xml", @response.content_type
449   end
450
451   def test_search_bad_params
452     get :search
453     assert_response :bad_request
454   end
455
456   def test_rss_success
457     get :feed, {:format => "rss"}
458     assert_response :success
459     assert_equal "application/rss+xml", @response.content_type
460
461     get :feed, {:bbox => "1,1,1.2,1.2", :format => "rss"}
462     assert_response :success    
463     assert_equal "application/rss+xml", @response.content_type
464   end
465
466   def test_rss_fail
467     get :feed, {:bbox => "1,1,1.2"}
468     assert_response :bad_request
469
470     get :feed, {:bbox => "1,1,1.2,1.2,1.2"}
471     assert_response :bad_request
472   end
473
474   def test_user_notes_success
475     get :mine, {:display_name => "test"}
476     assert_response :success
477
478     get :mine, {:display_name => "pulibc_test2"}
479     assert_response :success
480
481     get :mine, {:display_name => "non-existent"}
482     assert_response :not_found  
483   end
484 end