1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'way_controller'
4 class WayControllerTest < ActionController::TestCase
7 # -------------------------------------
9 # -------------------------------------
12 # check that a visible way is returned properly
13 get :read, :id => current_ways(:visible_way).id
14 assert_response :success
16 # check that an invisible way is not returned
17 get :read, :id => current_ways(:invisible_way).id
20 # check chat a non-existent way is not returned
22 assert_response :not_found
26 # check the "full" mode
28 Way.find(:all).each do |way|
29 get :full, :id => way.id
31 # full call should say "gone" for non-visible ways...
37 # otherwise it should say success
38 assert_response :success
40 # Check the way is correctly returned
41 assert_select "osm way[id=#{way.id}][version=#{way.version}][visible=#{way.visible}]", 1
43 # check that each node in the way appears once in the output as a
44 # reference and as the node element. note the slightly dodgy assumption
45 # that nodes appear only once. this is currently the case with the
46 # fixtures, but it doesn't have to be.
48 assert_select "osm way nd[ref=#{n.id}]", 1
49 assert_select "osm node[id=#{n.id}][version=#{n.version}][lat=#{n.lat}][lon=#{n.lon}]", 1
54 # -------------------------------------
55 # Test simple way creation.
56 # -------------------------------------
59 ## First check that it fails when creating a way using a non-public user
60 nid1 = current_nodes(:used_node_1).id
61 nid2 = current_nodes(:used_node_2).id
62 basic_authorization users(:normal_user).email, "test"
64 # use the first user's open changeset
65 changeset_id = changesets(:normal_user_first_change).id
67 # create a way with pre-existing nodes
68 content "<osm><way changeset='#{changeset_id}'>" +
69 "<nd ref='#{nid1}'/><nd ref='#{nid2}'/>" +
70 "<tag k='test' v='yes' /></way></osm>"
73 assert_response :forbidden,
74 "way upload did not return success status"
75 # read id of created way and search for it
76 wayid = @response.body
78 ## Now use a public user
79 nid1 = current_nodes(:used_node_1).id
80 nid2 = current_nodes(:used_node_2).id
81 basic_authorization users(:public_user).email, "test"
83 # use the first user's open changeset
84 changeset_id = changesets(:public_user_first_change).id
86 # create a way with pre-existing nodes
87 content "<osm><way changeset='#{changeset_id}'>" +
88 "<nd ref='#{nid1}'/><nd ref='#{nid2}'/>" +
89 "<tag k='test' v='yes' /></way></osm>"
92 assert_response :success,
93 "way upload did not return success status"
94 # read id of created way and search for it
95 wayid = @response.body
96 checkway = Way.find(wayid)
97 assert_not_nil checkway,
98 "uploaded way not found in data base after upload"
100 assert_equal checkway.nds.length, 2,
101 "saved way does not contain exactly one node"
102 assert_equal checkway.nds[0], nid1,
103 "saved way does not contain the right node on pos 0"
104 assert_equal checkway.nds[1], nid2,
105 "saved way does not contain the right node on pos 1"
106 assert_equal checkway.changeset_id, changeset_id,
107 "saved way does not belong to the correct changeset"
108 assert_equal users(:public_user).id, checkway.changeset.user_id,
109 "saved way does not belong to user that created it"
110 assert_equal true, checkway.visible,
111 "saved way is not visible"
114 # -------------------------------------
115 # Test creating some invalid ways.
116 # -------------------------------------
118 def test_create_invalid
119 ## First test with a private user to make sure that they are not authorized
120 basic_authorization users(:normal_user).email, "test"
122 # use the first user's open changeset
123 open_changeset_id = changesets(:normal_user_first_change).id
124 closed_changeset_id = changesets(:normal_user_closed_change).id
125 nid1 = current_nodes(:used_node_1).id
127 # create a way with non-existing node
128 content "<osm><way changeset='#{open_changeset_id}'>" +
129 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
132 assert_response :forbidden,
133 "way upload with invalid node using a private user did not return 'forbidden'"
135 # create a way with no nodes
136 content "<osm><way changeset='#{open_changeset_id}'>" +
137 "<tag k='test' v='yes' /></way></osm>"
140 assert_response :forbidden,
141 "way upload with no node using a private userdid not return 'forbidden'"
143 # create a way inside a closed changeset
144 content "<osm><way changeset='#{closed_changeset_id}'>" +
145 "<nd ref='#{nid1}'/></way></osm>"
148 assert_response :forbidden,
149 "way upload to closed changeset with a private user did not return 'forbidden'"
152 ## Now test with a public user
153 basic_authorization users(:public_user).email, "test"
155 # use the first user's open changeset
156 open_changeset_id = changesets(:public_user_first_change).id
157 closed_changeset_id = changesets(:public_user_closed_change).id
158 nid1 = current_nodes(:used_node_1).id
160 # create a way with non-existing node
161 content "<osm><way changeset='#{open_changeset_id}'>" +
162 "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
165 assert_response :precondition_failed,
166 "way upload with invalid node did not return 'precondition failed'"
167 assert_equal "Precondition failed: Way requires the node with id 0, which either does not exist, or is not visible.", @response.body
169 # create a way with no nodes
170 content "<osm><way changeset='#{open_changeset_id}'>" +
171 "<tag k='test' v='yes' /></way></osm>"
174 assert_response :precondition_failed,
175 "way upload with no node did not return 'precondition failed'"
176 assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
178 # create a way inside a closed changeset
179 content "<osm><way changeset='#{closed_changeset_id}'>" +
180 "<nd ref='#{nid1}'/></way></osm>"
183 assert_response :conflict,
184 "way upload to closed changeset did not return 'conflict'"
187 # -------------------------------------
188 # Test deleting ways.
189 # -------------------------------------
192 # first try to delete way without auth
193 delete :delete, :id => current_ways(:visible_way).id
194 assert_response :unauthorized
196 # now set auth using the private user
197 basic_authorization(users(:normal_user).email, "test");
199 # this shouldn't work as with the 0.6 api we need pay load to delete
200 delete :delete, :id => current_ways(:visible_way).id
201 assert_response :forbidden
203 # Now try without having a changeset
204 content "<osm><way id='#{current_ways(:visible_way).id}'></osm>"
205 delete :delete, :id => current_ways(:visible_way).id
206 assert_response :forbidden
208 # try to delete with an invalid (closed) changeset
209 content update_changeset(current_ways(:visible_way).to_xml,
210 changesets(:normal_user_closed_change).id)
211 delete :delete, :id => current_ways(:visible_way).id
212 assert_response :forbidden
214 # try to delete with an invalid (non-existent) changeset
215 content update_changeset(current_ways(:visible_way).to_xml,0)
216 delete :delete, :id => current_ways(:visible_way).id
217 assert_response :forbidden
219 # Now try with a valid changeset
220 content current_ways(:visible_way).to_xml
221 delete :delete, :id => current_ways(:visible_way).id
222 assert_response :forbidden
224 # check the returned value - should be the new version number
225 # valid delete should return the new version number, which should
226 # be greater than the old version number
227 #assert @response.body.to_i > current_ways(:visible_way).version,
228 # "delete request should return a new version number for way"
230 # this won't work since the way is already deleted
231 content current_ways(:invisible_way).to_xml
232 delete :delete, :id => current_ways(:invisible_way).id
233 assert_response :forbidden
235 # this shouldn't work as the way is used in a relation
236 content current_ways(:used_way).to_xml
237 delete :delete, :id => current_ways(:used_way).id
238 assert_response :forbidden,
239 "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
241 # this won't work since the way never existed
242 delete :delete, :id => 0
243 assert_response :forbidden
246 ### Now check with a public user
248 basic_authorization(users(:public_user).email, "test");
250 # this shouldn't work as with the 0.6 api we need pay load to delete
251 delete :delete, :id => current_ways(:visible_way).id
252 assert_response :bad_request
254 # Now try without having a changeset
255 content "<osm><way id='#{current_ways(:visible_way).id}'></osm>"
256 delete :delete, :id => current_ways(:visible_way).id
257 assert_response :bad_request
259 # try to delete with an invalid (closed) changeset
260 content update_changeset(current_ways(:visible_way).to_xml,
261 changesets(:public_user_closed_change).id)
262 delete :delete, :id => current_ways(:visible_way).id
263 assert_response :conflict
265 # try to delete with an invalid (non-existent) changeset
266 content update_changeset(current_ways(:visible_way).to_xml,0)
267 delete :delete, :id => current_ways(:visible_way).id
268 assert_response :conflict
270 # Now try with a valid changeset
271 content current_ways(:visible_way).to_xml
272 delete :delete, :id => current_ways(:visible_way).id
273 assert_response :success
275 # check the returned value - should be the new version number
276 # valid delete should return the new version number, which should
277 # be greater than the old version number
278 assert @response.body.to_i > current_ways(:visible_way).version,
279 "delete request should return a new version number for way"
281 # this won't work since the way is already deleted
282 content current_ways(:invisible_way).to_xml
283 delete :delete, :id => current_ways(:invisible_way).id
284 assert_response :gone
286 # this shouldn't work as the way is used in a relation
287 content current_ways(:used_way).to_xml
288 delete :delete, :id => current_ways(:used_way).id
289 assert_response :precondition_failed,
290 "shouldn't be able to delete a way used in a relation (#{@response.body})"
291 assert_equal "Precondition failed: Way 3 still used by relation 1.", @response.body
293 # this won't work since the way never existed
294 delete :delete, :id => 0
295 assert_response :not_found
298 # ------------------------------------------------------------
300 # ------------------------------------------------------------
303 # Try adding a duplicate of an existing tag to a way
304 def test_add_duplicate_tags
305 ## Try with the non-public user
307 basic_authorization(users(:normal_user).email, "test")
309 # add an identical tag to the way
310 tag_xml = XML::Node.new("tag")
311 tag_xml['k'] = current_way_tags(:t1).k
312 tag_xml['v'] = current_way_tags(:t1).v
314 # add the tag into the existing xml
315 way_xml = current_ways(:visible_way).to_xml
316 way_xml.find("//osm/way").first << tag_xml
320 put :update, :id => current_ways(:visible_way).id
321 assert_response :forbidden,
322 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
324 ## Now try with the public user
326 basic_authorization(users(:public_user).email, "test")
328 # add an identical tag to the way
329 tag_xml = XML::Node.new("tag")
330 tag_xml['k'] = current_way_tags(:t1).k
331 tag_xml['v'] = current_way_tags(:t1).v
333 # add the tag into the existing xml
334 way_xml = current_ways(:visible_way).to_xml
335 way_xml.find("//osm/way").first << tag_xml
339 put :update, :id => current_ways(:visible_way).id
340 assert_response :bad_request,
341 "adding a duplicate tag to a way should fail with 'bad request'"
342 assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key #{current_way_tags(:t1).k}.", @response.body
346 # Try adding a new duplicate tags to a way
347 def test_new_duplicate_tags
348 ## First test with the non-public user so should be rejected
350 basic_authorization(users(:normal_user).email, "test")
352 # create duplicate tag
353 tag_xml = XML::Node.new("tag")
354 tag_xml['k'] = "i_am_a_duplicate"
355 tag_xml['v'] = "foobar"
357 # add the tag into the existing xml
358 way_xml = current_ways(:visible_way).to_xml
360 # add two copies of the tag
361 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
365 put :update, :id => current_ways(:visible_way).id
366 assert_response :forbidden,
367 "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
369 ## Now test with the public user
371 basic_authorization(users(:public_user).email, "test")
373 # create duplicate tag
374 tag_xml = XML::Node.new("tag")
375 tag_xml['k'] = "i_am_a_duplicate"
376 tag_xml['v'] = "foobar"
378 # add the tag into the existing xml
379 way_xml = current_ways(:visible_way).to_xml
381 # add two copies of the tag
382 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
386 put :update, :id => current_ways(:visible_way).id
387 assert_response :bad_request,
388 "adding new duplicate tags to a way should fail with 'bad request'"
389 assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key i_am_a_duplicate.", @response.body
394 # Try adding a new duplicate tags to a way.
395 # But be a bit subtle - use unicode decoding ambiguities to use different
396 # binary strings which have the same decoding.
397 def test_invalid_duplicate_tags
398 ## First make sure that you can't with a non-public user
400 basic_authorization(users(:normal_user).email, "test")
402 # add the tag into the existing xml
403 way_str = "<osm><way changeset='1'>"
404 way_str << "<tag k='addr:housenumber' v='1'/>"
405 way_str << "<tag k='addr:housenumber' v='2'/>"
406 way_str << "</way></osm>";
411 assert_response :forbidden,
412 "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
414 ## Now do it with a public user
416 basic_authorization(users(:public_user).email, "test")
418 # add the tag into the existing xml
419 way_str = "<osm><way changeset='1'>"
420 way_str << "<tag k='addr:housenumber' v='1'/>"
421 way_str << "<tag k='addr:housenumber' v='2'/>"
422 way_str << "</way></osm>";
427 assert_response :bad_request,
428 "adding new duplicate tags to a way should fail with 'bad request'"
429 assert_equal "Element way/ has duplicate tags with key addr:housenumber.", @response.body
433 # test that a call to ways_for_node returns all ways that contain the node
434 # and none that don't.
435 def test_ways_for_node
436 # in current fixtures ways 1 and 3 all use node 3. ways 2 and 4
437 # *used* to use it but doesn't.
438 get :ways_for_node, :id => current_nodes(:used_node_1).id
439 assert_response :success
440 ways_xml = XML::Parser.string(@response.body).parse
441 assert_not_nil ways_xml, "failed to parse ways_for_node response"
443 # check that the set of IDs match expectations
444 expected_way_ids = [ current_ways(:visible_way).id,
445 current_ways(:used_way).id
447 found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
448 assert_equal expected_way_ids, found_way_ids,
449 "expected ways for node #{current_nodes(:used_node_1).id} did not match found"
451 # check the full ways to ensure we're not missing anything
452 expected_way_ids.each do |id|
453 way_xml = ways_xml.find("//osm/way[@id=#{id}]").first
454 assert_ways_are_equal(Way.find(id),
455 Way.from_xml_node(way_xml))
460 # update the changeset_id of a node element
461 def update_changeset(xml, changeset_id)
462 xml_attr_rewrite(xml, 'changeset', changeset_id)
466 # update an attribute in the node element
467 def xml_attr_rewrite(xml, name, value)
468 xml.find("//osm/way").first[name] = value.to_s