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