]> git.openstreetmap.org Git - rails.git/blob - test/functional/relation_controller_test.rb
way history needs to reference nodes with ref=... not id=...
[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
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!
65     if $VERBOSE
66         print @response.body
67     end
68
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!
73     if $VERBOSE
74         print @response.body
75     end
76   end
77
78   # -------------------------------------
79   # Test simple relation creation.
80   # -------------------------------------
81
82   def test_create
83     basic_authorization "test@openstreetmap.org", "test"
84
85     # create an relation without members
86     content "<osm><relation><tag k='test' v='yes' /></relation></osm>"
87     put :create
88     # hope for success
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"
96     # compare values
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
108
109
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>"
114     put :create
115     # hope for success
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"
123     # compare values
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?
133     
134     get :read, :id => relationid
135     assert_response :success
136
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>"
143     put :create
144     # hope for success
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"
152     # compare values
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
164
165   end
166
167   # -------------------------------------
168   # Test creating some invalid relations.
169   # -------------------------------------
170
171   def test_create_invalid
172     basic_authorization "test@openstreetmap.org", "test"
173
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>"
176     put :create
177     # expect failure
178     assert_response :precondition_failed, 
179         "relation upload with invalid node did not return 'precondition failed'"
180   end
181
182   # -------------------------------------
183   # Test deleting relations.
184   # -------------------------------------
185   
186   def test_delete
187   return true
188
189     # first try to delete relation without auth
190     delete :delete, :id => current_relations(:visible_relation).id
191     assert_response :unauthorized
192
193     # now set auth
194     basic_authorization("test@openstreetmap.org", "test");  
195
196     # this should work
197     delete :delete, :id => current_relations(:visible_relation).id
198     assert_response :success
199
200     # this won't work since the relation is already deleted
201     delete :delete, :id => current_relations(:invisible_relation).id
202     assert_response :gone
203
204     # this won't work since the relation never existed
205     delete :delete, :id => 0
206     assert_response :not_found
207   end
208
209 end