]> git.openstreetmap.org Git - rails.git/blob - test/functional/way_controller_test.rb
One final (I've checked the rest now!) mis-referenced id, plus make the error msg...
[rails.git] / test / functional / way_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'way_controller'
3
4 class WayControllerTest < ActionController::TestCase
5   api_fixtures
6
7   # -------------------------------------
8   # Test reading ways.
9   # -------------------------------------
10
11   def test_read
12     # check that a visible way is returned properly
13     get :read, :id => current_ways(:visible_way).id
14     assert_response :success
15
16     # check that an invisible way is not returned
17     get :read, :id => current_ways(:invisible_way).id
18     assert_response :gone
19
20     # check chat a non-existent way is not returned
21     get :read, :id => 0
22     assert_response :not_found
23   end
24
25   ##
26   # check the "full" mode
27   def test_full
28     Way.find(:all).each do |way|
29       get :full, :id => way.id
30
31       # full call should say "gone" for non-visible ways...
32       unless way.visible
33         assert_response :gone
34         next
35       end
36
37       # otherwise it should say success
38       assert_response :success
39       
40       # Check the way is correctly returned
41       assert_select "osm way[id=#{way.id}][version=#{way.version}][visible=#{way.visible}]", 1
42       
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.
47       way.nodes.each do |n|
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
50       end
51     end
52   end
53
54   # -------------------------------------
55   # Test simple way creation.
56   # -------------------------------------
57
58   def test_create
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"
63
64     # use the first user's open changeset
65     changeset_id = changesets(:normal_user_first_change).id
66     
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>"
71     put :create
72     # hope for success
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
77
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"
82
83     # use the first user's open changeset
84     changeset_id = changesets(:public_user_first_change).id
85     
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>"
90     put :create
91     # hope for success
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"
99     # compare values
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"
112   end
113
114   # -------------------------------------
115   # Test creating some invalid ways.
116   # -------------------------------------
117
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"
121
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
126
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>"
130     put :create
131     # expect failure
132     assert_response :forbidden, 
133     "way upload with invalid node using a private user did not return 'forbidden'"
134
135     # create a way with no nodes
136     content "<osm><way changeset='#{open_changeset_id}'>" +
137       "<tag k='test' v='yes' /></way></osm>"
138     put :create
139     # expect failure
140     assert_response :forbidden, 
141     "way upload with no node using a private userdid not return 'forbidden'"
142
143     # create a way inside a closed changeset
144     content "<osm><way changeset='#{closed_changeset_id}'>" +
145       "<nd ref='#{nid1}'/></way></osm>"
146     put :create
147     # expect failure
148     assert_response :forbidden, 
149     "way upload to closed changeset with a private user did not return 'forbidden'"    
150
151     
152     ## Now test with a public user
153     basic_authorization users(:public_user).email, "test"
154
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
159
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>"
163     put :create
164     # expect failure
165     assert_response :precondition_failed, 
166         "way upload with invalid node did not return 'precondition failed'"
167
168     # create a way with no nodes
169     content "<osm><way changeset='#{open_changeset_id}'>" +
170       "<tag k='test' v='yes' /></way></osm>"
171     put :create
172     # expect failure
173     assert_response :precondition_failed, 
174         "way upload with no node did not return 'precondition failed'"
175
176     # create a way inside a closed changeset
177     content "<osm><way changeset='#{closed_changeset_id}'>" +
178       "<nd ref='#{nid1}'/></way></osm>"
179     put :create
180     # expect failure
181     assert_response :conflict, 
182         "way upload to closed changeset did not return 'conflict'"    
183   end
184
185   # -------------------------------------
186   # Test deleting ways.
187   # -------------------------------------
188   
189   def test_delete
190     # first try to delete way without auth
191     delete :delete, :id => current_ways(:visible_way).id
192     assert_response :unauthorized
193
194     # now set auth using the private user
195     basic_authorization(users(:normal_user).email, "test");  
196
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
200     
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
205     
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
211
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
216
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
221
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"
227
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
232
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"
238
239     # this won't work since the way never existed
240     delete :delete, :id => 0
241     assert_response :forbidden
242
243     
244     ### Now check with a public user
245     # now set auth
246     basic_authorization(users(:public_user).email, "test");  
247
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
251     
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
256     
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
262
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
267
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
272
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"
278
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
283
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})"
289
290     # this won't work since the way never existed
291     delete :delete, :id => 0
292     assert_response :not_found
293   end
294
295   # ------------------------------------------------------------
296   # test tags handling
297   # ------------------------------------------------------------
298
299   ##
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
303     # setup auth
304     basic_authorization(users(:normal_user).email, "test")
305
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
310
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
314
315     # try and upload it
316     content way_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'"
320
321     ## Now try with the public user
322     # setup auth
323     basic_authorization(users(:public_user).email, "test")
324
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
329
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
333
334     # try and upload it
335     content way_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
340   end
341
342   ##
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
346     # setup auth
347     basic_authorization(users(:normal_user).email, "test")
348
349     # create duplicate tag
350     tag_xml = XML::Node.new("tag")
351     tag_xml['k'] = "i_am_a_duplicate"
352     tag_xml['v'] = "foobar"
353
354     # add the tag into the existing xml
355     way_xml = current_ways(:visible_way).to_xml
356
357     # add two copies of the tag
358     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
359
360     # try and upload it
361     content way_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'"
365     
366     ## Now test with the public user
367     # setup auth
368     basic_authorization(users(:public_user).email, "test")
369
370     # create duplicate tag
371     tag_xml = XML::Node.new("tag")
372     tag_xml['k'] = "i_am_a_duplicate"
373     tag_xml['v'] = "foobar"
374
375     # add the tag into the existing xml
376     way_xml = current_ways(:visible_way).to_xml
377
378     # add two copies of the tag
379     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
380
381     # try and upload it
382     content way_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
387     
388   end
389
390   ##
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
396     # setup auth
397     basic_authorization(users(:normal_user).email, "test")
398
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>";
404
405     # try and upload it
406     content way_str
407     put :create
408     assert_response :forbidden, 
409     "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
410     
411     ## Now do it with a public user
412     # setup auth
413     basic_authorization(users(:public_user).email, "test")
414
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>";
420
421     # try and upload it
422     content way_str
423     put :create
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
427   end
428
429   ##
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"
439
440     # check that the set of IDs match expectations
441     expected_way_ids = [ current_ways(:visible_way).id,
442                          current_ways(:used_way).id
443                        ]
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"
447     
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))
453     end
454   end
455
456   ##
457   # update the changeset_id of a node element
458   def update_changeset(xml, changeset_id)
459     xml_attr_rewrite(xml, 'changeset', changeset_id)
460   end
461
462   ##
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
466     return xml
467   end
468 end