1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'relation_controller'
4 class RelationControllerTest < ActionController::TestCase
7 def basic_authorization(user, pass)
8 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
12 @request.env["RAW_POST_DATA"] = c.to_s
15 # -------------------------------------
16 # Test reading relations.
17 # -------------------------------------
20 # check that a visible relation is returned properly
21 get :read, :id => current_relations(:visible_relation).id
22 assert_response :success
24 # check that an invisible relation is not returned
25 get :read, :id => current_relations(:invisible_relation).id
28 # check chat a non-existent relation is not returned
30 assert_response :not_found
34 # check that all relations containing a particular node, and no extra
35 # relations, are returned from the relations_for_node call.
36 def test_relations_for_node
37 check_relations_for_element(:relations_for_node, "node",
38 current_nodes(:node_used_by_relationship).id,
39 [ :visible_relation, :used_relation ])
42 def test_relations_for_way
43 check_relations_for_element(:relations_for_way, "way",
44 current_ways(:used_way).id,
45 [ :visible_relation ])
48 def test_relations_for_relation
49 check_relations_for_element(:relations_for_relation, "relation",
50 current_relations(:used_relation).id,
51 [ :visible_relation ])
54 def check_relations_for_element(method, type, id, expected_relations)
55 # check the "relations for relation" mode
57 assert_response :success
59 # count one osm element
60 assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
62 # we should have only the expected number of relations
63 assert_select "osm>relation", expected_relations.size
65 # and each of them should contain the node we originally searched for
66 expected_relations.each do |r|
67 relation_id = current_relations(r).id
68 assert_select "osm>relation#?", relation_id
69 assert_select "osm>relation#?>member[type=\"#{type}\"][ref=#{id}]", relation_id
74 # check the "full" mode
75 get :full, :id => current_relations(:visible_relation).id
76 assert_response :success
77 # FIXME check whether this contains the stuff we want!
83 # -------------------------------------
84 # Test simple relation creation.
85 # -------------------------------------
88 basic_authorization "test@openstreetmap.org", "test"
90 # put the relation in a dummy fixture changset
91 changeset_id = changesets(:normal_user_first_change).id
93 # create an relation without members
94 content "<osm><relation changeset='#{changeset_id}'><tag k='test' v='yes' /></relation></osm>"
97 assert_response :success,
98 "relation upload did not return success status"
99 # read id of created relation and search for it
100 relationid = @response.body
101 checkrelation = Relation.find(relationid)
102 assert_not_nil checkrelation,
103 "uploaded relation not found in data base after upload"
105 assert_equal checkrelation.members.length, 0,
106 "saved relation contains members but should not"
107 assert_equal checkrelation.tags.length, 1,
108 "saved relation does not contain exactly one tag"
109 assert_equal changeset_id, checkrelation.changeset.id,
110 "saved relation does not belong in the changeset it was assigned to"
111 assert_equal users(:normal_user).id, checkrelation.changeset.user_id,
112 "saved relation does not belong to user that created it"
113 assert_equal true, checkrelation.visible,
114 "saved relation is not visible"
115 # ok the relation is there but can we also retrieve it?
116 get :read, :id => relationid
117 assert_response :success
120 # create an relation with a node as member
121 nid = current_nodes(:used_node_1).id
122 content "<osm><relation changeset='#{changeset_id}'>" +
123 "<member type='node' ref='#{nid}' role='some'/>" +
124 "<tag k='test' v='yes' /></relation></osm>"
127 assert_response :success,
128 "relation upload did not return success status"
129 # read id of created relation and search for it
130 relationid = @response.body
131 checkrelation = Relation.find(relationid)
132 assert_not_nil checkrelation,
133 "uploaded relation not found in data base after upload"
135 assert_equal checkrelation.members.length, 1,
136 "saved relation does not contain exactly one member"
137 assert_equal checkrelation.tags.length, 1,
138 "saved relation does not contain exactly one tag"
139 assert_equal changeset_id, checkrelation.changeset.id,
140 "saved relation does not belong in the changeset it was assigned to"
141 assert_equal users(:normal_user).id, checkrelation.changeset.user_id,
142 "saved relation does not belong to user that created it"
143 assert_equal true, checkrelation.visible,
144 "saved relation is not visible"
145 # ok the relation is there but can we also retrieve it?
147 get :read, :id => relationid
148 assert_response :success
150 # create an relation with a way and a node as members
151 nid = current_nodes(:used_node_1).id
152 wid = current_ways(:used_way).id
153 content "<osm><relation changeset='#{changeset_id}'>" +
154 "<member type='node' ref='#{nid}' role='some'/>" +
155 "<member type='way' ref='#{wid}' role='other'/>" +
156 "<tag k='test' v='yes' /></relation></osm>"
159 assert_response :success,
160 "relation upload did not return success status"
161 # read id of created relation and search for it
162 relationid = @response.body
163 checkrelation = Relation.find(relationid)
164 assert_not_nil checkrelation,
165 "uploaded relation not found in data base after upload"
167 assert_equal checkrelation.members.length, 2,
168 "saved relation does not have exactly two members"
169 assert_equal checkrelation.tags.length, 1,
170 "saved relation does not contain exactly one tag"
171 assert_equal changeset_id, checkrelation.changeset.id,
172 "saved relation does not belong in the changeset it was assigned to"
173 assert_equal users(:normal_user).id, checkrelation.changeset.user_id,
174 "saved relation does not belong to user that created it"
175 assert_equal true, checkrelation.visible,
176 "saved relation is not visible"
177 # ok the relation is there but can we also retrieve it?
178 get :read, :id => relationid
179 assert_response :success
183 # -------------------------------------
184 # Test creating some invalid relations.
185 # -------------------------------------
187 def test_create_invalid
188 basic_authorization "test@openstreetmap.org", "test"
190 # put the relation in a dummy fixture changset
191 changeset_id = changesets(:normal_user_first_change).id
193 # create a relation with non-existing node as member
194 content "<osm><relation changeset='#{changeset_id}'>" +
195 "<member type='node' ref='0'/><tag k='test' v='yes' />" +
199 assert_response :precondition_failed,
200 "relation upload with invalid node did not return 'precondition failed'"
203 # -------------------------------------
204 # Test deleting relations.
205 # -------------------------------------
208 # first try to delete relation without auth
209 delete :delete, :id => current_relations(:visible_relation).id
210 assert_response :unauthorized
213 basic_authorization("test@openstreetmap.org", "test");
215 # this shouldn't work, as we should need the payload...
216 delete :delete, :id => current_relations(:visible_relation).id
217 assert_response :bad_request
219 # try to delete without specifying a changeset
220 content "<osm><relation id='#{current_relations(:visible_relation).id}'/></osm>"
221 delete :delete, :id => current_relations(:visible_relation).id
222 assert_response :conflict
224 # try to delete with an invalid (closed) changeset
225 content update_changeset(current_relations(:visible_relation).to_xml,
226 changesets(:normal_user_closed_change).id)
227 delete :delete, :id => current_relations(:visible_relation).id
228 assert_response :conflict
230 # try to delete with an invalid (non-existent) changeset
231 content update_changeset(current_relations(:visible_relation).to_xml,0)
232 delete :delete, :id => current_relations(:visible_relation).id
233 assert_response :conflict
235 # this won't work because the relation is in-use by another relation
236 content(relations(:used_relation).to_xml)
237 delete :delete, :id => current_relations(:used_relation).id
238 assert_response :precondition_failed,
239 "shouldn't be able to delete a relation used in a relation (#{@response.body})"
241 # this should work when we provide the appropriate payload...
242 content(relations(:visible_relation).to_xml)
243 delete :delete, :id => current_relations(:visible_relation).id
244 assert_response :success
246 # valid delete should return the new version number, which should
247 # be greater than the old version number
248 assert @response.body.to_i > current_relations(:visible_relation).version,
249 "delete request should return a new version number for relation"
251 # this won't work since the relation is already deleted
252 content(relations(:invisible_relation).to_xml)
253 delete :delete, :id => current_relations(:invisible_relation).id
254 assert_response :gone
256 # this works now because the relation which was using this one
258 content(relations(:used_relation).to_xml)
259 delete :delete, :id => current_relations(:used_relation).id
260 assert_response :success,
261 "should be able to delete a relation used in an old relation (#{@response.body})"
263 # this won't work since the relation never existed
264 delete :delete, :id => 0
265 assert_response :not_found
269 # update the changeset_id of a node element
270 def update_changeset(xml, changeset_id)
271 xml_attr_rewrite(xml, 'changeset', changeset_id)
275 # update an attribute in the node element
276 def xml_attr_rewrite(xml, name, value)
277 xml.find("//osm/relation").first[name] = value.to_s
284 parser = XML::Parser.new