]> git.openstreetmap.org Git - rails.git/blob - test/controllers/notes_controller_test.rb
Use users factory for user_blocks_controller tests.
[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{^http://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, :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, :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, :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, :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, :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, :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, :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, :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, :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, :lat => -1.0, :lon => "abc", :text => "This is a comment"
215       end
216     end
217     assert_response :bad_request
218   end
219
220   def test_comment_success
221     open_note_with_comment = create(:note_with_comments)
222     assert_difference "NoteComment.count", 1 do
223       assert_no_difference "ActionMailer::Base.deliveries.size" do
224         post :comment, :id => open_note_with_comment.id, :text => "This is an additional comment", :format => "json"
225       end
226     end
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 open_note_with_comment.id, js["properties"]["id"]
232     assert_equal "open", js["properties"]["status"]
233     assert_equal 2, js["properties"]["comments"].count
234     assert_equal "commented", js["properties"]["comments"].last["action"]
235     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
236     assert_nil js["properties"]["comments"].last["user"]
237
238     get :show, :id => open_note_with_comment.id, :format => "json"
239     assert_response :success
240     js = ActiveSupport::JSON.decode(@response.body)
241     assert_not_nil js
242     assert_equal "Feature", js["type"]
243     assert_equal open_note_with_comment.id, js["properties"]["id"]
244     assert_equal "open", js["properties"]["status"]
245     assert_equal 2, js["properties"]["comments"].count
246     assert_equal "commented", js["properties"]["comments"].last["action"]
247     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
248     assert_nil js["properties"]["comments"].last["user"]
249
250     # Ensure that emails are sent to users
251     first_user = create(:user)
252     second_user = create(:user)
253     third_user = create(:user)
254
255     note_with_comments_by_users = create(:note) do |note|
256       create(:note_comment, :note => note, :author => first_user)
257       create(:note_comment, :note => note, :author => second_user)
258     end
259     assert_difference "NoteComment.count", 1 do
260       assert_difference "ActionMailer::Base.deliveries.size", 2 do
261         post :comment, :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json"
262       end
263     end
264     assert_response :success
265     js = ActiveSupport::JSON.decode(@response.body)
266     assert_not_nil js
267     assert_equal "Feature", js["type"]
268     assert_equal note_with_comments_by_users.id, js["properties"]["id"]
269     assert_equal "open", js["properties"]["status"]
270     assert_equal 3, js["properties"]["comments"].count
271     assert_equal "commented", js["properties"]["comments"].last["action"]
272     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
273     assert_nil js["properties"]["comments"].last["user"]
274
275     email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
276     assert_not_nil email
277     assert_equal 1, email.to.length
278     assert_equal "[OpenStreetMap] An anonymous user has commented on one of your notes", email.subject
279
280     email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
281     assert_not_nil email
282     assert_equal 1, email.to.length
283     assert_equal "[OpenStreetMap] An anonymous user has commented on a note you are interested in", email.subject
284
285     get :show, :id => note_with_comments_by_users.id, :format => "json"
286     assert_response :success
287     js = ActiveSupport::JSON.decode(@response.body)
288     assert_not_nil js
289     assert_equal "Feature", js["type"]
290     assert_equal note_with_comments_by_users.id, js["properties"]["id"]
291     assert_equal "open", js["properties"]["status"]
292     assert_equal 3, js["properties"]["comments"].count
293     assert_equal "commented", js["properties"]["comments"].last["action"]
294     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
295     assert_nil js["properties"]["comments"].last["user"]
296
297     ActionMailer::Base.deliveries.clear
298
299     basic_authorization(third_user.email, "test")
300
301     assert_difference "NoteComment.count", 1 do
302       assert_difference "ActionMailer::Base.deliveries.size", 2 do
303         post :comment, :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json"
304       end
305     end
306     assert_response :success
307     js = ActiveSupport::JSON.decode(@response.body)
308     assert_not_nil js
309     assert_equal "Feature", js["type"]
310     assert_equal note_with_comments_by_users.id, js["properties"]["id"]
311     assert_equal "open", js["properties"]["status"]
312     assert_equal 4, js["properties"]["comments"].count
313     assert_equal "commented", js["properties"]["comments"].last["action"]
314     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
315     assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
316
317     email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
318     assert_not_nil email
319     assert_equal 1, email.to.length
320     assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on one of your notes", email.subject
321     assert_equal first_user.email, email.to.first
322
323     email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
324     assert_not_nil email
325     assert_equal 1, email.to.length
326     assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on a note you are interested in", email.subject
327
328     get :show, :id => note_with_comments_by_users.id, :format => "json"
329     assert_response :success
330     js = ActiveSupport::JSON.decode(@response.body)
331     assert_not_nil js
332     assert_equal "Feature", js["type"]
333     assert_equal note_with_comments_by_users.id, js["properties"]["id"]
334     assert_equal "open", js["properties"]["status"]
335     assert_equal 4, js["properties"]["comments"].count
336     assert_equal "commented", js["properties"]["comments"].last["action"]
337     assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
338     assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
339
340     ActionMailer::Base.deliveries.clear
341   end
342
343   def test_comment_fail
344     open_note_with_comment = create(:note_with_comments)
345
346     assert_no_difference "NoteComment.count" do
347       post :comment, :text => "This is an additional comment"
348     end
349     assert_response :bad_request
350
351     assert_no_difference "NoteComment.count" do
352       post :comment, :id => open_note_with_comment.id
353     end
354     assert_response :bad_request
355
356     assert_no_difference "NoteComment.count" do
357       post :comment, :id => open_note_with_comment.id, :text => ""
358     end
359     assert_response :bad_request
360
361     assert_no_difference "NoteComment.count" do
362       post :comment, :id => 12345, :text => "This is an additional comment"
363     end
364     assert_response :not_found
365
366     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
367
368     assert_no_difference "NoteComment.count" do
369       post :comment, :id => hidden_note_with_comment.id, :text => "This is an additional comment"
370     end
371     assert_response :gone
372
373     closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
374
375     assert_no_difference "NoteComment.count" do
376       post :comment, :id => closed_note_with_comment.id, :text => "This is an additional comment"
377     end
378     assert_response :conflict
379   end
380
381   def test_close_success
382     open_note_with_comment = create(:note_with_comments)
383     user = create(:user)
384
385     post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
386     assert_response :unauthorized
387
388     basic_authorization(user.email, "test")
389
390     post :close, :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json"
391     assert_response :success
392     js = ActiveSupport::JSON.decode(@response.body)
393     assert_not_nil js
394     assert_equal "Feature", js["type"]
395     assert_equal open_note_with_comment.id, js["properties"]["id"]
396     assert_equal "closed", js["properties"]["status"]
397     assert_equal 2, js["properties"]["comments"].count
398     assert_equal "closed", js["properties"]["comments"].last["action"]
399     assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
400     assert_equal user.display_name, js["properties"]["comments"].last["user"]
401
402     get :show, :id => open_note_with_comment.id, :format => "json"
403     assert_response :success
404     js = ActiveSupport::JSON.decode(@response.body)
405     assert_not_nil js
406     assert_equal "Feature", js["type"]
407     assert_equal open_note_with_comment.id, js["properties"]["id"]
408     assert_equal "closed", js["properties"]["status"]
409     assert_equal 2, js["properties"]["comments"].count
410     assert_equal "closed", js["properties"]["comments"].last["action"]
411     assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
412     assert_equal user.display_name, js["properties"]["comments"].last["user"]
413   end
414
415   def test_close_fail
416     post :close
417     assert_response :unauthorized
418
419     basic_authorization(create(:user).email, "test")
420
421     post :close
422     assert_response :bad_request
423
424     post :close, :id => 12345
425     assert_response :not_found
426
427     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
428
429     post :close, :id => hidden_note_with_comment.id
430     assert_response :gone
431
432     closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
433
434     post :close, :id => closed_note_with_comment.id
435     assert_response :conflict
436   end
437
438   def test_reopen_success
439     closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
440     user = create(:user)
441
442     post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
443     assert_response :unauthorized
444
445     basic_authorization(user.email, "test")
446
447     post :reopen, :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json"
448     assert_response :success
449     js = ActiveSupport::JSON.decode(@response.body)
450     assert_not_nil js
451     assert_equal "Feature", js["type"]
452     assert_equal closed_note_with_comment.id, js["properties"]["id"]
453     assert_equal "open", js["properties"]["status"]
454     assert_equal 2, js["properties"]["comments"].count
455     assert_equal "reopened", js["properties"]["comments"].last["action"]
456     assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
457     assert_equal user.display_name, js["properties"]["comments"].last["user"]
458
459     get :show, :id => closed_note_with_comment.id, :format => "json"
460     assert_response :success
461     js = ActiveSupport::JSON.decode(@response.body)
462     assert_not_nil js
463     assert_equal "Feature", js["type"]
464     assert_equal closed_note_with_comment.id, js["properties"]["id"]
465     assert_equal "open", js["properties"]["status"]
466     assert_equal 2, js["properties"]["comments"].count
467     assert_equal "reopened", js["properties"]["comments"].last["action"]
468     assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
469     assert_equal user.display_name, js["properties"]["comments"].last["user"]
470   end
471
472   def test_reopen_fail
473     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
474
475     post :reopen, :id => hidden_note_with_comment.id
476     assert_response :unauthorized
477
478     basic_authorization(create(:user).email, "test")
479
480     post :reopen, :id => 12345
481     assert_response :not_found
482
483     post :reopen, :id => hidden_note_with_comment.id
484     assert_response :gone
485
486     open_note_with_comment = create(:note_with_comments)
487
488     post :reopen, :id => open_note_with_comment.id
489     assert_response :conflict
490   end
491
492   def test_show_success
493     open_note = create(:note_with_comments)
494
495     get :show, :id => open_note.id, :format => "xml"
496     assert_response :success
497     assert_equal "application/xml", @response.content_type
498     assert_select "osm", :count => 1 do
499       assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
500         assert_select "id", open_note.id.to_s
501         assert_select "url", note_url(open_note, :format => "xml")
502         assert_select "comment_url", comment_note_url(open_note, :format => "xml")
503         assert_select "close_url", close_note_url(open_note, :format => "xml")
504         assert_select "date_created", open_note.created_at.to_s
505         assert_select "status", open_note.status
506         assert_select "comments", :count => 1 do
507           assert_select "comment", :count => 1
508         end
509       end
510     end
511
512     get :show, :id => open_note.id, :format => "rss"
513     assert_response :success
514     assert_equal "application/rss+xml", @response.content_type
515     assert_select "rss", :count => 1 do
516       assert_select "channel", :count => 1 do
517         assert_select "item", :count => 1 do
518           assert_select "link", browse_note_url(open_note)
519           assert_select "guid", note_url(open_note)
520           assert_select "pubDate", open_note.created_at.to_s(:rfc822)
521           #          assert_select "geo:lat", open_note.lat.to_s
522           #          assert_select "geo:long", open_note.lon
523           #          assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
524         end
525       end
526     end
527
528     get :show, :id => open_note.id, :format => "json"
529     assert_response :success
530     assert_equal "application/json", @response.content_type
531     js = ActiveSupport::JSON.decode(@response.body)
532     assert_not_nil js
533     assert_equal "Feature", js["type"]
534     assert_equal "Point", js["geometry"]["type"]
535     assert_equal open_note.lat, js["geometry"]["coordinates"][0]
536     assert_equal open_note.lon, js["geometry"]["coordinates"][1]
537     assert_equal open_note.id, js["properties"]["id"]
538     assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
539     assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
540     assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
541     assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
542     assert_equal open_note.status, js["properties"]["status"]
543
544     get :show, :id => open_note.id, :format => "gpx"
545     assert_response :success
546     assert_equal "application/gpx+xml", @response.content_type
547     assert_select "gpx", :count => 1 do
548       assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
549         assert_select "time", :count => 1
550         assert_select "name", "Note: #{open_note.id}"
551         assert_select "desc", :count => 1
552         assert_select "link[href='http://test.host/note/#{open_note.id}']", :count => 1
553         assert_select "extensions", :count => 1 do
554           assert_select "id", open_note.id.to_s
555           assert_select "url", note_url(open_note, :format => "gpx")
556           assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
557           assert_select "close_url", close_note_url(open_note, :format => "gpx")
558         end
559       end
560     end
561   end
562
563   def test_show_hidden_comment
564     note_with_hidden_comment = create(:note) do |note|
565       create(:note_comment, :note => note, :body => "Valid comment for hidden note")
566       create(:note_comment, :note => note, :visible => false)
567       create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
568     end
569
570     get :show, :id => note_with_hidden_comment.id, :format => "json"
571     assert_response :success
572     js = ActiveSupport::JSON.decode(@response.body)
573     assert_not_nil js
574     assert_equal "Feature", js["type"]
575     assert_equal note_with_hidden_comment.id, js["properties"]["id"]
576     assert_equal 2, js["properties"]["comments"].count
577     assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
578     assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
579   end
580
581   def test_show_fail
582     get :show, :id => 12345
583     assert_response :not_found
584
585     get :show, :id => create(:note, :status => "hidden").id
586     assert_response :gone
587   end
588
589   def test_destroy_success
590     open_note_with_comment = create(:note_with_comments)
591     user = create(:user)
592     moderator_user = create(:moderator_user)
593
594     delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
595     assert_response :unauthorized
596
597     basic_authorization(user.email, "test")
598
599     delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
600     assert_response :forbidden
601
602     basic_authorization(moderator_user.email, "test")
603
604     delete :destroy, :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json"
605     assert_response :success
606     js = ActiveSupport::JSON.decode(@response.body)
607     assert_not_nil js
608     assert_equal "Feature", js["type"]
609     assert_equal open_note_with_comment.id, js["properties"]["id"]
610     assert_equal "hidden", js["properties"]["status"]
611     assert_equal 2, js["properties"]["comments"].count
612     assert_equal "hidden", js["properties"]["comments"].last["action"]
613     assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
614     assert_equal moderator_user.display_name, js["properties"]["comments"].last["user"]
615
616     get :show, :id => open_note_with_comment.id, :format => "json"
617     assert_response :gone
618   end
619
620   def test_destroy_fail
621     user = create(:user)
622     moderator_user = create(:moderator_user)
623
624     delete :destroy, :id => 12345, :format => "json"
625     assert_response :unauthorized
626
627     basic_authorization(user.email, "test")
628
629     delete :destroy, :id => 12345, :format => "json"
630     assert_response :forbidden
631
632     basic_authorization(moderator_user.email, "test")
633
634     delete :destroy, :id => 12345, :format => "json"
635     assert_response :not_found
636
637     hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
638
639     delete :destroy, :id => hidden_note_with_comment.id, :format => "json"
640     assert_response :gone
641   end
642
643   def test_index_success
644     position = (1.1 * GeoRecord::SCALE).to_i
645     create(:note_with_comments, :latitude => position, :longitude => position)
646     create(:note_with_comments, :latitude => position, :longitude => position)
647
648     get :index, :bbox => "1,1,1.2,1.2", :format => "rss"
649     assert_response :success
650     assert_equal "application/rss+xml", @response.content_type
651     assert_select "rss", :count => 1 do
652       assert_select "channel", :count => 1 do
653         assert_select "item", :count => 2
654       end
655     end
656
657     get :index, :bbox => "1,1,1.2,1.2", :format => "json"
658     assert_response :success
659     assert_equal "application/json", @response.content_type
660     js = ActiveSupport::JSON.decode(@response.body)
661     assert_not_nil js
662     assert_equal "FeatureCollection", js["type"]
663     assert_equal 2, js["features"].count
664
665     get :index, :bbox => "1,1,1.2,1.2", :format => "xml"
666     assert_response :success
667     assert_equal "application/xml", @response.content_type
668     assert_select "osm", :count => 1 do
669       assert_select "note", :count => 2
670     end
671
672     get :index, :bbox => "1,1,1.2,1.2", :format => "gpx"
673     assert_response :success
674     assert_equal "application/gpx+xml", @response.content_type
675     assert_select "gpx", :count => 1 do
676       assert_select "wpt", :count => 2
677     end
678   end
679
680   def test_index_limit
681     position = (1.1 * GeoRecord::SCALE).to_i
682     create(:note_with_comments, :latitude => position, :longitude => position)
683     create(:note_with_comments, :latitude => position, :longitude => position)
684
685     get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "rss"
686     assert_response :success
687     assert_equal "application/rss+xml", @response.content_type
688     assert_select "rss", :count => 1 do
689       assert_select "channel", :count => 1 do
690         assert_select "item", :count => 1
691       end
692     end
693
694     get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "json"
695     assert_response :success
696     assert_equal "application/json", @response.content_type
697     js = ActiveSupport::JSON.decode(@response.body)
698     assert_not_nil js
699     assert_equal "FeatureCollection", js["type"]
700     assert_equal 1, js["features"].count
701
702     get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "xml"
703     assert_response :success
704     assert_equal "application/xml", @response.content_type
705     assert_select "osm", :count => 1 do
706       assert_select "note", :count => 1
707     end
708
709     get :index, :bbox => "1,1,1.2,1.2", :limit => 1, :format => "gpx"
710     assert_response :success
711     assert_equal "application/gpx+xml", @response.content_type
712     assert_select "gpx", :count => 1 do
713       assert_select "wpt", :count => 1
714     end
715   end
716
717   def test_index_empty_area
718     get :index, :bbox => "5,5,5.1,5.1", :format => "rss"
719     assert_response :success
720     assert_equal "application/rss+xml", @response.content_type
721     assert_select "rss", :count => 1 do
722       assert_select "channel", :count => 1 do
723         assert_select "item", :count => 0
724       end
725     end
726
727     get :index, :bbox => "5,5,5.1,5.1", :format => "json"
728     assert_response :success
729     assert_equal "application/json", @response.content_type
730     js = ActiveSupport::JSON.decode(@response.body)
731     assert_not_nil js
732     assert_equal "FeatureCollection", js["type"]
733     assert_equal 0, js["features"].count
734
735     get :index, :bbox => "5,5,5.1,5.1", :format => "xml"
736     assert_response :success
737     assert_equal "application/xml", @response.content_type
738     assert_select "osm", :count => 1 do
739       assert_select "note", :count => 0
740     end
741
742     get :index, :bbox => "5,5,5.1,5.1", :format => "gpx"
743     assert_response :success
744     assert_equal "application/gpx+xml", @response.content_type
745     assert_select "gpx", :count => 1 do
746       assert_select "wpt", :count => 0
747     end
748   end
749
750   def test_index_large_area
751     get :index, :bbox => "-2.5,-2.5,2.5,2.5", :format => :json
752     assert_response :success
753     assert_equal "application/json", @response.content_type
754
755     get :index, :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json
756     assert_response :success
757     assert_equal "application/json", @response.content_type
758
759     get :index, :bbox => "-10,-10,12,12", :format => :json
760     assert_response :bad_request
761     assert_equal "text/plain", @response.content_type
762
763     get :index, :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json
764     assert_response :bad_request
765     assert_equal "text/plain", @response.content_type
766   end
767
768   def test_index_closed
769     create(:note_with_comments, :status => "closed", :closed_at => Time.now - 5.days)
770     create(:note_with_comments, :status => "closed", :closed_at => Time.now - 100.days)
771     create(:note_with_comments, :status => "hidden")
772     create(:note_with_comments)
773
774     # Open notes + closed in last 7 days
775     get :index, :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json"
776     assert_response :success
777     assert_equal "application/json", @response.content_type
778     js = ActiveSupport::JSON.decode(@response.body)
779     assert_not_nil js
780     assert_equal "FeatureCollection", js["type"]
781     assert_equal 2, js["features"].count
782
783     # Only open notes
784     get :index, :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json"
785     assert_response :success
786     assert_equal "application/json", @response.content_type
787     js = ActiveSupport::JSON.decode(@response.body)
788     assert_not_nil js
789     assert_equal "FeatureCollection", js["type"]
790     assert_equal 1, js["features"].count
791
792     # Open notes + all closed notes
793     get :index, :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json"
794     assert_response :success
795     assert_equal "application/json", @response.content_type
796     js = ActiveSupport::JSON.decode(@response.body)
797     assert_not_nil js
798     assert_equal "FeatureCollection", js["type"]
799     assert_equal 3, js["features"].count
800   end
801
802   def test_index_bad_params
803     get :index, :bbox => "-2.5,-2.5,2.5"
804     assert_response :bad_request
805
806     get :index, :bbox => "-2.5,-2.5,2.5,2.5,2.5"
807     assert_response :bad_request
808
809     get :index, :b => "-2.5", :r => "2.5", :t => "2.5"
810     assert_response :bad_request
811
812     get :index, :l => "-2.5", :r => "2.5", :t => "2.5"
813     assert_response :bad_request
814
815     get :index, :l => "-2.5", :b => "-2.5", :t => "2.5"
816     assert_response :bad_request
817
818     get :index, :l => "-2.5", :b => "-2.5", :r => "2.5"
819     assert_response :bad_request
820
821     get :index, :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json"
822     assert_response :bad_request
823
824     get :index, :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json"
825     assert_response :bad_request
826   end
827
828   def test_search_success
829     create(:note_with_comments)
830
831     get :search, :q => "note comment", :format => "xml"
832     assert_response :success
833     assert_equal "application/xml", @response.content_type
834     assert_select "osm", :count => 1 do
835       assert_select "note", :count => 1
836     end
837
838     get :search, :q => "note comment", :format => "json"
839     assert_response :success
840     assert_equal "application/json", @response.content_type
841     js = ActiveSupport::JSON.decode(@response.body)
842     assert_not_nil js
843     assert_equal "FeatureCollection", js["type"]
844     assert_equal 1, js["features"].count
845
846     get :search, :q => "note comment", :format => "rss"
847     assert_response :success
848     assert_equal "application/rss+xml", @response.content_type
849     assert_select "rss", :count => 1 do
850       assert_select "channel", :count => 1 do
851         assert_select "item", :count => 1
852       end
853     end
854
855     get :search, :q => "note comment", :format => "gpx"
856     assert_response :success
857     assert_equal "application/gpx+xml", @response.content_type
858     assert_select "gpx", :count => 1 do
859       assert_select "wpt", :count => 1
860     end
861   end
862
863   def test_search_no_match
864     create(:note_with_comments)
865
866     get :search, :q => "no match", :format => "xml"
867     assert_response :success
868     assert_equal "application/xml", @response.content_type
869     assert_select "osm", :count => 1 do
870       assert_select "note", :count => 0
871     end
872
873     get :search, :q => "no match", :format => "json"
874     assert_response :success
875     assert_equal "application/json", @response.content_type
876     js = ActiveSupport::JSON.decode(@response.body)
877     assert_not_nil js
878     assert_equal "FeatureCollection", js["type"]
879     assert_equal 0, js["features"].count
880
881     get :search, :q => "no match", :format => "rss"
882     assert_response :success
883     assert_equal "application/rss+xml", @response.content_type
884     assert_select "rss", :count => 1 do
885       assert_select "channel", :count => 1 do
886         assert_select "item", :count => 0
887       end
888     end
889
890     get :search, :q => "no match", :format => "gpx"
891     assert_response :success
892     assert_equal "application/gpx+xml", @response.content_type
893     assert_select "gpx", :count => 1 do
894       assert_select "wpt", :count => 0
895     end
896   end
897
898   def test_search_bad_params
899     get :search
900     assert_response :bad_request
901
902     get :search, :q => "no match", :limit => "0", :format => "json"
903     assert_response :bad_request
904
905     get :search, :q => "no match", :limit => "10001", :format => "json"
906     assert_response :bad_request
907   end
908
909   def test_feed_success
910     position = (1.1 * GeoRecord::SCALE).to_i
911     create(:note_with_comments, :latitude => position, :longitude => position)
912     create(:note_with_comments, :latitude => position, :longitude => position)
913     position = (1.5 * GeoRecord::SCALE).to_i
914     create(:note_with_comments, :latitude => position, :longitude => position)
915     create(:note_with_comments, :latitude => position, :longitude => position)
916
917     get :feed, :format => "rss"
918     assert_response :success
919     assert_equal "application/rss+xml", @response.content_type
920     assert_select "rss", :count => 1 do
921       assert_select "channel", :count => 1 do
922         assert_select "item", :count => 4
923       end
924     end
925
926     get :feed, :bbox => "1,1,1.2,1.2", :format => "rss"
927     assert_response :success
928     assert_equal "application/rss+xml", @response.content_type
929     assert_select "rss", :count => 1 do
930       assert_select "channel", :count => 1 do
931         assert_select "item", :count => 2
932       end
933     end
934   end
935
936   def test_feed_fail
937     get :feed, :bbox => "1,1,1.2", :format => "rss"
938     assert_response :bad_request
939
940     get :feed, :bbox => "1,1,1.2,1.2,1.2", :format => "rss"
941     assert_response :bad_request
942
943     get :feed, :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss"
944     assert_response :bad_request
945
946     get :feed, :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss"
947     assert_response :bad_request
948   end
949
950   def test_mine_success
951     first_user = create(:user)
952     second_user = create(:user)
953     moderator_user = create(:moderator_user)
954
955     create(:note) do |note|
956       create(:note_comment, :note => note, :author => first_user)
957     end
958     create(:note) do |note|
959       create(:note_comment, :note => note, :author => second_user)
960     end
961     create(:note, :status => "hidden") do |note|
962       create(:note_comment, :note => note, :author => second_user)
963     end
964
965     # Note that the table rows include a header row
966     get :mine, :display_name => first_user.display_name
967     assert_response :success
968     assert_select "table.note_list tr", :count => 2
969
970     get :mine, :display_name => second_user.display_name
971     assert_response :success
972     assert_select "table.note_list tr", :count => 2
973
974     get :mine, :display_name => "non-existent"
975     assert_response :not_found
976
977     session[:user] = moderator_user.id
978
979     get :mine, :display_name => first_user.display_name
980     assert_response :success
981     assert_select "table.note_list tr", :count => 2
982
983     get :mine, :display_name => second_user.display_name
984     assert_response :success
985     assert_select "table.note_list tr", :count => 3
986
987     get :mine, :display_name => "non-existent"
988     assert_response :not_found
989   end
990 end