]> git.openstreetmap.org Git - rails.git/blob - test/controllers/changeset_controller_test.rb
WIP diary comment subscriptions
[rails.git] / test / controllers / changeset_controller_test.rb
1 require "test_helper"
2 require "changeset_controller"
3
4 class ChangesetControllerTest < ActionController::TestCase
5   api_fixtures
6   fixtures :friends, :changeset_comments, :changesets_subscribers
7
8   ##
9   # test all routes which lead to this controller
10   def test_routes
11     assert_routing(
12       { :path => "/api/0.6/changeset/create", :method => :put },
13       { :controller => "changeset", :action => "create" }
14     )
15     assert_routing(
16       { :path => "/api/0.6/changeset/1/upload", :method => :post },
17       { :controller => "changeset", :action => "upload", :id => "1" }
18     )
19     assert_routing(
20       { :path => "/api/0.6/changeset/1/download", :method => :get },
21       { :controller => "changeset", :action => "download", :id => "1" }
22     )
23     assert_routing(
24       { :path => "/api/0.6/changeset/1/expand_bbox", :method => :post },
25       { :controller => "changeset", :action => "expand_bbox", :id => "1" }
26     )
27     assert_routing(
28       { :path => "/api/0.6/changeset/1", :method => :get },
29       { :controller => "changeset", :action => "read", :id => "1" }
30     )
31     assert_routing(
32       { :path => "/api/0.6/changeset/1/subscribe", :method => :post },
33       { :controller => "changeset", :action => "subscribe", :id => "1" }
34     )
35     assert_routing(
36       { :path => "/api/0.6/changeset/1/unsubscribe", :method => :post },
37       { :controller => "changeset", :action => "unsubscribe", :id => "1" }
38     )
39     assert_routing(
40       { :path => "/api/0.6/changeset/1", :method => :put },
41       { :controller => "changeset", :action => "update", :id => "1" }
42     )
43     assert_routing(
44       { :path => "/api/0.6/changeset/1/close", :method => :put },
45       { :controller => "changeset", :action => "close", :id => "1" }
46     )
47     assert_routing(
48       { :path => "/api/0.6/changeset/1/comment", :method => :post },
49       { :controller => "changeset", :action => "comment", :id => "1" }
50     )
51     assert_routing(
52       { :path => "/api/0.6/changeset/comment/1/hide", :method => :post },
53       { :controller => "changeset", :action => "hide_comment", :id => "1" }
54     )
55     assert_routing(
56       { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
57       { :controller => "changeset", :action => "unhide_comment", :id => "1" }
58     )
59     assert_routing(
60       { :path => "/api/0.6/changesets", :method => :get },
61       { :controller => "changeset", :action => "query" }
62     )
63     assert_routing(
64       { :path => "/changeset/1/comments/feed", :method => :get },
65       { :controller => "changeset", :action => "comments_feed", :id => "1", :format => "rss" }
66     )
67     assert_routing(
68       { :path => "/user/name/history", :method => :get },
69       { :controller => "changeset", :action => "list", :display_name => "name" }
70     )
71     assert_routing(
72       { :path => "/user/name/history/feed", :method => :get },
73       { :controller => "changeset", :action => "feed", :display_name => "name", :format => :atom }
74     )
75     assert_routing(
76       { :path => "/history/friends", :method => :get },
77       { :controller => "changeset", :action => "list", :friends => true, :format => :html }
78     )
79     assert_routing(
80       { :path => "/history/nearby", :method => :get },
81       { :controller => "changeset", :action => "list", :nearby => true, :format => :html }
82     )
83     assert_routing(
84       { :path => "/history", :method => :get },
85       { :controller => "changeset", :action => "list" }
86     )
87     assert_routing(
88       { :path => "/history/feed", :method => :get },
89       { :controller => "changeset", :action => "feed", :format => :atom }
90     )
91     assert_routing(
92       { :path => "/history/comments/feed", :method => :get },
93       { :controller => "changeset", :action => "comments_feed", :format => "rss" }
94     )
95   end
96
97   # -----------------------
98   # Test simple changeset creation
99   # -----------------------
100
101   def test_create
102     basic_authorization users(:normal_user).email, "test"
103     # Create the first user's changeset
104     content "<osm><changeset>" +
105             "<tag k='created_by' v='osm test suite checking changesets'/>" +
106             "</changeset></osm>"
107     put :create
108     assert_require_public_data
109
110     basic_authorization users(:public_user).email, "test"
111     # Create the first user's changeset
112     content "<osm><changeset>" +
113             "<tag k='created_by' v='osm test suite checking changesets'/>" +
114             "</changeset></osm>"
115     put :create
116
117     assert_response :success, "Creation of changeset did not return sucess status"
118     newid = @response.body.to_i
119
120     # check end time, should be an hour ahead of creation time
121     cs = Changeset.find(newid)
122     duration = cs.closed_at - cs.created_at
123     # the difference can either be a rational, or a floating point number
124     # of seconds, depending on the code path taken :-(
125     if duration.class == Rational
126       assert_equal Rational(1, 24), duration, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
127     else
128       # must be number of seconds...
129       assert_equal 3600, duration.round, "initial idle timeout should be an hour (#{cs.created_at} -> #{cs.closed_at})"
130     end
131
132     # checks if uploader was subscribed
133     assert_equal 1, cs.subscribers.length
134   end
135
136   def test_create_invalid
137     basic_authorization users(:normal_user).email, "test"
138     content "<osm><changeset></osm>"
139     put :create
140     assert_require_public_data
141
142     ## Try the public user
143     basic_authorization users(:public_user).email, "test"
144     content "<osm><changeset></osm>"
145     put :create
146     assert_response :bad_request, "creating a invalid changeset should fail"
147   end
148
149   def test_create_invalid_no_content
150     ## First check with no auth
151     put :create
152     assert_response :unauthorized, "shouldn't be able to create a changeset with no auth"
153
154     ## Now try to with the non-public user
155     basic_authorization users(:normal_user).email, "test"
156     put :create
157     assert_require_public_data
158
159     ## Try the inactive user
160     basic_authorization users(:inactive_user).email, "test"
161     put :create
162     assert_inactive_user
163
164     ## Now try to use the public user
165     basic_authorization users(:public_user).email, "test"
166     put :create
167     assert_response :bad_request, "creating a changeset with no content should fail"
168   end
169
170   def test_create_wrong_method
171     basic_authorization users(:public_user).email, "test"
172     get :create
173     assert_response :method_not_allowed
174     post :create
175     assert_response :method_not_allowed
176   end
177
178   ##
179   # check that the changeset can be read and returns the correct
180   # document structure.
181   def test_read
182     changeset_id = changesets(:normal_user_first_change).id
183
184     get :read, :id => changeset_id
185     assert_response :success, "cannot get first changeset"
186
187     assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
188     assert_select "osm>changeset[id='#{changeset_id}']", 1
189     assert_select "osm>changeset>discussion", 0
190
191     get :read, :id => changeset_id, :include_discussion => true
192     assert_response :success, "cannot get first changeset with comments"
193
194     assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
195     assert_select "osm>changeset[id='#{changeset_id}']", 1
196     assert_select "osm>changeset>discussion", 1
197     assert_select "osm>changeset>discussion>comment", 0
198
199     changeset_id = changesets(:normal_user_closed_change).id
200
201     get :read, :id => changeset_id, :include_discussion => true
202     assert_response :success, "cannot get closed changeset with comments"
203
204     assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
205     assert_select "osm>changeset[id='#{changeset_id}']", 1
206     assert_select "osm>changeset>discussion", 1
207     assert_select "osm>changeset>discussion>comment", 3
208   end
209
210   ##
211   # check that a changeset that doesn't exist returns an appropriate message
212   def test_read_not_found
213     [0, -32, 233455644, "afg", "213"].each do |id|
214       begin
215         get :read, :id => id
216         assert_response :not_found, "should get a not found"
217       rescue ActionController::UrlGenerationError => ex
218         assert_match /No route matches/, ex.to_s
219       end
220     end
221   end
222
223   ##
224   # test that the user who opened a change can close it
225   def test_close
226     ## Try without authentication
227     put :close, :id => changesets(:public_user_first_change).id
228     assert_response :unauthorized
229
230     ## Try using the non-public user
231     basic_authorization users(:normal_user).email, "test"
232     put :close, :id => changesets(:normal_user_first_change).id
233     assert_require_public_data
234
235     ## The try with the public user
236     basic_authorization users(:public_user).email, "test"
237
238     cs_id = changesets(:public_user_first_change).id
239     put :close, :id => cs_id
240     assert_response :success
241
242     # test that it really is closed now
243     cs = Changeset.find(cs_id)
244     assert(!cs.is_open?,
245            "changeset should be closed now (#{cs.closed_at} > #{Time.now.getutc}.")
246   end
247
248   ##
249   # test that a different user can't close another user's changeset
250   def test_close_invalid
251     basic_authorization users(:public_user).email, "test"
252
253     put :close, :id => changesets(:normal_user_first_change).id
254     assert_response :conflict
255     assert_equal "The user doesn't own that changeset", @response.body
256   end
257
258   ##
259   # test that you can't close using another method
260   def test_close_method_invalid
261     basic_authorization users(:public_user).email, "test"
262
263     cs_id = changesets(:public_user_first_change).id
264     get :close, :id => cs_id
265     assert_response :method_not_allowed
266
267     post :close, :id => cs_id
268     assert_response :method_not_allowed
269   end
270
271   ##
272   # check that you can't close a changeset that isn't found
273   def test_close_not_found
274     cs_ids = [0, -132, "123"]
275
276     # First try to do it with no auth
277     cs_ids.each do |id|
278       begin
279         put :close, :id => id
280         assert_response :unauthorized, "Shouldn't be able close the non-existant changeset #{id}, when not authorized"
281       rescue ActionController::UrlGenerationError => ex
282         assert_match /No route matches/, ex.to_s
283       end
284     end
285
286     # Now try with auth
287     basic_authorization users(:public_user).email, "test"
288     cs_ids.each do |id|
289       begin
290         put :close, :id => id
291         assert_response :not_found, "The changeset #{id} doesn't exist, so can't be closed"
292       rescue ActionController::UrlGenerationError => ex
293         assert_match /No route matches/, ex.to_s
294       end
295     end
296   end
297
298   ##
299   # upload something simple, but valid and check that it can
300   # be read back ok
301   # Also try without auth and another user.
302   def test_upload_simple_valid
303     ## Try with no auth
304     changeset_id = changesets(:public_user_first_change).id
305
306     # simple diff to change a node, way and relation by removing
307     # their tags
308     diff = <<EOF
309 <osmChange>
310  <modify>
311   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
312   <way id='1' changeset='#{changeset_id}' version='1'>
313    <nd ref='3'/>
314   </way>
315  </modify>
316  <modify>
317   <relation id='1' changeset='#{changeset_id}' version='1'>
318    <member type='way' role='some' ref='3'/>
319    <member type='node' role='some' ref='5'/>
320    <member type='relation' role='some' ref='3'/>
321   </relation>
322  </modify>
323 </osmChange>
324 EOF
325
326     # upload it
327     content diff
328     post :upload, :id => changeset_id
329     assert_response :unauthorized,
330                     "shouldnn't be able to upload a simple valid diff to changeset: #{@response.body}"
331
332     ## Now try with a private user
333     basic_authorization users(:normal_user).email, "test"
334     changeset_id = changesets(:normal_user_first_change).id
335
336     # simple diff to change a node, way and relation by removing
337     # their tags
338     diff = <<EOF
339 <osmChange>
340  <modify>
341   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
342   <way id='1' changeset='#{changeset_id}' version='1'>
343    <nd ref='3'/>
344   </way>
345  </modify>
346  <modify>
347   <relation id='1' changeset='#{changeset_id}' version='1'>
348    <member type='way' role='some' ref='3'/>
349    <member type='node' role='some' ref='5'/>
350    <member type='relation' role='some' ref='3'/>
351   </relation>
352  </modify>
353 </osmChange>
354 EOF
355
356     # upload it
357     content diff
358     post :upload, :id => changeset_id
359     assert_response :forbidden,
360                     "can't upload a simple valid diff to changeset: #{@response.body}"
361
362     ## Now try with the public user
363     basic_authorization users(:public_user).email, "test"
364     changeset_id = changesets(:public_user_first_change).id
365
366     # simple diff to change a node, way and relation by removing
367     # their tags
368     diff = <<EOF
369 <osmChange>
370  <modify>
371   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
372   <way id='1' changeset='#{changeset_id}' version='1'>
373    <nd ref='3'/>
374   </way>
375  </modify>
376  <modify>
377   <relation id='1' changeset='#{changeset_id}' version='1'>
378    <member type='way' role='some' ref='3'/>
379    <member type='node' role='some' ref='5'/>
380    <member type='relation' role='some' ref='3'/>
381   </relation>
382  </modify>
383 </osmChange>
384 EOF
385
386     # upload it
387     content diff
388     post :upload, :id => changeset_id
389     assert_response :success,
390                     "can't upload a simple valid diff to changeset: #{@response.body}"
391
392     # check that the changes made it into the database
393     assert_equal 0, Node.find(1).tags.size, "node 1 should now have no tags"
394     assert_equal 0, Way.find(1).tags.size, "way 1 should now have no tags"
395     assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
396   end
397
398   ##
399   # upload something which creates new objects using placeholders
400   def test_upload_create_valid
401     basic_authorization users(:public_user).email, "test"
402     cs_id = changesets(:public_user_first_change).id
403
404     # simple diff to create a node way and relation using placeholders
405     diff = <<EOF
406 <osmChange>
407  <create>
408   <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
409    <tag k='foo' v='bar'/>
410    <tag k='baz' v='bat'/>
411   </node>
412   <way id='-1' changeset='#{cs_id}'>
413    <nd ref='3'/>
414   </way>
415  </create>
416  <create>
417   <relation id='-1' changeset='#{cs_id}'>
418    <member type='way' role='some' ref='3'/>
419    <member type='node' role='some' ref='5'/>
420    <member type='relation' role='some' ref='3'/>
421   </relation>
422  </create>
423 </osmChange>
424 EOF
425
426     # upload it
427     content diff
428     post :upload, :id => cs_id
429     assert_response :success,
430                     "can't upload a simple valid creation to changeset: #{@response.body}"
431
432     # check the returned payload
433     assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
434     assert_select "diffResult>node", 1
435     assert_select "diffResult>way", 1
436     assert_select "diffResult>relation", 1
437
438     # inspect the response to find out what the new element IDs are
439     doc = XML::Parser.string(@response.body).parse
440     new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
441     new_way_id = doc.find("//diffResult/way").first["new_id"].to_i
442     new_rel_id = doc.find("//diffResult/relation").first["new_id"].to_i
443
444     # check the old IDs are all present and negative one
445     assert_equal -1, doc.find("//diffResult/node").first["old_id"].to_i
446     assert_equal -1, doc.find("//diffResult/way").first["old_id"].to_i
447     assert_equal -1, doc.find("//diffResult/relation").first["old_id"].to_i
448
449     # check the versions are present and equal one
450     assert_equal 1, doc.find("//diffResult/node").first["new_version"].to_i
451     assert_equal 1, doc.find("//diffResult/way").first["new_version"].to_i
452     assert_equal 1, doc.find("//diffResult/relation").first["new_version"].to_i
453
454     # check that the changes made it into the database
455     assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
456     assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
457     assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
458   end
459
460   ##
461   # test a complex delete where we delete elements which rely on eachother
462   # in the same transaction.
463   def test_upload_delete
464     basic_authorization users(:public_user).display_name, "test"
465
466     diff = XML::Document.new
467     diff.root = XML::Node.new "osmChange"
468     delete = XML::Node.new "delete"
469     diff.root << delete
470     delete << current_relations(:visible_relation).to_xml_node
471     delete << current_relations(:used_relation).to_xml_node
472     delete << current_ways(:used_way).to_xml_node
473     delete << current_nodes(:node_used_by_relationship).to_xml_node
474
475     # update the changeset to one that this user owns
476     changeset_id = changesets(:public_user_first_change).id
477     %w(node way relation).each do |type|
478       delete.find("//osmChange/delete/#{type}").each do |n|
479         n["changeset"] = changeset_id.to_s
480       end
481     end
482
483     # upload it
484     content diff
485     post :upload, :id => changeset_id
486     assert_response :success,
487                     "can't upload a deletion diff to changeset: #{@response.body}"
488
489     # check the response is well-formed
490     assert_select "diffResult>node", 1
491     assert_select "diffResult>way", 1
492     assert_select "diffResult>relation", 2
493
494     # check that everything was deleted
495     assert_equal false, Node.find(current_nodes(:node_used_by_relationship).id).visible
496     assert_equal false, Way.find(current_ways(:used_way).id).visible
497     assert_equal false, Relation.find(current_relations(:visible_relation).id).visible
498     assert_equal false, Relation.find(current_relations(:used_relation).id).visible
499   end
500
501   ##
502   # test uploading a delete with no lat/lon, as they are optional in
503   # the osmChange spec.
504   def test_upload_nolatlon_delete
505     basic_authorization users(:public_user).display_name, "test"
506
507     node = current_nodes(:public_visible_node)
508     cs = changesets(:public_user_first_change)
509     diff = "<osmChange><delete><node id='#{node.id}' version='#{node.version}' changeset='#{cs.id}'/></delete></osmChange>"
510
511     # upload it
512     content diff
513     post :upload, :id => cs.id
514     assert_response :success,
515                     "can't upload a deletion diff to changeset: #{@response.body}"
516
517     # check the response is well-formed
518     assert_select "diffResult>node", 1
519
520     # check that everything was deleted
521     assert_equal false, Node.find(node.id).visible
522   end
523
524   def test_repeated_changeset_create
525     30.times do
526       basic_authorization users(:public_user).email, "test"
527
528       # create a temporary changeset
529       content "<osm><changeset>" +
530               "<tag k='created_by' v='osm test suite checking changesets'/>" +
531               "</changeset></osm>"
532       assert_difference "Changeset.count", 1 do
533         put :create
534       end
535       assert_response :success
536     end
537   end
538
539   def test_upload_large_changeset
540     basic_authorization users(:public_user).email, "test"
541
542     # create a changeset
543     content "<osm><changeset/></osm>"
544     put :create
545     assert_response :success, "Should be able to create a changeset: #{@response.body}"
546     changeset_id = @response.body.to_i
547
548     # upload some widely-spaced nodes, spiralling positive and negative to cause
549     # largest bbox over-expansion possible.
550     diff = <<EOF
551 <osmChange>
552  <create>
553   <node id='-1' lon='-20' lat='-10' changeset='#{changeset_id}'/>
554   <node id='-10' lon='20'  lat='10' changeset='#{changeset_id}'/>
555   <node id='-2' lon='-40' lat='-20' changeset='#{changeset_id}'/>
556   <node id='-11' lon='40'  lat='20' changeset='#{changeset_id}'/>
557   <node id='-3' lon='-60' lat='-30' changeset='#{changeset_id}'/>
558   <node id='-12' lon='60'  lat='30' changeset='#{changeset_id}'/>
559   <node id='-4' lon='-80' lat='-40' changeset='#{changeset_id}'/>
560   <node id='-13' lon='80'  lat='40' changeset='#{changeset_id}'/>
561   <node id='-5' lon='-100' lat='-50' changeset='#{changeset_id}'/>
562   <node id='-14' lon='100'  lat='50' changeset='#{changeset_id}'/>
563   <node id='-6' lon='-120' lat='-60' changeset='#{changeset_id}'/>
564   <node id='-15' lon='120'  lat='60' changeset='#{changeset_id}'/>
565   <node id='-7' lon='-140' lat='-70' changeset='#{changeset_id}'/>
566   <node id='-16' lon='140'  lat='70' changeset='#{changeset_id}'/>
567   <node id='-8' lon='-160' lat='-80' changeset='#{changeset_id}'/>
568   <node id='-17' lon='160'  lat='80' changeset='#{changeset_id}'/>
569   <node id='-9' lon='-179.9' lat='-89.9' changeset='#{changeset_id}'/>
570   <node id='-18' lon='179.9'  lat='89.9' changeset='#{changeset_id}'/>
571  </create>
572 </osmChange>
573 EOF
574
575     # upload it, which used to cause an error like "PGError: ERROR:
576     # integer out of range" (bug #2152). but shouldn't any more.
577     content diff
578     post :upload, :id => changeset_id
579     assert_response :success,
580                     "can't upload a spatially-large diff to changeset: #{@response.body}"
581
582     # check that the changeset bbox is within bounds
583     cs = Changeset.find(changeset_id)
584     assert cs.min_lon >= -180 * GeoRecord::SCALE, "Minimum longitude (#{cs.min_lon / GeoRecord::SCALE}) should be >= -180 to be valid."
585     assert cs.max_lon <= 180 * GeoRecord::SCALE, "Maximum longitude (#{cs.max_lon / GeoRecord::SCALE}) should be <= 180 to be valid."
586     assert cs.min_lat >= -90 * GeoRecord::SCALE, "Minimum latitude (#{cs.min_lat / GeoRecord::SCALE}) should be >= -90 to be valid."
587     assert cs.max_lat >= 90 * GeoRecord::SCALE, "Maximum latitude (#{cs.max_lat / GeoRecord::SCALE}) should be <= 90 to be valid."
588   end
589
590   ##
591   # test that deleting stuff in a transaction doesn't bypass the checks
592   # to ensure that used elements are not deleted.
593   def test_upload_delete_invalid
594     basic_authorization users(:public_user).email, "test"
595
596     diff = XML::Document.new
597     diff.root = XML::Node.new "osmChange"
598     delete = XML::Node.new "delete"
599     diff.root << delete
600     delete << current_relations(:public_visible_relation).to_xml_node
601     delete << current_ways(:used_way).to_xml_node
602     delete << current_nodes(:node_used_by_relationship).to_xml_node
603
604     # upload it
605     content diff
606     post :upload, :id => 2
607     assert_response :precondition_failed,
608                     "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
609     assert_equal "Precondition failed: Way 3 is still used by relations 1.", @response.body
610
611     # check that nothing was, in fact, deleted
612     assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
613     assert_equal true, Way.find(current_ways(:used_way).id).visible
614     assert_equal true, Relation.find(current_relations(:visible_relation).id).visible
615   end
616
617   ##
618   # test that a conditional delete of an in use object works.
619   def test_upload_delete_if_unused
620     basic_authorization users(:public_user).email, "test"
621
622     diff = XML::Document.new
623     diff.root = XML::Node.new "osmChange"
624     delete = XML::Node.new "delete"
625     diff.root << delete
626     delete["if-unused"] = ""
627     delete << current_relations(:public_used_relation).to_xml_node
628     delete << current_ways(:used_way).to_xml_node
629     delete << current_nodes(:node_used_by_relationship).to_xml_node
630
631     # upload it
632     content diff
633     post :upload, :id => 2
634     assert_response :success,
635                     "can't do a conditional delete of in use objects: #{@response.body}"
636
637     # check the returned payload
638     assert_select "diffResult[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
639     assert_select "diffResult>node", 1
640     assert_select "diffResult>way", 1
641     assert_select "diffResult>relation", 1
642
643     # parse the response
644     doc = XML::Parser.string(@response.body).parse
645
646     # check the old IDs are all present and what we expect
647     assert_equal current_nodes(:node_used_by_relationship).id, doc.find("//diffResult/node").first["old_id"].to_i
648     assert_equal current_ways(:used_way).id, doc.find("//diffResult/way").first["old_id"].to_i
649     assert_equal current_relations(:public_used_relation).id, doc.find("//diffResult/relation").first["old_id"].to_i
650
651     # check the new IDs are all present and unchanged
652     assert_equal current_nodes(:node_used_by_relationship).id, doc.find("//diffResult/node").first["new_id"].to_i
653     assert_equal current_ways(:used_way).id, doc.find("//diffResult/way").first["new_id"].to_i
654     assert_equal current_relations(:public_used_relation).id, doc.find("//diffResult/relation").first["new_id"].to_i
655
656     # check the new versions are all present and unchanged
657     assert_equal current_nodes(:node_used_by_relationship).version, doc.find("//diffResult/node").first["new_version"].to_i
658     assert_equal current_ways(:used_way).version, doc.find("//diffResult/way").first["new_version"].to_i
659     assert_equal current_relations(:public_used_relation).version, doc.find("//diffResult/relation").first["new_version"].to_i
660
661     # check that nothing was, in fact, deleted
662     assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
663     assert_equal true, Way.find(current_ways(:used_way).id).visible
664     assert_equal true, Relation.find(current_relations(:public_used_relation).id).visible
665   end
666
667   ##
668   # upload an element with a really long tag value
669   def test_upload_invalid_too_long_tag
670     basic_authorization users(:public_user).email, "test"
671     cs_id = changesets(:public_user_first_change).id
672
673     # simple diff to create a node way and relation using placeholders
674     diff = <<EOF
675 <osmChange>
676  <create>
677   <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
678    <tag k='foo' v='#{'x' * 256}'/>
679   </node>
680  </create>
681 </osmChange>
682 EOF
683
684     # upload it
685     content diff
686     post :upload, :id => cs_id
687     assert_response :bad_request,
688                     "shoudln't be able to upload too long a tag to changeset: #{@response.body}"
689   end
690
691   ##
692   # upload something which creates new objects and inserts them into
693   # existing containers using placeholders.
694   def test_upload_complex
695     basic_authorization users(:public_user).email, "test"
696     cs_id = changesets(:public_user_first_change).id
697
698     # simple diff to create a node way and relation using placeholders
699     diff = <<EOF
700 <osmChange>
701  <create>
702   <node id='-1' lon='0' lat='0' changeset='#{cs_id}'>
703    <tag k='foo' v='bar'/>
704    <tag k='baz' v='bat'/>
705   </node>
706  </create>
707  <modify>
708   <way id='1' changeset='#{cs_id}' version='1'>
709    <nd ref='-1'/>
710    <nd ref='3'/>
711   </way>
712   <relation id='1' changeset='#{cs_id}' version='1'>
713    <member type='way' role='some' ref='3'/>
714    <member type='node' role='some' ref='-1'/>
715    <member type='relation' role='some' ref='3'/>
716   </relation>
717  </modify>
718 </osmChange>
719 EOF
720
721     # upload it
722     content diff
723     post :upload, :id => cs_id
724     assert_response :success,
725                     "can't upload a complex diff to changeset: #{@response.body}"
726
727     # check the returned payload
728     assert_select "diffResult[version='#{API_VERSION}'][generator='#{GENERATOR}']", 1
729     assert_select "diffResult>node", 1
730     assert_select "diffResult>way", 1
731     assert_select "diffResult>relation", 1
732
733     # inspect the response to find out what the new element IDs are
734     doc = XML::Parser.string(@response.body).parse
735     new_node_id = doc.find("//diffResult/node").first["new_id"].to_i
736
737     # check that the changes made it into the database
738     assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
739     assert_equal [new_node_id, 3], Way.find(1).nds, "way nodes should match"
740     Relation.find(1).members.each do |type, id, _role|
741       if type == "node"
742         assert_equal new_node_id, id, "relation should contain new node"
743       end
744     end
745   end
746
747   ##
748   # create a diff which references several changesets, which should cause
749   # a rollback and none of the diff gets committed
750   def test_upload_invalid_changesets
751     basic_authorization users(:public_user).email, "test"
752     cs_id = changesets(:public_user_first_change).id
753
754     # simple diff to create a node way and relation using placeholders
755     diff = <<EOF
756 <osmChange>
757  <modify>
758   <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
759   <way id='1' changeset='#{cs_id}' version='1'>
760    <nd ref='3'/>
761   </way>
762  </modify>
763  <modify>
764   <relation id='1' changeset='#{cs_id}' version='1'>
765    <member type='way' role='some' ref='3'/>
766    <member type='node' role='some' ref='5'/>
767    <member type='relation' role='some' ref='3'/>
768   </relation>
769  </modify>
770  <create>
771   <node id='-1' lon='0' lat='0' changeset='4'>
772    <tag k='foo' v='bar'/>
773    <tag k='baz' v='bat'/>
774   </node>
775  </create>
776 </osmChange>
777 EOF
778     # cache the objects before uploading them
779     node = current_nodes(:visible_node)
780     way = current_ways(:visible_way)
781     rel = current_relations(:visible_relation)
782
783     # upload it
784     content diff
785     post :upload, :id => cs_id
786     assert_response :conflict,
787                     "uploading a diff with multiple changsets should have failed"
788
789     # check that objects are unmodified
790     assert_nodes_are_equal(node, Node.find(1))
791     assert_ways_are_equal(way, Way.find(1))
792     assert_relations_are_equal(rel, Relation.find(1))
793   end
794
795   ##
796   # upload multiple versions of the same element in the same diff.
797   def test_upload_multiple_valid
798     basic_authorization users(:public_user).email, "test"
799     cs_id = changesets(:public_user_first_change).id
800
801     # change the location of a node multiple times, each time referencing
802     # the last version. doesn't this depend on version numbers being
803     # sequential?
804     diff = <<EOF
805 <osmChange>
806  <modify>
807   <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
808   <node id='1' lon='1' lat='0' changeset='#{cs_id}' version='2'/>
809   <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='3'/>
810   <node id='1' lon='1' lat='2' changeset='#{cs_id}' version='4'/>
811   <node id='1' lon='2' lat='2' changeset='#{cs_id}' version='5'/>
812   <node id='1' lon='3' lat='2' changeset='#{cs_id}' version='6'/>
813   <node id='1' lon='3' lat='3' changeset='#{cs_id}' version='7'/>
814   <node id='1' lon='9' lat='9' changeset='#{cs_id}' version='8'/>
815  </modify>
816 </osmChange>
817 EOF
818
819     # upload it
820     content diff
821     post :upload, :id => cs_id
822     assert_response :success,
823                     "can't upload multiple versions of an element in a diff: #{@response.body}"
824
825     # check the response is well-formed. its counter-intuitive, but the
826     # API will return multiple elements with the same ID and different
827     # version numbers for each change we made.
828     assert_select "diffResult>node", 8
829   end
830
831   ##
832   # upload multiple versions of the same element in the same diff, but
833   # keep the version numbers the same.
834   def test_upload_multiple_duplicate
835     basic_authorization users(:public_user).email, "test"
836     cs_id = changesets(:public_user_first_change).id
837
838     diff = <<EOF
839 <osmChange>
840  <modify>
841   <node id='1' lon='0' lat='0' changeset='#{cs_id}' version='1'/>
842   <node id='1' lon='1' lat='1' changeset='#{cs_id}' version='1'/>
843  </modify>
844 </osmChange>
845 EOF
846
847     # upload it
848     content diff
849     post :upload, :id => cs_id
850     assert_response :conflict,
851                     "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
852   end
853
854   ##
855   # try to upload some elements without specifying the version
856   def test_upload_missing_version
857     basic_authorization users(:public_user).email, "test"
858     cs_id = changesets(:public_user_first_change).id
859
860     diff = <<EOF
861 <osmChange>
862  <modify>
863  <node id='1' lon='1' lat='1' changeset='cs_id'/>
864  </modify>
865 </osmChange>
866 EOF
867
868     # upload it
869     content diff
870     post :upload, :id => cs_id
871     assert_response :bad_request,
872                     "shouldn't be able to upload an element without version: #{@response.body}"
873   end
874
875   ##
876   # try to upload with commands other than create, modify, or delete
877   def test_action_upload_invalid
878     basic_authorization users(:public_user).email, "test"
879     cs_id = changesets(:public_user_first_change).id
880
881     diff = <<EOF
882 <osmChange>
883   <ping>
884    <node id='1' lon='1' lat='1' changeset='#{cs_id}' />
885   </ping>
886 </osmChange>
887 EOF
888     content diff
889     post :upload, :id => cs_id
890     assert_response :bad_request, "Shouldn't be able to upload a diff with the action ping"
891     assert_equal @response.body, "Unknown action ping, choices are create, modify, delete"
892   end
893
894   ##
895   # upload a valid changeset which has a mixture of whitespace
896   # to check a bug reported by ivansanchez (#1565).
897   def test_upload_whitespace_valid
898     basic_authorization users(:public_user).email, "test"
899     changeset_id = changesets(:public_user_first_change).id
900
901     diff = <<EOF
902 <osmChange>
903  <modify><node id='1' lon='0' lat='0' changeset='#{changeset_id}'
904   version='1'></node>
905   <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='2'><tag k='k' v='v'/></node></modify>
906  <modify>
907  <relation id='1' changeset='#{changeset_id}' version='1'><member
908    type='way' role='some' ref='3'/><member
909     type='node' role='some' ref='5'/>
910    <member type='relation' role='some' ref='3'/>
911   </relation>
912  </modify></osmChange>
913 EOF
914
915     # upload it
916     content diff
917     post :upload, :id => changeset_id
918     assert_response :success,
919                     "can't upload a valid diff with whitespace variations to changeset: #{@response.body}"
920
921     # check the response is well-formed
922     assert_select "diffResult>node", 2
923     assert_select "diffResult>relation", 1
924
925     # check that the changes made it into the database
926     assert_equal 1, Node.find(1).tags.size, "node 1 should now have one tag"
927     assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
928   end
929
930   ##
931   # upload a valid changeset which has a mixture of whitespace
932   # to check a bug reported by ivansanchez.
933   def test_upload_reuse_placeholder_valid
934     basic_authorization users(:public_user).email, "test"
935     changeset_id = changesets(:public_user_first_change).id
936
937     diff = <<EOF
938 <osmChange>
939  <create>
940   <node id='-1' lon='0' lat='0' changeset='#{changeset_id}'>
941    <tag k="foo" v="bar"/>
942   </node>
943  </create>
944  <modify>
945   <node id='-1' lon='1' lat='1' changeset='#{changeset_id}' version='1'/>
946  </modify>
947  <delete>
948   <node id='-1' lon='2' lat='2' changeset='#{changeset_id}' version='2'/>
949  </delete>
950 </osmChange>
951 EOF
952
953     # upload it
954     content diff
955     post :upload, :id => changeset_id
956     assert_response :success,
957                     "can't upload a valid diff with re-used placeholders to changeset: #{@response.body}"
958
959     # check the response is well-formed
960     assert_select "diffResult>node", 3
961     assert_select "diffResult>node[old_id='-1']", 3
962   end
963
964   ##
965   # test what happens if a diff upload re-uses placeholder IDs in an
966   # illegal way.
967   def test_upload_placeholder_invalid
968     basic_authorization users(:public_user).email, "test"
969     changeset_id = changesets(:public_user_first_change).id
970
971     diff = <<EOF
972 <osmChange>
973  <create>
974   <node id='-1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
975   <node id='-1' lon='1' lat='1' changeset='#{changeset_id}' version='1'/>
976   <node id='-1' lon='2' lat='2' changeset='#{changeset_id}' version='2'/>
977  </create>
978 </osmChange>
979 EOF
980
981     # upload it
982     content diff
983     post :upload, :id => changeset_id
984     assert_response :bad_request,
985                     "shouldn't be able to re-use placeholder IDs"
986   end
987
988   ##
989   # test that uploading a way referencing invalid placeholders gives a
990   # proper error, not a 500.
991   def test_upload_placeholder_invalid_way
992     basic_authorization users(:public_user).email, "test"
993     changeset_id = changesets(:public_user_first_change).id
994
995     diff = <<EOF
996 <osmChange>
997  <create>
998   <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
999   <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1000   <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1001   <way id="-1" changeset="#{changeset_id}" version="1">
1002    <nd ref="-1"/>
1003    <nd ref="-2"/>
1004    <nd ref="-3"/>
1005    <nd ref="-4"/>
1006   </way>
1007  </create>
1008 </osmChange>
1009 EOF
1010
1011     # upload it
1012     content diff
1013     post :upload, :id => changeset_id
1014     assert_response :bad_request,
1015                     "shouldn't be able to use invalid placeholder IDs"
1016     assert_equal "Placeholder node not found for reference -4 in way -1", @response.body
1017
1018     # the same again, but this time use an existing way
1019     diff = <<EOF
1020 <osmChange>
1021  <create>
1022   <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1023   <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1024   <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1025   <way id="1" changeset="#{changeset_id}" version="1">
1026    <nd ref="-1"/>
1027    <nd ref="-2"/>
1028    <nd ref="-3"/>
1029    <nd ref="-4"/>
1030   </way>
1031  </create>
1032 </osmChange>
1033 EOF
1034
1035     # upload it
1036     content diff
1037     post :upload, :id => changeset_id
1038     assert_response :bad_request,
1039                     "shouldn't be able to use invalid placeholder IDs"
1040     assert_equal "Placeholder node not found for reference -4 in way 1", @response.body
1041   end
1042
1043   ##
1044   # test that uploading a relation referencing invalid placeholders gives a
1045   # proper error, not a 500.
1046   def test_upload_placeholder_invalid_relation
1047     basic_authorization users(:public_user).email, "test"
1048     changeset_id = changesets(:public_user_first_change).id
1049
1050     diff = <<EOF
1051 <osmChange>
1052  <create>
1053   <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1054   <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1055   <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1056   <relation id="-1" changeset="#{changeset_id}" version="1">
1057    <member type="node" role="foo" ref="-1"/>
1058    <member type="node" role="foo" ref="-2"/>
1059    <member type="node" role="foo" ref="-3"/>
1060    <member type="node" role="foo" ref="-4"/>
1061   </relation>
1062  </create>
1063 </osmChange>
1064 EOF
1065
1066     # upload it
1067     content diff
1068     post :upload, :id => changeset_id
1069     assert_response :bad_request,
1070                     "shouldn't be able to use invalid placeholder IDs"
1071     assert_equal "Placeholder Node not found for reference -4 in relation -1.", @response.body
1072
1073     # the same again, but this time use an existing way
1074     diff = <<EOF
1075 <osmChange>
1076  <create>
1077   <node id="-1" lon="0" lat="0" changeset="#{changeset_id}" version="1"/>
1078   <node id="-2" lon="1" lat="1" changeset="#{changeset_id}" version="1"/>
1079   <node id="-3" lon="2" lat="2" changeset="#{changeset_id}" version="1"/>
1080   <relation id="1" changeset="#{changeset_id}" version="1">
1081    <member type="node" role="foo" ref="-1"/>
1082    <member type="node" role="foo" ref="-2"/>
1083    <member type="node" role="foo" ref="-3"/>
1084    <member type="way" role="bar" ref="-1"/>
1085   </relation>
1086  </create>
1087 </osmChange>
1088 EOF
1089
1090     # upload it
1091     content diff
1092     post :upload, :id => changeset_id
1093     assert_response :bad_request,
1094                     "shouldn't be able to use invalid placeholder IDs"
1095     assert_equal "Placeholder Way not found for reference -1 in relation 1.", @response.body
1096   end
1097
1098   ##
1099   # test what happens if a diff is uploaded containing only a node
1100   # move.
1101   def test_upload_node_move
1102     basic_authorization users(:public_user).email, "test"
1103
1104     content "<osm><changeset>" +
1105             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1106             "</changeset></osm>"
1107     put :create
1108     assert_response :success
1109     changeset_id = @response.body.to_i
1110
1111     old_node = current_nodes(:visible_node)
1112
1113     diff = XML::Document.new
1114     diff.root = XML::Node.new "osmChange"
1115     modify = XML::Node.new "modify"
1116     xml_old_node = old_node.to_xml_node
1117     xml_old_node["lat"] = 2.0.to_s
1118     xml_old_node["lon"] = 2.0.to_s
1119     xml_old_node["changeset"] = changeset_id.to_s
1120     modify << xml_old_node
1121     diff.root << modify
1122
1123     # upload it
1124     content diff
1125     post :upload, :id => changeset_id
1126     assert_response :success,
1127                     "diff should have uploaded OK"
1128
1129     # check the bbox
1130     changeset = Changeset.find(changeset_id)
1131     assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1132     assert_equal 2 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 2 degrees"
1133     assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1134     assert_equal 2 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 2 degrees"
1135   end
1136
1137   ##
1138   # test what happens if a diff is uploaded adding a node to a way.
1139   def test_upload_way_extend
1140     basic_authorization users(:public_user).email, "test"
1141
1142     content "<osm><changeset>" +
1143             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1144             "</changeset></osm>"
1145     put :create
1146     assert_response :success
1147     changeset_id = @response.body.to_i
1148
1149     old_way = current_ways(:visible_way)
1150
1151     diff = XML::Document.new
1152     diff.root = XML::Node.new "osmChange"
1153     modify = XML::Node.new "modify"
1154     xml_old_way = old_way.to_xml_node
1155     nd_ref = XML::Node.new "nd"
1156     nd_ref["ref"] = current_nodes(:visible_node).id.to_s
1157     xml_old_way << nd_ref
1158     xml_old_way["changeset"] = changeset_id.to_s
1159     modify << xml_old_way
1160     diff.root << modify
1161
1162     # upload it
1163     content diff
1164     post :upload, :id => changeset_id
1165     assert_response :success,
1166                     "diff should have uploaded OK"
1167
1168     # check the bbox
1169     changeset = Changeset.find(changeset_id)
1170     assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree"
1171     assert_equal 3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees"
1172     assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree"
1173     assert_equal 3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees"
1174   end
1175
1176   ##
1177   # test for more issues in #1568
1178   def test_upload_empty_invalid
1179     basic_authorization users(:public_user).email, "test"
1180
1181     ["<osmChange/>",
1182      "<osmChange></osmChange>",
1183      "<osmChange><modify/></osmChange>",
1184      "<osmChange><modify></modify></osmChange>"].each do |diff|
1185       # upload it
1186       content diff
1187       post :upload, :id => changesets(:public_user_first_change).id
1188       assert_response(:success, "should be able to upload " +
1189                       "empty changeset: " + diff)
1190     end
1191   end
1192
1193   ##
1194   # test that the X-Error-Format header works to request XML errors
1195   def test_upload_xml_errors
1196     basic_authorization users(:public_user).email, "test"
1197
1198     # try and delete a node that is in use
1199     diff = XML::Document.new
1200     diff.root = XML::Node.new "osmChange"
1201     delete = XML::Node.new "delete"
1202     diff.root << delete
1203     delete << current_nodes(:node_used_by_relationship).to_xml_node
1204
1205     # upload it
1206     content diff
1207     error_format "xml"
1208     post :upload, :id => 2
1209     assert_response :success,
1210                     "failed to return error in XML format"
1211
1212     # check the returned payload
1213     assert_select "osmError[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1
1214     assert_select "osmError>status", 1
1215     assert_select "osmError>message", 1
1216   end
1217
1218   ##
1219   # when we make some simple changes we get the same changes back from the
1220   # diff download.
1221   def test_diff_download_simple
1222     ## First try with the normal user, which should get a forbidden
1223     basic_authorization(users(:normal_user).email, "test")
1224
1225     # create a temporary changeset
1226     content "<osm><changeset>" +
1227             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1228             "</changeset></osm>"
1229     put :create
1230     assert_response :forbidden
1231
1232     ## Now try with the public user
1233     basic_authorization(users(:public_user).email, "test")
1234
1235     # create a temporary changeset
1236     content "<osm><changeset>" +
1237             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1238             "</changeset></osm>"
1239     put :create
1240     assert_response :success
1241     changeset_id = @response.body.to_i
1242
1243     # add a diff to it
1244     diff = <<EOF
1245 <osmChange>
1246  <modify>
1247   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1248   <node id='1' lon='1' lat='0' changeset='#{changeset_id}' version='2'/>
1249   <node id='1' lon='1' lat='1' changeset='#{changeset_id}' version='3'/>
1250   <node id='1' lon='1' lat='2' changeset='#{changeset_id}' version='4'/>
1251   <node id='1' lon='2' lat='2' changeset='#{changeset_id}' version='5'/>
1252   <node id='1' lon='3' lat='2' changeset='#{changeset_id}' version='6'/>
1253   <node id='1' lon='3' lat='3' changeset='#{changeset_id}' version='7'/>
1254   <node id='1' lon='9' lat='9' changeset='#{changeset_id}' version='8'/>
1255  </modify>
1256 </osmChange>
1257 EOF
1258
1259     # upload it
1260     content diff
1261     post :upload, :id => changeset_id
1262     assert_response :success,
1263                     "can't upload multiple versions of an element in a diff: #{@response.body}"
1264
1265     get :download, :id => changeset_id
1266     assert_response :success
1267
1268     assert_select "osmChange", 1
1269     assert_select "osmChange>modify", 8
1270     assert_select "osmChange>modify>node", 8
1271   end
1272
1273   ##
1274   # culled this from josm to ensure that nothing in the way that josm
1275   # is formatting the request is causing it to fail.
1276   #
1277   # NOTE: the error turned out to be something else completely!
1278   def test_josm_upload
1279     basic_authorization(users(:public_user).email, "test")
1280
1281     # create a temporary changeset
1282     content "<osm><changeset>" +
1283             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1284             "</changeset></osm>"
1285     put :create
1286     assert_response :success
1287     changeset_id = @response.body.to_i
1288
1289     diff = <<OSMFILE
1290 <osmChange version="0.6" generator="JOSM">
1291 <create version="0.6" generator="JOSM">
1292   <node id='-1' visible='true' changeset='#{changeset_id}' lat='51.49619982187321' lon='-0.18722061869438314' />
1293   <node id='-2' visible='true' changeset='#{changeset_id}' lat='51.496359883909605' lon='-0.18653093576241928' />
1294   <node id='-3' visible='true' changeset='#{changeset_id}' lat='51.49598132358285' lon='-0.18719613290981638' />
1295   <node id='-4' visible='true' changeset='#{changeset_id}' lat='51.4961591711078' lon='-0.18629015888084607' />
1296   <node id='-5' visible='true' changeset='#{changeset_id}' lat='51.49582126021711' lon='-0.18708186591517145' />
1297   <node id='-6' visible='true' changeset='#{changeset_id}' lat='51.49591018437858' lon='-0.1861432441734455' />
1298   <node id='-7' visible='true' changeset='#{changeset_id}' lat='51.49560784152179' lon='-0.18694719410005425' />
1299   <node id='-8' visible='true' changeset='#{changeset_id}' lat='51.49567389979617' lon='-0.1860289771788006' />
1300   <node id='-9' visible='true' changeset='#{changeset_id}' lat='51.49543761398892' lon='-0.186820684213126' />
1301   <way id='-10' action='modiy' visible='true' changeset='#{changeset_id}'>
1302     <nd ref='-1' />
1303     <nd ref='-2' />
1304     <nd ref='-3' />
1305     <nd ref='-4' />
1306     <nd ref='-5' />
1307     <nd ref='-6' />
1308     <nd ref='-7' />
1309     <nd ref='-8' />
1310     <nd ref='-9' />
1311     <tag k='highway' v='residential' />
1312     <tag k='name' v='Foobar Street' />
1313   </way>
1314 </create>
1315 </osmChange>
1316 OSMFILE
1317
1318     # upload it
1319     content diff
1320     post :upload, :id => changeset_id
1321     assert_response :success,
1322                     "can't upload a diff from JOSM: #{@response.body}"
1323
1324     get :download, :id => changeset_id
1325     assert_response :success
1326
1327     assert_select "osmChange", 1
1328     assert_select "osmChange>create>node", 9
1329     assert_select "osmChange>create>way", 1
1330     assert_select "osmChange>create>way>nd", 9
1331     assert_select "osmChange>create>way>tag", 2
1332   end
1333
1334   ##
1335   # when we make some complex changes we get the same changes back from the
1336   # diff download.
1337   def test_diff_download_complex
1338     basic_authorization(users(:public_user).email, "test")
1339
1340     # create a temporary changeset
1341     content "<osm><changeset>" +
1342             "<tag k='created_by' v='osm test suite checking changesets'/>" +
1343             "</changeset></osm>"
1344     put :create
1345     assert_response :success
1346     changeset_id = @response.body.to_i
1347
1348     # add a diff to it
1349     diff = <<EOF
1350 <osmChange>
1351  <delete>
1352   <node id='1' lon='0' lat='0' changeset='#{changeset_id}' version='1'/>
1353  </delete>
1354  <create>
1355   <node id='-1' lon='9' lat='9' changeset='#{changeset_id}' version='0'/>
1356   <node id='-2' lon='8' lat='9' changeset='#{changeset_id}' version='0'/>
1357   <node id='-3' lon='7' lat='9' changeset='#{changeset_id}' version='0'/>
1358  </create>
1359  <modify>
1360   <node id='3' lon='20' lat='15' changeset='#{changeset_id}' version='1'/>
1361   <way id='1' changeset='#{changeset_id}' version='1'>
1362    <nd ref='3'/>
1363    <nd ref='-1'/>
1364    <nd ref='-2'/>
1365    <nd ref='-3'/>
1366   </way>
1367  </modify>
1368 </osmChange>
1369 EOF
1370
1371     # upload it
1372     content diff
1373     post :upload, :id => changeset_id
1374     assert_response :success,
1375                     "can't upload multiple versions of an element in a diff: #{@response.body}"
1376
1377     get :download, :id => changeset_id
1378     assert_response :success
1379
1380     assert_select "osmChange", 1
1381     assert_select "osmChange>create", 3
1382     assert_select "osmChange>delete", 1
1383     assert_select "osmChange>modify", 2
1384     assert_select "osmChange>create>node", 3
1385     assert_select "osmChange>delete>node", 1
1386     assert_select "osmChange>modify>node", 1
1387     assert_select "osmChange>modify>way", 1
1388   end
1389
1390   def test_changeset_download
1391     get :download, :id => changesets(:normal_user_first_change).id
1392     assert_response :success
1393     assert_template nil
1394     # print @response.body
1395     # FIXME: needs more assert_select tests
1396     assert_select "osmChange[version='#{API_VERSION}'][generator='#{GENERATOR}']" do
1397       assert_select "create", :count => 5
1398       assert_select "create>node[id='#{nodes(:used_node_2).node_id}'][visible='#{nodes(:used_node_2).visible?}'][version='#{nodes(:used_node_2).version}']" do
1399         assert_select "tag[k='#{node_tags(:t3).k}'][v='#{node_tags(:t3).v}']"
1400       end
1401       assert_select "create>node[id='#{nodes(:visible_node).node_id}']"
1402     end
1403   end
1404
1405   ##
1406   # check that the bounding box of a changeset gets updated correctly
1407   # FIXME: This should really be moded to a integration test due to the with_controller
1408   def test_changeset_bbox
1409     basic_authorization users(:public_user).email, "test"
1410
1411     # create a new changeset
1412     content "<osm><changeset/></osm>"
1413     put :create
1414     assert_response :success, "Creating of changeset failed."
1415     changeset_id = @response.body.to_i
1416
1417     # add a single node to it
1418     with_controller(NodeController.new) do
1419       content "<osm><node lon='1' lat='2' changeset='#{changeset_id}'/></osm>"
1420       put :create
1421       assert_response :success, "Couldn't create node."
1422     end
1423
1424     # get the bounding box back from the changeset
1425     get :read, :id => changeset_id
1426     assert_response :success, "Couldn't read back changeset."
1427     assert_select "osm>changeset[min_lon='1.0']", 1
1428     assert_select "osm>changeset[max_lon='1.0']", 1
1429     assert_select "osm>changeset[min_lat='2.0']", 1
1430     assert_select "osm>changeset[max_lat='2.0']", 1
1431
1432     # add another node to it
1433     with_controller(NodeController.new) do
1434       content "<osm><node lon='2' lat='1' changeset='#{changeset_id}'/></osm>"
1435       put :create
1436       assert_response :success, "Couldn't create second node."
1437     end
1438
1439     # get the bounding box back from the changeset
1440     get :read, :id => changeset_id
1441     assert_response :success, "Couldn't read back changeset for the second time."
1442     assert_select "osm>changeset[min_lon='1.0']", 1
1443     assert_select "osm>changeset[max_lon='2.0']", 1
1444     assert_select "osm>changeset[min_lat='1.0']", 1
1445     assert_select "osm>changeset[max_lat='2.0']", 1
1446
1447     # add (delete) a way to it, which contains a point at (3,3)
1448     with_controller(WayController.new) do
1449       content update_changeset(current_ways(:visible_way).to_xml,
1450                                changeset_id)
1451       put :delete, :id => current_ways(:visible_way).id
1452       assert_response :success, "Couldn't delete a way."
1453     end
1454
1455     # get the bounding box back from the changeset
1456     get :read, :id => changeset_id
1457     assert_response :success, "Couldn't read back changeset for the third time."
1458     # note that the 3.1 here is because of the bbox overexpansion
1459     assert_select "osm>changeset[min_lon='1.0']", 1
1460     assert_select "osm>changeset[max_lon='3.1']", 1
1461     assert_select "osm>changeset[min_lat='1.0']", 1
1462     assert_select "osm>changeset[max_lat='3.1']", 1
1463   end
1464
1465   ##
1466   # test that the changeset :include method works as it should
1467   def test_changeset_include
1468     basic_authorization users(:public_user).display_name, "test"
1469
1470     # create a new changeset
1471     content "<osm><changeset/></osm>"
1472     put :create
1473     assert_response :success, "Creating of changeset failed."
1474     changeset_id = @response.body.to_i
1475
1476     # NOTE: the include method doesn't over-expand, like inserting
1477     # a real method does. this is because we expect the client to
1478     # know what it is doing!
1479     check_after_include(changeset_id, 1, 1, [1, 1, 1, 1])
1480     check_after_include(changeset_id, 3, 3, [1, 1, 3, 3])
1481     check_after_include(changeset_id, 4, 2, [1, 1, 4, 3])
1482     check_after_include(changeset_id, 2, 2, [1, 1, 4, 3])
1483     check_after_include(changeset_id, -1, -1, [-1, -1, 4, 3])
1484     check_after_include(changeset_id, -2, 5, [-2, -1, 4, 5])
1485   end
1486
1487   ##
1488   # test that a not found, wrong method with the expand bbox works as expected
1489   def test_changeset_expand_bbox_error
1490     basic_authorization users(:public_user).display_name, "test"
1491
1492     # create a new changeset
1493     content "<osm><changeset/></osm>"
1494     put :create
1495     assert_response :success, "Creating of changeset failed."
1496     changeset_id = @response.body.to_i
1497
1498     lon = 58.2
1499     lat = -0.45
1500
1501     # Try and put
1502     content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1503     put :expand_bbox, :id => changeset_id
1504     assert_response :method_not_allowed, "shouldn't be able to put a bbox expand"
1505
1506     # Try to get the update
1507     content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1508     get :expand_bbox, :id => changeset_id
1509     assert_response :method_not_allowed, "shouldn't be able to get a bbox expand"
1510
1511     # Try to use a hopefully missing changeset
1512     content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
1513     post :expand_bbox, :id => changeset_id + 13245
1514     assert_response :not_found, "shouldn't be able to do a bbox expand on a nonexistant changeset"
1515   end
1516
1517   ##
1518   # test the query functionality of changesets
1519   def test_query
1520     get :query, :bbox => "-10,-10, 10, 10"
1521     assert_response :success, "can't get changesets in bbox"
1522     assert_changesets [1, 4, 6]
1523
1524     get :query, :bbox => "4.5,4.5,4.6,4.6"
1525     assert_response :success, "can't get changesets in bbox"
1526     assert_changesets [1]
1527
1528     # not found when looking for changesets of non-existing users
1529     get :query, :user => User.maximum(:id) + 1
1530     assert_response :not_found
1531     get :query, :display_name => " "
1532     assert_response :not_found
1533
1534     # can't get changesets of user 1 without authenticating
1535     get :query, :user => users(:normal_user).id
1536     assert_response :not_found, "shouldn't be able to get changesets by non-public user (ID)"
1537     get :query, :display_name => users(:normal_user).display_name
1538     assert_response :not_found, "shouldn't be able to get changesets by non-public user (name)"
1539
1540     # but this should work
1541     basic_authorization "test@openstreetmap.org", "test"
1542     get :query, :user => users(:normal_user).id
1543     assert_response :success, "can't get changesets by user ID"
1544     assert_changesets [1, 3, 6, 8]
1545
1546     get :query, :display_name => users(:normal_user).display_name
1547     assert_response :success, "can't get changesets by user name"
1548     assert_changesets [1, 3, 6, 8]
1549
1550     # check that the correct error is given when we provide both UID and name
1551     get :query, :user => users(:normal_user).id, :display_name => users(:normal_user).display_name
1552     assert_response :bad_request, "should be a bad request to have both ID and name specified"
1553
1554     get :query, :user => users(:normal_user).id, :open => true
1555     assert_response :success, "can't get changesets by user and open"
1556     assert_changesets [1]
1557
1558     get :query, :time => "2007-12-31"
1559     assert_response :success, "can't get changesets by time-since"
1560     assert_changesets [1, 2, 4, 5, 6]
1561
1562     get :query, :time => "2008-01-01T12:34Z"
1563     assert_response :success, "can't get changesets by time-since with hour"
1564     assert_changesets [1, 2, 4, 5, 6]
1565
1566     get :query, :time => "2007-12-31T23:59Z,2008-01-01T00:01Z"
1567     assert_response :success, "can't get changesets by time-range"
1568     assert_changesets [1, 5, 6]
1569
1570     get :query, :open => "true"
1571     assert_response :success, "can't get changesets by open-ness"
1572     assert_changesets [1, 2, 4]
1573
1574     get :query, :closed => "true"
1575     assert_response :success, "can't get changesets by closed-ness"
1576     assert_changesets [3, 5, 6, 7, 8, 9]
1577
1578     get :query, :closed => "true", :user => users(:normal_user).id
1579     assert_response :success, "can't get changesets by closed-ness and user"
1580     assert_changesets [3, 6, 8]
1581
1582     get :query, :closed => "true", :user => users(:public_user).id
1583     assert_response :success, "can't get changesets by closed-ness and user"
1584     assert_changesets [7]
1585
1586     get :query, :changesets => "1,2,3"
1587     assert_response :success, "can't get changesets by id (as comma-separated string)"
1588     assert_changesets [1, 2, 3]
1589
1590     get :query, :changesets => ""
1591     assert_response :bad_request, "should be a bad request since changesets is empty"
1592   end
1593
1594   ##
1595   # check that errors are returned if garbage is inserted
1596   # into query strings
1597   def test_query_invalid
1598     ["abracadabra!",
1599      "1,2,3,F",
1600      ";drop table users;"].each do |bbox|
1601       get :query, :bbox => bbox
1602       assert_response :bad_request, "'#{bbox}' isn't a bbox"
1603     end
1604
1605     ["now()",
1606      "00-00-00",
1607      ";drop table users;",
1608      ",",
1609      "-,-"].each do |time|
1610       get :query, :time => time
1611       assert_response :bad_request, "'#{time}' isn't a valid time range"
1612     end
1613
1614     ["me",
1615      "foobar",
1616      "-1",
1617      "0"].each do |uid|
1618       get :query, :user => uid
1619       assert_response :bad_request, "'#{uid}' isn't a valid user ID"
1620     end
1621   end
1622
1623   ##
1624   # check updating tags on a changeset
1625   def test_changeset_update
1626     ## First try with the non-public user
1627     changeset = changesets(:normal_user_first_change)
1628     new_changeset = changeset.to_xml
1629     new_tag = XML::Node.new "tag"
1630     new_tag["k"] = "tagtesting"
1631     new_tag["v"] = "valuetesting"
1632     new_changeset.find("//osm/changeset").first << new_tag
1633     content new_changeset
1634
1635     # try without any authorization
1636     put :update, :id => changeset.id
1637     assert_response :unauthorized
1638
1639     # try with the wrong authorization
1640     basic_authorization users(:public_user).email, "test"
1641     put :update, :id => changeset.id
1642     assert_response :conflict
1643
1644     # now this should get an unauthorized
1645     basic_authorization users(:normal_user).email, "test"
1646     put :update, :id => changeset.id
1647     assert_require_public_data "user with their data non-public, shouldn't be able to edit their changeset"
1648
1649     ## Now try with the public user
1650     changeset = changesets(:public_user_first_change)
1651     new_changeset = changeset.to_xml
1652     new_tag = XML::Node.new "tag"
1653     new_tag["k"] = "tagtesting"
1654     new_tag["v"] = "valuetesting"
1655     new_changeset.find("//osm/changeset").first << new_tag
1656     content new_changeset
1657
1658     # try without any authorization
1659     @request.env["HTTP_AUTHORIZATION"] = nil
1660     put :update, :id => changeset.id
1661     assert_response :unauthorized
1662
1663     # try with the wrong authorization
1664     basic_authorization users(:second_public_user).email, "test"
1665     put :update, :id => changeset.id
1666     assert_response :conflict
1667
1668     # now this should work...
1669     basic_authorization users(:public_user).email, "test"
1670     put :update, :id => changeset.id
1671     assert_response :success
1672
1673     assert_select "osm>changeset[id='#{changeset.id}']", 1
1674     assert_select "osm>changeset>tag", 2
1675     assert_select "osm>changeset>tag[k='tagtesting'][v='valuetesting']", 1
1676   end
1677
1678   ##
1679   # check that a user different from the one who opened the changeset
1680   # can't modify it.
1681   def test_changeset_update_invalid
1682     basic_authorization users(:public_user).email, "test"
1683
1684     changeset = changesets(:normal_user_first_change)
1685     new_changeset = changeset.to_xml
1686     new_tag = XML::Node.new "tag"
1687     new_tag["k"] = "testing"
1688     new_tag["v"] = "testing"
1689     new_changeset.find("//osm/changeset").first << new_tag
1690
1691     content new_changeset
1692     put :update, :id => changeset.id
1693     assert_response :conflict
1694   end
1695
1696   ##
1697   # check that a changeset can contain a certain max number of changes.
1698   ## FIXME should be changed to an integration test due to the with_controller
1699   def test_changeset_limits
1700     basic_authorization users(:public_user).email, "test"
1701
1702     # open a new changeset
1703     content "<osm><changeset/></osm>"
1704     put :create
1705     assert_response :success, "can't create a new changeset"
1706     cs_id = @response.body.to_i
1707
1708     # start the counter just short of where the changeset should finish.
1709     offset = 10
1710     # alter the database to set the counter on the changeset directly,
1711     # otherwise it takes about 6 minutes to fill all of them.
1712     changeset = Changeset.find(cs_id)
1713     changeset.num_changes = Changeset::MAX_ELEMENTS - offset
1714     changeset.save!
1715
1716     with_controller(NodeController.new) do
1717       # create a new node
1718       content "<osm><node changeset='#{cs_id}' lat='0.0' lon='0.0'/></osm>"
1719       put :create
1720       assert_response :success, "can't create a new node"
1721       node_id = @response.body.to_i
1722
1723       get :read, :id => node_id
1724       assert_response :success, "can't read back new node"
1725       node_doc = XML::Parser.string(@response.body).parse
1726       node_xml = node_doc.find("//osm/node").first
1727
1728       # loop until we fill the changeset with nodes
1729       offset.times do |i|
1730         node_xml["lat"] = rand.to_s
1731         node_xml["lon"] = rand.to_s
1732         node_xml["version"] = (i + 1).to_s
1733
1734         content node_doc
1735         put :update, :id => node_id
1736         assert_response :success, "attempt #{i} should have succeeded"
1737       end
1738
1739       # trying again should fail
1740       node_xml["lat"] = rand.to_s
1741       node_xml["lon"] = rand.to_s
1742       node_xml["version"] = offset.to_s
1743
1744       content node_doc
1745       put :update, :id => node_id
1746       assert_response :conflict, "final attempt should have failed"
1747     end
1748
1749     changeset = Changeset.find(cs_id)
1750     assert_equal Changeset::MAX_ELEMENTS + 1, changeset.num_changes
1751
1752     # check that the changeset is now closed as well
1753     assert(!changeset.is_open?,
1754            "changeset should have been auto-closed by exceeding " +
1755            "element limit.")
1756   end
1757
1758   ##
1759   # This should display the last 20 changesets closed
1760   def test_list
1761     get :list, :format => "html"
1762     assert_response :success
1763     assert_template "history"
1764     assert_template :layout => "map"
1765     assert_select "h2", :text => "Changesets", :count => 1
1766
1767     xhr :get, :list, :format => "html", :list => "1"
1768     assert_response :success
1769     assert_template "list"
1770
1771     check_list_result(Changeset.all)
1772   end
1773
1774   ##
1775   # This should display the last 20 changesets closed
1776   def test_list_xhr
1777     xhr :get, :list, :format => "html"
1778     assert_response :success
1779     assert_template "history"
1780     assert_template :layout => "xhr"
1781     assert_select "h2", :text => "Changesets", :count => 1
1782
1783     xhr :get, :list, :format => "html", :list => "1"
1784     assert_response :success
1785     assert_template "list"
1786
1787     check_list_result(Changeset.all)
1788   end
1789
1790   ##
1791   # This should display the last 20 changesets closed in a specific area
1792   def test_list_bbox
1793     get :list, :format => "html", :bbox => "4.5,4.5,5.5,5.5"
1794     assert_response :success
1795     assert_template "history"
1796     assert_template :layout => "map"
1797     assert_select "h2", :text => "Changesets", :count => 1
1798
1799     xhr :get, :list, :format => "html", :bbox => "4.5,4.5,5.5,5.5", :list => "1"
1800     assert_response :success
1801     assert_template "list"
1802
1803     check_list_result(Changeset.where("min_lon < 55000000 and max_lon > 45000000 and min_lat < 55000000 and max_lat > 45000000"))
1804   end
1805
1806   ##
1807   # Checks the display of the user changesets listing
1808   def test_list_user
1809     user = users(:public_user)
1810
1811     get :list, :format => "html", :display_name => user.display_name
1812     assert_response :success
1813     assert_template "history"
1814
1815     xhr :get, :list, :format => "html", :display_name => user.display_name, :list => "1"
1816     assert_response :success
1817     assert_template "list"
1818
1819     check_list_result(user.changesets)
1820   end
1821
1822   ##
1823   # Checks the display of the user changesets listing for a private user
1824   def test_list_private_user
1825     user = users(:normal_user)
1826
1827     get :list, :format => "html", :display_name => user.display_name
1828     assert_response :success
1829     assert_template "history"
1830
1831     xhr :get, :list, :format => "html", :display_name => user.display_name, :list => "1"
1832     assert_response :success
1833     assert_template "list"
1834
1835     check_list_result(Changeset.none)
1836   end
1837
1838   ##
1839   # Check the not found of the list user changesets
1840   def test_list_user_not_found
1841     get :list, :format => "html", :display_name => "Some random user"
1842     assert_response :not_found
1843     assert_template "user/no_such_user"
1844
1845     xhr :get, :list, :format => "html", :display_name => "Some random user", :list => "1"
1846     assert_response :not_found
1847     assert_template "user/no_such_user"
1848   end
1849
1850   ##
1851   # Checks the display of the friends changesets listing
1852   def test_list_friends
1853     user = users(:normal_user)
1854
1855     get :list, :friends => true
1856     assert_response :redirect
1857     assert_redirected_to :controller => :user, :action => :login, :referer => friend_changesets_path
1858
1859     session[:user] = user.id
1860
1861     get :list, :friends => true
1862     assert_response :success
1863     assert_template "history"
1864
1865     xhr :get, :list, :friends => true, :list => "1"
1866     assert_response :success
1867     assert_template "list"
1868
1869     check_list_result(Changeset.where(:user => user.friend_users.identifiable))
1870   end
1871
1872   ##
1873   # Checks the display of the nearby user changesets listing
1874   def test_list_nearby
1875     user = users(:normal_user)
1876
1877     get :list, :nearby => true
1878     assert_response :redirect
1879     assert_redirected_to :controller => :user, :action => :login, :referer => nearby_changesets_path
1880
1881     session[:user] = user.id
1882
1883     get :list, :nearby => true
1884     assert_response :success
1885     assert_template "history"
1886
1887     xhr :get, :list, :nearby => true, :list => "1"
1888     assert_response :success
1889     assert_template "list"
1890
1891     check_list_result(Changeset.where(:user => user.nearby))
1892   end
1893
1894   ##
1895   # Check that we can't request later pages of the changesets list
1896   def test_list_max_id
1897     xhr :get, :list, :format => "html", :max_id => 4
1898     assert_response :success
1899     assert_template "history"
1900     assert_template :layout => "xhr"
1901     assert_select "h2", :text => "Changesets", :count => 1
1902
1903     xhr :get, :list, :format => "html", :list => "1", :max_id => 4
1904     assert_response :success
1905     assert_template "list"
1906
1907     check_list_result(Changeset.where("id <= 4"))
1908   end
1909
1910   ##
1911   # This should display the last 20 changesets closed
1912   def test_feed
1913     get :feed, :format => :atom
1914     assert_response :success
1915     assert_template "list"
1916     assert_equal "application/atom+xml", response.content_type
1917
1918     check_feed_result(Changeset.all)
1919   end
1920
1921   ##
1922   # This should display the last 20 changesets closed in a specific area
1923   def test_feed_bbox
1924     get :feed, :format => :atom, :bbox => "4.5,4.5,5.5,5.5"
1925     assert_response :success
1926     assert_template "list"
1927     assert_equal "application/atom+xml", response.content_type
1928
1929     check_feed_result(Changeset.where("min_lon < 55000000 and max_lon > 45000000 and min_lat < 55000000 and max_lat > 45000000"))
1930   end
1931
1932   ##
1933   # Checks the display of the user changesets feed
1934   def test_feed_user
1935     user = users(:public_user)
1936
1937     get :feed, :format => :atom, :display_name => user.display_name
1938     assert_response :success
1939     assert_template "list"
1940     assert_equal "application/atom+xml", response.content_type
1941
1942     check_feed_result(user.changesets)
1943   end
1944
1945   ##
1946   # Check the not found of the user changesets feed
1947   def test_feed_user_not_found
1948     get :feed, :format => "atom", :display_name => "Some random user"
1949     assert_response :not_found
1950   end
1951
1952   ##
1953   # Check that we can't request later pages of the changesets feed
1954   def test_feed_max_id
1955     get :feed, :format => "atom", :max_id => 100
1956     assert_response :redirect
1957     assert_redirected_to :action => :feed
1958   end
1959
1960   ##
1961   # check that the changeset download for a changeset with a redacted
1962   # element in it doesn't contain that element.
1963   def test_diff_download_redacted
1964     changeset_id = changesets(:public_user_first_change).id
1965
1966     get :download, :id => changeset_id
1967     assert_response :success
1968
1969     assert_select "osmChange", 1
1970     # this changeset contains node 17 in versions 1 & 2, but 1 should
1971     # be hidden.
1972     assert_select "osmChange node[id='17']", 1
1973     assert_select "osmChange node[id='17'][version='1']", 0
1974   end
1975
1976   ##
1977   # create comment success
1978   def test_create_comment_success
1979     basic_authorization(users(:public_user).email, "test")
1980
1981     assert_difference "ChangesetComment.count", 1 do
1982       assert_no_difference "ActionMailer::Base.deliveries.size" do
1983         post :comment, :id => changesets(:normal_user_closed_change).id, :text => "This is a comment"
1984       end
1985     end
1986     assert_response :success
1987
1988     assert_difference "ChangesetComment.count", 1 do
1989       assert_difference "ActionMailer::Base.deliveries.size", 1 do
1990         post :comment, :id => changesets(:normal_user_subscribed_change).id, :text => "This is a comment"
1991       end
1992     end
1993     assert_response :success
1994
1995     email = ActionMailer::Base.deliveries.first
1996     assert_equal 1, email.to.length
1997     assert_equal "[OpenStreetMap] test2 has commented on one of your changesets", email.subject
1998     assert_equal "test@openstreetmap.org", email.to.first
1999
2000     ActionMailer::Base.deliveries.clear
2001
2002     basic_authorization(users(:second_public_user).email, "test")
2003
2004     assert_difference "ChangesetComment.count", 1 do
2005       assert_difference "ActionMailer::Base.deliveries.size", 2 do
2006         post :comment, :id => changesets(:normal_user_subscribed_change).id, :text => "This is a comment"
2007       end
2008     end
2009     assert_response :success
2010
2011     email = ActionMailer::Base.deliveries.find { |e| e.to.first == "test@openstreetmap.org" }
2012     assert_not_nil email
2013     assert_equal 1, email.to.length
2014     assert_equal "[OpenStreetMap] pulibc_test2 has commented on one of your changesets", email.subject
2015
2016     email = ActionMailer::Base.deliveries.find { |e| e.to.first == "test@example.com" }
2017     assert_not_nil email
2018     assert_equal 1, email.to.length
2019     assert_equal "[OpenStreetMap] pulibc_test2 has commented on a changeset you are interested in", email.subject
2020
2021     ActionMailer::Base.deliveries.clear
2022   end
2023
2024   ##
2025   # create comment fail
2026   def test_create_comment_fail
2027     # unauthorized
2028     post :comment, :id => changesets(:normal_user_closed_change).id, :text => "This is a comment"
2029     assert_response :unauthorized
2030
2031     basic_authorization(users(:public_user).email, "test")
2032
2033     # bad changeset id
2034     assert_no_difference "ChangesetComment.count" do
2035       post :comment, :id => 999111, :text => "This is a comment"
2036     end
2037     assert_response :not_found
2038
2039     # not closed changeset
2040     assert_no_difference "ChangesetComment.count" do
2041       post :comment, :id => changesets(:normal_user_first_change).id, :text => "This is a comment"
2042     end
2043     assert_response :conflict
2044
2045     # no text
2046     assert_no_difference "ChangesetComment.count" do
2047       post :comment, :id => changesets(:normal_user_closed_change).id
2048     end
2049     assert_response :bad_request
2050
2051     # empty text
2052     assert_no_difference "ChangesetComment.count" do
2053       post :comment, :id => changesets(:normal_user_closed_change).id, :text => ""
2054     end
2055     assert_response :bad_request
2056   end
2057
2058   ##
2059   # test subscribe success
2060   def test_subscribe_success
2061     basic_authorization(users(:public_user).email, "test")
2062     changeset = changesets(:normal_user_closed_change)
2063
2064     assert_difference "changeset.subscribers.count", 1 do
2065       post :subscribe, :id => changeset.id
2066     end
2067     assert_response :success
2068   end
2069
2070   ##
2071   # test subscribe fail
2072   def test_subscribe_fail
2073     # unauthorized
2074     changeset = changesets(:normal_user_closed_change)
2075     assert_no_difference "changeset.subscribers.count" do
2076       post :subscribe, :id => changeset.id
2077     end
2078     assert_response :unauthorized
2079
2080     basic_authorization(users(:public_user).email, "test")
2081
2082     # bad changeset id
2083     assert_no_difference "changeset.subscribers.count" do
2084       post :subscribe, :id => 999111
2085     end
2086     assert_response :not_found
2087
2088     # not closed changeset
2089     changeset = changesets(:normal_user_first_change)
2090     assert_no_difference "changeset.subscribers.count" do
2091       post :subscribe, :id => changeset.id
2092     end
2093     assert_response :conflict
2094
2095     # trying to subscribe when already subscribed
2096     changeset = changesets(:normal_user_subscribed_change)
2097     assert_no_difference "changeset.subscribers.count" do
2098       post :subscribe, :id => changeset.id
2099     end
2100     assert_response :conflict
2101   end
2102
2103   ##
2104   # test unsubscribe success
2105   def test_unsubscribe_success
2106     basic_authorization(users(:public_user).email, "test")
2107     changeset = changesets(:normal_user_subscribed_change)
2108
2109     assert_difference "changeset.subscribers.count", -1 do
2110       post :unsubscribe, :id => changeset.id
2111     end
2112     assert_response :success
2113   end
2114
2115   ##
2116   # test unsubscribe fail
2117   def test_unsubscribe_fail
2118     # unauthorized
2119     changeset = changesets(:normal_user_closed_change)
2120     assert_no_difference "changeset.subscribers.count" do
2121       post :unsubscribe, :id => changeset.id
2122     end
2123     assert_response :unauthorized
2124
2125     basic_authorization(users(:public_user).email, "test")
2126
2127     # bad changeset id
2128     assert_no_difference "changeset.subscribers.count" do
2129       post :unsubscribe, :id => 999111
2130     end
2131     assert_response :not_found
2132
2133     # not closed changeset
2134     changeset = changesets(:normal_user_first_change)
2135     assert_no_difference "changeset.subscribers.count" do
2136       post :unsubscribe, :id => changeset.id
2137     end
2138     assert_response :conflict
2139
2140     # trying to unsubscribe when not subscribed
2141     changeset = changesets(:normal_user_closed_change)
2142     assert_no_difference "changeset.subscribers.count" do
2143       post :unsubscribe, :id => changeset.id
2144     end
2145     assert_response :not_found
2146   end
2147
2148   ##
2149   # test hide comment fail
2150   def test_hide_comment_fail
2151     # unauthorized
2152     comment = changeset_comments(:normal_comment_1)
2153     assert_equal true, comment.visible
2154
2155     post :hide_comment, :id => comment.id
2156     assert_response :unauthorized
2157     assert_equal true, comment.reload.visible
2158
2159     basic_authorization(users(:public_user).email, "test")
2160
2161     # not a moderator
2162     post :hide_comment, :id => comment.id
2163     assert_response :forbidden
2164     assert_equal true, comment.reload.visible
2165
2166     basic_authorization(users(:moderator_user).email, "test")
2167
2168     # bad comment id
2169     post :hide_comment, :id => 999111
2170     assert_response :not_found
2171     assert_equal true, comment.reload.visible
2172   end
2173
2174   ##
2175   # test hide comment succes
2176   def test_hide_comment_success
2177     comment = changeset_comments(:normal_comment_1)
2178     assert_equal true, comment.visible
2179
2180     basic_authorization(users(:moderator_user).email, "test")
2181
2182     post :hide_comment, :id => comment.id
2183     assert_response :success
2184     assert_equal false, comment.reload.visible
2185   end
2186
2187   ##
2188   # test unhide comment fail
2189   def test_unhide_comment_fail
2190     # unauthorized
2191     comment = changeset_comments(:hidden_comment)
2192     assert_equal false, comment.visible
2193
2194     post :unhide_comment, :id => comment.id
2195     assert_response :unauthorized
2196     assert_equal false, comment.reload.visible
2197
2198     basic_authorization(users(:public_user).email, "test")
2199
2200     # not a moderator
2201     post :unhide_comment, :id => comment.id
2202     assert_response :forbidden
2203     assert_equal false, comment.reload.visible
2204
2205     basic_authorization(users(:moderator_user).email, "test")
2206
2207     # bad comment id
2208     post :unhide_comment, :id => 999111
2209     assert_response :not_found
2210     assert_equal false, comment.reload.visible
2211   end
2212
2213   ##
2214   # test unhide comment succes
2215   def test_unhide_comment_success
2216     comment = changeset_comments(:hidden_comment)
2217     assert_equal false, comment.visible
2218
2219     basic_authorization(users(:moderator_user).email, "test")
2220
2221     post :unhide_comment, :id => comment.id
2222     assert_response :success
2223     assert_equal true, comment.reload.visible
2224   end
2225
2226   ##
2227   # test comments feed
2228   def test_comments_feed
2229     get :comments_feed, :format => "rss"
2230     assert_response :success
2231     assert_equal "application/rss+xml", @response.content_type
2232     assert_select "rss", :count => 1 do
2233       assert_select "channel", :count => 1 do
2234         assert_select "item", :count => 3
2235       end
2236     end
2237
2238     get :comments_feed, :format => "rss", :limit => 2
2239     assert_response :success
2240     assert_equal "application/rss+xml", @response.content_type
2241     assert_select "rss", :count => 1 do
2242       assert_select "channel", :count => 1 do
2243         assert_select "item", :count => 2
2244       end
2245     end
2246
2247     get :comments_feed, :id => changesets(:normal_user_closed_change), :format => "rss"
2248     assert_response :success
2249     assert_equal "application/rss+xml", @response.content_type
2250     assert_select "rss", :count => 1 do
2251       assert_select "channel", :count => 1 do
2252         assert_select "item", :count => 3
2253       end
2254     end
2255   end
2256
2257   ##
2258   # test comments feed
2259   def test_comments_feed_bad_limit
2260     get :comments_feed, :format => "rss", :limit => 0
2261     assert_response :bad_request
2262
2263     get :comments_feed, :format => "rss", :limit => 100001
2264     assert_response :bad_request
2265   end
2266
2267   private
2268
2269   ##
2270   # boilerplate for checking that certain changesets exist in the
2271   # output.
2272   def assert_changesets(ids)
2273     assert_select "osm>changeset", ids.size
2274     ids.each do |id|
2275       assert_select "osm>changeset[id='#{id}']", 1
2276     end
2277   end
2278
2279   ##
2280   # call the include method and assert properties of the bbox
2281   def check_after_include(changeset_id, lon, lat, bbox)
2282     content "<osm><node lon='#{lon}' lat='#{lat}'/></osm>"
2283     post :expand_bbox, :id => changeset_id
2284     assert_response :success, "Setting include of changeset failed: #{@response.body}"
2285
2286     # check exactly one changeset
2287     assert_select "osm>changeset", 1
2288     assert_select "osm>changeset[id='#{changeset_id}']", 1
2289
2290     # check the bbox
2291     doc = XML::Parser.string(@response.body).parse
2292     changeset = doc.find("//osm/changeset").first
2293     assert_equal bbox[0], changeset["min_lon"].to_f, "min lon"
2294     assert_equal bbox[1], changeset["min_lat"].to_f, "min lat"
2295     assert_equal bbox[2], changeset["max_lon"].to_f, "max lon"
2296     assert_equal bbox[3], changeset["max_lat"].to_f, "max lat"
2297   end
2298
2299   ##
2300   # update the changeset_id of a way element
2301   def update_changeset(xml, changeset_id)
2302     xml_attr_rewrite(xml, "changeset", changeset_id)
2303   end
2304
2305   ##
2306   # update an attribute in a way element
2307   def xml_attr_rewrite(xml, name, value)
2308     xml.find("//osm/way").first[name] = value.to_s
2309     xml
2310   end
2311
2312   ##
2313   # check the result of a list
2314   def check_list_result(changesets)
2315     changesets = changesets.where("num_changes > 0")
2316                            .order(:created_at => :desc)
2317                            .limit(20)
2318     assert changesets.size <= 20
2319
2320     assert_select "ol.changesets", :count => [changesets.size, 1].min do
2321       assert_select "li", :count => changesets.size
2322
2323       changesets.each do |changeset|
2324         assert_select "li#changeset_#{changeset.id}", :count => 1
2325       end
2326     end
2327   end
2328
2329   ##
2330   # check the result of a feed
2331   def check_feed_result(changesets)
2332     changesets = changesets.where("num_changes > 0")
2333                            .order(:created_at => :desc)
2334                            .limit(20)
2335     assert changesets.size <= 20
2336
2337     assert_select "feed", :count => [changesets.size, 1].min do
2338       assert_select "> title", :count => 1, :text => /^Changesets/
2339       assert_select "> entry", :count => changesets.size
2340
2341       changesets.each do |changeset|
2342         assert_select "> entry > id", changeset_url(:id => changeset.id)
2343       end
2344     end
2345   end
2346 end