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