]> git.openstreetmap.org Git - rails.git/blob - test/controllers/way_controller_test.rb
Vagrant deploy works
[rails.git] / test / controllers / way_controller_test.rb
1 require 'test_helper'
2 require 'way_controller'
3
4 class WayControllerTest < ActionController::TestCase
5   api_fixtures
6
7   ##
8   # test all routes which lead to this controller
9   def test_routes
10     assert_routing(
11       { :path => "/api/0.6/way/create", :method => :put },
12       { :controller => "way", :action => "create" }
13     )
14     assert_routing(
15       { :path => "/api/0.6/way/1/full", :method => :get },
16       { :controller => "way", :action => "full", :id => "1" }
17     )
18     assert_routing(
19       { :path => "/api/0.6/way/1", :method => :get },
20       { :controller => "way", :action => "read", :id => "1" }
21     )
22     assert_routing(
23       { :path => "/api/0.6/way/1", :method => :put },
24       { :controller => "way", :action => "update", :id => "1" }
25     )
26     assert_routing(
27       { :path => "/api/0.6/way/1", :method => :delete },
28       { :controller => "way", :action => "delete", :id => "1" }
29     )
30     assert_routing(
31       { :path => "/api/0.6/ways", :method => :get },
32       { :controller => "way", :action => "ways" }
33     )
34   end
35
36   # -------------------------------------
37   # Test reading ways.
38   # -------------------------------------
39
40   def test_read
41     # check that a visible way is returned properly
42     get :read, :id => current_ways(:visible_way).id
43     assert_response :success
44
45     # check that an invisible way is not returned
46     get :read, :id => current_ways(:invisible_way).id
47     assert_response :gone
48
49     # check chat a non-existent way is not returned
50     get :read, :id => 0
51     assert_response :not_found
52   end
53
54   ##
55   # check the "full" mode
56   def test_full
57     Way.all.each do |way|
58       get :full, :id => way.id
59
60       # full call should say "gone" for non-visible ways...
61       unless way.visible
62         assert_response :gone
63         next
64       end
65
66       # otherwise it should say success
67       assert_response :success
68       
69       # Check the way is correctly returned
70       assert_select "osm way[id='#{way.id}'][version='#{way.version}'][visible='#{way.visible}']", 1
71       
72       # check that each node in the way appears once in the output as a 
73       # reference and as the node element.
74       way.nodes.each do |n|
75         count = (way.nodes - (way.nodes - [n])).length
76         assert_select "osm way nd[ref='#{n.id}']", count
77         assert_select "osm node[id='#{n.id}'][version='#{n.version}'][lat='#{n.lat}'][lon='#{n.lon}']", 1
78       end
79     end
80   end
81
82   ##
83   # test fetching multiple ways
84   def test_ways
85     # check error when no parameter provided
86     get :ways
87     assert_response :bad_request
88
89     # check error when no parameter value provided
90     get :ways, :ways => ""
91     assert_response :bad_request
92
93     # test a working call
94     get :ways, :ways => "1,2,4,6"
95     assert_response :success
96     assert_select "osm" do
97       assert_select "way", :count => 4
98       assert_select "way[id='1'][visible='true']", :count => 1
99       assert_select "way[id='2'][visible='false']", :count => 1
100       assert_select "way[id='4'][visible='true']", :count => 1
101       assert_select "way[id='6'][visible='true']", :count => 1
102     end
103
104     # check error when a non-existent way is included
105     get :ways, :ways => "1,2,4,6,400"
106     assert_response :not_found
107   end
108
109   # -------------------------------------
110   # Test simple way creation.
111   # -------------------------------------
112
113   def test_create
114     ## First check that it fails when creating a way using a non-public user
115     nid1 = current_nodes(:used_node_1).id
116     nid2 = current_nodes(:used_node_2).id
117     basic_authorization users(:normal_user).email, "test"
118
119     # use the first user's open changeset
120     changeset_id = changesets(:normal_user_first_change).id
121     
122     # create a way with pre-existing nodes
123     content "<osm><way changeset='#{changeset_id}'>" +
124       "<nd ref='#{nid1}'/><nd ref='#{nid2}'/>" + 
125       "<tag k='test' v='yes' /></way></osm>"
126     put :create
127     # hope for success
128     assert_response :forbidden, 
129         "way upload did not return success status"
130     # read id of created way and search for it
131     wayid = @response.body
132
133     ## Now use a public user
134     nid1 = current_nodes(:used_node_1).id
135     nid2 = current_nodes(:used_node_2).id
136     basic_authorization users(:public_user).email, "test"
137
138     # use the first user's open changeset
139     changeset_id = changesets(:public_user_first_change).id
140     
141     # create a way with pre-existing nodes
142     content "<osm><way changeset='#{changeset_id}'>" +
143       "<nd ref='#{nid1}'/><nd ref='#{nid2}'/>" + 
144       "<tag k='test' v='yes' /></way></osm>"
145     put :create
146     # hope for success
147     assert_response :success, 
148         "way upload did not return success status"
149     # read id of created way and search for it
150     wayid = @response.body
151     checkway = Way.find(wayid)
152     assert_not_nil checkway, 
153         "uploaded way not found in data base after upload"
154     # compare values
155     assert_equal checkway.nds.length, 2, 
156         "saved way does not contain exactly one node"
157     assert_equal checkway.nds[0], nid1, 
158         "saved way does not contain the right node on pos 0"
159     assert_equal checkway.nds[1], nid2, 
160         "saved way does not contain the right node on pos 1"
161     assert_equal checkway.changeset_id, changeset_id,
162         "saved way does not belong to the correct changeset"
163     assert_equal users(:public_user).id, checkway.changeset.user_id, 
164         "saved way does not belong to user that created it"
165     assert_equal true, checkway.visible, 
166         "saved way is not visible"
167   end
168
169   # -------------------------------------
170   # Test creating some invalid ways.
171   # -------------------------------------
172
173   def test_create_invalid
174     ## First test with a private user to make sure that they are not authorized
175     basic_authorization users(:normal_user).email, "test"
176
177     # use the first user's open changeset
178     open_changeset_id = changesets(:normal_user_first_change).id
179     closed_changeset_id = changesets(:normal_user_closed_change).id
180     nid1 = current_nodes(:used_node_1).id
181
182     # create a way with non-existing node
183     content "<osm><way changeset='#{open_changeset_id}'>" + 
184       "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
185     put :create
186     # expect failure
187     assert_response :forbidden, 
188     "way upload with invalid node using a private user did not return 'forbidden'"
189
190     # create a way with no nodes
191     content "<osm><way changeset='#{open_changeset_id}'>" +
192       "<tag k='test' v='yes' /></way></osm>"
193     put :create
194     # expect failure
195     assert_response :forbidden, 
196     "way upload with no node using a private userdid not return 'forbidden'"
197
198     # create a way inside a closed changeset
199     content "<osm><way changeset='#{closed_changeset_id}'>" +
200       "<nd ref='#{nid1}'/></way></osm>"
201     put :create
202     # expect failure
203     assert_response :forbidden, 
204     "way upload to closed changeset with a private user did not return 'forbidden'"    
205
206     
207     ## Now test with a public user
208     basic_authorization users(:public_user).email, "test"
209
210     # use the first user's open changeset
211     open_changeset_id = changesets(:public_user_first_change).id
212     closed_changeset_id = changesets(:public_user_closed_change).id
213     nid1 = current_nodes(:used_node_1).id
214
215     # create a way with non-existing node
216     content "<osm><way changeset='#{open_changeset_id}'>" + 
217       "<nd ref='0'/><tag k='test' v='yes' /></way></osm>"
218     put :create
219     # expect failure
220     assert_response :precondition_failed, 
221         "way upload with invalid node did not return 'precondition failed'"
222     assert_equal "Precondition failed: Way  requires the nodes with id in (0), which either do not exist, or are not visible.", @response.body
223
224     # create a way with no nodes
225     content "<osm><way changeset='#{open_changeset_id}'>" +
226       "<tag k='test' v='yes' /></way></osm>"
227     put :create
228     # expect failure
229     assert_response :precondition_failed, 
230         "way upload with no node did not return 'precondition failed'"
231     assert_equal "Precondition failed: Cannot create way: data is invalid.", @response.body
232
233     # create a way inside a closed changeset
234     content "<osm><way changeset='#{closed_changeset_id}'>" +
235       "<nd ref='#{nid1}'/></way></osm>"
236     put :create
237     # expect failure
238     assert_response :conflict, 
239         "way upload to closed changeset did not return 'conflict'"    
240
241     # create a way with a tag which is too long
242     content "<osm><way changeset='#{open_changeset_id}'>" +
243       "<nd ref='#{nid1}'/>" +
244       "<tag k='foo' v='#{'x'*256}'/>" +
245       "</way></osm>"
246     put :create
247     # expect failure
248     assert_response :bad_request, 
249         "way upload to with too long tag did not return 'bad_request'"
250   end
251
252   # -------------------------------------
253   # Test deleting ways.
254   # -------------------------------------
255   
256   def test_delete
257     # first try to delete way without auth
258     delete :delete, :id => current_ways(:visible_way).id
259     assert_response :unauthorized
260
261     # now set auth using the private user
262     basic_authorization(users(:normal_user).email, "test");  
263
264     # this shouldn't work as with the 0.6 api we need pay load to delete
265     delete :delete, :id => current_ways(:visible_way).id
266     assert_response :forbidden
267     
268     # Now try without having a changeset
269     content "<osm><way id='#{current_ways(:visible_way).id}'/></osm>"
270     delete :delete, :id => current_ways(:visible_way).id
271     assert_response :forbidden
272     
273     # try to delete with an invalid (closed) changeset
274     content update_changeset(current_ways(:visible_way).to_xml,
275                              changesets(:normal_user_closed_change).id)
276     delete :delete, :id => current_ways(:visible_way).id
277     assert_response :forbidden
278
279     # try to delete with an invalid (non-existent) changeset
280     content update_changeset(current_ways(:visible_way).to_xml,0)
281     delete :delete, :id => current_ways(:visible_way).id
282     assert_response :forbidden
283
284     # Now try with a valid changeset
285     content current_ways(:visible_way).to_xml
286     delete :delete, :id => current_ways(:visible_way).id
287     assert_response :forbidden
288
289     # check the returned value - should be the new version number
290     # valid delete should return the new version number, which should
291     # be greater than the old version number
292     #assert @response.body.to_i > current_ways(:visible_way).version,
293     #   "delete request should return a new version number for way"
294
295     # this won't work since the way is already deleted
296     content current_ways(:invisible_way).to_xml
297     delete :delete, :id => current_ways(:invisible_way).id
298     assert_response :forbidden
299
300     # this shouldn't work as the way is used in a relation
301     content current_ways(:used_way).to_xml
302     delete :delete, :id => current_ways(:used_way).id
303     assert_response :forbidden, 
304     "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user"
305
306     # this won't work since the way never existed
307     delete :delete, :id => 0
308     assert_response :forbidden
309
310     
311     ### Now check with a public user
312     # now set auth
313     basic_authorization(users(:public_user).email, "test");  
314
315     # this shouldn't work as with the 0.6 api we need pay load to delete
316     delete :delete, :id => current_ways(:visible_way).id
317     assert_response :bad_request
318     
319     # Now try without having a changeset
320     content "<osm><way id='#{current_ways(:visible_way).id}'/></osm>"
321     delete :delete, :id => current_ways(:visible_way).id
322     assert_response :bad_request
323     
324     # try to delete with an invalid (closed) changeset
325     content update_changeset(current_ways(:visible_way).to_xml,
326                              changesets(:public_user_closed_change).id)
327     delete :delete, :id => current_ways(:visible_way).id
328     assert_response :conflict
329
330     # try to delete with an invalid (non-existent) changeset
331     content update_changeset(current_ways(:visible_way).to_xml,0)
332     delete :delete, :id => current_ways(:visible_way).id
333     assert_response :conflict
334
335     # Now try with a valid changeset
336     content current_ways(:visible_way).to_xml
337     delete :delete, :id => current_ways(:visible_way).id
338     assert_response :success
339
340     # check the returned value - should be the new version number
341     # valid delete should return the new version number, which should
342     # be greater than the old version number
343     assert @response.body.to_i > current_ways(:visible_way).version,
344        "delete request should return a new version number for way"
345
346     # this won't work since the way is already deleted
347     content current_ways(:invisible_way).to_xml
348     delete :delete, :id => current_ways(:invisible_way).id
349     assert_response :gone
350
351     # this shouldn't work as the way is used in a relation
352     content current_ways(:used_way).to_xml
353     delete :delete, :id => current_ways(:used_way).id
354     assert_response :precondition_failed, 
355        "shouldn't be able to delete a way used in a relation (#{@response.body})"
356     assert_equal "Precondition failed: Way 3 is still used by relations 1.", @response.body
357
358     # this won't work since the way never existed
359     delete :delete, :id => 0
360     assert_response :not_found
361   end
362
363   # ------------------------------------------------------------
364   # test tags handling
365   # ------------------------------------------------------------
366
367   ##
368   # Try adding a duplicate of an existing tag to a way
369   def test_add_duplicate_tags
370     ## Try with the non-public user
371     # setup auth
372     basic_authorization(users(:normal_user).email, "test")
373
374     # add an identical tag to the way
375     tag_xml = XML::Node.new("tag")
376     tag_xml['k'] = current_way_tags(:t1).k
377     tag_xml['v'] = current_way_tags(:t1).v
378
379     # add the tag into the existing xml
380     way_xml = current_ways(:visible_way).to_xml
381     way_xml.find("//osm/way").first << tag_xml
382
383     # try and upload it
384     content way_xml
385     put :update, :id => current_ways(:visible_way).id
386     assert_response :forbidden, 
387     "adding a duplicate tag to a way for a non-public should fail with 'forbidden'"
388
389     ## Now try with the public user
390     # setup auth
391     basic_authorization(users(:public_user).email, "test")
392
393     # add an identical tag to the way
394     tag_xml = XML::Node.new("tag")
395     tag_xml['k'] = current_way_tags(:t1).k
396     tag_xml['v'] = current_way_tags(:t1).v
397
398     # add the tag into the existing xml
399     way_xml = current_ways(:visible_way).to_xml
400     way_xml.find("//osm/way").first << tag_xml
401
402     # try and upload it
403     content way_xml
404     put :update, :id => current_ways(:visible_way).id
405     assert_response :bad_request, 
406        "adding a duplicate tag to a way should fail with 'bad request'"
407     assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key #{current_way_tags(:t1).k}", @response.body
408   end
409
410   ##
411   # Try adding a new duplicate tags to a way
412   def test_new_duplicate_tags
413     ## First test with the non-public user so should be rejected
414     # setup auth
415     basic_authorization(users(:normal_user).email, "test")
416
417     # create duplicate tag
418     tag_xml = XML::Node.new("tag")
419     tag_xml['k'] = "i_am_a_duplicate"
420     tag_xml['v'] = "foobar"
421
422     # add the tag into the existing xml
423     way_xml = current_ways(:visible_way).to_xml
424
425     # add two copies of the tag
426     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
427
428     # try and upload it
429     content way_xml
430     put :update, :id => current_ways(:visible_way).id
431     assert_response :forbidden, 
432     "adding new duplicate tags to a way using a non-public user should fail with 'forbidden'"
433     
434     ## Now test with the public user
435     # setup auth
436     basic_authorization(users(:public_user).email, "test")
437
438     # create duplicate tag
439     tag_xml = XML::Node.new("tag")
440     tag_xml['k'] = "i_am_a_duplicate"
441     tag_xml['v'] = "foobar"
442
443     # add the tag into the existing xml
444     way_xml = current_ways(:visible_way).to_xml
445
446     # add two copies of the tag
447     way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml
448
449     # try and upload it
450     content way_xml
451     put :update, :id => current_ways(:visible_way).id
452     assert_response :bad_request, 
453        "adding new duplicate tags to a way should fail with 'bad request'"
454     assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key i_am_a_duplicate", @response.body
455     
456   end
457
458   ##
459   # Try adding a new duplicate tags to a way.
460   # But be a bit subtle - use unicode decoding ambiguities to use different
461   # binary strings which have the same decoding.
462   def test_invalid_duplicate_tags
463     ## First make sure that you can't with a non-public user
464     # setup auth
465     basic_authorization(users(:normal_user).email, "test")
466
467     # add the tag into the existing xml
468     way_str = "<osm><way changeset='1'>"
469     way_str << "<tag k='addr:housenumber' v='1'/>"
470     way_str << "<tag k='addr:housenumber' v='2'/>"
471     way_str << "</way></osm>";
472
473     # try and upload it
474     content way_str
475     put :create
476     assert_response :forbidden, 
477     "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'"
478     
479     ## Now do it with a public user
480     # setup auth
481     basic_authorization(users(:public_user).email, "test")
482
483     # add the tag into the existing xml
484     way_str = "<osm><way changeset='1'>"
485     way_str << "<tag k='addr:housenumber' v='1'/>"
486     way_str << "<tag k='addr:housenumber' v='2'/>"
487     way_str << "</way></osm>";
488
489     # try and upload it
490     content way_str
491     put :create
492     assert_response :bad_request, 
493     "adding new duplicate tags to a way should fail with 'bad request'"
494     assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body
495   end
496
497   ##
498   # test that a call to ways_for_node returns all ways that contain the node
499   # and none that don't.
500   def test_ways_for_node
501     # in current fixtures ways 1 and 3 all use node 3. ways 2 and 4 
502     # *used* to use it but doesn't.
503     get :ways_for_node, :id => current_nodes(:used_node_1).id
504     assert_response :success
505     ways_xml = XML::Parser.string(@response.body).parse
506     assert_not_nil ways_xml, "failed to parse ways_for_node response"
507
508     # check that the set of IDs match expectations
509     expected_way_ids = [ current_ways(:visible_way).id,
510                          current_ways(:used_way).id
511                        ]
512     found_way_ids = ways_xml.find("//osm/way").collect { |w| w["id"].to_i }
513     assert_equal expected_way_ids.sort, found_way_ids.sort,
514       "expected ways for node #{current_nodes(:used_node_1).id} did not match found"
515     
516     # check the full ways to ensure we're not missing anything
517     expected_way_ids.each do |id|
518       way_xml = ways_xml.find("//osm/way[@id='#{id}']").first
519       assert_ways_are_equal(Way.find(id),
520                             Way.from_xml_node(way_xml))
521     end
522   end
523
524   ##
525   # update the changeset_id of a node element
526   def update_changeset(xml, changeset_id)
527     xml_attr_rewrite(xml, 'changeset', changeset_id)
528   end
529
530   ##
531   # update an attribute in the node element
532   def xml_attr_rewrite(xml, name, value)
533     xml.find("//osm/way").first[name] = value.to_s
534     return xml
535   end
536 end