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