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