]> git.openstreetmap.org Git - rails.git/blob - test/functional/node_controller_test.rb
Remove unnecessary div/class/css
[rails.git] / test / functional / node_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class NodeControllerTest < ActionController::TestCase
4   api_fixtures
5
6   ##
7   # test all routes which lead to this controller
8   def test_routes
9     assert_routing(
10       { :path => "/api/0.6/node/create", :method => :put },
11       { :controller => "node", :action => "create" }
12     )
13     assert_routing(
14       { :path => "/api/0.6/node/1", :method => :get },
15       { :controller => "node", :action => "read", :id => "1" }
16     )
17     assert_routing(
18       { :path => "/api/0.6/node/1", :method => :put },
19       { :controller => "node", :action => "update", :id => "1" }
20     )
21     assert_routing(
22       { :path => "/api/0.6/node/1", :method => :delete },
23       { :controller => "node", :action => "delete", :id => "1" }
24     )
25     assert_routing(
26       { :path => "/api/0.6/nodes", :method => :get },
27       { :controller => "node", :action => "nodes" }
28     )
29   end
30
31   def test_create
32     # cannot read password from fixture as it is stored as MD5 digest
33     ## First try with no auth
34     
35     # create a node with random lat/lon
36     lat = rand(100)-50 + rand
37     lon = rand(100)-50 + rand
38     # normal user has a changeset open, so we'll use that.
39     changeset = changesets(:normal_user_first_change)
40     # create a minimal xml file
41     content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
42     assert_difference('OldNode.count', 0) do
43       put :create
44     end
45     # hope for unauthorized
46     assert_response :unauthorized, "node upload did not return unauthorized status"
47
48     
49     
50     ## Now try with the user which doesn't have their data public
51     basic_authorization(users(:normal_user).email, "test")
52     
53     # create a node with random lat/lon
54     lat = rand(100)-50 + rand
55     lon = rand(100)-50 + rand
56     # normal user has a changeset open, so we'll use that.
57     changeset = changesets(:normal_user_first_change)
58     # create a minimal xml file
59     content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
60     assert_difference('Node.count', 0) do
61       put :create
62     end
63     # hope for success
64     assert_require_public_data "node create did not return forbidden status"
65
66
67     
68     ## Now try with the user that has the public data
69     basic_authorization(users(:public_user).email, "test")
70     
71     # create a node with random lat/lon
72     lat = rand(100)-50 + rand
73     lon = rand(100)-50 + rand
74     # normal user has a changeset open, so we'll use that.
75     changeset = changesets(:public_user_first_change)
76     # create a minimal xml file
77     content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
78     put :create
79     # hope for success
80     assert_response :success, "node upload did not return success status"
81
82     # read id of created node and search for it
83     nodeid = @response.body
84     checknode = Node.find(nodeid)
85     assert_not_nil checknode, "uploaded node not found in data base after upload"
86     # compare values
87     assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
88     assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
89     assert_equal changesets(:public_user_first_change).id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
90     assert_equal true, checknode.visible, "saved node is not visible"
91   end
92
93   def test_create_invalid_xml
94     ## Only test public user here, as test_create should cover what's the forbiddens
95     ## that would occur here
96     # Initial setup
97     basic_authorization(users(:public_user).email, "test")
98     # normal user has a changeset open, so we'll use that.
99     changeset = changesets(:public_user_first_change)
100     lat = 3.434
101     lon = 3.23
102     
103     # test that the upload is rejected when xml is valid, but osm doc isn't
104     content("<create/>")
105     put :create
106     assert_response :bad_request, "node upload did not return bad_request status"
107     assert_equal "Cannot parse valid node from xml string <create/>. XML doesn't contain an osm/node element.", @response.body
108
109     # test that the upload is rejected when no lat is supplied
110     # create a minimal xml file
111     content("<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>")
112     put :create
113     # hope for success
114     assert_response :bad_request, "node upload did not return bad_request status"
115     assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
116
117     # test that the upload is rejected when no lon is supplied
118     # create a minimal xml file
119     content("<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>")
120     put :create
121     # hope for success
122     assert_response :bad_request, "node upload did not return bad_request status"
123     assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
124
125     # test that the upload is rejected when lat is non-numeric
126     # create a minimal xml file
127     content("<osm><node lat='abc' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
128     put :create
129     # hope for success
130     assert_response :bad_request, "node upload did not return bad_request status"
131     assert_equal "Cannot parse valid node from xml string <node lat=\"abc\" lon=\"#{lon}\" changeset=\"#{changeset.id}\"/>. lat not a number", @response.body
132
133     # test that the upload is rejected when lon is non-numeric
134     # create a minimal xml file
135     content("<osm><node lat='#{lat}' lon='abc' changeset='#{changeset.id}'/></osm>")
136     put :create
137     # hope for success
138     assert_response :bad_request, "node upload did not return bad_request status"
139     assert_equal "Cannot parse valid node from xml string <node lat=\"#{lat}\" lon=\"abc\" changeset=\"#{changeset.id}\"/>. lon not a number", @response.body
140
141     # test that the upload is rejected when we have a tag which is too long
142     content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'><tag k='foo' v='#{'x'*256}'/></node></osm>")
143     put :create
144     assert_response :bad_request, "node upload did not return bad_request status"
145     assert_equal ["NodeTag ", " v: is too long (maximum is 255 characters) (\"#{'x'*256}\")"], @response.body.split(/[0-9]+,foo:/)
146
147   end
148
149   def test_read
150     # check that a visible node is returned properly
151     get :read, :id => current_nodes(:visible_node).id
152     assert_response :success
153
154     # check that an invisible node is not returned
155     get :read, :id => current_nodes(:invisible_node).id
156     assert_response :gone
157
158     # check chat a non-existent node is not returned
159     get :read, :id => 0
160     assert_response :not_found
161   end
162
163   # this tests deletion restrictions - basic deletion is tested in the unit
164   # tests for node!
165   def test_delete
166     ## first try to delete node without auth
167     delete :delete, :id => current_nodes(:visible_node).id
168     assert_response :unauthorized
169     
170     
171     ## now set auth for the non-data public user
172     basic_authorization(users(:normal_user).email, "test");  
173
174     # try to delete with an invalid (closed) changeset
175     content update_changeset(current_nodes(:visible_node).to_xml,
176                              changesets(:normal_user_closed_change).id)
177     delete :delete, :id => current_nodes(:visible_node).id
178     assert_require_public_data("non-public user shouldn't be able to delete node")
179
180     # try to delete with an invalid (non-existent) changeset
181     content update_changeset(current_nodes(:visible_node).to_xml,0)
182     delete :delete, :id => current_nodes(:visible_node).id
183     assert_require_public_data("shouldn't be able to delete node, when user's data is private")
184
185     # valid delete now takes a payload
186     content(nodes(:visible_node).to_xml)
187     delete :delete, :id => current_nodes(:visible_node).id
188     assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
189
190     # this won't work since the node is already deleted
191     content(nodes(:invisible_node).to_xml)
192     delete :delete, :id => current_nodes(:invisible_node).id
193     assert_require_public_data
194
195     # this won't work since the node never existed
196     delete :delete, :id => 0
197     assert_require_public_data
198
199     ## these test whether nodes which are in-use can be deleted:
200     # in a way...
201     content(nodes(:used_node_1).to_xml)
202     delete :delete, :id => current_nodes(:used_node_1).id
203     assert_require_public_data
204        "shouldn't be able to delete a node used in a way (#{@response.body})"
205
206     # in a relation...
207     content(nodes(:node_used_by_relationship).to_xml)
208     delete :delete, :id => current_nodes(:node_used_by_relationship).id
209     assert_require_public_data
210        "shouldn't be able to delete a node used in a relation (#{@response.body})"
211
212     
213
214     ## now set auth for the public data user
215     basic_authorization(users(:public_user).email, "test");  
216
217     # try to delete with an invalid (closed) changeset
218     content update_changeset(current_nodes(:visible_node).to_xml,
219                              changesets(:normal_user_closed_change).id)
220     delete :delete, :id => current_nodes(:visible_node).id
221     assert_response :conflict
222
223     # try to delete with an invalid (non-existent) changeset
224     content update_changeset(current_nodes(:visible_node).to_xml,0)
225     delete :delete, :id => current_nodes(:visible_node).id
226     assert_response :conflict
227
228     # try to delete a node with a different ID
229     content(nodes(:public_visible_node).to_xml)
230     delete :delete, :id => current_nodes(:visible_node).id
231     assert_response :bad_request, 
232        "should not be able to delete a node with a different ID from the XML"
233
234     # try to delete a node rubbish in the payloads
235     content("<delete/>")
236     delete :delete, :id => current_nodes(:visible_node).id
237     assert_response :bad_request, 
238        "should not be able to delete a node without a valid XML payload"
239
240     # valid delete now takes a payload
241     content(nodes(:public_visible_node).to_xml)
242     delete :delete, :id => current_nodes(:public_visible_node).id
243     assert_response :success
244
245     # valid delete should return the new version number, which should
246     # be greater than the old version number
247     assert @response.body.to_i > current_nodes(:public_visible_node).version,
248        "delete request should return a new version number for node"
249
250     # deleting the same node twice doesn't work
251     content(nodes(:public_visible_node).to_xml)
252     delete :delete, :id => current_nodes(:public_visible_node).id
253     assert_response :gone
254
255     # this won't work since the node never existed
256     delete :delete, :id => 0
257     assert_response :not_found
258
259     ## these test whether nodes which are in-use can be deleted:
260     # in a way...
261     content(nodes(:used_node_1).to_xml)
262     delete :delete, :id => current_nodes(:used_node_1).id
263     assert_response :precondition_failed,
264        "shouldn't be able to delete a node used in a way (#{@response.body})"
265     assert_equal "Precondition failed: Node 3 is still used by ways 1,3.", @response.body
266
267     # in a relation...
268     content(nodes(:node_used_by_relationship).to_xml)
269     delete :delete, :id => current_nodes(:node_used_by_relationship).id
270     assert_response :precondition_failed,
271        "shouldn't be able to delete a node used in a relation (#{@response.body})"
272     assert_equal "Precondition failed: Node 5 is still used by relations 1,3.", @response.body
273   end
274
275   ##
276   # tests whether the API works and prevents incorrect use while trying
277   # to update nodes.
278   def test_update
279     ## First test with no user credentials
280     # try and update a node without authorisation
281     # first try to delete node without auth
282     content current_nodes(:visible_node).to_xml
283     put :update, :id => current_nodes(:visible_node).id
284     assert_response :unauthorized
285     
286     
287     
288     ## Second test with the private user
289     
290     # setup auth
291     basic_authorization(users(:normal_user).email, "test")
292
293     ## trying to break changesets
294
295     # try and update in someone else's changeset
296     content update_changeset(current_nodes(:visible_node).to_xml,
297                              changesets(:public_user_first_change).id)
298     put :update, :id => current_nodes(:visible_node).id
299     assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
300
301     # try and update in a closed changeset
302     content update_changeset(current_nodes(:visible_node).to_xml,
303                              changesets(:normal_user_closed_change).id)
304     put :update, :id => current_nodes(:visible_node).id
305     assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
306
307     # try and update in a non-existant changeset
308     content update_changeset(current_nodes(:visible_node).to_xml, 0)
309     put :update, :id => current_nodes(:visible_node).id
310     assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
311
312     ## try and submit invalid updates
313     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
314     put :update, :id => current_nodes(:visible_node).id
315     assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
316
317     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
318     put :update, :id => current_nodes(:visible_node).id
319     assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
320     
321     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
322     put :update, :id => current_nodes(:visible_node).id
323     assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
324
325     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
326     put :update, :id => current_nodes(:visible_node).id
327     assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
328     
329     ## finally, produce a good request which should work
330     content current_nodes(:visible_node).to_xml
331     put :update, :id => current_nodes(:visible_node).id
332     assert_require_public_data "should have failed with a forbidden when data isn't public"
333     
334     ## Finally test with the public user
335     
336     # try and update a node without authorisation
337     # first try to delete node without auth
338     content current_nodes(:visible_node).to_xml
339     put :update, :id => current_nodes(:visible_node).id
340     assert_response :forbidden
341     
342     # setup auth
343     basic_authorization(users(:public_user).email, "test")
344
345     ## trying to break changesets
346
347     # try and update in someone else's changeset
348     content update_changeset(current_nodes(:visible_node).to_xml,
349                               changesets(:normal_user_first_change).id)
350     put :update, :id => current_nodes(:visible_node).id
351     assert_response :conflict, "update with other user's changeset should be rejected"
352
353     # try and update in a closed changeset
354     content update_changeset(current_nodes(:visible_node).to_xml,
355                              changesets(:normal_user_closed_change).id)
356     put :update, :id => current_nodes(:visible_node).id
357     assert_response :conflict, "update with closed changeset should be rejected"
358
359     # try and update in a non-existant changeset
360     content update_changeset(current_nodes(:visible_node).to_xml, 0)
361     put :update, :id => current_nodes(:visible_node).id
362     assert_response :conflict, "update with changeset=0 should be rejected"
363
364     ## try and submit invalid updates
365     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
366     put :update, :id => current_nodes(:visible_node).id
367     assert_response :bad_request, "node at lat=91 should be rejected"
368
369     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
370     put :update, :id => current_nodes(:visible_node).id
371     assert_response :bad_request, "node at lat=-91 should be rejected"
372     
373     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
374     put :update, :id => current_nodes(:visible_node).id
375     assert_response :bad_request, "node at lon=181 should be rejected"
376
377     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
378     put :update, :id => current_nodes(:visible_node).id
379     assert_response :bad_request, "node at lon=-181 should be rejected"
380
381     ## next, attack the versioning
382     current_node_version = current_nodes(:visible_node).version
383
384     # try and submit a version behind
385     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 
386                              'version', current_node_version - 1);
387     put :update, :id => current_nodes(:visible_node).id
388     assert_response :conflict, "should have failed on old version number"
389     
390     # try and submit a version ahead
391     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 
392                              'version', current_node_version + 1);
393     put :update, :id => current_nodes(:visible_node).id
394     assert_response :conflict, "should have failed on skipped version number"
395
396     # try and submit total crap in the version field
397     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 
398                              'version', 'p1r4t3s!');
399     put :update, :id => current_nodes(:visible_node).id
400     assert_response :conflict, 
401        "should not be able to put 'p1r4at3s!' in the version field"
402     
403     ## try an update with the wrong ID
404     content current_nodes(:public_visible_node).to_xml
405     put :update, :id => current_nodes(:visible_node).id
406     assert_response :bad_request, 
407        "should not be able to update a node with a different ID from the XML"
408
409     ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
410     content "<update/>"
411     put :update, :id => current_nodes(:visible_node).id
412     assert_response :bad_request, 
413        "should not be able to update a node with non-OSM XML doc."
414
415     ## finally, produce a good request which should work
416     content current_nodes(:public_visible_node).to_xml
417     put :update, :id => current_nodes(:public_visible_node).id
418     assert_response :success, "a valid update request failed"
419   end
420
421   ##
422   # test fetching multiple nodes
423   def test_nodes
424     # check error when no parameter provided
425     get :nodes
426     assert_response :bad_request
427
428     # check error when no parameter value provided
429     get :nodes, :nodes => ""
430     assert_response :bad_request
431
432     # test a working call
433     get :nodes, :nodes => "1,2,4,15,17"
434     assert_response :success
435     assert_select "osm" do
436       assert_select "node", :count => 5
437       assert_select "node[id=1][visible=true]", :count => 1
438       assert_select "node[id=2][visible=false]", :count => 1
439       assert_select "node[id=4][visible=true]", :count => 1
440       assert_select "node[id=15][visible=true]", :count => 1
441       assert_select "node[id=17][visible=false]", :count => 1
442     end
443
444     # check error when a non-existent node is included
445     get :nodes, :nodes => "1,2,4,15,17,400"
446     assert_response :not_found
447   end
448
449   ##
450   # test adding tags to a node
451   def test_duplicate_tags
452     # setup auth
453     basic_authorization(users(:public_user).email, "test")
454
455     # add an identical tag to the node
456     tag_xml = XML::Node.new("tag")
457     tag_xml['k'] = current_node_tags(:public_v_t1).k
458     tag_xml['v'] = current_node_tags(:public_v_t1).v
459
460     # add the tag into the existing xml
461     node_xml = current_nodes(:public_visible_node).to_xml
462     node_xml.find("//osm/node").first << tag_xml
463
464     # try and upload it
465     content node_xml
466     put :update, :id => current_nodes(:public_visible_node).id
467     assert_response :bad_request, 
468       "adding duplicate tags to a node should fail with 'bad request'"
469     assert_equal "Element node/#{current_nodes(:public_visible_node).id} has duplicate tags with key #{current_node_tags(:t1).k}", @response.body
470   end
471
472   # test whether string injection is possible
473   def test_string_injection
474     ## First try with the non-data public user
475     basic_authorization(users(:normal_user).email, "test")
476     changeset_id = changesets(:normal_user_first_change).id
477
478     # try and put something into a string that the API might 
479     # use unquoted and therefore allow code injection...
480     content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
481       '<tag k="#{@user.inspect}" v="0"/>' +
482       '</node></osm>'
483     put :create
484     assert_require_public_data "Shouldn't be able to create with non-public user"
485     
486     
487     ## Then try with the public data user
488     basic_authorization(users(:public_user).email, "test")
489     changeset_id = changesets(:public_user_first_change).id
490
491     # try and put something into a string that the API might 
492     # use unquoted and therefore allow code injection...
493     content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
494       '<tag k="#{@user.inspect}" v="0"/>' +
495       '</node></osm>'
496     put :create
497     assert_response :success
498     nodeid = @response.body
499
500     # find the node in the database
501     checknode = Node.find(nodeid)
502     assert_not_nil checknode, "node not found in data base after upload"
503     
504     # and grab it using the api
505     get :read, :id => nodeid
506     assert_response :success
507     apinode = Node.from_xml(@response.body)
508     assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
509     
510     # check the tags are not corrupted
511     assert_equal checknode.tags, apinode.tags
512     assert apinode.tags.include?('#{@user.inspect}')
513   end
514
515   def basic_authorization(user, pass)
516     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
517   end
518
519   def content(c)
520     @request.env["RAW_POST_DATA"] = c.to_s
521   end
522
523   ##
524   # update the changeset_id of a node element
525   def update_changeset(xml, changeset_id)
526     xml_attr_rewrite(xml, 'changeset', changeset_id)
527   end
528
529   ##
530   # update an attribute in the node element
531   def xml_attr_rewrite(xml, name, value)
532     xml.find("//osm/node").first[name] = value.to_s
533     return xml
534   end
535
536   ##
537   # parse some xml
538   def xml_parse(xml)
539     parser = XML::Parser.string(xml)
540     parser.parse
541   end
542 end