]> git.openstreetmap.org Git - rails.git/blob - test/controllers/notes_controller_test.rb
Localisation updates from https://translatewiki.net.
[rails.git] / test / controllers / notes_controller_test.rb
1 require "test_helper"
2
3 class NotesControllerTest < ActionController::TestCase
4   fixtures :users, :user_roles, :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/reopen", :method => :post },
43       { :controller => "notes", :action => "reopen", :id => "1", :format => "xml" }
44     )
45     assert_routing(
46       { :path => "/api/0.6/notes/1", :method => :delete },
47       { :controller => "notes", :action => "destroy", :id => "1", :format => "xml" }
48     )
49
50     assert_routing(
51       { :path => "/api/0.6/notes", :method => :get },
52       { :controller => "notes", :action => "index", :format => "xml" }
53     )
54     assert_recognizes(
55       { :controller => "notes", :action => "index", :format => "xml" },
56       { :path => "/api/0.6/notes.xml", :method => :get }
57     )
58     assert_routing(
59       { :path => "/api/0.6/notes.rss", :method => :get },
60       { :controller => "notes", :action => "index", :format => "rss" }
61     )
62     assert_routing(
63       { :path => "/api/0.6/notes.json", :method => :get },
64       { :controller => "notes", :action => "index", :format => "json" }
65     )
66     assert_routing(
67       { :path => "/api/0.6/notes.gpx", :method => :get },
68       { :controller => "notes", :action => "index", :format => "gpx" }
69     )
70
71     assert_routing(
72       { :path => "/api/0.6/notes/search", :method => :get },
73       { :controller => "notes", :action => "search", :format => "xml" }
74     )
75     assert_recognizes(
76       { :controller => "notes", :action => "search", :format => "xml" },
77       { :path => "/api/0.6/notes/search.xml", :method => :get }
78     )
79     assert_routing(
80       { :path => "/api/0.6/notes/search.rss", :method => :get },
81       { :controller => "notes", :action => "search", :format => "rss" }
82     )
83     assert_routing(
84       { :path => "/api/0.6/notes/search.json", :method => :get },
85       { :controller => "notes", :action => "search", :format => "json" }
86     )
87     assert_routing(
88       { :path => "/api/0.6/notes/search.gpx", :method => :get },
89       { :controller => "notes", :action => "search", :format => "gpx" }
90     )
91
92     assert_routing(
93       { :path => "/api/0.6/notes/feed", :method => :get },
94       { :controller => "notes", :action => "feed", :format => "rss" }
95     )
96
97     assert_recognizes(
98       { :controller => "notes", :action => "create" },
99       { :path => "/api/0.6/notes/addPOIexec", :method => :post }
100     )
101     assert_recognizes(
102       { :controller => "notes", :action => "close" },
103       { :path => "/api/0.6/notes/closePOIexec", :method => :post }
104     )
105     assert_recognizes(
106       { :controller => "notes", :action => "comment" },
107       { :path => "/api/0.6/notes/editPOIexec", :method => :post }
108     )
109     assert_recognizes(
110       { :controller => "notes", :action => "index", :format => "gpx" },
111       { :path => "/api/0.6/notes/getGPX", :method => :get }
112     )
113     assert_recognizes(
114       { :controller => "notes", :action => "feed", :format => "rss" },
115       { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
116     )
117
118     assert_routing(
119       { :path => "/user/username/notes", :method => :get },
120       { :controller => "notes", :action => "mine", :display_name => "username" }
121     )
122   end
123
124   def test_create_success
125     assert_difference "Note.count", 1 do
126       assert_difference "NoteComment.count", 1 do
127         post :create, :lat => -1.0, :lon => -1.0, :text => "This is a comment", :format => "json"
128       end
129     end
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 "open", js["properties"]["status"]
137     assert_equal 1, js["properties"]["comments"].count
138     assert_equal "opened", js["properties"]["comments"].last["action"]
139     assert_equal "This is a comment", js["properties"]["comments"].last["text"]
140     assert_nil js["properties"]["comments"].last["user"]
141     id = js["properties"]["id"]
142
143     get :show, :id => id, :format => "json"
144     assert_response :success
145     js = ActiveSupport::JSON.decode(@response.body)
146     assert_not_nil js
147     assert_equal "Feature", js["type"]
148     assert_equal "Point", js["geometry"]["type"]
149     assert_equal [-1.0, -1.0], js["geometry"]["coordinates"]
150     assert_equal id, js["properties"]["id"]
151     assert_equal "open", js["properties"]["status"]
152     assert_equal 1, js["properties"]["comments"].count
153     assert_equal "opened", js["properties"]["comments"].last["action"]
154     assert_equal "This is a comment", js["properties"]["comments"].last["text"]
155     assert_nil js["properties"]["comments"].last["user"]
156   end
157
158   def test_create_fail
159     assert_no_difference "Note.count" do
160       assert_no_difference "NoteComment.count" do
161         post :create, :lon => -1.0, :text => "This is a comment"
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 => -1.0, :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 => -1.0
176       end
177     end
178     assert_response :bad_request
179
180     assert_no_difference "Note.count" do
181       assert_no_difference "NoteComment.count" do
182         post :create, :lat => -1.0, :lon => -1.0, :text => ""
183       end
184     end
185     assert_response :bad_request
186
187     assert_no_difference "Note.count" do
188       assert_no_difference "NoteComment.count" do
189         post :create, :lat => -100.0, :lon => -1.0, :text => "This is a comment"
190       end
191     end
192     assert_response :bad_request
193
194     assert_no_difference "Note.count" do
195       assert_no_difference "NoteComment.count" do
196         post :create, :lat => -1.0, :lon => -200.0, :text => "This is a comment"
197       end
198     end
199     assert_response :bad_request
200
201     assert_no_difference "Note.count" do
202       assert_no_difference "NoteComment.count" do
203         post :create, :lat => "abc", :lon => -1.0, :text => "This is a comment"
204       end
205     end
206     assert_response :bad_request
207
208     assert_no_difference "Note.count" do
209       assert_no_difference "NoteComment.count" do
210         post :create, :lat => -1.0, :lon => "abc", :text => "This is a comment"
211       end
212     end
213     assert_response :bad_request
214   end
215
216   def test_comment_success
217     assert_difference "NoteComment.count", 1 do
218       assert_no_difference "ActionMailer::Base.deliveries.size" do
219         post :comment, :id => notes(:open_note_with_comment).id, :text => "This is an additional comment", :format => "json"
220       end
221     end
222     assert_response :success
223     js = ActiveSupport::JSON.decode(@response.body)
224     assert_not_nil js
225     assert_equal "Feature", js["type"]
226     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
227     assert_equal "open", js["properties"]["status"]
228     assert_equal 5, js["properties"]["comments"].count
229     assert_equal "commented", js["properties"]["comments"].last["action"]
230     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
231     assert_nil js["properties"]["comments"].last["user"]
232
233     get :show, :id => notes(:open_note_with_comment).id, :format => "json"
234     assert_response :success
235     js = ActiveSupport::JSON.decode(@response.body)
236     assert_not_nil js
237     assert_equal "Feature", js["type"]
238     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
239     assert_equal "open", js["properties"]["status"]
240     assert_equal 5, js["properties"]["comments"].count
241     assert_equal "commented", js["properties"]["comments"].last["action"]
242     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
243     assert_nil js["properties"]["comments"].last["user"]
244
245     assert_difference "NoteComment.count", 1 do
246       assert_difference "ActionMailer::Base.deliveries.size", 2 do
247         post :comment, :id => notes(:note_with_comments_by_users).id, :text => "This is an additional comment", :format => "json"
248       end
249     end
250     assert_response :success
251     js = ActiveSupport::JSON.decode(@response.body)
252     assert_not_nil js
253     assert_equal "Feature", js["type"]
254     assert_equal notes(:note_with_comments_by_users).id, js["properties"]["id"]
255     assert_equal "open", js["properties"]["status"]
256     assert_equal 3, js["properties"]["comments"].count
257     assert_equal "commented", js["properties"]["comments"].last["action"]
258     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
259     assert_nil js["properties"]["comments"].last["user"]
260
261     email = ActionMailer::Base.deliveries.find { |e| e.to.first == "test@openstreetmap.org" }
262     assert_not_nil email
263     assert_equal 1, email.to.length
264     assert_equal "[OpenStreetMap] An anonymous user has commented on one of your notes", email.subject
265
266     email = ActionMailer::Base.deliveries.find { |e| e.to.first == "public@OpenStreetMap.org" }
267     assert_not_nil email
268     assert_equal 1, email.to.length
269     assert_equal "[OpenStreetMap] An anonymous user has commented on a note you are interested in", email.subject
270
271     get :show, :id => notes(:note_with_comments_by_users).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(:note_with_comments_by_users).id, js["properties"]["id"]
277     assert_equal "open", js["properties"]["status"]
278     assert_equal 3, js["properties"]["comments"].count
279     assert_equal "commented", js["properties"]["comments"].last["action"]
280     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
281     assert_nil js["properties"]["comments"].last["user"]
282
283     ActionMailer::Base.deliveries.clear
284
285     basic_authorization(users(:public_user).email, "test")
286
287     assert_difference "NoteComment.count", 1 do
288       assert_difference "ActionMailer::Base.deliveries.size", 2 do
289         post :comment, :id => notes(:note_with_comments_by_users).id, :text => "This is an additional comment", :format => "json"
290       end
291     end
292     assert_response :success
293     js = ActiveSupport::JSON.decode(@response.body)
294     assert_not_nil js
295     assert_equal "Feature", js["type"]
296     assert_equal notes(:note_with_comments_by_users).id, js["properties"]["id"]
297     assert_equal "open", js["properties"]["status"]
298     assert_equal 4, js["properties"]["comments"].count
299     assert_equal "commented", js["properties"]["comments"].last["action"]
300     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
301     assert_equal "test2", js["properties"]["comments"].last["user"]
302
303     email = ActionMailer::Base.deliveries.find { |e| e.to.first == "test@openstreetmap.org" }
304     assert_not_nil email
305     assert_equal 1, email.to.length
306     assert_equal "[OpenStreetMap] test2 has commented on one of your notes", email.subject
307     assert_equal "test@openstreetmap.org", email.to.first
308
309     email = ActionMailer::Base.deliveries.find { |e| e.to.first == "public@OpenStreetMap.org" }
310     assert_not_nil email
311     assert_equal 1, email.to.length
312     assert_equal "[OpenStreetMap] test2 has commented on a note you are interested in", email.subject
313
314     get :show, :id => notes(:note_with_comments_by_users).id, :format => "json"
315     assert_response :success
316     js = ActiveSupport::JSON.decode(@response.body)
317     assert_not_nil js
318     assert_equal "Feature", js["type"]
319     assert_equal notes(:note_with_comments_by_users).id, js["properties"]["id"]
320     assert_equal "open", js["properties"]["status"]
321     assert_equal 4, js["properties"]["comments"].count
322     assert_equal "commented", js["properties"]["comments"].last["action"]
323     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
324     assert_equal "test2", js["properties"]["comments"].last["user"]
325
326     ActionMailer::Base.deliveries.clear
327   end
328
329   def test_comment_fail
330     assert_no_difference "NoteComment.count" do
331       post :comment, :text => "This is an additional comment"
332     end
333     assert_response :bad_request
334
335     assert_no_difference "NoteComment.count" do
336       post :comment, :id => notes(:open_note_with_comment).id
337     end
338     assert_response :bad_request
339
340     assert_no_difference "NoteComment.count" do
341       post :comment, :id => notes(:open_note_with_comment).id, :text => ""
342     end
343     assert_response :bad_request
344
345     assert_no_difference "NoteComment.count" do
346       post :comment, :id => 12345, :text => "This is an additional comment"
347     end
348     assert_response :not_found
349
350     assert_no_difference "NoteComment.count" do
351       post :comment, :id => notes(:hidden_note_with_comment).id, :text => "This is an additional comment"
352     end
353     assert_response :gone
354
355     assert_no_difference "NoteComment.count" do
356       post :comment, :id => notes(:closed_note_with_comment).id, :text => "This is an additional comment"
357     end
358     assert_response :conflict
359   end
360
361   def test_close_success
362     post :close, :id => notes(:open_note_with_comment).id, :text => "This is a close comment", :format => "json"
363     assert_response :unauthorized
364
365     basic_authorization(users(:public_user).email, "test")
366
367     post :close, :id => notes(:open_note_with_comment).id, :text => "This is a close comment", :format => "json"
368     assert_response :success
369     js = ActiveSupport::JSON.decode(@response.body)
370     assert_not_nil js
371     assert_equal "Feature", js["type"]
372     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
373     assert_equal "closed", js["properties"]["status"]
374     assert_equal 5, js["properties"]["comments"].count
375     assert_equal "closed", js["properties"]["comments"].last["action"]
376     assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
377     assert_equal "test2", js["properties"]["comments"].last["user"]
378
379     get :show, :id => notes(:open_note_with_comment).id, :format => "json"
380     assert_response :success
381     js = ActiveSupport::JSON.decode(@response.body)
382     assert_not_nil js
383     assert_equal "Feature", js["type"]
384     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
385     assert_equal "closed", js["properties"]["status"]
386     assert_equal 5, js["properties"]["comments"].count
387     assert_equal "closed", js["properties"]["comments"].last["action"]
388     assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
389     assert_equal "test2", js["properties"]["comments"].last["user"]
390   end
391
392   def test_close_fail
393     post :close
394     assert_response :unauthorized
395
396     basic_authorization(users(:public_user).email, "test")
397
398     post :close
399     assert_response :bad_request
400
401     post :close, :id => 12345
402     assert_response :not_found
403
404     post :close, :id => notes(:hidden_note_with_comment).id
405     assert_response :gone
406
407     post :close, :id => notes(:closed_note_with_comment).id
408     assert_response :conflict
409   end
410
411   def test_reopen_success
412     post :reopen, :id => notes(:closed_note_with_comment).id, :text => "This is a reopen comment", :format => "json"
413     assert_response :unauthorized
414
415     basic_authorization(users(:public_user).email, "test")
416
417     post :reopen, :id => notes(:closed_note_with_comment).id, :text => "This is a reopen comment", :format => "json"
418     assert_response :success
419     js = ActiveSupport::JSON.decode(@response.body)
420     assert_not_nil js
421     assert_equal "Feature", js["type"]
422     assert_equal notes(:closed_note_with_comment).id, js["properties"]["id"]
423     assert_equal "open", js["properties"]["status"]
424     assert_equal 2, js["properties"]["comments"].count
425     assert_equal "reopened", js["properties"]["comments"].last["action"]
426     assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
427     assert_equal "test2", js["properties"]["comments"].last["user"]
428
429     get :show, :id => notes(:closed_note_with_comment).id, :format => "json"
430     assert_response :success
431     js = ActiveSupport::JSON.decode(@response.body)
432     assert_not_nil js
433     assert_equal "Feature", js["type"]
434     assert_equal notes(:closed_note_with_comment).id, js["properties"]["id"]
435     assert_equal "open", js["properties"]["status"]
436     assert_equal 2, js["properties"]["comments"].count
437     assert_equal "reopened", js["properties"]["comments"].last["action"]
438     assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
439     assert_equal "test2", js["properties"]["comments"].last["user"]
440   end
441
442   def test_reopen_fail
443     post :reopen, :id => notes(:hidden_note_with_comment).id
444     assert_response :unauthorized
445
446     basic_authorization(users(:public_user).email, "test")
447
448     post :reopen, :id => 12345
449     assert_response :not_found
450
451     post :reopen, :id => notes(:hidden_note_with_comment).id
452     assert_response :gone
453
454     post :reopen, :id => notes(:open_note_with_comment).id
455     assert_response :conflict
456   end
457
458   def test_show_success
459     get :show, :id => notes(:open_note).id, :format => "xml"
460     assert_response :success
461     assert_equal "application/xml", @response.content_type
462     assert_select "osm", :count => 1 do
463       assert_select "note[lat='#{notes(:open_note).lat}'][lon='#{notes(:open_note).lon}']", :count => 1 do
464         assert_select "id", notes(:open_note).id
465         assert_select "url", note_url(notes(:open_note), :format => "xml")
466         assert_select "comment_url", comment_note_url(notes(:open_note), :format => "xml")
467         assert_select "close_url", close_note_url(notes(:open_note), :format => "xml")
468         assert_select "date_created", notes(:open_note).created_at.to_s
469         assert_select "status", notes(:open_note).status
470         assert_select "comments", :count => 1 do
471           assert_select "comment", :count => 1
472         end
473       end
474     end
475
476     get :show, :id => notes(:open_note).id, :format => "rss"
477     assert_response :success
478     assert_equal "application/rss+xml", @response.content_type
479     assert_select "rss", :count => 1 do
480       assert_select "channel", :count => 1 do
481         assert_select "item", :count => 1 do
482           assert_select "link", browse_note_url(notes(:open_note))
483           assert_select "guid", note_url(notes(:open_note))
484           assert_select "pubDate", notes(:open_note).created_at.to_s(:rfc822)
485           #          assert_select "geo:lat", notes(:open_note).lat.to_s
486           #          assert_select "geo:long", notes(:open_note).lon
487           #          assert_select "georss:point", "#{notes(:open_note).lon} #{notes(:open_note).lon}"
488         end
489       end
490     end
491
492     get :show, :id => notes(:open_note).id, :format => "json"
493     assert_response :success
494     assert_equal "application/json", @response.content_type
495     js = ActiveSupport::JSON.decode(@response.body)
496     assert_not_nil js
497     assert_equal "Feature", js["type"]
498     assert_equal "Point", js["geometry"]["type"]
499     assert_equal notes(:open_note).lat, js["geometry"]["coordinates"][0]
500     assert_equal notes(:open_note).lon, js["geometry"]["coordinates"][1]
501     assert_equal notes(:open_note).id, js["properties"]["id"]
502     assert_equal note_url(notes(:open_note), :format => "json"), js["properties"]["url"]
503     assert_equal comment_note_url(notes(:open_note), :format => "json"), js["properties"]["comment_url"]
504     assert_equal close_note_url(notes(:open_note), :format => "json"), js["properties"]["close_url"]
505     assert_equal notes(:open_note).created_at, js["properties"]["date_created"]
506     assert_equal notes(:open_note).status, js["properties"]["status"]
507
508     get :show, :id => notes(:open_note).id, :format => "gpx"
509     assert_response :success
510     assert_equal "application/gpx+xml", @response.content_type
511     assert_select "gpx", :count => 1 do
512       assert_select "wpt[lat='#{notes(:open_note).lat}'][lon='#{notes(:open_note).lon}']", :count => 1 do
513         assert_select "time", :count => 1
514         assert_select "name", "Note: #{notes(:open_note).id}"
515         assert_select "desc", :count => 1
516         assert_select "link[href='http://www.openstreetmap.org/note/#{notes(:open_note).id}']", :count => 1
517         assert_select "extensions", :count => 1 do
518           assert_select "id", notes(:open_note).id
519           assert_select "url", note_url(notes(:open_note), :format => "gpx")
520           assert_select "comment_url", comment_note_url(notes(:open_note), :format => "gpx")
521           assert_select "close_url", close_note_url(notes(:open_note), :format => "gpx")
522         end
523       end
524     end
525   end
526
527   def test_show_hidden_comment
528     get :show, :id => notes(:note_with_hidden_comment).id, :format => "json"
529     assert_response :success
530     js = ActiveSupport::JSON.decode(@response.body)
531     assert_not_nil js
532     assert_equal "Feature", js["type"]
533     assert_equal notes(:note_with_hidden_comment).id, js["properties"]["id"]
534     assert_equal 2, js["properties"]["comments"].count
535     assert_equal "Valid comment for note 5", js["properties"]["comments"][0]["text"]
536     assert_equal "Another valid comment for note 5", js["properties"]["comments"][1]["text"]
537   end
538
539   def test_show_fail
540     get :show, :id => 12345
541     assert_response :not_found
542
543     get :show, :id => notes(:hidden_note_with_comment).id
544     assert_response :gone
545   end
546
547   def test_destroy_success
548     delete :destroy, :id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"
549     assert_response :unauthorized
550
551     basic_authorization(users(:public_user).email, "test")
552
553     delete :destroy, :id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"
554     assert_response :forbidden
555
556     basic_authorization(users(:moderator_user).email, "test")
557
558     delete :destroy, :id => notes(:open_note_with_comment).id, :text => "This is a hide comment", :format => "json"
559     assert_response :success
560     js = ActiveSupport::JSON.decode(@response.body)
561     assert_not_nil js
562     assert_equal "Feature", js["type"]
563     assert_equal notes(:open_note_with_comment).id, js["properties"]["id"]
564     assert_equal "hidden", js["properties"]["status"]
565     assert_equal 5, js["properties"]["comments"].count
566     assert_equal "hidden", js["properties"]["comments"].last["action"]
567     assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
568     assert_equal "moderator", js["properties"]["comments"].last["user"]
569
570     get :show, :id => notes(:open_note_with_comment).id, :format => "json"
571     assert_response :gone
572   end
573
574   def test_destroy_fail
575     delete :destroy, :id => 12345, :format => "json"
576     assert_response :unauthorized
577
578     basic_authorization(users(:public_user).email, "test")
579
580     delete :destroy, :id => 12345, :format => "json"
581     assert_response :forbidden
582
583     basic_authorization(users(:moderator_user).email, "test")
584
585     delete :destroy, :id => 12345, :format => "json"
586     assert_response :not_found
587
588     delete :destroy, :id => notes(:hidden_note_with_comment).id, :format => "json"
589     assert_response :gone
590   end
591
592   def test_index_success
593     get :index, :bbox => "1,1,1.2,1.2", :format => "rss"
594     assert_response :success
595     assert_equal "application/rss+xml", @response.content_type
596     assert_select "rss", :count => 1 do
597       assert_select "channel", :count => 1 do
598         assert_select "item", :count => 2
599       end
600     end
601
602     get :index, :bbox => "1,1,1.2,1.2", :format => "json"
603     assert_response :success
604     assert_equal "application/json", @response.content_type
605     js = ActiveSupport::JSON.decode(@response.body)
606     assert_not_nil js
607     assert_equal "FeatureCollection", js["type"]
608     assert_equal 2, js["features"].count
609
610     get :index, :bbox => "1,1,1.2,1.2", :format => "xml"
611     assert_response :success
612     assert_equal "application/xml", @response.content_type
613     assert_select "osm", :count => 1 do
614       assert_select "note", :count => 2
615     end
616
617     get :index, :bbox => "1,1,1.2,1.2", :format => "gpx"
618     assert_response :success
619     assert_equal "application/gpx+xml", @response.content_type
620     assert_select "gpx", :count => 1 do
621       assert_select "wpt", :count => 2
622     end
623   end
624
625   def test_index_limit
626     get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss"
627     assert_response :success
628     assert_equal "application/rss+xml", @response.content_type
629     assert_select "rss", :count => 1 do
630       assert_select "channel", :count => 1 do
631         assert_select "item", :count => 1
632       end
633     end
634
635     get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json"
636     assert_response :success
637     assert_equal "application/json", @response.content_type
638     js = ActiveSupport::JSON.decode(@response.body)
639     assert_not_nil js
640     assert_equal "FeatureCollection", js["type"]
641     assert_equal 1, js["features"].count
642
643     get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml"
644     assert_response :success
645     assert_equal "application/xml", @response.content_type
646     assert_select "osm", :count => 1 do
647       assert_select "note", :count => 1
648     end
649
650     get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx"
651     assert_response :success
652     assert_equal "application/gpx+xml", @response.content_type
653     assert_select "gpx", :count => 1 do
654       assert_select "wpt", :count => 1
655     end
656   end
657
658   def test_index_empty_area
659     get :index, :bbox => "5,5,5.1,5.1", :format => "rss"
660     assert_response :success
661     assert_equal "application/rss+xml", @response.content_type
662     assert_select "rss", :count => 1 do
663       assert_select "channel", :count => 1 do
664         assert_select "item", :count => 0
665       end
666     end
667
668     get :index, :bbox => "5,5,5.1,5.1", :format => "json"
669     assert_response :success
670     assert_equal "application/json", @response.content_type
671     js = ActiveSupport::JSON.decode(@response.body)
672     assert_not_nil js
673     assert_equal "FeatureCollection", js["type"]
674     assert_equal 0, js["features"].count
675
676     get :index, :bbox => "5,5,5.1,5.1", :format => "xml"
677     assert_response :success
678     assert_equal "application/xml", @response.content_type
679     assert_select "osm", :count => 1 do
680       assert_select "note", :count => 0
681     end
682
683     get :index, :bbox => "5,5,5.1,5.1", :format => "gpx"
684     assert_response :success
685     assert_equal "application/gpx+xml", @response.content_type
686     assert_select "gpx", :count => 1 do
687       assert_select "wpt", :count => 0
688     end
689   end
690
691   def test_index_large_area
692     get :index, :bbox => "-2.5,-2.5,2.5,2.5", :format => :json
693     assert_response :success
694     assert_equal "application/json", @response.content_type
695
696     get :index, :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json
697     assert_response :success
698     assert_equal "application/json", @response.content_type
699
700     get :index, :bbox => "-10,-10,12,12", :format => :json
701     assert_response :bad_request
702     assert_equal "text/plain", @response.content_type
703
704     get :index, :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json
705     assert_response :bad_request
706     assert_equal "text/plain", @response.content_type
707   end
708
709   def test_index_closed
710     get :index, :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json"
711     assert_response :success
712     assert_equal "application/json", @response.content_type
713     js = ActiveSupport::JSON.decode(@response.body)
714     assert_not_nil js
715     assert_equal "FeatureCollection", js["type"]
716     assert_equal 4, js["features"].count
717
718     get :index, :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json"
719     assert_response :success
720     assert_equal "application/json", @response.content_type
721     js = ActiveSupport::JSON.decode(@response.body)
722     assert_not_nil js
723     assert_equal "FeatureCollection", js["type"]
724     assert_equal 4, js["features"].count
725
726     get :index, :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json"
727     assert_response :success
728     assert_equal "application/json", @response.content_type
729     js = ActiveSupport::JSON.decode(@response.body)
730     assert_not_nil js
731     assert_equal "FeatureCollection", js["type"]
732     assert_equal 6, js["features"].count
733   end
734
735   def test_index_bad_params
736     get :index, :bbox => "-2.5,-2.5,2.5"
737     assert_response :bad_request
738
739     get :index, :bbox => "-2.5,-2.5,2.5,2.5,2.5"
740     assert_response :bad_request
741
742     get :index, :b => "-2.5", :r => "2.5", :t => "2.5"
743     assert_response :bad_request
744
745     get :index, :l => "-2.5", :r => "2.5", :t => "2.5"
746     assert_response :bad_request
747
748     get :index, :l => "-2.5", :b => "-2.5", :t => "2.5"
749     assert_response :bad_request
750
751     get :index, :l => "-2.5", :b => "-2.5", :r => "2.5"
752     assert_response :bad_request
753
754     get :index, :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json"
755     assert_response :bad_request
756
757     get :index, :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json"
758     assert_response :bad_request
759   end
760
761   def test_search_success
762     get :search, :q => "note 1", :format => "xml"
763     assert_response :success
764     assert_equal "application/xml", @response.content_type
765     assert_select "osm", :count => 1 do
766       assert_select "note", :count => 1
767     end
768
769     get :search, :q => "note 1", :format => "json"
770     assert_response :success
771     assert_equal "application/json", @response.content_type
772     js = ActiveSupport::JSON.decode(@response.body)
773     assert_not_nil js
774     assert_equal "FeatureCollection", js["type"]
775     assert_equal 1, js["features"].count
776
777     get :search, :q => "note 1", :format => "rss"
778     assert_response :success
779     assert_equal "application/rss+xml", @response.content_type
780     assert_select "rss", :count => 1 do
781       assert_select "channel", :count => 1 do
782         assert_select "item", :count => 1
783       end
784     end
785
786     get :search, :q => "note 1", :format => "gpx"
787     assert_response :success
788     assert_equal "application/gpx+xml", @response.content_type
789     assert_select "gpx", :count => 1 do
790       assert_select "wpt", :count => 1
791     end
792   end
793
794   def test_search_no_match
795     get :search, :q => "no match", :format => "xml"
796     assert_response :success
797     assert_equal "application/xml", @response.content_type
798     assert_select "osm", :count => 1 do
799       assert_select "note", :count => 0
800     end
801
802     get :search, :q => "no match", :format => "json"
803     assert_response :success
804     assert_equal "application/json", @response.content_type
805     js = ActiveSupport::JSON.decode(@response.body)
806     assert_not_nil js
807     assert_equal "FeatureCollection", js["type"]
808     assert_equal 0, js["features"].count
809
810     get :search, :q => "no match", :format => "rss"
811     assert_response :success
812     assert_equal "application/rss+xml", @response.content_type
813     assert_select "rss", :count => 1 do
814       assert_select "channel", :count => 1 do
815         assert_select "item", :count => 0
816       end
817     end
818
819     get :search, :q => "no match", :format => "gpx"
820     assert_response :success
821     assert_equal "application/gpx+xml", @response.content_type
822     assert_select "gpx", :count => 1 do
823       assert_select "wpt", :count => 0
824     end
825   end
826
827   def test_search_bad_params
828     get :search
829     assert_response :bad_request
830
831     get :search, :q => "no match", :limit => "0", :format => "json"
832     assert_response :bad_request
833
834     get :search, :q => "no match", :limit => "10001", :format => "json"
835     assert_response :bad_request
836   end
837
838   def test_feed_success
839     get :feed, :format => "rss"
840     assert_response :success
841     assert_equal "application/rss+xml", @response.content_type
842     assert_select "rss", :count => 1 do
843       assert_select "channel", :count => 1 do
844         assert_select "item", :count => 10
845       end
846     end
847
848     get :feed, :bbox => "1,1,1.2,1.2", :format => "rss"
849     assert_response :success
850     assert_equal "application/rss+xml", @response.content_type
851     assert_select "rss", :count => 1 do
852       assert_select "channel", :count => 1 do
853         assert_select "item", :count => 5
854       end
855     end
856   end
857
858   def test_feed_fail
859     get :feed, :bbox => "1,1,1.2", :format => "rss"
860     assert_response :bad_request
861
862     get :feed, :bbox => "1,1,1.2,1.2,1.2", :format => "rss"
863     assert_response :bad_request
864
865     get :feed, :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss"
866     assert_response :bad_request
867
868     get :feed, :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss"
869     assert_response :bad_request
870   end
871
872   def test_mine_success
873     get :mine, :display_name => "test"
874     assert_response :success
875     assert_select "table.note_list tr", :count => 2
876
877     get :mine, :display_name => "pulibc_test2"
878     assert_response :success
879     assert_select "table.note_list tr", :count => 3
880
881     get :mine, :display_name => "non-existent"
882     assert_response :not_found
883
884     session[:user] = users(:moderator_user).id
885
886     get :mine, :display_name => "test"
887     assert_response :success
888     assert_select "table.note_list tr", :count => 2
889
890     get :mine, :display_name => "pulibc_test2"
891     assert_response :success
892     assert_select "table.note_list tr", :count => 4
893
894     get :mine, :display_name => "non-existent"
895     assert_response :not_found
896   end
897 end