]> git.openstreetmap.org Git - rails.git/blob - test/functional/relation_controller_test.rb
reverting the javascript change in changeset 10926, that needs to be specific to...
[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     # see the test_read in way_controller_test.rb for the assert_select
49     assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
50     assert_select "osm relation"
51     if $VERBOSE
52         print @response.body
53     end
54
55     # check the "relations for way" mode
56     get :relations_for_way, :id => current_ways(:used_way).id
57     assert_response :success
58     # FIXME check whether this contains the stuff we want!
59     if $VERBOSE
60         print @response.body
61     end
62
63     # check the "relations for relation" mode
64     get :relations_for_relation, :id => current_relations(:used_relation).id
65     assert_response :success
66     # FIXME check whether this contains the stuff we want!
67     if $VERBOSE
68         print @response.body
69     end
70
71     # check the "full" mode
72     get :full, :id => current_relations(:visible_relation).id
73     assert_response :success
74     # FIXME check whether this contains the stuff we want!
75     if $VERBOSE
76         print @response.body
77     end
78   end
79
80   # -------------------------------------
81   # Test simple relation creation.
82   # -------------------------------------
83
84   def test_create
85     basic_authorization "test@openstreetmap.org", "test"
86     
87     # FIXME create a new changeset and use the id that is returned for the next step
88
89     # create an relation without members
90     content "<osm><relation><tag k='test' v='yes' /></relation></osm>"
91     put :create
92     # hope for success
93     assert_response :success, 
94         "relation upload did not return success status"
95     # read id of created relation and search for it
96     relationid = @response.body
97     checkrelation = Relation.find(relationid)
98     assert_not_nil checkrelation, 
99         "uploaded relation not found in data base after upload"
100     # compare values
101     assert_equal checkrelation.members.length, 0, 
102         "saved relation contains members but should not"
103     assert_equal checkrelation.tags.length, 1, 
104         "saved relation does not contain exactly one tag"
105     assert_equal users(:normal_user).id, checkrelation.changeset.user_id, 
106         "saved relation does not belong to user that created it"
107     assert_equal true, checkrelation.visible, 
108         "saved relation is not visible"
109     # ok the relation is there but can we also retrieve it?
110     get :read, :id => relationid
111     assert_response :success
112
113
114     # create an relation with a node as member
115     nid = current_nodes(:used_node_1).id
116     content "<osm><relation><member type='node' ref='#{nid}' role='some'/>" +
117         "<tag k='test' v='yes' /></relation></osm>"
118     put :create
119     # hope for success
120     assert_response :success, 
121         "relation upload did not return success status"
122     # read id of created relation and search for it
123     relationid = @response.body
124     checkrelation = Relation.find(relationid)
125     assert_not_nil checkrelation, 
126         "uploaded relation not found in data base after upload"
127     # compare values
128     assert_equal checkrelation.members.length, 1, 
129         "saved relation does not contain exactly one member"
130     assert_equal checkrelation.tags.length, 1, 
131         "saved relation does not contain exactly one tag"
132     assert_equal users(:normal_user).id, checkrelation.user_id, 
133         "saved relation does not belong to user that created it"
134     assert_equal true, checkrelation.visible, 
135         "saved relation is not visible"
136     # ok the relation is there but can we also retrieve it?
137     
138     get :read, :id => relationid
139     assert_response :success
140
141     # create an relation with a way and a node as members
142     nid = current_nodes(:used_node_1).id
143     wid = current_ways(:used_way).id
144     content "<osm><relation><member type='node' ref='#{nid}' role='some'/>" +
145         "<member type='way' ref='#{wid}' role='other'/>" +
146         "<tag k='test' v='yes' /></relation></osm>"
147     put :create
148     # hope for success
149     assert_response :success, 
150         "relation upload did not return success status"
151     # read id of created relation and search for it
152     relationid = @response.body
153     checkrelation = Relation.find(relationid)
154     assert_not_nil checkrelation, 
155         "uploaded relation not found in data base after upload"
156     # compare values
157     assert_equal checkrelation.members.length, 2, 
158         "saved relation does not have exactly two members"
159     assert_equal checkrelation.tags.length, 1, 
160         "saved relation does not contain exactly one tag"
161     assert_equal users(:normal_user).id, checkrelation.user_id, 
162         "saved relation does not belong to user that created it"
163     assert_equal true, checkrelation.visible, 
164         "saved relation is not visible"
165     # ok the relation is there but can we also retrieve it?
166     get :read, :id => relationid
167     assert_response :success
168
169   end
170
171   # -------------------------------------
172   # Test creating some invalid relations.
173   # -------------------------------------
174
175   def test_create_invalid
176     basic_authorization "test@openstreetmap.org", "test"
177
178     # create a relation with non-existing node as member
179     content "<osm><relation><member type='node' ref='0'/><tag k='test' v='yes' /></relation></osm>"
180     put :create
181     # expect failure
182     assert_response :precondition_failed, 
183         "relation upload with invalid node did not return 'precondition failed'"
184   end
185
186   # -------------------------------------
187   # Test deleting relations.
188   # -------------------------------------
189   
190   def test_delete
191   return true
192
193     # first try to delete relation without auth
194     delete :delete, :id => current_relations(:visible_relation).id
195     assert_response :unauthorized
196
197     # now set auth
198     basic_authorization("test@openstreetmap.org", "test");  
199
200     # this should work
201     delete :delete, :id => current_relations(:visible_relation).id
202     assert_response :success
203
204     # this won't work since the relation is already deleted
205     delete :delete, :id => current_relations(:invisible_relation).id
206     assert_response :gone
207
208     # this won't work since the relation never existed
209     delete :delete, :id => 0
210     assert_response :not_found
211   end
212
213 end