]> git.openstreetmap.org Git - rails.git/blob - test/functional/way_controller_test.rb
Now all the unit tests work
[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
47     # check the "full" mode
48     get :full, :id => current_ways(:visible_way).id
49     assert_response :success
50     # FIXME check whether this contains the stuff we want!
51     print @response.body
52   end
53
54   # -------------------------------------
55   # Test simple way creation.
56   # -------------------------------------
57
58   def test_create
59     nid1 = current_nodes(:used_node_1).id
60     nid2 = current_nodes(:used_node_2).id
61     basic_authorization "test@openstreetmap.org", "test"
62
63     # create a way with pre-existing nodes
64     content "<osm><way><nd ref='#{nid1}'/><nd ref='#{nid2}'/><tag k='test' v='yes' /></way></osm>"
65     put :create
66     # hope for success
67     assert_response :success, 
68         "way upload did not return success status"
69     # read id of created way and search for it
70     wayid = @response.body
71     checkway = Way.find(wayid)
72     assert_not_nil checkway, 
73         "uploaded way not found in data base after upload"
74     # compare values
75     assert_equal checkway.nds.length, 2, 
76         "saved way does not contain exactly one node"
77     assert_equal checkway.nds[0], nid1, 
78         "saved way does not contain the right node on pos 0"
79     assert_equal checkway.nds[1], nid2, 
80         "saved way does not contain the right node on pos 1"
81     assert_equal users(:normal_user).id, checkway.user_id, 
82         "saved way does not belong to user that created it"
83     assert_equal true, checkway.visible, 
84         "saved way is not visible"
85   end
86
87   # -------------------------------------
88   # Test creating some invalid ways.
89   # -------------------------------------
90
91   def test_create_invalid
92     basic_authorization "test@openstreetmap.org", "test"
93
94     # create a way with non-existing node
95     content "<osm><way><nd ref='0'/><tag k='test' v='yes' /></way></osm>"
96     put :create
97     # expect failure
98     assert_response :precondition_failed, 
99         "way upload with invalid node did not return 'precondition failed'"
100
101     # create a way with no nodes
102     content "<osm><way><tag k='test' v='yes' /></way></osm>"
103     put :create
104     # expect failure
105     assert_response :precondition_failed, 
106         "way upload with no node did not return 'precondition failed'"
107   end
108
109   # -------------------------------------
110   # Test deleting ways.
111   # -------------------------------------
112   
113   def test_delete
114
115     # first try to delete way without auth
116     delete :delete, :id => current_ways(:visible_way).id
117     assert_response :unauthorized
118
119     # now set auth
120     basic_authorization("test@openstreetmap.org", "test");  
121
122     # this should work
123     delete :delete, :id => current_ways(:visible_way).id
124     assert_response :success
125
126     # this won't work since the way is already deleted
127     delete :delete, :id => current_ways(:invisible_way).id
128     assert_response :gone
129
130     # this won't work since the way never existed
131     delete :delete, :id => 0
132     assert_response :not_found
133   end
134
135 end