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!
52 # check the "relations for way" mode
53 get :relations_for_way, :id => current_ways(:used_way).id
54 assert_response :success
55 # FIXME check whether this contains the stuff we want!
60 # check the "relations for relation" mode
61 get :relations_for_relation, :id => current_relations(:used_relation).id
62 assert_response :success
63 # FIXME check whether this contains the stuff we want!
68 # check the "full" mode
69 get :full, :id => current_relations(:visible_relation).id
70 assert_response :success
71 # FIXME check whether this contains the stuff we want!
77 # -------------------------------------
78 # Test simple relation creation.
79 # -------------------------------------
82 basic_authorization "test@openstreetmap.org", "test"
84 # create an relation without members
85 content "<osm><relation><tag k='test' v='yes' /></relation></osm>"
88 assert_response :success,
89 "relation upload did not return success status"
90 # read id of created relation and search for it
91 relationid = @response.body
92 checkrelation = Relation.find(relationid)
93 assert_not_nil checkrelation,
94 "uploaded relation not found in data base after upload"
96 assert_equal checkrelation.members.length, 0,
97 "saved relation contains members but should not"
98 assert_equal checkrelation.tags.length, 1,
99 "saved relation does not contain exactly one tag"
100 assert_equal users(:normal_user).id, checkrelation.user_id,
101 "saved relation does not belong to user that created it"
102 assert_equal true, checkrelation.visible,
103 "saved relation is not visible"
104 # ok the relation is there but can we also retrieve it?
105 get :read, :id => relationid
106 assert_response :success
109 # create an relation with a node as member
110 nid = current_nodes(:used_node_1).id
111 content "<osm><relation><member type='node' ref='#{nid}' role='some'/>" +
112 "<tag k='test' v='yes' /></relation></osm>"
115 assert_response :success,
116 "relation upload did not return success status"
117 # read id of created relation and search for it
118 relationid = @response.body
119 checkrelation = Relation.find(relationid)
120 assert_not_nil checkrelation,
121 "uploaded relation not found in data base after upload"
123 assert_equal checkrelation.members.length, 1,
124 "saved relation does not contain exactly one member"
125 assert_equal checkrelation.tags.length, 1,
126 "saved relation does not contain exactly one tag"
127 assert_equal users(:normal_user).id, checkrelation.user_id,
128 "saved relation does not belong to user that created it"
129 assert_equal true, checkrelation.visible,
130 "saved relation is not visible"
131 # ok the relation is there but can we also retrieve it?
133 get :read, :id => relationid
134 assert_response :success
136 # create an relation with a way and a node as members
137 nid = current_nodes(:used_node_1).id
138 wid = current_ways(:used_way).id
139 content "<osm><relation><member type='node' ref='#{nid}' role='some'/>" +
140 "<member type='way' ref='#{wid}' role='other'/>" +
141 "<tag k='test' v='yes' /></relation></osm>"
144 assert_response :success,
145 "relation upload did not return success status"
146 # read id of created relation and search for it
147 relationid = @response.body
148 checkrelation = Relation.find(relationid)
149 assert_not_nil checkrelation,
150 "uploaded relation not found in data base after upload"
152 assert_equal checkrelation.members.length, 2,
153 "saved relation does not have exactly two members"
154 assert_equal checkrelation.tags.length, 1,
155 "saved relation does not contain exactly one tag"
156 assert_equal users(:normal_user).id, checkrelation.user_id,
157 "saved relation does not belong to user that created it"
158 assert_equal true, checkrelation.visible,
159 "saved relation is not visible"
160 # ok the relation is there but can we also retrieve it?
161 get :read, :id => relationid
162 assert_response :success
166 # -------------------------------------
167 # Test creating some invalid relations.
168 # -------------------------------------
170 def test_create_invalid
171 basic_authorization "test@openstreetmap.org", "test"
173 # create a relation with non-existing node as member
174 content "<osm><relation><member type='node' ref='0'/><tag k='test' v='yes' /></relation></osm>"
177 assert_response :precondition_failed,
178 "relation upload with invalid node did not return 'precondition failed'"
181 # -------------------------------------
182 # Test deleting relations.
183 # -------------------------------------
188 # first try to delete relation without auth
189 delete :delete, :id => current_relations(:visible_relation).id
190 assert_response :unauthorized
193 basic_authorization("test@openstreetmap.org", "test");
196 delete :delete, :id => current_relations(:visible_relation).id
197 assert_response :success
199 # this won't work since the relation is already deleted
200 delete :delete, :id => current_relations(:invisible_relation).id
201 assert_response :gone
203 # this won't work since the relation never existed
204 delete :delete, :id => 0
205 assert_response :not_found