]> git.openstreetmap.org Git - rails.git/blob - test/controllers/node_controller_test.rb
Don't send note comment notifications to deleted users
[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 => current_nodes(:visible_node).id
147     assert_response :success
148
149     # check that an invisible node is not returned
150     get :read, :id => current_nodes(:invisible_node).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     ## first try to delete node without auth
162     delete :delete, :id => current_nodes(:visible_node).id
163     assert_response :unauthorized
164
165     ## now set auth for the non-data public user
166     basic_authorization(users(:normal_user).email, "test")
167
168     # try to delete with an invalid (closed) changeset
169     content update_changeset(current_nodes(:visible_node).to_xml,
170                              changesets(:normal_user_closed_change).id)
171     delete :delete, :id => current_nodes(:visible_node).id
172     assert_require_public_data("non-public user shouldn't be able to delete node")
173
174     # try to delete with an invalid (non-existent) changeset
175     content update_changeset(current_nodes(:visible_node).to_xml, 0)
176     delete :delete, :id => current_nodes(:visible_node).id
177     assert_require_public_data("shouldn't be able to delete node, when user's data is private")
178
179     # valid delete now takes a payload
180     content(nodes(:visible_node).to_xml)
181     delete :delete, :id => current_nodes(:visible_node).id
182     assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
183
184     # this won't work since the node is already deleted
185     content(nodes(:invisible_node).to_xml)
186     delete :delete, :id => current_nodes(:invisible_node).id
187     assert_require_public_data
188
189     # this won't work since the node never existed
190     delete :delete, :id => 0
191     assert_require_public_data
192
193     ## these test whether nodes which are in-use can be deleted:
194     # in a way...
195     content(nodes(:used_node_1).to_xml)
196     delete :delete, :id => current_nodes(:used_node_1).id
197     assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
198
199     # in a relation...
200     content(nodes(:node_used_by_relationship).to_xml)
201     delete :delete, :id => current_nodes(:node_used_by_relationship).id
202     assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
203
204     ## now set auth for the public data user
205     basic_authorization(users(:public_user).email, "test")
206
207     # try to delete with an invalid (closed) changeset
208     content update_changeset(current_nodes(:visible_node).to_xml,
209                              changesets(:normal_user_closed_change).id)
210     delete :delete, :id => current_nodes(:visible_node).id
211     assert_response :conflict
212
213     # try to delete with an invalid (non-existent) changeset
214     content update_changeset(current_nodes(:visible_node).to_xml, 0)
215     delete :delete, :id => current_nodes(:visible_node).id
216     assert_response :conflict
217
218     # try to delete a node with a different ID
219     content(nodes(:public_visible_node).to_xml)
220     delete :delete, :id => current_nodes(:visible_node).id
221     assert_response :bad_request,
222                     "should not be able to delete a node with a different ID from the XML"
223
224     # try to delete a node rubbish in the payloads
225     content("<delete/>")
226     delete :delete, :id => current_nodes(:visible_node).id
227     assert_response :bad_request,
228                     "should not be able to delete a node without a valid XML payload"
229
230     # valid delete now takes a payload
231     content(nodes(:public_visible_node).to_xml)
232     delete :delete, :id => current_nodes(:public_visible_node).id
233     assert_response :success
234
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_nodes(:public_visible_node).version,
238            "delete request should return a new version number for node"
239
240     # deleting the same node twice doesn't work
241     content(nodes(:public_visible_node).to_xml)
242     delete :delete, :id => current_nodes(:public_visible_node).id
243     assert_response :gone
244
245     # this won't work since the node never existed
246     delete :delete, :id => 0
247     assert_response :not_found
248
249     ## these test whether nodes which are in-use can be deleted:
250     # in a way...
251     content(nodes(:used_node_1).to_xml)
252     delete :delete, :id => current_nodes(:used_node_1).id
253     assert_response :precondition_failed,
254                     "shouldn't be able to delete a node used in a way (#{@response.body})"
255     assert_equal "Precondition failed: Node 3 is still used by ways 1,3.", @response.body
256
257     # in a relation...
258     content(nodes(:node_used_by_relationship).to_xml)
259     delete :delete, :id => current_nodes(:node_used_by_relationship).id
260     assert_response :precondition_failed,
261                     "shouldn't be able to delete a node used in a relation (#{@response.body})"
262     assert_equal "Precondition failed: Node 5 is still used by relations 1,3.", @response.body
263   end
264
265   ##
266   # tests whether the API works and prevents incorrect use while trying
267   # to update nodes.
268   def test_update
269     ## First test with no user credentials
270     # try and update a node without authorisation
271     # first try to delete node without auth
272     content current_nodes(:visible_node).to_xml
273     put :update, :id => current_nodes(:visible_node).id
274     assert_response :unauthorized
275
276     ## Second test with the private user
277
278     # setup auth
279     basic_authorization(users(:normal_user).email, "test")
280
281     ## trying to break changesets
282
283     # try and update in someone else's changeset
284     content update_changeset(current_nodes(:visible_node).to_xml,
285                              changesets(:public_user_first_change).id)
286     put :update, :id => current_nodes(:visible_node).id
287     assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
288
289     # try and update in a closed changeset
290     content update_changeset(current_nodes(:visible_node).to_xml,
291                              changesets(:normal_user_closed_change).id)
292     put :update, :id => current_nodes(:visible_node).id
293     assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
294
295     # try and update in a non-existant changeset
296     content update_changeset(current_nodes(:visible_node).to_xml, 0)
297     put :update, :id => current_nodes(:visible_node).id
298     assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
299
300     ## try and submit invalid updates
301     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", 91.0)
302     put :update, :id => current_nodes(:visible_node).id
303     assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
304
305     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", -91.0)
306     put :update, :id => current_nodes(:visible_node).id
307     assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
308
309     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", 181.0)
310     put :update, :id => current_nodes(:visible_node).id
311     assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
312
313     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", -181.0)
314     put :update, :id => current_nodes(:visible_node).id
315     assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
316
317     ## finally, produce a good request which should work
318     content current_nodes(:visible_node).to_xml
319     put :update, :id => current_nodes(:visible_node).id
320     assert_require_public_data "should have failed with a forbidden when data isn't public"
321
322     ## Finally test with the public user
323
324     # try and update a node without authorisation
325     # first try to delete node without auth
326     content current_nodes(:visible_node).to_xml
327     put :update, :id => current_nodes(:visible_node).id
328     assert_response :forbidden
329
330     # setup auth
331     basic_authorization(users(:public_user).email, "test")
332
333     ## trying to break changesets
334
335     # try and update in someone else's changeset
336     content update_changeset(current_nodes(:visible_node).to_xml,
337                              changesets(:normal_user_first_change).id)
338     put :update, :id => current_nodes(:visible_node).id
339     assert_response :conflict, "update with other user's changeset should be rejected"
340
341     # try and update in a closed changeset
342     content update_changeset(current_nodes(:visible_node).to_xml,
343                              changesets(:normal_user_closed_change).id)
344     put :update, :id => current_nodes(:visible_node).id
345     assert_response :conflict, "update with closed changeset should be rejected"
346
347     # try and update in a non-existant changeset
348     content update_changeset(current_nodes(:visible_node).to_xml, 0)
349     put :update, :id => current_nodes(:visible_node).id
350     assert_response :conflict, "update with changeset=0 should be rejected"
351
352     ## try and submit invalid updates
353     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", 91.0)
354     put :update, :id => current_nodes(:visible_node).id
355     assert_response :bad_request, "node at lat=91 should be rejected"
356
357     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", -91.0)
358     put :update, :id => current_nodes(:visible_node).id
359     assert_response :bad_request, "node at lat=-91 should be rejected"
360
361     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", 181.0)
362     put :update, :id => current_nodes(:visible_node).id
363     assert_response :bad_request, "node at lon=181 should be rejected"
364
365     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", -181.0)
366     put :update, :id => current_nodes(:visible_node).id
367     assert_response :bad_request, "node at lon=-181 should be rejected"
368
369     ## next, attack the versioning
370     current_node_version = current_nodes(:visible_node).version
371
372     # try and submit a version behind
373     content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
374                              "version", current_node_version - 1)
375     put :update, :id => current_nodes(:visible_node).id
376     assert_response :conflict, "should have failed on old version number"
377
378     # try and submit a version ahead
379     content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
380                              "version", current_node_version + 1)
381     put :update, :id => current_nodes(:visible_node).id
382     assert_response :conflict, "should have failed on skipped version number"
383
384     # try and submit total crap in the version field
385     content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
386                              "version", "p1r4t3s!")
387     put :update, :id => current_nodes(:visible_node).id
388     assert_response :conflict,
389                     "should not be able to put 'p1r4at3s!' in the version field"
390
391     ## try an update with the wrong ID
392     content current_nodes(:public_visible_node).to_xml
393     put :update, :id => current_nodes(:visible_node).id
394     assert_response :bad_request,
395                     "should not be able to update a node with a different ID from the XML"
396
397     ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
398     content "<update/>"
399     put :update, :id => current_nodes(:visible_node).id
400     assert_response :bad_request,
401                     "should not be able to update a node with non-OSM XML doc."
402
403     ## finally, produce a good request which should work
404     content current_nodes(:public_visible_node).to_xml
405     put :update, :id => current_nodes(:public_visible_node).id
406     assert_response :success, "a valid update request failed"
407   end
408
409   ##
410   # test fetching multiple nodes
411   def test_nodes
412     # check error when no parameter provided
413     get :nodes
414     assert_response :bad_request
415
416     # check error when no parameter value provided
417     get :nodes, :nodes => ""
418     assert_response :bad_request
419
420     # test a working call
421     get :nodes, :nodes => "1,2,4,15,17"
422     assert_response :success
423     assert_select "osm" do
424       assert_select "node", :count => 5
425       assert_select "node[id='1'][visible='true']", :count => 1
426       assert_select "node[id='2'][visible='false']", :count => 1
427       assert_select "node[id='4'][visible='true']", :count => 1
428       assert_select "node[id='15'][visible='true']", :count => 1
429       assert_select "node[id='17'][visible='false']", :count => 1
430     end
431
432     # check error when a non-existent node is included
433     get :nodes, :nodes => "1,2,4,15,17,400"
434     assert_response :not_found
435   end
436
437   ##
438   # test adding tags to a node
439   def test_duplicate_tags
440     # setup auth
441     basic_authorization(users(:public_user).email, "test")
442
443     # add an identical tag to the node
444     tag_xml = XML::Node.new("tag")
445     tag_xml["k"] = current_node_tags(:public_v_t1).k
446     tag_xml["v"] = current_node_tags(:public_v_t1).v
447
448     # add the tag into the existing xml
449     node_xml = current_nodes(:public_visible_node).to_xml
450     node_xml.find("//osm/node").first << tag_xml
451
452     # try and upload it
453     content node_xml
454     put :update, :id => current_nodes(:public_visible_node).id
455     assert_response :bad_request,
456                     "adding duplicate tags to a node should fail with 'bad request'"
457     assert_equal "Element node/#{current_nodes(:public_visible_node).id} has duplicate tags with key #{current_node_tags(:t1).k}", @response.body
458   end
459
460   # test whether string injection is possible
461   def test_string_injection
462     ## First try with the non-data public user
463     basic_authorization(users(:normal_user).email, "test")
464     changeset_id = changesets(:normal_user_first_change).id
465
466     # try and put something into a string that the API might
467     # use unquoted and therefore allow code injection...
468     content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
469       '<tag k="#{@user.inspect}" v="0"/>' +
470       "</node></osm>"
471     put :create
472     assert_require_public_data "Shouldn't be able to create with non-public user"
473
474     ## Then try with the public data user
475     basic_authorization(users(:public_user).email, "test")
476     changeset_id = changesets(:public_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_response :success
485     nodeid = @response.body
486
487     # find the node in the database
488     checknode = Node.find(nodeid)
489     assert_not_nil checknode, "node not found in data base after upload"
490
491     # and grab it using the api
492     get :read, :id => nodeid
493     assert_response :success
494     apinode = Node.from_xml(@response.body)
495     assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
496
497     # check the tags are not corrupted
498     assert_equal checknode.tags, apinode.tags
499     assert apinode.tags.include?("\#{@user.inspect}")
500   end
501
502   ##
503   # update the changeset_id of a node element
504   def update_changeset(xml, changeset_id)
505     xml_attr_rewrite(xml, "changeset", changeset_id)
506   end
507
508   ##
509   # update an attribute in the node element
510   def xml_attr_rewrite(xml, name, value)
511     xml.find("//osm/node").first[name] = value.to_s
512     xml
513   end
514
515   ##
516   # parse some xml
517   def xml_parse(xml)
518     parser = XML::Parser.string(xml)
519     parser.parse
520   end
521 end