]> git.openstreetmap.org Git - rails.git/blob - test/functional/notes_controller_test.rb
Restructure notes URLs according to standard rails conventions
[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, :name => "new_tester", :text => "This is a comment"}
124       end
125     end
126     assert_response :success
127     id = @response.body.sub(/ok/,"").to_i
128
129     get :show, {:id => id, :format => 'json'}
130     assert_response :success
131     js = ActiveSupport::JSON.decode(@response.body)
132     assert_not_nil js
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"]
142   end
143
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"}
148       end
149     end
150     assert_response :bad_request
151
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"}
155       end
156     end
157     assert_response :bad_request
158
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"}
162       end
163     end
164     assert_response :bad_request
165
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"}
169       end
170     end
171     assert_response :bad_request
172
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"}
176       end
177     end
178     assert_response :bad_request
179   end
180
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"}
184     end
185     assert_response :success
186
187     get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
188     assert_response :success
189     js = ActiveSupport::JSON.decode(@response.body)
190     assert_not_nil js
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"]
198   end
199
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"}
203     end
204     assert_response :bad_request
205
206     assert_no_difference('NoteComment.count') do
207       post :comment, {:id => notes(:open_note_with_comment).id, :name => "new_tester2"}
208     end
209     assert_response :bad_request
210
211     assert_no_difference('NoteComment.count') do
212       post :comment, {:id => 12345, :name => "new_tester2", :text => "This is an additional comment"}
213     end
214     assert_response :not_found
215
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"}
218     end
219     assert_response :gone
220   end
221
222   def test_note_close_success
223     post :close, {:id => notes(:open_note_with_comment).id}
224     assert_response :success
225
226     get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
227     assert_response :success
228     js = ActiveSupport::JSON.decode(@response.body)
229     assert_not_nil js
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"]
237   end
238
239   def test_note_close_fail
240     post :close
241     assert_response :bad_request
242
243     post :close, {:id => 12345}
244     assert_response :not_found
245
246     post :close, {:id => notes(:hidden_note_with_comment).id}
247     assert_response :gone
248   end
249
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
254
255     get :show, {:id => notes(:open_note).id, :format => "rss"}
256     assert_response :success
257     assert_equal "application/rss+xml", @response.content_type
258
259     get :show, {:id => notes(:open_note).id, :format => "json"}
260     assert_response :success
261     assert_equal "application/json", @response.content_type
262
263     get :show, {:id => notes(:open_note).id, :format => "gpx"}
264     assert_response :success
265     assert_equal "application/gpx+xml", @response.content_type
266   end
267
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)
272     assert_not_nil js
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"]
277   end
278
279   def test_note_read_fail
280     post :show
281     assert_response :bad_request
282
283     get :show, {:id => 12345}
284     assert_response :not_found
285
286     get :show, {:id => notes(:hidden_note_with_comment).id}
287     assert_response :gone
288   end
289
290   def test_note_delete_success
291     delete :destroy, {:id => notes(:open_note_with_comment).id}
292     assert_response :success
293
294     get :show, {:id => notes(:open_note_with_comment).id, :format => 'json'}
295     assert_response :gone
296   end
297
298   def test_note_delete_fail
299     delete :destroy
300     assert_response :bad_request
301
302     delete :destroy, {:id => 12345}
303     assert_response :not_found
304
305     delete :destroy, {:id => notes(:hidden_note_with_comment).id}
306     assert_response :gone
307   end
308
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
313
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
317
318     get :index, {:bbox => '1,1,1.2,1.2', :format => 'json'}
319     assert_response :success
320     assert_equal "application/json", @response.content_type
321
322     get :index, {:bbox => '1,1,1.2,1.2', :format => 'xml'}
323     assert_response :success
324     assert_equal "application/xml", @response.content_type
325
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
329   end
330
331   def test_get_notes_large_area
332 #    get :index, {:bbox => '-2.5,-2.5,2.5,2.5'}
333 #    assert_response :success
334
335 #    get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5', :t => '2.5'}
336 #    assert_response :success
337
338     get :index, {:bbox => '-10,-10,12,12'}
339     assert_response :bad_request
340
341     get :index, {:l => '-10', :b => '-10', :r => '12', :t => '12'}
342     assert_response :bad_request
343   end
344
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)
350     assert_not_nil js
351     assert_equal "FeatureCollection", js["type"]
352     assert_equal 4, js["features"].count
353
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)
358     assert_not_nil js
359     assert_equal "FeatureCollection", js["type"]
360     assert_equal 4, js["features"].count
361
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)
366     assert_not_nil js
367     assert_equal "FeatureCollection", js["type"]
368     assert_equal 6, js["features"].count
369   end
370
371   def test_get_notes_bad_params
372     get :index, {:bbox => '-2.5,-2.5,2.5'}
373     assert_response :bad_request
374
375     get :index, {:bbox => '-2.5,-2.5,2.5,2.5,2.5'}
376     assert_response :bad_request
377
378     get :index, {:b => '-2.5', :r => '2.5', :t => '2.5'}
379     assert_response :bad_request
380
381     get :index, {:l => '-2.5', :r => '2.5', :t => '2.5'}
382     assert_response :bad_request
383
384     get :index, {:l => '-2.5', :b => '-2.5', :t => '2.5'}
385     assert_response :bad_request
386
387     get :index, {:l => '-2.5', :b => '-2.5', :r => '2.5'}
388     assert_response :bad_request
389   end
390
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
395
396     get :search, {:q => 'note 1', :format => 'json'}
397     assert_response :success
398     assert_equal "application/json", @response.content_type
399
400     get :search, {:q => 'note 1', :format => 'rss'}
401     assert_response :success
402     assert_equal "application/rss+xml", @response.content_type
403
404     get :search, {:q => 'note 1', :format => 'gpx'}
405     assert_response :success
406     assert_equal "application/gpx+xml", @response.content_type
407   end
408
409   def test_search_bad_params
410     get :search
411     assert_response :bad_request
412   end
413
414   def test_rss_success
415     get :feed, {:format => 'rss'}
416     assert_response :success
417     assert_equal "application/rss+xml", @response.content_type
418
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
422   end
423
424   def test_rss_fail
425     get :feed, {:bbox=>'1,1,1.2'}
426     assert_response :bad_request
427
428     get :feed, {:bbox=>'1,1,1.2,1.2,1.2'}
429     assert_response :bad_request
430   end
431
432   def test_user_notes_success
433     get :mine, {:display_name=>'test'}
434     assert_response :success
435
436     get :mine, {:display_name=>'pulibc_test2'}
437     assert_response :success
438
439     get :mine, {:display_name=>'non-existent'}
440     assert_response :not_found  
441   end
442 end