]> git.openstreetmap.org Git - rails.git/blob - test/functional/relation_controller_test.rb
changed from nd id=... to nd ref=...
[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   end
44
45   # -------------------------------------
46   # Test simple relation creation.
47   # -------------------------------------
48
49   def test_create
50     basic_authorization "test@openstreetmap.org", "test"
51
52     # create an relation without members
53     content "<osm><relation><tag k='test' v='yes' /></relation></osm>"
54     put :create
55     # hope for success
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"
63     # compare values
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
75
76
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>"
81     put :create
82     # hope for success
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"
90     # compare values
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?
100     
101     get :read, :id => relationid
102     assert_response :success
103
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>"
110     put :create
111     # hope for success
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"
119     # compare values
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
131
132   end
133
134   # -------------------------------------
135   # Test creating some invalid relations.
136   # -------------------------------------
137
138   def test_create_invalid
139     basic_authorization "test@openstreetmap.org", "test"
140
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>"
143     put :create
144     # expect failure
145     assert_response :precondition_failed, 
146         "relation upload with invalid node did not return 'precondition failed'"
147   end
148
149   # -------------------------------------
150   # Test deleting relations.
151   # -------------------------------------
152   
153   def test_delete
154   return true
155
156     # first try to delete relation without auth
157     delete :delete, :id => current_relations(:visible_relation).id
158     assert_response :unauthorized
159
160     # now set auth
161     basic_authorization("test@openstreetmap.org", "test");  
162
163     # this should work
164     delete :delete, :id => current_relations(:visible_relation).id
165     assert_response :success
166
167     # this won't work since the relation is already deleted
168     delete :delete, :id => current_relations(:invisible_relation).id
169     assert_response :gone
170
171     # this won't work since the relation never existed
172     delete :delete, :id => 0
173     assert_response :not_found
174   end
175
176 end