source: subversion/sites/rails_port_branches/api06/test/functional/node_controller_test.rb @ 12168

Revision 12168, 10.8 KB checked in by smsm1, 5 years ago (diff)

ensure that uploads that don't supply a lat and lon for a node. Adding related test and fixing other tests.

Line 
1require File.dirname(__FILE__) + '/../test_helper'
2require 'node_controller'
3
4class NodeControllerTest < ActionController::TestCase
5  api_fixtures
6
7  def test_create
8    # cannot read password from fixture as it is stored as MD5 digest
9    basic_authorization(users(:normal_user).email, "test")
10   
11    # create a node with random lat/lon
12    lat = rand(100)-50 + rand
13    lon = rand(100)-50 + rand
14    # normal user has a changeset open, so we'll use that.
15    changeset = changesets(:normal_user_first_change)
16    # create a minimal xml file
17    content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
18    put :create
19    # hope for success
20    assert_response :success, "node upload did not return success status"
21
22    # read id of created node and search for it
23    nodeid = @response.body
24    checknode = Node.find(nodeid)
25    assert_not_nil checknode, "uploaded node not found in data base after upload"
26    # compare values
27    assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
28    assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
29    assert_equal changesets(:normal_user_first_change).id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
30    assert_equal true, checknode.visible, "saved node is not visible"
31  end
32
33  def test_create_invalid_xml
34    # Initial setup
35    basic_authorization(users(:normal_user).email, "test")
36    # normal user has a changeset open, so we'll use that.
37    changeset = changesets(:normal_user_first_change)
38    lat = 3.434
39    lon = 3.23
40   
41    # test that the upload is rejected when no lat is supplied
42    # create a minimal xml file
43    content("<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>")
44    put :create
45    # hope for success
46    assert_response :bad_request, "node upload did not return bad_request status"
47    assert_equal 'Cannot parse valid node from xml string <node lon="3.23" changeset="1"/>. lat missing', @response.body
48
49    # test that the upload is rejected when no lon is supplied
50    # create a minimal xml file
51    content("<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>")
52    put :create
53    # hope for success
54    assert_response :bad_request, "node upload did not return bad_request status"
55    assert_equal 'Cannot parse valid node from xml string <node lat="3.434" changeset="1"/>. lon missing', @response.body
56
57  end
58
59  def test_read
60    # check that a visible node is returned properly
61    get :read, :id => current_nodes(:visible_node).id
62    assert_response :success
63
64    # check that an invisible node is not returned
65    get :read, :id => current_nodes(:invisible_node).id
66    assert_response :gone
67
68    # check chat a non-existent node is not returned
69    get :read, :id => 0
70    assert_response :not_found
71  end
72
73  # this tests deletion restrictions - basic deletion is tested in the unit
74  # tests for node!
75  def test_delete
76    # first try to delete node without auth
77    delete :delete, :id => current_nodes(:visible_node).id
78    assert_response :unauthorized
79
80    # now set auth
81    basic_authorization(users(:normal_user).email, "test"); 
82
83    # try to delete with an invalid (closed) changeset
84    content update_changeset(current_nodes(:visible_node).to_xml,
85                             changesets(:normal_user_closed_change).id)
86    delete :delete, :id => current_nodes(:visible_node).id
87    assert_response :conflict
88
89    # try to delete with an invalid (non-existent) changeset
90    content update_changeset(current_nodes(:visible_node).to_xml,0)
91    delete :delete, :id => current_nodes(:visible_node).id
92    assert_response :conflict
93
94    # valid delete now takes a payload
95    content(nodes(:visible_node).to_xml)
96    delete :delete, :id => current_nodes(:visible_node).id
97    assert_response :success
98
99    # valid delete should return the new version number, which should
100    # be greater than the old version number
101    assert @response.body.to_i > current_nodes(:visible_node).version,
102       "delete request should return a new version number for node"
103
104    # this won't work since the node is already deleted
105    content(nodes(:invisible_node).to_xml)
106    delete :delete, :id => current_nodes(:invisible_node).id
107    assert_response :gone
108
109    # this won't work since the node never existed
110    delete :delete, :id => 0
111    assert_response :not_found
112
113    ## these test whether nodes which are in-use can be deleted:
114    # in a way...
115    content(nodes(:used_node_1).to_xml)
116    delete :delete, :id => current_nodes(:used_node_1).id
117    assert_response :precondition_failed,
118       "shouldn't be able to delete a node used in a way (#{@response.body})"
119
120    # in a relation...
121    content(nodes(:node_used_by_relationship).to_xml)
122    delete :delete, :id => current_nodes(:node_used_by_relationship).id
123    assert_response :precondition_failed,
124       "shouldn't be able to delete a node used in a relation (#{@response.body})"
125  end
126
127  ##
128  # tests whether the API works and prevents incorrect use while trying
129  # to update nodes.
130  def test_update
131    # try and update a node without authorisation
132    # first try to delete node without auth
133    content current_nodes(:visible_node).to_xml
134    put :update, :id => current_nodes(:visible_node).id
135    assert_response :unauthorized
136   
137    # setup auth
138    basic_authorization(users(:normal_user).email, "test")
139
140    ## trying to break changesets
141
142    # try and update in someone else's changeset
143    content update_changeset(current_nodes(:visible_node).to_xml,
144                             changesets(:second_user_first_change).id)
145    put :update, :id => current_nodes(:visible_node).id
146    assert_response :conflict, "update with other user's changeset should be rejected"
147
148    # try and update in a closed changeset
149    content update_changeset(current_nodes(:visible_node).to_xml,
150                             changesets(:normal_user_closed_change).id)
151    put :update, :id => current_nodes(:visible_node).id
152    assert_response :conflict, "update with closed changeset should be rejected"
153
154    # try and update in a non-existant changeset
155    content update_changeset(current_nodes(:visible_node).to_xml, 0)
156    put :update, :id => current_nodes(:visible_node).id
157    assert_response :conflict, "update with changeset=0 should be rejected"
158
159    ## try and submit invalid updates
160    content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
161    put :update, :id => current_nodes(:visible_node).id
162    assert_response :bad_request, "node at lat=91 should be rejected"
163
164    content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
165    put :update, :id => current_nodes(:visible_node).id
166    assert_response :bad_request, "node at lat=-91 should be rejected"
167   
168    content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
169    put :update, :id => current_nodes(:visible_node).id
170    assert_response :bad_request, "node at lon=181 should be rejected"
171
172    content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
173    put :update, :id => current_nodes(:visible_node).id
174    assert_response :bad_request, "node at lon=-181 should be rejected"
175
176    ## next, attack the versioning
177    current_node_version = current_nodes(:visible_node).version
178
179    # try and submit a version behind
180    content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 
181                             'version', current_node_version - 1);
182    put :update, :id => current_nodes(:visible_node).id
183    assert_response :conflict, "should have failed on old version number"
184   
185    # try and submit a version ahead
186    content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 
187                             'version', current_node_version + 1);
188    put :update, :id => current_nodes(:visible_node).id
189    assert_response :conflict, "should have failed on skipped version number"
190
191    # try and submit total crap in the version field
192    content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 
193                             'version', 'p1r4t3s!');
194    put :update, :id => current_nodes(:visible_node).id
195    assert_response :conflict, 
196       "should not be able to put 'p1r4at3s!' in the version field"
197   
198    ## finally, produce a good request which should work
199    content current_nodes(:visible_node).to_xml
200    put :update, :id => current_nodes(:visible_node).id
201    assert_response :success, "a valid update request failed"
202  end
203
204  ##
205  # test adding tags to a node
206  def test_duplicate_tags
207    # setup auth
208    basic_authorization(users(:normal_user).email, "test")
209
210    # add an identical tag to the node
211    tag_xml = XML::Node.new("tag")
212    tag_xml['k'] = current_node_tags(:t1).k
213    tag_xml['v'] = current_node_tags(:t1).v
214
215    # add the tag into the existing xml
216    node_xml = current_nodes(:visible_node).to_xml
217    node_xml.find("//osm/node").first << tag_xml
218
219    # try and upload it
220    content node_xml
221    put :update, :id => current_nodes(:visible_node).id
222    assert_response :bad_request, 
223      "adding duplicate tags to a node should fail with 'bad request'"
224    assert_equal "Element node/#{current_nodes(:visible_node).id} has duplicate tags with key #{current_node_tags(:t1).k}.", @response.body
225  end
226
227  # test whether string injection is possible
228  def test_string_injection
229    basic_authorization(users(:normal_user).email, "test")
230    changeset_id = changesets(:normal_user_first_change).id
231
232    # try and put something into a string that the API might
233    # use unquoted and therefore allow code injection...
234    content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
235      '<tag k="#{@user.inspect}" v="0"/>' +
236      '</node></osm>'
237    put :create
238    assert_response :success
239    nodeid = @response.body
240
241    # find the node in the database
242    checknode = Node.find(nodeid)
243    assert_not_nil checknode, "node not found in data base after upload"
244   
245    # and grab it using the api
246    get :read, :id => nodeid
247    assert_response :success
248    apinode = Node.from_xml(@response.body)
249    assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
250   
251    # check the tags are not corrupted
252    assert_equal checknode.tags, apinode.tags
253    assert apinode.tags.include?('#{@user.inspect}')
254  end
255
256  def basic_authorization(user, pass)
257    @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
258  end
259
260  def content(c)
261    @request.env["RAW_POST_DATA"] = c.to_s
262  end
263
264  ##
265  # update the changeset_id of a node element
266  def update_changeset(xml, changeset_id)
267    xml_attr_rewrite(xml, 'changeset', changeset_id)
268  end
269
270  ##
271  # update an attribute in the node element
272  def xml_attr_rewrite(xml, name, value)
273    xml.find("//osm/node").first[name] = value.to_s
274    return xml
275  end
276
277  ##
278  # parse some xml
279  def xml_parse(xml)
280    parser = XML::Parser.new
281    parser.string = xml
282    parser.parse
283  end
284end
Note: See TracBrowser for help on using the repository browser.