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
45 # -------------------------------------
46 # Test simple relation creation.
47 # -------------------------------------
50 basic_authorization "test@openstreetmap.org", "test"
52 # create an relation without members
53 content "<osm><relation><tag k='test' v='yes' /></relation></osm>"
56 assert_response :success,
57 "relation upload did not return success status"
58 # read id of created relation and search for it
59 relationid = @response.body
60 checkrelation = Relation.find(relationid)
61 assert_not_nil checkrelation,
62 "uploaded relation not found in data base after upload"
64 assert_equal checkrelation.members.length, 0,
65 "saved relation contains members but should not"
66 assert_equal checkrelation.tags.length, 1,
67 "saved relation does not contain exactly one tag"
68 assert_equal users(:normal_user).id, checkrelation.user_id,
69 "saved relation does not belong to user that created it"
70 assert_equal true, checkrelation.visible,
71 "saved relation is not visible"
72 # ok the relation is there but can we also retrieve it?
73 get :read, :id => relationid
74 assert_response :success
77 # create an relation with a node as member
78 nid = current_nodes(:used_node_1).id
79 content "<osm><relation><member type='node' ref='#{nid}' role='some'/>" +
80 "<tag k='test' v='yes' /></relation></osm>"
83 assert_response :success,
84 "relation upload did not return success status"
85 # read id of created relation and search for it
86 relationid = @response.body
87 checkrelation = Relation.find(relationid)
88 assert_not_nil checkrelation,
89 "uploaded relation not found in data base after upload"
91 assert_equal checkrelation.members.length, 1,
92 "saved relation does not contain exactly one member"
93 assert_equal checkrelation.tags.length, 1,
94 "saved relation does not contain exactly one tag"
95 assert_equal users(:normal_user).id, checkrelation.user_id,
96 "saved relation does not belong to user that created it"
97 assert_equal true, checkrelation.visible,
98 "saved relation is not visible"
99 # ok the relation is there but can we also retrieve it?
101 get :read, :id => relationid
102 assert_response :success
104 # create an relation with a way and a node as members
105 nid = current_nodes(:used_node_1).id
106 wid = current_ways(:used_way).id
107 content "<osm><relation><member type='node' ref='#{nid}' role='some'/>" +
108 "<member type='way' ref='#{wid}' role='other'/>" +
109 "<tag k='test' v='yes' /></relation></osm>"
112 assert_response :success,
113 "relation upload did not return success status"
114 # read id of created relation and search for it
115 relationid = @response.body
116 checkrelation = Relation.find(relationid)
117 assert_not_nil checkrelation,
118 "uploaded relation not found in data base after upload"
120 assert_equal checkrelation.members.length, 2,
121 "saved relation does not have exactly two members"
122 assert_equal checkrelation.tags.length, 1,
123 "saved relation does not contain exactly one tag"
124 assert_equal users(:normal_user).id, checkrelation.user_id,
125 "saved relation does not belong to user that created it"
126 assert_equal true, checkrelation.visible,
127 "saved relation is not visible"
128 # ok the relation is there but can we also retrieve it?
129 get :read, :id => relationid
130 assert_response :success
134 # -------------------------------------
135 # Test creating some invalid relations.
136 # -------------------------------------
138 def test_create_invalid
139 basic_authorization "test@openstreetmap.org", "test"
141 # create a relation with non-existing node as member
142 content "<osm><relation><member type='node' ref='0'/><tag k='test' v='yes' /></relation></osm>"
145 assert_response :precondition_failed,
146 "relation upload with invalid node did not return 'precondition failed'"
149 # -------------------------------------
150 # Test deleting relations.
151 # -------------------------------------
156 # first try to delete relation without auth
157 delete :delete, :id => current_relations(:visible_relation).id
158 assert_response :unauthorized
161 basic_authorization("test@openstreetmap.org", "test");
164 delete :delete, :id => current_relations(:visible_relation).id
165 assert_response :success
167 # this won't work since the relation is already deleted
168 delete :delete, :id => current_relations(:invisible_relation).id
169 assert_response :gone
171 # this won't work since the relation never existed
172 delete :delete, :id => 0
173 assert_response :not_found