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