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