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