]> git.openstreetmap.org Git - rails.git/blob - test/functional/way_controller_test.rb
Add a capabilities API call. Fixes #410.
[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   fixtures :current_nodes, :nodes, :users, :current_segments, :segments, :ways, :current_ways, :way_tags, :current_way_tags, :way_segments, :current_way_segments
9   set_fixture_class :current_ways => :Way
10   set_fixture_class :ways => :OldWay
11   set_fixture_class :current_segments => :Segment
12   set_fixture_class :segments => :OldSegment
13
14   def setup
15     @controller = WayController.new
16     @request    = ActionController::TestRequest.new
17     @response   = ActionController::TestResponse.new
18   end
19
20   def basic_authorization(user, pass)
21     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
22   end
23
24   def content(c)
25     @request.env["RAW_POST_DATA"] = c
26   end
27
28   # -------------------------------------
29   # Test reading ways.
30   # -------------------------------------
31
32   def test_read
33     # check that a visible way is returned properly
34     get :read, :id => current_ways(:visible_way).id
35     assert_response :success
36
37     # check that an invisible way is not returned
38     get :read, :id => current_ways(:invisible_way).id
39     assert_response :gone
40
41     # check chat a non-existent way is not returned
42     get :read, :id => 0
43     assert_response :not_found
44   end
45
46   # -------------------------------------
47   # Test simple way creation.
48   # -------------------------------------
49
50   def test_create
51     sid = current_segments(:used_segment).id
52     basic_authorization "test@openstreetmap.org", "test"
53
54     # create a way with pre-existing segment
55     content "<osm><way><seg id='#{sid}'/><tag k='test' v='yes' /></way></osm>"
56     put :create
57     # hope for success
58     assert_response :success, 
59         "way upload did not return success status"
60     # read id of created way and search for it
61     wayid = @response.body
62     checkway = Way.find(wayid)
63     assert_not_nil checkway, 
64         "uploaded way not found in data base after upload"
65     # compare values
66     assert_equal checkway.segs.length, 1, 
67         "saved way does not contain exactly one segment"
68     assert_equal checkway.segs[0], sid, 
69         "saved way does not contain the right segment"
70     assert_equal users(:normal_user).id, checkway.user_id, 
71         "saved way does not belong to user that created it"
72     assert_equal true, checkway.visible, 
73         "saved way is not visible"
74   end
75
76   # -------------------------------------
77   # Test creating some invalid ways.
78   # -------------------------------------
79
80   def test_create_invalid
81     basic_authorization "test@openstreetmap.org", "test"
82
83     # create a way with non-existing segment
84     content "<osm><way><seg id='0'/><tag k='test' v='yes' /></way></osm>"
85     put :create
86     # expect failure
87     assert_response :precondition_failed, 
88         "way upload with invalid segment did not return 'precondition failed'"
89
90     # create a way with no segments
91     content "<osm><way><tag k='test' v='yes' /></way></osm>"
92     put :create
93     # expect failure
94     assert_response :precondition_failed, 
95         "way upload with no segments did not return 'precondition failed'"
96
97     # create a way that has the same segment, twice
98     # (commented out - this is currently allowed!)
99     #sid = current_segments(:used_segment).id
100     #content "<osm><way><seg id='#{sid}'/><seg id='#{sid}'/><tag k='test' v='yes' /></way></osm>"
101     #put :create
102     #assert_response :internal_server_error,
103     #    "way upload with double segment did not return 'internal server error'"
104   end
105
106   # -------------------------------------
107   # Test deleting ways.
108   # -------------------------------------
109   
110   def test_delete
111
112     # first try to delete way without auth
113     delete :delete, :id => current_ways(:visible_way).id
114     assert_response :unauthorized
115
116     # now set auth
117     basic_authorization("test@openstreetmap.org", "test");  
118
119     # this should work
120     delete :delete, :id => current_ways(:visible_way).id
121     assert_response :success
122
123     # this won't work since the way is already deleted
124     delete :delete, :id => current_ways(:invisible_way).id
125     assert_response :gone
126
127     # this won't work since the way never existed
128     delete :delete, :id => 0
129     assert_response :not_found
130   end
131
132 end