]> git.openstreetmap.org Git - rails.git/blob - test/functional/way_controller_test.rb
Tweak the password reset pages a bit.
[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 nodes with id in (0), which either do not exist, or are 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
186     # create a way with a tag which is too long
187     content "<osm><way changeset='#{open_changeset_id}'>" +
188       "<nd ref='#{nid1}'/>" +
189       "<tag k='foo' v='#{'x'*256}'/>" +
190       "</way></osm>"
191     put :create
192     # expect failure
193     assert_response :bad_request, 
194         "way upload to with too long tag did not return 'bad_request'"
195   end
196
197   # -------------------------------------
198   # Test deleting ways.
199   # -------------------------------------
200   
201   def test_delete
202     # first try to delete way without auth
203     delete :delete, :id => current_ways(:visible_way).id
204     assert_response :unauthorized
205
206     # now set auth using the private user
207     basic_authorization(users(:normal_user).email, "test");  
208
209     # this shouldn't work as with the 0.6 api we need pay load to delete
210     delete :delete, :id => current_ways(:visible_way).id
211     assert_response :forbidden
212     
213     # Now try without having a changeset
214     content "<osm><way id='#{current_ways(:visible_way).id}'></osm>"
215     delete :delete, :id => current_ways(:visible_way).id
216     assert_response :forbidden
217     
218     # try to delete with an invalid (closed) changeset
219     content update_changeset(current_ways(:visible_way).to_xml,
220                              changesets(:normal_user_closed_change).id)
221     delete :delete, :id => current_ways(:visible_way).id
222     assert_response :forbidden
223
224     # try to delete with an invalid (non-existent) changeset
225     content update_changeset(current_ways(:visible_way).to_xml,0)
226     delete :delete, :id => current_ways(:visible_way).id
227     assert_response :forbidden
228
229     # Now try with a valid changeset
230     content current_ways(:visible_way).to_xml
231     delete :delete, :id => current_ways(:visible_way).id
232     assert_response :forbidden
233
234     # check the returned value - should be the new version number
235     # valid delete should return the new version number, which should
236     # be greater than the old version number
237     #assert @response.body.to_i > current_ways(:visible_way).version,
238     #   "delete request should return a new version number for way"
239
240     # this won't work since the way is already deleted
241     content current_ways(:invisible_way).to_xml
242     delete :delete, :id => current_ways(:invisible_way).id
243     assert_response :forbidden
244
245     # this shouldn't work as the way is used in a relation
246     content current_ways(:used_way).to_xml
247     delete :delete, :id => current_ways(:used_way).id
248     assert_response :forbidden, 
249     "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
250
251     # this won't work since the way never existed
252     delete :delete, :id => 0
253     assert_response :forbidden
254
255     
256     ### Now check with a public user
257     # now set auth
258     basic_authorization(users(:public_user).email, "test");  
259
260     # this shouldn't work as with the 0.6 api we need pay load to delete
261     delete :delete, :id => current_ways(:visible_way).id
262     assert_response :bad_request
263     
264     # Now try without having a changeset
265     content "<osm><way id='#{current_ways(:visible_way).id}'></osm>"
266     delete :delete, :id => current_ways(:visible_way).id
267     assert_response :bad_request
268     
269     # try to delete with an invalid (closed) changeset
270     content update_changeset(current_ways(:visible_way).to_xml,
271                              changesets(:public_user_closed_change).id)
272     delete :delete, :id => current_ways(:visible_way).id
273     assert_response :conflict
274
275     # try to delete with an invalid (non-existent) changeset
276     content update_changeset(current_ways(:visible_way).to_xml,0)
277     delete :delete, :id => current_ways(:visible_way).id
278     assert_response :conflict
279
280     # Now try with a valid changeset
281     content current_ways(:visible_way).to_xml
282     delete :delete, :id => current_ways(:visible_way).id
283     assert_response :success
284
285     # check the returned value - should be the new version number
286     # valid delete should return the new version number, which should
287     # be greater than the old version number
288     assert @response.body.to_i > current_ways(:visible_way).version,
289        "delete request should return a new version number for way"
290
291     # this won't work since the way is already deleted
292     content current_ways(:invisible_way).to_xml
293     delete :delete, :id => current_ways(:invisible_way).id
294     assert_response :gone
295
296     # this shouldn't work as the way is used in a relation
297     content current_ways(:used_way).to_xml
298     delete :delete, :id => current_ways(:used_way).id
299     assert_response :precondition_failed, 
300        "shouldn't be able to delete a way used in a relation (#{@response.body})"
301     assert_equal "Precondition failed: Way 3 still used by relation 1.", @response.body
302
303     # this won't work since the way never existed
304     delete :delete, :id => 0
305     assert_response :not_found
306   end
307
308   # ------------------------------------------------------------
309   # test tags handling
310   # ------------------------------------------------------------
311
312   ##
313   # Try adding a duplicate of an existing tag to a way
314   def test_add_duplicate_tags
315     ## Try with the non-public user
316     # setup auth
317     basic_authorization(users(:normal_user).email, "test")
318
319     # add an identical tag to the way
320     tag_xml = XML::Node.new("tag")
321     tag_xml['k'] = current_way_tags(:t1).k
322     tag_xml['v'] = current_way_tags(:t1).v
323
324     # add the tag into the existing xml
325     way_xml = current_ways(:visible_way).to_xml
326     way_xml.find("//osm/way").first << tag_xml
327
328     # try and upload it
329     content way_xml
330     put :update, :id => current_ways(:visible_way).id
331     assert_response :forbidden, 
332     "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
333
334     ## Now try with the public user
335     # setup auth
336     basic_authorization(users(:public_user).email, "test")
337
338     # add an identical tag to the way
339     tag_xml = XML::Node.new("tag")
340     tag_xml['k'] = current_way_tags(:t1).k
341     tag_xml['v'] = current_way_tags(:t1).v
342
343     # add the tag into the existing xml
344     way_xml = current_ways(:visible_way).to_xml
345     way_xml.find("//osm/way").first << tag_xml
346
347     # try and upload it
348     content way_xml
349     put :update, :id => current_ways(:visible_way).id
350     assert_response :bad_request, 
351        "adding a duplicate tag to a way should fail with 'bad request'"
352     assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key #{current_way_tags(:t1).k}", @response.body
353   end
354
355   ##
356   # Try adding a new duplicate tags to a way
357   def test_new_duplicate_tags
358     ## First test with the non-public user so should be rejected
359     # setup auth
360     basic_authorization(users(:normal_user).email, "test")
361
362     # create duplicate tag
363     tag_xml = XML::Node.new("tag")
364     tag_xml['k'] = "i_am_a_duplicate"
365     tag_xml['v'] = "foobar"
366
367     # add the tag into the existing xml
368     way_xml = current_ways(:visible_way).to_xml
369
370     # add two copies of the tag
371     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
372
373     # try and upload it
374     content way_xml
375     put :update, :id => current_ways(:visible_way).id
376     assert_response :forbidden, 
377     "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
378     
379     ## Now test with the public user
380     # setup auth
381     basic_authorization(users(:public_user).email, "test")
382
383     # create duplicate tag
384     tag_xml = XML::Node.new("tag")
385     tag_xml['k'] = "i_am_a_duplicate"
386     tag_xml['v'] = "foobar"
387
388     # add the tag into the existing xml
389     way_xml = current_ways(:visible_way).to_xml
390
391     # add two copies of the tag
392     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
393
394     # try and upload it
395     content way_xml
396     put :update, :id => current_ways(:visible_way).id
397     assert_response :bad_request, 
398        "adding new duplicate tags to a way should fail with 'bad request'"
399     assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key i_am_a_duplicate", @response.body
400     
401   end
402
403   ##
404   # Try adding a new duplicate tags to a way.
405   # But be a bit subtle - use unicode decoding ambiguities to use different
406   # binary strings which have the same decoding.
407   def test_invalid_duplicate_tags
408     ## First make sure that you can't with a non-public user
409     # setup auth
410     basic_authorization(users(:normal_user).email, "test")
411
412     # add the tag into the existing xml
413     way_str = "<osm><way changeset='1'>"
414     way_str << "<tag k='addr:housenumber' v='1'/>"
415     way_str << "<tag k='addr:housenumber' v='2'/>"
416     way_str << "</way></osm>";
417
418     # try and upload it
419     content way_str
420     put :create
421     assert_response :forbidden, 
422     "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
423     
424     ## Now do it with a public user
425     # setup auth
426     basic_authorization(users(:public_user).email, "test")
427
428     # add the tag into the existing xml
429     way_str = "<osm><way changeset='1'>"
430     way_str << "<tag k='addr:housenumber' v='1'/>"
431     way_str << "<tag k='addr:housenumber' v='2'/>"
432     way_str << "</way></osm>";
433
434     # try and upload it
435     content way_str
436     put :create
437     assert_response :bad_request, 
438     "adding new duplicate tags to a way should fail with 'bad request'"
439     assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
440   end
441
442   ##
443   # test that a call to ways_for_node returns all ways that contain the node
444   # and none that don't.
445   def test_ways_for_node
446     # in current fixtures ways 1 and 3 all use node 3. ways 2 and 4 
447     # *used* to use it but doesn't.
448     get :ways_for_node, :id => current_nodes(:used_node_1).id
449     assert_response :success
450     ways_xml = XML::Parser.string(@response.body).parse
451     assert_not_nil ways_xml, "failed to parse ways_for_node response"
452
453     # check that the set of IDs match expectations
454     expected_way_ids = [ current_ways(:visible_way).id,
455                          current_ways(:used_way).id
456                        ]
457     found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
458     assert_equal expected_way_ids, found_way_ids,
459       "expected ways for node #{current_nodes(:used_node_1).id} did not match found"
460     
461     # check the full ways to ensure we're not missing anything
462     expected_way_ids.each do |id|
463       way_xml = ways_xml.find("//osm/way[@id=#{id}]").first
464       assert_ways_are_equal(Way.find(id),
465                             Way.from_xml_node(way_xml))
466     end
467   end
468
469   ##
470   # update the changeset_id of a node element
471   def update_changeset(xml, changeset_id)
472     xml_attr_rewrite(xml, 'changeset', changeset_id)
473   end
474
475   ##
476   # update an attribute in the node element
477   def xml_attr_rewrite(xml, name, value)
478     xml.find("//osm/way").first[name] = value.to_s
479     return xml
480   end
481 end