]> git.openstreetmap.org Git - rails.git/blob - test/controllers/node_controller_test.rb
Convert the duplicate_tags node controller test 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     content current_nodes(:visible_node).to_xml
296     put :update, :id => current_nodes(:visible_node).id
297     assert_response :unauthorized
298
299     ## Second test with the private user
300
301     # setup auth
302     basic_authorization(users(:normal_user).email, "test")
303
304     ## trying to break changesets
305
306     # try and update in someone else's changeset
307     content update_changeset(current_nodes(:visible_node).to_xml,
308                              changesets(:public_user_first_change).id)
309     put :update, :id => current_nodes(:visible_node).id
310     assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
311
312     # try and update in a closed changeset
313     content update_changeset(current_nodes(:visible_node).to_xml,
314                              changesets(:normal_user_closed_change).id)
315     put :update, :id => current_nodes(:visible_node).id
316     assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
317
318     # try and update in a non-existant changeset
319     content update_changeset(current_nodes(:visible_node).to_xml, 0)
320     put :update, :id => current_nodes(:visible_node).id
321     assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
322
323     ## try and submit invalid updates
324     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", 91.0)
325     put :update, :id => current_nodes(:visible_node).id
326     assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
327
328     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", -91.0)
329     put :update, :id => current_nodes(:visible_node).id
330     assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
331
332     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", 181.0)
333     put :update, :id => current_nodes(:visible_node).id
334     assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
335
336     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", -181.0)
337     put :update, :id => current_nodes(:visible_node).id
338     assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
339
340     ## finally, produce a good request which should work
341     content current_nodes(:visible_node).to_xml
342     put :update, :id => current_nodes(:visible_node).id
343     assert_require_public_data "should have failed with a forbidden when data isn't public"
344
345     ## Finally test with the public user
346
347     # try and update a node without authorisation
348     # first try to delete node without auth
349     content current_nodes(:visible_node).to_xml
350     put :update, :id => current_nodes(:visible_node).id
351     assert_response :forbidden
352
353     # setup auth
354     basic_authorization(users(:public_user).email, "test")
355
356     ## trying to break changesets
357
358     # try and update in someone else's changeset
359     content update_changeset(current_nodes(:visible_node).to_xml,
360                              changesets(:normal_user_first_change).id)
361     put :update, :id => current_nodes(:visible_node).id
362     assert_response :conflict, "update with other user's changeset should be rejected"
363
364     # try and update in a closed changeset
365     content update_changeset(current_nodes(:visible_node).to_xml,
366                              changesets(:normal_user_closed_change).id)
367     put :update, :id => current_nodes(:visible_node).id
368     assert_response :conflict, "update with closed changeset should be rejected"
369
370     # try and update in a non-existant changeset
371     content update_changeset(current_nodes(:visible_node).to_xml, 0)
372     put :update, :id => current_nodes(:visible_node).id
373     assert_response :conflict, "update with changeset=0 should be rejected"
374
375     ## try and submit invalid updates
376     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", 91.0)
377     put :update, :id => current_nodes(:visible_node).id
378     assert_response :bad_request, "node at lat=91 should be rejected"
379
380     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lat", -91.0)
381     put :update, :id => current_nodes(:visible_node).id
382     assert_response :bad_request, "node at lat=-91 should be rejected"
383
384     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", 181.0)
385     put :update, :id => current_nodes(:visible_node).id
386     assert_response :bad_request, "node at lon=181 should be rejected"
387
388     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, "lon", -181.0)
389     put :update, :id => current_nodes(:visible_node).id
390     assert_response :bad_request, "node at lon=-181 should be rejected"
391
392     ## next, attack the versioning
393     current_node_version = current_nodes(:visible_node).version
394
395     # try and submit a version behind
396     content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
397                              "version", current_node_version - 1)
398     put :update, :id => current_nodes(:visible_node).id
399     assert_response :conflict, "should have failed on old version number"
400
401     # try and submit a version ahead
402     content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
403                              "version", current_node_version + 1)
404     put :update, :id => current_nodes(:visible_node).id
405     assert_response :conflict, "should have failed on skipped version number"
406
407     # try and submit total crap in the version field
408     content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
409                              "version", "p1r4t3s!")
410     put :update, :id => current_nodes(:visible_node).id
411     assert_response :conflict,
412                     "should not be able to put 'p1r4at3s!' in the version field"
413
414     ## try an update with the wrong ID
415     content current_nodes(:public_visible_node).to_xml
416     put :update, :id => current_nodes(:visible_node).id
417     assert_response :bad_request,
418                     "should not be able to update a node with a different ID from the XML"
419
420     ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
421     content "<update/>"
422     put :update, :id => current_nodes(:visible_node).id
423     assert_response :bad_request,
424                     "should not be able to update a node with non-OSM XML doc."
425
426     ## finally, produce a good request which should work
427     content current_nodes(:public_visible_node).to_xml
428     put :update, :id => current_nodes(:public_visible_node).id
429     assert_response :success, "a valid update request failed"
430   end
431
432   ##
433   # test fetching multiple nodes
434   def test_nodes
435     # check error when no parameter provided
436     get :nodes
437     assert_response :bad_request
438
439     # check error when no parameter value provided
440     get :nodes, :nodes => ""
441     assert_response :bad_request
442
443     # test a working call
444     get :nodes, :nodes => "1,2,4,15,17"
445     assert_response :success
446     assert_select "osm" do
447       assert_select "node", :count => 5
448       assert_select "node[id='1'][visible='true']", :count => 1
449       assert_select "node[id='2'][visible='false']", :count => 1
450       assert_select "node[id='4'][visible='true']", :count => 1
451       assert_select "node[id='15'][visible='true']", :count => 1
452       assert_select "node[id='17'][visible='false']", :count => 1
453     end
454
455     # check error when a non-existent node is included
456     get :nodes, :nodes => "1,2,4,15,17,400"
457     assert_response :not_found
458   end
459
460   ##
461   # test adding tags to a node
462   def test_duplicate_tags
463     existing_tag = create(:node_tag)
464     assert_equal true, existing_tag.node.changeset.user.data_public
465     # setup auth
466     basic_authorization(existing_tag.node.changeset.user.email, "test")
467
468     # add an identical tag to the node
469     tag_xml = XML::Node.new("tag")
470     tag_xml["k"] = existing_tag.k
471     tag_xml["v"] = existing_tag.v
472
473     # add the tag into the existing xml
474     node_xml = existing_tag.node.to_xml
475     node_xml.find("//osm/node").first << tag_xml
476
477     # try and upload it
478     content node_xml
479     put :update, :id => existing_tag.node.id
480     assert_response :bad_request,
481                     "adding duplicate tags to a node should fail with 'bad request'"
482     assert_equal "Element node/#{existing_tag.node.id} has duplicate tags with key #{existing_tag.k}", @response.body
483   end
484
485   # test whether string injection is possible
486   def test_string_injection
487     ## First try with the non-data public user
488     basic_authorization(users(:normal_user).email, "test")
489     changeset_id = changesets(:normal_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_require_public_data "Shouldn't be able to create with non-public user"
498
499     ## Then try with the public data user
500     basic_authorization(users(:public_user).email, "test")
501     changeset_id = changesets(:public_user_first_change).id
502
503     # try and put something into a string that the API might
504     # use unquoted and therefore allow code injection...
505     content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
506             '<tag k="#{@user.inspect}" v="0"/>' +
507             "</node></osm>"
508     put :create
509     assert_response :success
510     nodeid = @response.body
511
512     # find the node in the database
513     checknode = Node.find(nodeid)
514     assert_not_nil checknode, "node not found in data base after upload"
515
516     # and grab it using the api
517     get :read, :id => nodeid
518     assert_response :success
519     apinode = Node.from_xml(@response.body)
520     assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
521
522     # check the tags are not corrupted
523     assert_equal checknode.tags, apinode.tags
524     assert apinode.tags.include?("\#{@user.inspect}")
525   end
526
527   ##
528   # update the changeset_id of a node element
529   def update_changeset(xml, changeset_id)
530     xml_attr_rewrite(xml, "changeset", changeset_id)
531   end
532
533   ##
534   # update an attribute in the node element
535   def xml_attr_rewrite(xml, name, value)
536     xml.find("//osm/node").first[name] = value.to_s
537     xml
538   end
539
540   ##
541   # parse some xml
542   def xml_parse(xml)
543     parser = XML::Parser.string(xml)
544     parser.parse
545   end
546 end