1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'relation_controller'
4 # Re-raise errors caught by the controller.
5 class RelationController; def rescue_action(e) raise e end; end
7 class RelationControllerTest < Test::Unit::TestCase
9 fixtures :relations, :current_relations, :relation_members, :current_relation_members, :relation_tags, :current_relation_tags
10 set_fixture_class :current_relations => :Relation
11 set_fixture_class :relations => :OldRelation
14 @controller = RelationController.new
15 @request = ActionController::TestRequest.new
16 @response = ActionController::TestResponse.new
19 def basic_authorization(user, pass)
20 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
24 @request.env["RAW_POST_DATA"] = c
27 # -------------------------------------
28 # Test reading relations.
29 # -------------------------------------
32 # check that a visible relation is returned properly
33 get :read, :id => current_relations(:visible_relation).id
34 assert_response :success
36 # check that an invisible relation is not returned
37 get :read, :id => current_relations(:invisible_relation).id
40 # check chat a non-existent relation is not returned
42 assert_response :not_found
44 # check the "relations for node" mode
45 get :relations_for_node, :id => current_nodes(:node_used_by_relationship).id
46 assert_response :success
47 # FIXME check whether this contains the stuff we want!
48 # see the test_read in way_controller_test.rb for the assert_select
49 assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
50 assert_select "osm relation"
55 # check the "relations for way" mode
56 get :relations_for_way, :id => current_ways(:used_way).id
57 assert_response :success
58 # FIXME check whether this contains the stuff we want!
63 # check the "relations for relation" mode
64 get :relations_for_relation, :id => current_relations(:used_relation).id
65 assert_response :success
66 # FIXME check whether this contains the stuff we want!
71 # check the "full" mode
72 get :full, :id => current_relations(:visible_relation).id
73 assert_response :success
74 # FIXME check whether this contains the stuff we want!
80 # -------------------------------------
81 # Test simple relation creation.
82 # -------------------------------------
85 basic_authorization "test@openstreetmap.org", "test"
87 # FIXME create a new changeset and use the id that is returned for the next step
89 # create an relation without members
90 content "<osm><relation><tag k='test' v='yes' /></relation></osm>"
93 assert_response :success,
94 "relation upload did not return success status"
95 # read id of created relation and search for it
96 relationid = @response.body
97 checkrelation = Relation.find(relationid)
98 assert_not_nil checkrelation,
99 "uploaded relation not found in data base after upload"
101 assert_equal checkrelation.members.length, 0,
102 "saved relation contains members but should not"
103 assert_equal checkrelation.tags.length, 1,
104 "saved relation does not contain exactly one tag"
105 assert_equal users(:normal_user).id, checkrelation.changeset.user_id,
106 "saved relation does not belong to user that created it"
107 assert_equal true, checkrelation.visible,
108 "saved relation is not visible"
109 # ok the relation is there but can we also retrieve it?
110 get :read, :id => relationid
111 assert_response :success
114 # create an relation with a node as member
115 nid = current_nodes(:used_node_1).id
116 content "<osm><relation><member type='node' ref='#{nid}' role='some'/>" +
117 "<tag k='test' v='yes' /></relation></osm>"
120 assert_response :success,
121 "relation upload did not return success status"
122 # read id of created relation and search for it
123 relationid = @response.body
124 checkrelation = Relation.find(relationid)
125 assert_not_nil checkrelation,
126 "uploaded relation not found in data base after upload"
128 assert_equal checkrelation.members.length, 1,
129 "saved relation does not contain exactly one member"
130 assert_equal checkrelation.tags.length, 1,
131 "saved relation does not contain exactly one tag"
132 assert_equal users(:normal_user).id, checkrelation.user_id,
133 "saved relation does not belong to user that created it"
134 assert_equal true, checkrelation.visible,
135 "saved relation is not visible"
136 # ok the relation is there but can we also retrieve it?
138 get :read, :id => relationid
139 assert_response :success
141 # create an relation with a way and a node as members
142 nid = current_nodes(:used_node_1).id
143 wid = current_ways(:used_way).id
144 content "<osm><relation><member type='node' ref='#{nid}' role='some'/>" +
145 "<member type='way' ref='#{wid}' role='other'/>" +
146 "<tag k='test' v='yes' /></relation></osm>"
149 assert_response :success,
150 "relation upload did not return success status"
151 # read id of created relation and search for it
152 relationid = @response.body
153 checkrelation = Relation.find(relationid)
154 assert_not_nil checkrelation,
155 "uploaded relation not found in data base after upload"
157 assert_equal checkrelation.members.length, 2,
158 "saved relation does not have exactly two members"
159 assert_equal checkrelation.tags.length, 1,
160 "saved relation does not contain exactly one tag"
161 assert_equal users(:normal_user).id, checkrelation.user_id,
162 "saved relation does not belong to user that created it"
163 assert_equal true, checkrelation.visible,
164 "saved relation is not visible"
165 # ok the relation is there but can we also retrieve it?
166 get :read, :id => relationid
167 assert_response :success
171 # -------------------------------------
172 # Test creating some invalid relations.
173 # -------------------------------------
175 def test_create_invalid
176 basic_authorization "test@openstreetmap.org", "test"
178 # create a relation with non-existing node as member
179 content "<osm><relation><member type='node' ref='0'/><tag k='test' v='yes' /></relation></osm>"
182 assert_response :precondition_failed,
183 "relation upload with invalid node did not return 'precondition failed'"
186 # -------------------------------------
187 # Test deleting relations.
188 # -------------------------------------
193 # first try to delete relation without auth
194 delete :delete, :id => current_relations(:visible_relation).id
195 assert_response :unauthorized
198 basic_authorization("test@openstreetmap.org", "test");
201 delete :delete, :id => current_relations(:visible_relation).id
202 assert_response :success
204 # this won't work since the relation is already deleted
205 delete :delete, :id => current_relations(:invisible_relation).id
206 assert_response :gone
208 # this won't work since the relation never existed
209 delete :delete, :id => 0
210 assert_response :not_found