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