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