]> git.openstreetmap.org Git - rails.git/blob - test/functional/way_controller_test.rb
more changeset tests. No need for @ vars in the tests
[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     assert_equal "Precondition failed: Way  requires the node with id 0, which either does not exist, or is not visible.", @response.body
168
169     # create a way with no nodes
170     content "<osm><way changeset='#{open_changeset_id}'>" +
171       "<tag k='test' v='yes' /></way></osm>"
172     put :create
173     # expect failure
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
177
178     # create a way inside a closed changeset
179     content "<osm><way changeset='#{closed_changeset_id}'>" +
180       "<nd ref='#{nid1}'/></way></osm>"
181     put :create
182     # expect failure
183     assert_response :conflict, 
184         "way upload to closed changeset did not return 'conflict'"    
185   end
186
187   # -------------------------------------
188   # Test deleting ways.
189   # -------------------------------------
190   
191   def test_delete
192     # first try to delete way without auth
193     delete :delete, :id => current_ways(:visible_way).id
194     assert_response :unauthorized
195
196     # now set auth using the private user
197     basic_authorization(users(:normal_user).email, "test");  
198
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
202     
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
207     
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
213
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
218
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
223
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"
229
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
234
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"
240
241     # this won't work since the way never existed
242     delete :delete, :id => 0
243     assert_response :forbidden
244
245     
246     ### Now check with a public user
247     # now set auth
248     basic_authorization(users(:public_user).email, "test");  
249
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
253     
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
258     
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
264
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
269
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
274
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"
280
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
285
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
292
293     # this won't work since the way never existed
294     delete :delete, :id => 0
295     assert_response :not_found
296   end
297
298   # ------------------------------------------------------------
299   # test tags handling
300   # ------------------------------------------------------------
301
302   ##
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
306     # setup auth
307     basic_authorization(users(:normal_user).email, "test")
308
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
313
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
317
318     # try and upload it
319     content way_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'"
323
324     ## Now try with the public user
325     # setup auth
326     basic_authorization(users(:public_user).email, "test")
327
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
332
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
336
337     # try and upload it
338     content way_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
343   end
344
345   ##
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
349     # setup auth
350     basic_authorization(users(:normal_user).email, "test")
351
352     # create duplicate tag
353     tag_xml = XML::Node.new("tag")
354     tag_xml['k'] = "i_am_a_duplicate"
355     tag_xml['v'] = "foobar"
356
357     # add the tag into the existing xml
358     way_xml = current_ways(:visible_way).to_xml
359
360     # add two copies of the tag
361     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
362
363     # try and upload it
364     content way_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'"
368     
369     ## Now test with the public user
370     # setup auth
371     basic_authorization(users(:public_user).email, "test")
372
373     # create duplicate tag
374     tag_xml = XML::Node.new("tag")
375     tag_xml['k'] = "i_am_a_duplicate"
376     tag_xml['v'] = "foobar"
377
378     # add the tag into the existing xml
379     way_xml = current_ways(:visible_way).to_xml
380
381     # add two copies of the tag
382     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
383
384     # try and upload it
385     content way_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
390     
391   end
392
393   ##
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
399     # setup auth
400     basic_authorization(users(:normal_user).email, "test")
401
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>";
407
408     # try and upload it
409     content way_str
410     put :create
411     assert_response :forbidden, 
412     "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
413     
414     ## Now do it with a public user
415     # setup auth
416     basic_authorization(users(:public_user).email, "test")
417
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>";
423
424     # try and upload it
425     content way_str
426     put :create
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
430   end
431
432   ##
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"
442
443     # check that the set of IDs match expectations
444     expected_way_ids = [ current_ways(:visible_way).id,
445                          current_ways(:used_way).id
446                        ]
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"
450     
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))
456     end
457   end
458
459   ##
460   # update the changeset_id of a node element
461   def update_changeset(xml, changeset_id)
462     xml_attr_rewrite(xml, 'changeset', changeset_id)
463   end
464
465   ##
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
469     return xml
470   end
471 end