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