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'"
168 # create a way with no nodes
169 content "<osm><way changeset='#{open_changeset_id}'>" +
170 "<tag k='test' v='yes' /></way></osm>"
173 assert_response :precondition_failed,
174 "way upload with no node did not return 'precondition failed'"
176 # create a way inside a closed changeset
177 content "<osm><way changeset='#{closed_changeset_id}'>" +
178 "<nd ref='#{nid1}'/></way></osm>"
181 assert_response :conflict,
182 "way upload to closed changeset did not return 'conflict'"
185 # -------------------------------------
186 # Test deleting ways.
187 # -------------------------------------
190 # first try to delete way without auth
191 delete :delete, :id => current_ways(:visible_way).id
192 assert_response :unauthorized
194 # now set auth using the private user
195 basic_authorization(users(:normal_user).email, "test");
197 # this shouldn't work as with the 0.6 api we need pay load to delete
198 delete :delete, :id => current_ways(:visible_way).id
199 assert_response :forbidden
201 # Now try without having a changeset
202 content "<osm><way id='#{current_ways(:visible_way).id}'></osm>"
203 delete :delete, :id => current_ways(:visible_way).id
204 assert_response :forbidden
206 # try to delete with an invalid (closed) changeset
207 content update_changeset(current_ways(:visible_way).to_xml,
208 changesets(:normal_user_closed_change).id)
209 delete :delete, :id => current_ways(:visible_way).id
210 assert_response :forbidden
212 # try to delete with an invalid (non-existent) changeset
213 content update_changeset(current_ways(:visible_way).to_xml,0)
214 delete :delete, :id => current_ways(:visible_way).id
215 assert_response :forbidden
217 # Now try with a valid changeset
218 content current_ways(:visible_way).to_xml
219 delete :delete, :id => current_ways(:visible_way).id
220 assert_response :forbidden
222 # check the returned value - should be the new version number
223 # valid delete should return the new version number, which should
224 # be greater than the old version number
225 #assert @response.body.to_i > current_ways(:visible_way).version,
226 # "delete request should return a new version number for way"
228 # this won't work since the way is already deleted
229 content current_ways(:invisible_way).to_xml
230 delete :delete, :id => current_ways(:invisible_way).id
231 assert_response :forbidden
233 # this shouldn't work as the way is used in a relation
234 content current_ways(:used_way).to_xml
235 delete :delete, :id => current_ways(:used_way).id
236 assert_response :forbidden,
237 "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
239 # this won't work since the way never existed
240 delete :delete, :id => 0
241 assert_response :forbidden
244 ### Now check with a public user
246 basic_authorization(users(:public_user).email, "test");
248 # this shouldn't work as with the 0.6 api we need pay load to delete
249 delete :delete, :id => current_ways(:visible_way).id
250 assert_response :bad_request
252 # Now try without having a changeset
253 content "<osm><way id='#{current_ways(:visible_way).id}'></osm>"
254 delete :delete, :id => current_ways(:visible_way).id
255 assert_response :bad_request
257 # try to delete with an invalid (closed) changeset
258 content update_changeset(current_ways(:visible_way).to_xml,
259 changesets(:public_user_closed_change).id)
260 delete :delete, :id => current_ways(:visible_way).id
261 assert_response :conflict
263 # try to delete with an invalid (non-existent) changeset
264 content update_changeset(current_ways(:visible_way).to_xml,0)
265 delete :delete, :id => current_ways(:visible_way).id
266 assert_response :conflict
268 # Now try with a valid changeset
269 content current_ways(:visible_way).to_xml
270 delete :delete, :id => current_ways(:visible_way).id
271 assert_response :success
273 # check the returned value - should be the new version number
274 # valid delete should return the new version number, which should
275 # be greater than the old version number
276 assert @response.body.to_i > current_ways(:visible_way).version,
277 "delete request should return a new version number for way"
279 # this won't work since the way is already deleted
280 content current_ways(:invisible_way).to_xml
281 delete :delete, :id => current_ways(:invisible_way).id
282 assert_response :gone
284 # this shouldn't work as the way is used in a relation
285 content current_ways(:used_way).to_xml
286 delete :delete, :id => current_ways(:used_way).id
287 assert_response :precondition_failed,
288 "shouldn't be able to delete a way used in a relation (#{@response.body})"
290 # this won't work since the way never existed
291 delete :delete, :id => 0
292 assert_response :not_found
295 # ------------------------------------------------------------
297 # ------------------------------------------------------------
300 # Try adding a duplicate of an existing tag to a way
301 def test_add_duplicate_tags
302 ## Try with the non-public user
304 basic_authorization(users(:normal_user).email, "test")
306 # add an identical tag to the way
307 tag_xml = XML::Node.new("tag")
308 tag_xml['k'] = current_way_tags(:t1).k
309 tag_xml['v'] = current_way_tags(:t1).v
311 # add the tag into the existing xml
312 way_xml = current_ways(:visible_way).to_xml
313 way_xml.find("//osm/way").first << tag_xml
317 put :update, :id => current_ways(:visible_way).id
318 assert_response :forbidden,
319 "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
321 ## Now try with the public user
323 basic_authorization(users(:public_user).email, "test")
325 # add an identical tag to the way
326 tag_xml = XML::Node.new("tag")
327 tag_xml['k'] = current_way_tags(:t1).k
328 tag_xml['v'] = current_way_tags(:t1).v
330 # add the tag into the existing xml
331 way_xml = current_ways(:visible_way).to_xml
332 way_xml.find("//osm/way").first << tag_xml
336 put :update, :id => current_ways(:visible_way).id
337 assert_response :bad_request,
338 "adding a duplicate tag to a way should fail with 'bad request'"
339 assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key #{current_way_tags(:t1).k}.", @response.body
343 # Try adding a new duplicate tags to a way
344 def test_new_duplicate_tags
345 ## First test with the non-public user so should be rejected
347 basic_authorization(users(:normal_user).email, "test")
349 # create duplicate tag
350 tag_xml = XML::Node.new("tag")
351 tag_xml['k'] = "i_am_a_duplicate"
352 tag_xml['v'] = "foobar"
354 # add the tag into the existing xml
355 way_xml = current_ways(:visible_way).to_xml
357 # add two copies of the tag
358 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
362 put :update, :id => current_ways(:visible_way).id
363 assert_response :forbidden,
364 "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
366 ## Now test with the public user
368 basic_authorization(users(:public_user).email, "test")
370 # create duplicate tag
371 tag_xml = XML::Node.new("tag")
372 tag_xml['k'] = "i_am_a_duplicate"
373 tag_xml['v'] = "foobar"
375 # add the tag into the existing xml
376 way_xml = current_ways(:visible_way).to_xml
378 # add two copies of the tag
379 way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
383 put :update, :id => current_ways(:visible_way).id
384 assert_response :bad_request,
385 "adding new duplicate tags to a way should fail with 'bad request'"
386 assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key i_am_a_duplicate.", @response.body
391 # Try adding a new duplicate tags to a way.
392 # But be a bit subtle - use unicode decoding ambiguities to use different
393 # binary strings which have the same decoding.
394 def test_invalid_duplicate_tags
395 ## First make sure that you can't with a non-public user
397 basic_authorization(users(:normal_user).email, "test")
399 # add the tag into the existing xml
400 way_str = "<osm><way changeset='1'>"
401 way_str << "<tag k='addr:housenumber' v='1'/>"
402 way_str << "<tag k='addr:housenumber' v='2'/>"
403 way_str << "</way></osm>";
408 assert_response :forbidden,
409 "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
411 ## Now do it with a public user
413 basic_authorization(users(:public_user).email, "test")
415 # add the tag into the existing xml
416 way_str = "<osm><way changeset='1'>"
417 way_str << "<tag k='addr:housenumber' v='1'/>"
418 way_str << "<tag k='addr:housenumber' v='2'/>"
419 way_str << "</way></osm>";
424 assert_response :bad_request,
425 "adding new duplicate tags to a way should fail with 'bad request'"
426 assert_equal "Element way/ has duplicate tags with key addr:housenumber.", @response.body
430 # test that a call to ways_for_node returns all ways that contain the node
431 # and none that don't.
432 def test_ways_for_node
433 # in current fixtures ways 1 and 3 all use node 3. ways 2 and 4
434 # *used* to use it but doesn't.
435 get :ways_for_node, :id => current_nodes(:used_node_1).id
436 assert_response :success
437 ways_xml = XML::Parser.string(@response.body).parse
438 assert_not_nil ways_xml, "failed to parse ways_for_node response"
440 # check that the set of IDs match expectations
441 expected_way_ids = [ current_ways(:visible_way).id,
442 current_ways(:used_way).id
444 found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
445 assert_equal expected_way_ids, found_way_ids,
446 "expected ways for node #{current_nodes(:used_node_1).id} did not match found"
448 # check the full ways to ensure we're not missing anything
449 expected_way_ids.each do |id|
450 way_xml = ways_xml.find("//osm/way[@id=#{id}]").first
451 assert_ways_are_equal(Way.find(id),
452 Way.from_xml_node(way_xml))
457 # update the changeset_id of a node element
458 def update_changeset(xml, changeset_id)
459 xml_attr_rewrite(xml, 'changeset', changeset_id)
463 # update an attribute in the node element
464 def xml_attr_rewrite(xml, name, value)
465 xml.find("//osm/way").first[name] = value.to_s