]> git.openstreetmap.org Git - rails.git/blob - test/functional/relation_controller_test.rb
Refactor the GPX upload to try and avoid the import daemon loading traces
[rails.git] / test / functional / relation_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'relation_controller'
3
4 # Re-raise errors caught by the controller.
5 class RelationController; def rescue_action(e) raise e end; end
6
7 class RelationControllerTest < Test::Unit::TestCase
8   api_fixtures
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
12
13   def setup
14     @controller = RelationController.new
15     @request    = ActionController::TestRequest.new
16     @response   = ActionController::TestResponse.new
17   end
18
19   def basic_authorization(user, pass)
20     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
21   end
22
23   def content(c)
24     @request.env["RAW_POST_DATA"] = c
25   end
26
27   # -------------------------------------
28   # Test reading relations.
29   # -------------------------------------
30
31   def test_read
32     # check that a visible relation is returned properly
33     get :read, :id => current_relations(:visible_relation).id
34     assert_response :success
35
36     # check that an invisible relation is not returned
37     get :read, :id => current_relations(:invisible_relation).id
38     assert_response :gone
39
40     # check chat a non-existent relation is not returned
41     get :read, :id => 0
42     assert_response :not_found
43
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     if $VERBOSE
49         print @response.body
50     end
51
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!
56     if $VERBOSE
57         print @response.body
58     end
59
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!
64     if $VERBOSE
65         print @response.body
66     end
67
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!
72     if $VERBOSE
73         print @response.body
74     end
75   end
76
77   # -------------------------------------
78   # Test simple relation creation.
79   # -------------------------------------
80
81   def test_create
82     basic_authorization "test@openstreetmap.org", "test"
83
84     # create an relation without members
85     content "<osm><relation><tag k='test' v='yes' /></relation></osm>"
86     put :create
87     # hope for success
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"
95     # compare values
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
107
108
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>"
113     put :create
114     # hope for success
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"
122     # compare values
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?
132     
133     get :read, :id => relationid
134     assert_response :success
135
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>"
142     put :create
143     # hope for success
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"
151     # compare values
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
163
164   end
165
166   # -------------------------------------
167   # Test creating some invalid relations.
168   # -------------------------------------
169
170   def test_create_invalid
171     basic_authorization "test@openstreetmap.org", "test"
172
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>"
175     put :create
176     # expect failure
177     assert_response :precondition_failed, 
178         "relation upload with invalid node did not return 'precondition failed'"
179   end
180
181   # -------------------------------------
182   # Test deleting relations.
183   # -------------------------------------
184   
185   def test_delete
186   return true
187
188     # first try to delete relation without auth
189     delete :delete, :id => current_relations(:visible_relation).id
190     assert_response :unauthorized
191
192     # now set auth
193     basic_authorization("test@openstreetmap.org", "test");  
194
195     # this should work
196     delete :delete, :id => current_relations(:visible_relation).id
197     assert_response :success
198
199     # this won't work since the relation is already deleted
200     delete :delete, :id => current_relations(:invisible_relation).id
201     assert_response :gone
202
203     # this won't work since the relation never existed
204     delete :delete, :id => 0
205     assert_response :not_found
206   end
207
208 end