]> git.openstreetmap.org Git - rails.git/blob - test/functional/way_controller_test.rb
Fixed node functional tests. A couple of changes to the old_node model and node contr...
[rails.git] / test / functional / way_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'way_controller'
3
4 # Re-raise errors caught by the controller.
5 class WayController; def rescue_action(e) raise e end; end
6
7 class WayControllerTest < Test::Unit::TestCase
8   api_fixtures
9
10   def setup
11     @controller = WayController.new
12     @request    = ActionController::TestRequest.new
13     @response   = ActionController::TestResponse.new
14   end
15
16   def basic_authorization(user, pass)
17     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
18   end
19
20   def content(c)
21     @request.env["RAW_POST_DATA"] = c
22   end
23
24   # -------------------------------------
25   # Test reading ways.
26   # -------------------------------------
27
28   def test_read
29     # check that a visible way is returned properly
30     get :read, :id => current_ways(:visible_way).id
31     assert_response :success
32
33     # check that an invisible way is not returned
34     get :read, :id => current_ways(:invisible_way).id
35     assert_response :gone
36
37     # check chat a non-existent way is not returned
38     get :read, :id => 0
39     assert_response :not_found
40
41     # check the "ways for node" mode
42     get :ways_for_node, :id => current_nodes(:used_node_1).id
43     assert_response :success
44     # FIXME check whether this contains the stuff we want!
45     #print @response.body
46     # Needs to be updated when changing fixtures
47     # The generator should probably be defined in the environment.rb file
48     # in the same place as the api version
49     assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
50     assert_select "osm way", 3
51     assert_select "osm way nd", 3
52     assert_select "osm way tag", 3
53
54     # check the "full" mode
55     get :full, :id => current_ways(:visible_way).id
56     assert_response :success
57     # FIXME check whether this contains the stuff we want!
58     #print @response.body
59     # Check the way is correctly returned
60     way = current_ways(:visible_way)
61     assert_select "osm way[id=#{way.id}][version=#{way.version}][visible=#{way.visible}]", 1
62     assert_select "osm way nd[ref=#{way.way_nodes[0].node_id}]", 1
63     # Check that the node is correctly returned
64     nd = current_ways(:visible_way).nodes
65     assert_equal 1, nd.count
66     nda = nd[0]
67     assert_select "osm node[id=#{nda.id}][version=#{nda.version}][lat=#{nda.lat}][lon=#{nda.lon}]", 1 
68   end
69
70   # -------------------------------------
71   # Test simple way creation.
72   # -------------------------------------
73
74   def test_create
75     nid1 = current_nodes(:used_node_1).id
76     nid2 = current_nodes(:used_node_2).id
77     basic_authorization "test@openstreetmap.org", "test"
78
79     # FIXME create a new changeset and use the id that is returned for the next step
80     
81     # create a way with pre-existing nodes
82     content "<osm><way><nd ref='#{nid1}'/><nd ref='#{nid2}'/><tag k='test' v='yes' /></way></osm>"
83     put :create
84     # hope for success
85     assert_response :success, 
86         "way upload did not return success status"
87     # read id of created way and search for it
88     wayid = @response.body
89     checkway = Way.find(wayid)
90     assert_not_nil checkway, 
91         "uploaded way not found in data base after upload"
92     # compare values
93     assert_equal checkway.nds.length, 2, 
94         "saved way does not contain exactly one node"
95     assert_equal checkway.nds[0], nid1, 
96         "saved way does not contain the right node on pos 0"
97     assert_equal checkway.nds[1], nid2, 
98         "saved way does not contain the right node on pos 1"
99     assert_equal users(:normal_user).id, checkway.user_id, 
100         "saved way does not belong to user that created it"
101     assert_equal true, checkway.visible, 
102         "saved way is not visible"
103   end
104
105   # -------------------------------------
106   # Test creating some invalid ways.
107   # -------------------------------------
108
109   def test_create_invalid
110     basic_authorization "test@openstreetmap.org", "test"
111
112     # FIXME All of these will fail because they don't have a valid changeset 
113     # create a way with non-existing node
114     content "<osm><way><nd ref='0'/><tag k='test' v='yes' /></way></osm>"
115     put :create
116     # expect failure
117     assert_response :precondition_failed, 
118         "way upload with invalid node did not return 'precondition failed'"
119
120     # create a way with no nodes
121     content "<osm><way><tag k='test' v='yes' /></way></osm>"
122     put :create
123     # expect failure
124     assert_response :precondition_failed, 
125         "way upload with no node did not return 'precondition failed'"
126   end
127
128   # -------------------------------------
129   # Test deleting ways.
130   # -------------------------------------
131   
132   def test_delete
133
134     # first try to delete way without auth
135     delete :delete, :id => current_ways(:visible_way).id
136     assert_response :unauthorized
137
138     # now set auth
139     basic_authorization("test@openstreetmap.org", "test");  
140
141     # this shouldn't work as with the 0.6 api we need pay load to delete
142     delete :delete, :id => current_ways(:visible_way).id
143     assert_response :bad_request
144     
145     # Now try without having a changeset
146     content "<osm><way id='#{current_ways(:visible_way).id}'</osm>"
147     delete :delete, :id => current_ways(:visible_way).id
148     assert_response :bad_request
149     
150     # Now try and get a changeset
151
152     # this won't work since the way is already deleted
153     delete :delete, :id => current_ways(:invisible_way).id
154     assert_response :gone
155
156     # this won't work since the way never existed
157     delete :delete, :id => 0
158     assert_response :not_found
159   end
160
161 end