]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/notes_controller_test.rb
Rubocop fixes
[rails.git] / test / controllers / api / notes_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class NotesControllerTest < ActionController::TestCase
5     def setup
6       # Stub nominatim response for note locations
7       stub_request(:get, %r{^https://nominatim\.openstreetmap\.org/reverse\?})
8         .to_return(:status => 404)
9     end
10
11     ##
12     # test all routes which lead to this controller
13     def test_routes
14       assert_routing(
15         { :path => "/api/0.6/notes", :method => :post },
16         { :controller => "api/notes", :action => "create", :format => "xml" }
17       )
18       assert_routing(
19         { :path => "/api/0.6/notes/1", :method => :get },
20         { :controller => "api/notes", :action => "show", :id => "1", :format => "xml" }
21       )
22       assert_recognizes(
23         { :controller => "api/notes", :action => "show", :id => "1", :format => "xml" },
24         { :path => "/api/0.6/notes/1.xml", :method => :get }
25       )
26       assert_routing(
27         { :path => "/api/0.6/notes/1.rss", :method => :get },
28         { :controller => "api/notes", :action => "show", :id => "1", :format => "rss" }
29       )
30       assert_routing(
31         { :path => "/api/0.6/notes/1.json", :method => :get },
32         { :controller => "api/notes", :action => "show", :id => "1", :format => "json" }
33       )
34       assert_routing(
35         { :path => "/api/0.6/notes/1.gpx", :method => :get },
36         { :controller => "api/notes", :action => "show", :id => "1", :format => "gpx" }
37       )
38       assert_routing(
39         { :path => "/api/0.6/notes/1/comment", :method => :post },
40         { :controller => "api/notes", :action => "comment", :id => "1", :format => "xml" }
41       )
42       assert_routing(
43         { :path => "/api/0.6/notes/1/close", :method => :post },
44         { :controller => "api/notes", :action => "close", :id => "1", :format => "xml" }
45       )
46       assert_routing(
47         { :path => "/api/0.6/notes/1/reopen", :method => :post },
48         { :controller => "api/notes", :action => "reopen", :id => "1", :format => "xml" }
49       )
50       assert_routing(
51         { :path => "/api/0.6/notes/1", :method => :delete },
52         { :controller => "api/notes", :action => "destroy", :id => "1", :format => "xml" }
53       )
54
55       assert_routing(
56         { :path => "/api/0.6/notes", :method => :get },
57         { :controller => "api/notes", :action => "index", :format => "xml" }
58       )
59       assert_recognizes(
60         { :controller => "api/notes", :action => "index", :format => "xml" },
61         { :path => "/api/0.6/notes.xml", :method => :get }
62       )
63       assert_routing(
64         { :path => "/api/0.6/notes.rss", :method => :get },
65         { :controller => "api/notes", :action => "index", :format => "rss" }
66       )
67       assert_routing(
68         { :path => "/api/0.6/notes.json", :method => :get },
69         { :controller => "api/notes", :action => "index", :format => "json" }
70       )
71       assert_routing(
72         { :path => "/api/0.6/notes.gpx", :method => :get },
73         { :controller => "api/notes", :action => "index", :format => "gpx" }
74       )
75
76       assert_routing(
77         { :path => "/api/0.6/notes/search", :method => :get },
78         { :controller => "api/notes", :action => "search", :format => "xml" }
79       )
80       assert_recognizes(
81         { :controller => "api/notes", :action => "search", :format => "xml" },
82         { :path => "/api/0.6/notes/search.xml", :method => :get }
83       )
84       assert_routing(
85         { :path => "/api/0.6/notes/search.rss", :method => :get },
86         { :controller => "api/notes", :action => "search", :format => "rss" }
87       )
88       assert_routing(
89         { :path => "/api/0.6/notes/search.json", :method => :get },
90         { :controller => "api/notes", :action => "search", :format => "json" }
91       )
92       assert_routing(
93         { :path => "/api/0.6/notes/search.gpx", :method => :get },
94         { :controller => "api/notes", :action => "search", :format => "gpx" }
95       )
96
97       assert_routing(
98         { :path => "/api/0.6/notes/feed", :method => :get },
99         { :controller => "api/notes", :action => "feed", :format => "rss" }
100       )
101
102       assert_recognizes(
103         { :controller => "api/notes", :action => "create" },
104         { :path => "/api/0.6/notes/addPOIexec", :method => :post }
105       )
106       assert_recognizes(
107         { :controller => "api/notes", :action => "close" },
108         { :path => "/api/0.6/notes/closePOIexec", :method => :post }
109       )
110       assert_recognizes(
111         { :controller => "api/notes", :action => "comment" },
112         { :path => "/api/0.6/notes/editPOIexec", :method => :post }
113       )
114       assert_recognizes(
115         { :controller => "api/notes", :action => "index", :format => "gpx" },
116         { :path => "/api/0.6/notes/getGPX", :method => :get }
117       )
118       assert_recognizes(
119         { :controller => "api/notes", :action => "feed", :format => "rss" },
120         { :path => "/api/0.6/notes/getRSSfeed", :method => :get }
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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :lat => -1.0, :lon => "abc", :text => "This is a comment" }
211         end
212       end
213       assert_response :bad_request
214
215       assert_no_difference "Note.count" do
216         assert_no_difference "NoteComment.count" do
217           post :create, :params => { :lat => -1.0, :lon => -1.0, :text => "x\u0000y" }
218         end
219       end
220       assert_response :bad_request
221     end
222
223     def test_comment_success
224       open_note_with_comment = create(:note_with_comments)
225       assert_difference "NoteComment.count", 1 do
226         assert_no_difference "ActionMailer::Base.deliveries.size" do
227           perform_enqueued_jobs do
228             post :comment, :params => { :id => open_note_with_comment.id, :text => "This is an additional comment", :format => "json" }
229           end
230         end
231       end
232       assert_response :success
233       js = ActiveSupport::JSON.decode(@response.body)
234       assert_not_nil js
235       assert_equal "Feature", js["type"]
236       assert_equal open_note_with_comment.id, js["properties"]["id"]
237       assert_equal "open", js["properties"]["status"]
238       assert_equal 2, js["properties"]["comments"].count
239       assert_equal "commented", js["properties"]["comments"].last["action"]
240       assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
241       assert_nil js["properties"]["comments"].last["user"]
242
243       get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
244       assert_response :success
245       js = ActiveSupport::JSON.decode(@response.body)
246       assert_not_nil js
247       assert_equal "Feature", js["type"]
248       assert_equal open_note_with_comment.id, js["properties"]["id"]
249       assert_equal "open", js["properties"]["status"]
250       assert_equal 2, js["properties"]["comments"].count
251       assert_equal "commented", js["properties"]["comments"].last["action"]
252       assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
253       assert_nil js["properties"]["comments"].last["user"]
254
255       # Ensure that emails are sent to users
256       first_user = create(:user)
257       second_user = create(:user)
258       third_user = create(:user)
259
260       note_with_comments_by_users = create(:note) do |note|
261         create(:note_comment, :note => note, :author => first_user)
262         create(:note_comment, :note => note, :author => second_user)
263       end
264       assert_difference "NoteComment.count", 1 do
265         assert_difference "ActionMailer::Base.deliveries.size", 2 do
266           perform_enqueued_jobs do
267             post :comment, :params => { :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json" }
268           end
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, :params => { :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           perform_enqueued_jobs do
311             post :comment, :params => { :id => note_with_comments_by_users.id, :text => "This is an additional comment", :format => "json" }
312           end
313         end
314       end
315       assert_response :success
316       js = ActiveSupport::JSON.decode(@response.body)
317       assert_not_nil js
318       assert_equal "Feature", js["type"]
319       assert_equal note_with_comments_by_users.id, js["properties"]["id"]
320       assert_equal "open", js["properties"]["status"]
321       assert_equal 4, js["properties"]["comments"].count
322       assert_equal "commented", js["properties"]["comments"].last["action"]
323       assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
324       assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
325
326       email = ActionMailer::Base.deliveries.find { |e| e.to.first == first_user.email }
327       assert_not_nil email
328       assert_equal 1, email.to.length
329       assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on one of your notes", email.subject
330       assert_equal first_user.email, email.to.first
331
332       email = ActionMailer::Base.deliveries.find { |e| e.to.first == second_user.email }
333       assert_not_nil email
334       assert_equal 1, email.to.length
335       assert_equal "[OpenStreetMap] #{third_user.display_name} has commented on a note you are interested in", email.subject
336
337       get :show, :params => { :id => note_with_comments_by_users.id, :format => "json" }
338       assert_response :success
339       js = ActiveSupport::JSON.decode(@response.body)
340       assert_not_nil js
341       assert_equal "Feature", js["type"]
342       assert_equal note_with_comments_by_users.id, js["properties"]["id"]
343       assert_equal "open", js["properties"]["status"]
344       assert_equal 4, js["properties"]["comments"].count
345       assert_equal "commented", js["properties"]["comments"].last["action"]
346       assert_equal "This is an additional comment", js["properties"]["comments"].last["text"]
347       assert_equal third_user.display_name, js["properties"]["comments"].last["user"]
348
349       ActionMailer::Base.deliveries.clear
350     end
351
352     def test_comment_fail
353       open_note_with_comment = create(:note_with_comments)
354
355       assert_no_difference "NoteComment.count" do
356         post :comment, :params => { :text => "This is an additional comment" }
357       end
358       assert_response :bad_request
359
360       assert_no_difference "NoteComment.count" do
361         post :comment, :params => { :id => open_note_with_comment.id }
362       end
363       assert_response :bad_request
364
365       assert_no_difference "NoteComment.count" do
366         post :comment, :params => { :id => open_note_with_comment.id, :text => "" }
367       end
368       assert_response :bad_request
369
370       assert_no_difference "NoteComment.count" do
371         post :comment, :params => { :id => 12345, :text => "This is an additional comment" }
372       end
373       assert_response :not_found
374
375       hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
376
377       assert_no_difference "NoteComment.count" do
378         post :comment, :params => { :id => hidden_note_with_comment.id, :text => "This is an additional comment" }
379       end
380       assert_response :gone
381
382       closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
383
384       assert_no_difference "NoteComment.count" do
385         post :comment, :params => { :id => closed_note_with_comment.id, :text => "This is an additional comment" }
386       end
387       assert_response :conflict
388
389       assert_no_difference "NoteComment.count" do
390         post :comment, :params => { :id => open_note_with_comment.id, :text => "x\u0000y" }
391       end
392       assert_response :bad_request
393     end
394
395     def test_close_success
396       open_note_with_comment = create(:note_with_comments)
397       user = create(:user)
398
399       post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
400       assert_response :unauthorized
401
402       basic_authorization user.email, "test"
403
404       post :close, :params => { :id => open_note_with_comment.id, :text => "This is a close comment", :format => "json" }
405       assert_response :success
406       js = ActiveSupport::JSON.decode(@response.body)
407       assert_not_nil js
408       assert_equal "Feature", js["type"]
409       assert_equal open_note_with_comment.id, js["properties"]["id"]
410       assert_equal "closed", js["properties"]["status"]
411       assert_equal 2, js["properties"]["comments"].count
412       assert_equal "closed", js["properties"]["comments"].last["action"]
413       assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
414       assert_equal user.display_name, js["properties"]["comments"].last["user"]
415
416       get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
417       assert_response :success
418       js = ActiveSupport::JSON.decode(@response.body)
419       assert_not_nil js
420       assert_equal "Feature", js["type"]
421       assert_equal open_note_with_comment.id, js["properties"]["id"]
422       assert_equal "closed", js["properties"]["status"]
423       assert_equal 2, js["properties"]["comments"].count
424       assert_equal "closed", js["properties"]["comments"].last["action"]
425       assert_equal "This is a close comment", js["properties"]["comments"].last["text"]
426       assert_equal user.display_name, js["properties"]["comments"].last["user"]
427     end
428
429     def test_close_fail
430       post :close
431       assert_response :unauthorized
432
433       basic_authorization create(:user).email, "test"
434
435       post :close
436       assert_response :bad_request
437
438       post :close, :params => { :id => 12345 }
439       assert_response :not_found
440
441       hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
442
443       post :close, :params => { :id => hidden_note_with_comment.id }
444       assert_response :gone
445
446       closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
447
448       post :close, :params => { :id => closed_note_with_comment.id }
449       assert_response :conflict
450     end
451
452     def test_reopen_success
453       closed_note_with_comment = create(:note_with_comments, :status => "closed", :closed_at => Time.now)
454       user = create(:user)
455
456       post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
457       assert_response :unauthorized
458
459       basic_authorization user.email, "test"
460
461       post :reopen, :params => { :id => closed_note_with_comment.id, :text => "This is a reopen comment", :format => "json" }
462       assert_response :success
463       js = ActiveSupport::JSON.decode(@response.body)
464       assert_not_nil js
465       assert_equal "Feature", js["type"]
466       assert_equal closed_note_with_comment.id, js["properties"]["id"]
467       assert_equal "open", js["properties"]["status"]
468       assert_equal 2, js["properties"]["comments"].count
469       assert_equal "reopened", js["properties"]["comments"].last["action"]
470       assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
471       assert_equal user.display_name, js["properties"]["comments"].last["user"]
472
473       get :show, :params => { :id => closed_note_with_comment.id, :format => "json" }
474       assert_response :success
475       js = ActiveSupport::JSON.decode(@response.body)
476       assert_not_nil js
477       assert_equal "Feature", js["type"]
478       assert_equal closed_note_with_comment.id, js["properties"]["id"]
479       assert_equal "open", js["properties"]["status"]
480       assert_equal 2, js["properties"]["comments"].count
481       assert_equal "reopened", js["properties"]["comments"].last["action"]
482       assert_equal "This is a reopen comment", js["properties"]["comments"].last["text"]
483       assert_equal user.display_name, js["properties"]["comments"].last["user"]
484     end
485
486     def test_reopen_fail
487       hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
488
489       post :reopen, :params => { :id => hidden_note_with_comment.id }
490       assert_response :unauthorized
491
492       basic_authorization create(:user).email, "test"
493
494       post :reopen, :params => { :id => 12345 }
495       assert_response :not_found
496
497       post :reopen, :params => { :id => hidden_note_with_comment.id }
498       assert_response :gone
499
500       open_note_with_comment = create(:note_with_comments)
501
502       post :reopen, :params => { :id => open_note_with_comment.id }
503       assert_response :conflict
504     end
505
506     def test_show_success
507       open_note = create(:note_with_comments)
508
509       get :show, :params => { :id => open_note.id, :format => "xml" }
510       assert_response :success
511       assert_equal "application/xml", @response.content_type
512       assert_select "osm", :count => 1 do
513         assert_select "note[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
514           assert_select "id", open_note.id.to_s
515           assert_select "url", note_url(open_note, :format => "xml")
516           assert_select "comment_url", comment_note_url(open_note, :format => "xml")
517           assert_select "close_url", close_note_url(open_note, :format => "xml")
518           assert_select "date_created", open_note.created_at.to_s
519           assert_select "status", open_note.status
520           assert_select "comments", :count => 1 do
521             assert_select "comment", :count => 1
522           end
523         end
524       end
525
526       get :show, :params => { :id => open_note.id, :format => "rss" }
527       assert_response :success
528       assert_equal "application/rss+xml", @response.content_type
529       assert_select "rss", :count => 1 do
530         assert_select "channel", :count => 1 do
531           assert_select "item", :count => 1 do
532             assert_select "link", browse_note_url(open_note)
533             assert_select "guid", note_url(open_note)
534             assert_select "pubDate", open_note.created_at.to_s(:rfc822)
535             #          assert_select "geo:lat", open_note.lat.to_s
536             #          assert_select "geo:long", open_note.lon
537             #          assert_select "georss:point", "#{open_note.lon} #{open_note.lon}"
538           end
539         end
540       end
541
542       get :show, :params => { :id => open_note.id, :format => "json" }
543       assert_response :success
544       assert_equal "application/json", @response.content_type
545       js = ActiveSupport::JSON.decode(@response.body)
546       assert_not_nil js
547       assert_equal "Feature", js["type"]
548       assert_equal "Point", js["geometry"]["type"]
549       assert_equal open_note.lat, js["geometry"]["coordinates"][0]
550       assert_equal open_note.lon, js["geometry"]["coordinates"][1]
551       assert_equal open_note.id, js["properties"]["id"]
552       assert_equal note_url(open_note, :format => "json"), js["properties"]["url"]
553       assert_equal comment_note_url(open_note, :format => "json"), js["properties"]["comment_url"]
554       assert_equal close_note_url(open_note, :format => "json"), js["properties"]["close_url"]
555       assert_equal open_note.created_at.to_s, js["properties"]["date_created"]
556       assert_equal open_note.status, js["properties"]["status"]
557
558       get :show, :params => { :id => open_note.id, :format => "gpx" }
559       assert_response :success
560       assert_equal "application/gpx+xml", @response.content_type
561       assert_select "gpx", :count => 1 do
562         assert_select "wpt[lat='#{open_note.lat}'][lon='#{open_note.lon}']", :count => 1 do
563           assert_select "time", :count => 1
564           assert_select "name", "Note: #{open_note.id}"
565           assert_select "desc", :count => 1
566           assert_select "link[href='http://test.host/note/#{open_note.id}']", :count => 1
567           assert_select "extensions", :count => 1 do
568             assert_select "id", open_note.id.to_s
569             assert_select "url", note_url(open_note, :format => "gpx")
570             assert_select "comment_url", comment_note_url(open_note, :format => "gpx")
571             assert_select "close_url", close_note_url(open_note, :format => "gpx")
572           end
573         end
574       end
575     end
576
577     def test_show_hidden_comment
578       note_with_hidden_comment = create(:note) do |note|
579         create(:note_comment, :note => note, :body => "Valid comment for hidden note")
580         create(:note_comment, :note => note, :visible => false)
581         create(:note_comment, :note => note, :body => "Another valid comment for hidden note")
582       end
583
584       get :show, :params => { :id => note_with_hidden_comment.id, :format => "json" }
585       assert_response :success
586       js = ActiveSupport::JSON.decode(@response.body)
587       assert_not_nil js
588       assert_equal "Feature", js["type"]
589       assert_equal note_with_hidden_comment.id, js["properties"]["id"]
590       assert_equal 2, js["properties"]["comments"].count
591       assert_equal "Valid comment for hidden note", js["properties"]["comments"][0]["text"]
592       assert_equal "Another valid comment for hidden note", js["properties"]["comments"][1]["text"]
593     end
594
595     def test_show_fail
596       get :show, :params => { :id => 12345 }
597       assert_response :not_found
598
599       get :show, :params => { :id => create(:note, :status => "hidden").id }
600       assert_response :gone
601     end
602
603     def test_destroy_success
604       open_note_with_comment = create(:note_with_comments)
605       user = create(:user)
606       moderator_user = create(:moderator_user)
607
608       delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
609       assert_response :unauthorized
610
611       basic_authorization user.email, "test"
612
613       delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
614       assert_response :forbidden
615
616       basic_authorization moderator_user.email, "test"
617
618       delete :destroy, :params => { :id => open_note_with_comment.id, :text => "This is a hide comment", :format => "json" }
619       assert_response :success
620       js = ActiveSupport::JSON.decode(@response.body)
621       assert_not_nil js
622       assert_equal "Feature", js["type"]
623       assert_equal open_note_with_comment.id, js["properties"]["id"]
624       assert_equal "hidden", js["properties"]["status"]
625       assert_equal 2, js["properties"]["comments"].count
626       assert_equal "hidden", js["properties"]["comments"].last["action"]
627       assert_equal "This is a hide comment", js["properties"]["comments"].last["text"]
628       assert_equal moderator_user.display_name, js["properties"]["comments"].last["user"]
629
630       get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
631       assert_response :success
632
633       basic_authorization user.email, "test"
634       get :show, :params => { :id => open_note_with_comment.id, :format => "json" }
635       assert_response :gone
636     end
637
638     def test_destroy_fail
639       user = create(:user)
640       moderator_user = create(:moderator_user)
641
642       delete :destroy, :params => { :id => 12345, :format => "json" }
643       assert_response :unauthorized
644
645       basic_authorization user.email, "test"
646
647       delete :destroy, :params => { :id => 12345, :format => "json" }
648       assert_response :forbidden
649
650       basic_authorization moderator_user.email, "test"
651
652       delete :destroy, :params => { :id => 12345, :format => "json" }
653       assert_response :not_found
654
655       hidden_note_with_comment = create(:note_with_comments, :status => "hidden")
656
657       delete :destroy, :params => { :id => hidden_note_with_comment.id, :format => "json" }
658       assert_response :gone
659     end
660
661     def test_index_success
662       position = (1.1 * GeoRecord::SCALE).to_i
663       create(:note_with_comments, :latitude => position, :longitude => position)
664       create(:note_with_comments, :latitude => position, :longitude => position)
665
666       get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "rss" }
667       assert_response :success
668       assert_equal "application/rss+xml", @response.content_type
669       assert_select "rss", :count => 1 do
670         assert_select "channel", :count => 1 do
671           assert_select "item", :count => 2
672         end
673       end
674
675       get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "json" }
676       assert_response :success
677       assert_equal "application/json", @response.content_type
678       js = ActiveSupport::JSON.decode(@response.body)
679       assert_not_nil js
680       assert_equal "FeatureCollection", js["type"]
681       assert_equal 2, js["features"].count
682
683       get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "xml" }
684       assert_response :success
685       assert_equal "application/xml", @response.content_type
686       assert_select "osm", :count => 1 do
687         assert_select "note", :count => 2
688       end
689
690       get :index, :params => { :bbox => "1,1,1.2,1.2", :format => "gpx" }
691       assert_response :success
692       assert_equal "application/gpx+xml", @response.content_type
693       assert_select "gpx", :count => 1 do
694         assert_select "wpt", :count => 2
695       end
696     end
697
698     def test_index_limit
699       position = (1.1 * GeoRecord::SCALE).to_i
700       create(:note_with_comments, :latitude => position, :longitude => position)
701       create(:note_with_comments, :latitude => position, :longitude => position)
702
703       get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 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 => 1
709         end
710       end
711
712       get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 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 1, js["features"].count
719
720       get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 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 => 1
725       end
726
727       get :index, :params => { :bbox => "1,1,1.2,1.2", :limit => 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 => 1
732       end
733     end
734
735     def test_index_empty_area
736       get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "rss" }
737       assert_response :success
738       assert_equal "application/rss+xml", @response.content_type
739       assert_select "rss", :count => 1 do
740         assert_select "channel", :count => 1 do
741           assert_select "item", :count => 0
742         end
743       end
744
745       get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "json" }
746       assert_response :success
747       assert_equal "application/json", @response.content_type
748       js = ActiveSupport::JSON.decode(@response.body)
749       assert_not_nil js
750       assert_equal "FeatureCollection", js["type"]
751       assert_equal 0, js["features"].count
752
753       get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "xml" }
754       assert_response :success
755       assert_equal "application/xml", @response.content_type
756       assert_select "osm", :count => 1 do
757         assert_select "note", :count => 0
758       end
759
760       get :index, :params => { :bbox => "5,5,5.1,5.1", :format => "gpx" }
761       assert_response :success
762       assert_equal "application/gpx+xml", @response.content_type
763       assert_select "gpx", :count => 1 do
764         assert_select "wpt", :count => 0
765       end
766     end
767
768     def test_index_large_area
769       get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5", :format => :json }
770       assert_response :success
771       assert_equal "application/json", @response.content_type
772
773       get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5", :t => "2.5", :format => :json }
774       assert_response :success
775       assert_equal "application/json", @response.content_type
776
777       get :index, :params => { :bbox => "-10,-10,12,12", :format => :json }
778       assert_response :bad_request
779       assert_equal "application/json", @response.content_type
780
781       get :index, :params => { :l => "-10", :b => "-10", :r => "12", :t => "12", :format => :json }
782       assert_response :bad_request
783       assert_equal "application/json", @response.content_type
784     end
785
786     def test_index_closed
787       create(:note_with_comments, :status => "closed", :closed_at => Time.now - 5.days)
788       create(:note_with_comments, :status => "closed", :closed_at => Time.now - 100.days)
789       create(:note_with_comments, :status => "hidden")
790       create(:note_with_comments)
791
792       # Open notes + closed in last 7 days
793       get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "7", :format => "json" }
794       assert_response :success
795       assert_equal "application/json", @response.content_type
796       js = ActiveSupport::JSON.decode(@response.body)
797       assert_not_nil js
798       assert_equal "FeatureCollection", js["type"]
799       assert_equal 2, js["features"].count
800
801       # Only open notes
802       get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "0", :format => "json" }
803       assert_response :success
804       assert_equal "application/json", @response.content_type
805       js = ActiveSupport::JSON.decode(@response.body)
806       assert_not_nil js
807       assert_equal "FeatureCollection", js["type"]
808       assert_equal 1, js["features"].count
809
810       # Open notes + all closed notes
811       get :index, :params => { :bbox => "1,1,1.7,1.7", :closed => "-1", :format => "json" }
812       assert_response :success
813       assert_equal "application/json", @response.content_type
814       js = ActiveSupport::JSON.decode(@response.body)
815       assert_not_nil js
816       assert_equal "FeatureCollection", js["type"]
817       assert_equal 3, js["features"].count
818     end
819
820     def test_index_bad_params
821       get :index, :params => { :bbox => "-2.5,-2.5,2.5" }
822       assert_response :bad_request
823
824       get :index, :params => { :bbox => "-2.5,-2.5,2.5,2.5,2.5" }
825       assert_response :bad_request
826
827       get :index, :params => { :b => "-2.5", :r => "2.5", :t => "2.5" }
828       assert_response :bad_request
829
830       get :index, :params => { :l => "-2.5", :r => "2.5", :t => "2.5" }
831       assert_response :bad_request
832
833       get :index, :params => { :l => "-2.5", :b => "-2.5", :t => "2.5" }
834       assert_response :bad_request
835
836       get :index, :params => { :l => "-2.5", :b => "-2.5", :r => "2.5" }
837       assert_response :bad_request
838
839       get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "0", :format => "json" }
840       assert_response :bad_request
841
842       get :index, :params => { :bbox => "1,1,1.7,1.7", :limit => "10001", :format => "json" }
843       assert_response :bad_request
844     end
845
846     def test_search_success
847       create(:note_with_comments)
848
849       get :search, :params => { :q => "note comment", :format => "xml" }
850       assert_response :success
851       assert_equal "application/xml", @response.content_type
852       assert_select "osm", :count => 1 do
853         assert_select "note", :count => 1
854       end
855
856       get :search, :params => { :q => "note comment", :format => "json" }
857       assert_response :success
858       assert_equal "application/json", @response.content_type
859       js = ActiveSupport::JSON.decode(@response.body)
860       assert_not_nil js
861       assert_equal "FeatureCollection", js["type"]
862       assert_equal 1, js["features"].count
863
864       get :search, :params => { :q => "note comment", :format => "rss" }
865       assert_response :success
866       assert_equal "application/rss+xml", @response.content_type
867       assert_select "rss", :count => 1 do
868         assert_select "channel", :count => 1 do
869           assert_select "item", :count => 1
870         end
871       end
872
873       get :search, :params => { :q => "note comment", :format => "gpx" }
874       assert_response :success
875       assert_equal "application/gpx+xml", @response.content_type
876       assert_select "gpx", :count => 1 do
877         assert_select "wpt", :count => 1
878       end
879     end
880
881     def test_search_by_display_name_success
882       user = create(:user)
883
884       create(:note) do |note|
885         create(:note_comment, :note => note, :author => user)
886       end
887
888       get :search, :params => { :display_name => user.display_name, :format => "xml" }
889       assert_response :success
890       assert_equal "application/xml", @response.content_type
891       assert_select "osm", :count => 1 do
892         assert_select "note", :count => 1
893       end
894
895       get :search, :params => { :display_name => user.display_name, :format => "json" }
896       assert_response :success
897       assert_equal "application/json", @response.content_type
898       js = ActiveSupport::JSON.decode(@response.body)
899       assert_not_nil js
900       assert_equal "FeatureCollection", js["type"]
901       assert_equal 1, js["features"].count
902
903       get :search, :params => { :display_name => user.display_name, :format => "rss" }
904       assert_response :success
905       assert_equal "application/rss+xml", @response.content_type
906       assert_select "rss", :count => 1 do
907         assert_select "channel", :count => 1 do
908           assert_select "item", :count => 1
909         end
910       end
911
912       get :search, :params => { :display_name => user.display_name, :format => "gpx" }
913       assert_response :success
914       assert_equal "application/gpx+xml", @response.content_type
915       assert_select "gpx", :count => 1 do
916         assert_select "wpt", :count => 1
917       end
918     end
919
920     def test_search_by_user_success
921       user = create(:user)
922
923       create(:note) do |note|
924         create(:note_comment, :note => note, :author => user)
925       end
926
927       get :search, :params => { :user => user.id, :format => "xml" }
928       assert_response :success
929       assert_equal "application/xml", @response.content_type
930       assert_select "osm", :count => 1 do
931         assert_select "note", :count => 1
932       end
933
934       get :search, :params => { :user => user.id, :format => "json" }
935       assert_response :success
936       assert_equal "application/json", @response.content_type
937       js = ActiveSupport::JSON.decode(@response.body)
938       assert_not_nil js
939       assert_equal "FeatureCollection", js["type"]
940       assert_equal 1, js["features"].count
941
942       get :search, :params => { :user => user.id, :format => "rss" }
943       assert_response :success
944       assert_equal "application/rss+xml", @response.content_type
945       assert_select "rss", :count => 1 do
946         assert_select "channel", :count => 1 do
947           assert_select "item", :count => 1
948         end
949       end
950
951       get :search, :params => { :user => user.id, :format => "gpx" }
952       assert_response :success
953       assert_equal "application/gpx+xml", @response.content_type
954       assert_select "gpx", :count => 1 do
955         assert_select "wpt", :count => 1
956       end
957     end
958
959     def test_search_no_match
960       create(:note_with_comments)
961
962       get :search, :params => { :q => "no match", :format => "xml" }
963       assert_response :success
964       assert_equal "application/xml", @response.content_type
965       assert_select "osm", :count => 1 do
966         assert_select "note", :count => 0
967       end
968
969       get :search, :params => { :q => "no match", :format => "json" }
970       assert_response :success
971       assert_equal "application/json", @response.content_type
972       js = ActiveSupport::JSON.decode(@response.body)
973       assert_not_nil js
974       assert_equal "FeatureCollection", js["type"]
975       assert_equal 0, js["features"].count
976
977       get :search, :params => { :q => "no match", :format => "rss" }
978       assert_response :success
979       assert_equal "application/rss+xml", @response.content_type
980       assert_select "rss", :count => 1 do
981         assert_select "channel", :count => 1 do
982           assert_select "item", :count => 0
983         end
984       end
985
986       get :search, :params => { :q => "no match", :format => "gpx" }
987       assert_response :success
988       assert_equal "application/gpx+xml", @response.content_type
989       assert_select "gpx", :count => 1 do
990         assert_select "wpt", :count => 0
991       end
992     end
993
994     def test_search_by_time_no_match
995       create(:note_with_comments)
996
997       get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "xml" }
998       assert_response :success
999       assert_equal "application/xml", @response.content_type
1000       assert_select "osm", :count => 1 do
1001         assert_select "note", :count => 0
1002       end
1003
1004       get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "json" }
1005       assert_response :success
1006       assert_equal "application/json", @response.content_type
1007       js = ActiveSupport::JSON.decode(@response.body)
1008       assert_not_nil js
1009       assert_equal "FeatureCollection", js["type"]
1010       assert_equal 0, js["features"].count
1011
1012       get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "rss" }
1013       assert_response :success
1014       assert_equal "application/rss+xml", @response.content_type
1015       assert_select "rss", :count => 1 do
1016         assert_select "channel", :count => 1 do
1017           assert_select "item", :count => 0
1018         end
1019       end
1020
1021       get :search, :params => { :from => "01.01.2010", :to => "01.10.2010", :format => "gpx" }
1022       assert_response :success
1023       assert_equal "application/gpx+xml", @response.content_type
1024       assert_select "gpx", :count => 1 do
1025         assert_select "wpt", :count => 0
1026       end
1027     end
1028
1029     def test_search_bad_params
1030       get :search, :params => { :q => "no match", :limit => "0", :format => "json" }
1031       assert_response :bad_request
1032
1033       get :search, :params => { :q => "no match", :limit => "10001", :format => "json" }
1034       assert_response :bad_request
1035
1036       get :search, :params => { :display_name => "non-existent" }
1037       assert_response :bad_request
1038
1039       get :search, :params => { :user => "-1" }
1040       assert_response :bad_request
1041
1042       get :search, :params => { :from => "wrong-date", :to => "wrong-date" }
1043       assert_response :bad_request
1044
1045       get :search, :params => { :from => "01.01.2010", :to => "2010.01.2010" }
1046       assert_response :bad_request
1047     end
1048
1049     def test_feed_success
1050       position = (1.1 * GeoRecord::SCALE).to_i
1051       create(:note_with_comments, :latitude => position, :longitude => position)
1052       create(:note_with_comments, :latitude => position, :longitude => position)
1053       position = (1.5 * GeoRecord::SCALE).to_i
1054       create(:note_with_comments, :latitude => position, :longitude => position)
1055       create(:note_with_comments, :latitude => position, :longitude => position)
1056
1057       get :feed, :params => { :format => "rss" }
1058       assert_response :success
1059       assert_equal "application/rss+xml", @response.content_type
1060       assert_select "rss", :count => 1 do
1061         assert_select "channel", :count => 1 do
1062           assert_select "item", :count => 4
1063         end
1064       end
1065
1066       get :feed, :params => { :bbox => "1,1,1.2,1.2", :format => "rss" }
1067       assert_response :success
1068       assert_equal "application/rss+xml", @response.content_type
1069       assert_select "rss", :count => 1 do
1070         assert_select "channel", :count => 1 do
1071           assert_select "item", :count => 2
1072         end
1073       end
1074     end
1075
1076     def test_feed_fail
1077       get :feed, :params => { :bbox => "1,1,1.2", :format => "rss" }
1078       assert_response :bad_request
1079
1080       get :feed, :params => { :bbox => "1,1,1.2,1.2,1.2", :format => "rss" }
1081       assert_response :bad_request
1082
1083       get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "0", :format => "rss" }
1084       assert_response :bad_request
1085
1086       get :feed, :params => { :bbox => "1,1,1.2,1.2", :limit => "10001", :format => "rss" }
1087       assert_response :bad_request
1088     end
1089   end
1090 end