]> git.openstreetmap.org Git - rails.git/blob - test/functional/way_controller_test.rb
Doing a resync from mainline 8633:10895. There was one simple to resolve conflict...
[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     # create a way with pre-existing nodes
80     content "<osm><way><nd ref='#{nid1}'/><nd ref='#{nid2}'/><tag k='test' v='yes' /></way></osm>"
81     put :create
82     # hope for success
83     assert_response :success, 
84         "way upload did not return success status"
85     # read id of created way and search for it
86     wayid = @response.body
87     checkway = Way.find(wayid)
88     assert_not_nil checkway, 
89         "uploaded way not found in data base after upload"
90     # compare values
91     assert_equal checkway.nds.length, 2, 
92         "saved way does not contain exactly one node"
93     assert_equal checkway.nds[0], nid1, 
94         "saved way does not contain the right node on pos 0"
95     assert_equal checkway.nds[1], nid2, 
96         "saved way does not contain the right node on pos 1"
97     assert_equal users(:normal_user).id, checkway.user_id, 
98         "saved way does not belong to user that created it"
99     assert_equal true, checkway.visible, 
100         "saved way is not visible"
101   end
102
103   # -------------------------------------
104   # Test creating some invalid ways.
105   # -------------------------------------
106
107   def test_create_invalid
108     basic_authorization "test@openstreetmap.org", "test"
109
110     # create a way with non-existing node
111     content "<osm><way><nd ref='0'/><tag k='test' v='yes' /></way></osm>"
112     put :create
113     # expect failure
114     assert_response :precondition_failed, 
115         "way upload with invalid node did not return 'precondition failed'"
116
117     # create a way with no nodes
118     content "<osm><way><tag k='test' v='yes' /></way></osm>"
119     put :create
120     # expect failure
121     assert_response :precondition_failed, 
122         "way upload with no node did not return 'precondition failed'"
123   end
124
125   # -------------------------------------
126   # Test deleting ways.
127   # -------------------------------------
128   
129   def test_delete
130
131     # first try to delete way without auth
132     delete :delete, :id => current_ways(:visible_way).id
133     assert_response :unauthorized
134
135     # now set auth
136     basic_authorization("test@openstreetmap.org", "test");  
137
138     # this should work
139     delete :delete, :id => current_ways(:visible_way).id
140     assert_response :success
141
142     # this won't work since the way is already deleted
143     delete :delete, :id => current_ways(:invisible_way).id
144     assert_response :gone
145
146     # this won't work since the way never existed
147     delete :delete, :id => 0
148     assert_response :not_found
149   end
150
151 end