1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'changeset_controller'
4 # Re-raise errors caught by the controller.
5 class ChangesetController; def rescue_action(e) raise e end; end
7 class ChangesetControllerTest < Test::Unit::TestCase
13 @controller = ChangesetController.new
14 @request = ActionController::TestRequest.new
15 @response = ActionController::TestResponse.new
18 def basic_authorization(user, pass)
19 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
23 @request.env["RAW_POST_DATA"] = c.to_s
26 # -----------------------
27 # Test simple changeset creation
28 # -----------------------
31 basic_authorization "test@openstreetmap.org", "test"
33 # Create the first user's changeset
34 content "<osm><changeset>" +
35 "<tag k='created_by' v='osm test suite checking changesets'/>" +
39 assert_response :success, "Creation of changeset did not return sucess status"
40 newid = @response.body
43 def test_create_invalid
44 basic_authorization "test@openstreetmap.org", "test"
45 content "<osm><changeset></osm>"
47 assert_response :bad_request, "creating a invalid changeset should fail"
59 # upload something simple, but valid and check that it can
61 def test_upload_simple_valid
62 basic_authorization "test@openstreetmap.org", "test"
64 # simple diff to change a node, way and relation by removing
69 <node id='1' lon='0' lat='0' changeset='1' version='1'/>
70 <way id='1' changeset='1' version='1'>
75 <relation id='1' changeset='1' version='1'>
76 <member type='way' role='some' ref='3'/>
77 <member type='node' role='some' ref='5'/>
78 <member type='relation' role='some' ref='3'/>
86 post :upload, :id => 1
87 assert_response :success,
88 "can't upload a simple valid diff to changeset: #{@response.body}"
90 # check that the changes made it into the database
91 assert_equal 0, Node.find(1).tags.size, "node 1 should now have no tags"
92 assert_equal 0, Way.find(1).tags.size, "way 1 should now have no tags"
93 assert_equal 0, Relation.find(1).tags.size, "relation 1 should now have no tags"
97 # upload something which creates new objects using placeholders
98 def test_upload_create_valid
99 basic_authorization "test@openstreetmap.org", "test"
101 # simple diff to create a node way and relation using placeholders
105 <node id='-1' lon='0' lat='0' changeset='1'>
106 <tag k='foo' v='bar'/>
107 <tag k='baz' v='bat'/>
109 <way id='-1' changeset='1'>
114 <relation id='-1' changeset='1'>
115 <member type='way' role='some' ref='3'/>
116 <member type='node' role='some' ref='5'/>
117 <member type='relation' role='some' ref='3'/>
125 post :upload, :id => 1
126 assert_response :success,
127 "can't upload a simple valid creation to changeset: #{@response.body}"
129 # check the returned payload
130 assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
131 assert_select "osm>node", 1
132 assert_select "osm>way", 1
133 assert_select "osm>relation", 1
135 # inspect the response to find out what the new element IDs are
136 doc = XML::Parser.string(@response.body).parse
137 new_node_id = doc.find("//osm/node").first["new_id"].to_i
138 new_way_id = doc.find("//osm/way").first["new_id"].to_i
139 new_rel_id = doc.find("//osm/relation").first["new_id"].to_i
141 # check the old IDs are all present and negative one
142 assert_equal -1, doc.find("//osm/node").first["old_id"].to_i
143 assert_equal -1, doc.find("//osm/way").first["old_id"].to_i
144 assert_equal -1, doc.find("//osm/relation").first["old_id"].to_i
146 # check the versions are present and equal one
147 assert_equal 1, doc.find("//osm/node").first["new_version"].to_i
148 assert_equal 1, doc.find("//osm/way").first["new_version"].to_i
149 assert_equal 1, doc.find("//osm/relation").first["new_version"].to_i
151 # check that the changes made it into the database
152 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
153 assert_equal 0, Way.find(new_way_id).tags.size, "new way should have no tags"
154 assert_equal 0, Relation.find(new_rel_id).tags.size, "new relation should have no tags"
158 # test a complex delete where we delete elements which rely on eachother
159 # in the same transaction.
160 def test_upload_delete
161 basic_authorization "test@openstreetmap.org", "test"
163 diff = XML::Document.new
164 diff.root = XML::Node.new "osmChange"
165 delete = XML::Node.new "delete"
167 delete << current_relations(:visible_relation).to_xml_node
168 delete << current_relations(:used_relation).to_xml_node
169 delete << current_ways(:used_way).to_xml_node
170 delete << current_nodes(:node_used_by_relationship).to_xml_node
174 post :upload, :id => 1
175 assert_response :success,
176 "can't upload a deletion diff to changeset: #{@response.body}"
178 # check that everything was deleted
179 assert_equal false, Node.find(current_nodes(:node_used_by_relationship).id).visible
180 assert_equal false, Way.find(current_ways(:used_way).id).visible
181 assert_equal false, Relation.find(current_relations(:visible_relation).id).visible
182 assert_equal false, Relation.find(current_relations(:used_relation).id).visible
186 # test that deleting stuff in a transaction doesn't bypass the checks
187 # to ensure that used elements are not deleted.
188 def test_upload_delete_invalid
189 basic_authorization "test@openstreetmap.org", "test"
191 diff = XML::Document.new
192 diff.root = XML::Node.new "osmChange"
193 delete = XML::Node.new "delete"
195 delete << current_relations(:visible_relation).to_xml_node
196 delete << current_ways(:used_way).to_xml_node
197 delete << current_nodes(:node_used_by_relationship).to_xml_node
201 post :upload, :id => 1
202 assert_response :precondition_failed,
203 "shouldn't be able to upload a invalid deletion diff: #{@response.body}"
205 # check that nothing was, in fact, deleted
206 assert_equal true, Node.find(current_nodes(:node_used_by_relationship).id).visible
207 assert_equal true, Way.find(current_ways(:used_way).id).visible
208 assert_equal true, Relation.find(current_relations(:visible_relation).id).visible
212 # upload something which creates new objects and inserts them into
213 # existing containers using placeholders.
214 def test_upload_complex
215 basic_authorization "test@openstreetmap.org", "test"
217 # simple diff to create a node way and relation using placeholders
221 <node id='-1' lon='0' lat='0' changeset='1'>
222 <tag k='foo' v='bar'/>
223 <tag k='baz' v='bat'/>
227 <way id='1' changeset='1' version='1'>
231 <relation id='1' changeset='1' version='1'>
232 <member type='way' role='some' ref='3'/>
233 <member type='node' role='some' ref='-1'/>
234 <member type='relation' role='some' ref='3'/>
242 post :upload, :id => 1
243 assert_response :success,
244 "can't upload a complex diff to changeset: #{@response.body}"
246 # check the returned payload
247 assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
248 assert_select "osm>node", 1
249 assert_select "osm>way", 1
250 assert_select "osm>relation", 1
252 # inspect the response to find out what the new element IDs are
253 doc = XML::Parser.string(@response.body).parse
254 new_node_id = doc.find("//osm/node").first["new_id"].to_i
256 # check that the changes made it into the database
257 assert_equal 2, Node.find(new_node_id).tags.size, "new node should have two tags"
258 assert_equal [new_node_id, 3], Way.find(1).nds, "way nodes should match"
259 Relation.find(1).members.each do |type,id,role|
261 assert_equal new_node_id, id, "relation should contain new node"
267 # create a diff which references several changesets, which should cause
268 # a rollback and none of the diff gets committed
269 def test_upload_invalid_changesets
270 basic_authorization "test@openstreetmap.org", "test"
272 # simple diff to create a node way and relation using placeholders
276 <node id='1' lon='0' lat='0' changeset='1' version='1'/>
277 <way id='1' changeset='1' version='1'>
282 <relation id='1' changeset='1' version='1'>
283 <member type='way' role='some' ref='3'/>
284 <member type='node' role='some' ref='5'/>
285 <member type='relation' role='some' ref='3'/>
289 <node id='-1' changeset='4'>
290 <tag k='foo' v='bar'/>
291 <tag k='baz' v='bat'/>
296 # cache the objects before uploading them
297 node = current_nodes(:visible_node)
298 way = current_ways(:visible_way)
299 rel = current_relations(:visible_relation)
303 post :upload, :id => 1
304 assert_response :conflict,
305 "uploading a diff with multiple changsets should have failed"
307 # check that objects are unmodified
308 assert_nodes_are_equal(node, Node.find(1))
309 assert_ways_are_equal(way, Way.find(1))
313 # upload multiple versions of the same element in the same diff.
314 def test_upload_multiple_valid
315 basic_authorization "test@openstreetmap.org", "test"
317 # change the location of a node multiple times, each time referencing
318 # the last version. doesn't this depend on version numbers being
323 <node id='1' lon='0' lat='0' changeset='1' version='1'/>
324 <node id='1' lon='1' lat='0' changeset='1' version='2'/>
325 <node id='1' lon='1' lat='1' changeset='1' version='3'/>
326 <node id='1' lon='1' lat='2' changeset='1' version='4'/>
327 <node id='1' lon='2' lat='2' changeset='1' version='5'/>
328 <node id='1' lon='3' lat='2' changeset='1' version='6'/>
329 <node id='1' lon='3' lat='3' changeset='1' version='7'/>
330 <node id='1' lon='9' lat='9' changeset='1' version='8'/>
337 post :upload, :id => 1
338 assert_response :success,
339 "can't upload multiple versions of an element in a diff: #{@response.body}"
343 # upload multiple versions of the same element in the same diff, but
344 # keep the version numbers the same.
345 def test_upload_multiple_duplicate
346 basic_authorization "test@openstreetmap.org", "test"
351 <node id='1' lon='0' lat='0' changeset='1' version='1'/>
352 <node id='1' lon='1' lat='1' changeset='1' version='1'/>
359 post :upload, :id => 1
360 assert_response :conflict,
361 "shouldn't be able to upload the same element twice in a diff: #{@response.body}"
365 # try to upload some elements without specifying the version
366 def test_upload_missing_version
367 basic_authorization "test@openstreetmap.org", "test"
372 <node id='1' lon='1' lat='1' changeset='1'/>
379 post :upload, :id => 1
380 assert_response :bad_request,
381 "shouldn't be able to upload an element without version: #{@response.body}"