1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'way_controller'
4 # Re-raise errors caught by the controller.
5 class WayController; def rescue_action(e) raise e end; end
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
15 @controller = WayController.new
16 @request = ActionController::TestRequest.new
17 @response = ActionController::TestResponse.new
20 def basic_authorization(user, pass)
21 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
25 @request.env["RAW_POST_DATA"] = c
28 # -------------------------------------
30 # -------------------------------------
33 # check that a visible way is returned properly
34 get :read, :id => current_ways(:visible_way).id
35 assert_response :success
37 # check that an invisible way is not returned
38 get :read, :id => current_ways(:invisible_way).id
41 # check chat a non-existent way is not returned
43 assert_response :not_found
46 # -------------------------------------
47 # Test simple way creation.
48 # -------------------------------------
51 sid = current_segments(:used_segment).id
52 basic_authorization "test@openstreetmap.org", "test"
54 # create a way with pre-existing segment
55 content "<osm><way><seg id='#{sid}'/><tag k='test' v='yes' /></way></osm>"
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"
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"
76 # -------------------------------------
77 # Test creating some invalid ways.
78 # -------------------------------------
80 def test_create_invalid
81 basic_authorization "test@openstreetmap.org", "test"
83 # create a way with non-existing segment
84 content "<osm><way><seg id='0'/><tag k='test' v='yes' /></way></osm>"
87 assert_response :precondition_failed,
88 "way upload with invalid segment did not return 'precondition failed'"
90 # create a way with no segments
91 content "<osm><way><tag k='test' v='yes' /></way></osm>"
94 assert_response :precondition_failed,
95 "way upload with no segments did not return 'precondition failed'"
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>"
102 #assert_response :internal_server_error,
103 # "way upload with double segment did not return 'internal server error'"
106 # -------------------------------------
107 # Test deleting ways.
108 # -------------------------------------
112 # first try to delete way without auth
113 delete :delete, :id => current_ways(:visible_way).id
114 assert_response :unauthorized
117 basic_authorization("test@openstreetmap.org", "test");
120 delete :delete, :id => current_ways(:visible_way).id
121 assert_response :success
123 # this won't work since the way is already deleted
124 delete :delete, :id => current_ways(:invisible_way).id
125 assert_response :gone
127 # this won't work since the way never existed
128 delete :delete, :id => 0
129 assert_response :not_found