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
11 @controller = WayController.new
12 @request = ActionController::TestRequest.new
13 @response = ActionController::TestResponse.new
16 def basic_authorization(user, pass)
17 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
21 @request.env["RAW_POST_DATA"] = c
24 # -------------------------------------
26 # -------------------------------------
29 # check that a visible way is returned properly
30 get :read, :id => current_ways(:visible_way).id
31 assert_response :success
33 # check that an invisible way is not returned
34 get :read, :id => current_ways(:invisible_way).id
37 # check chat a non-existent way is not returned
39 assert_response :not_found
42 # -------------------------------------
43 # Test simple way creation.
44 # -------------------------------------
47 nid1 = current_nodes(:used_node_1).id
48 nid2 = current_nodes(:used_node_2).id
49 basic_authorization "test@openstreetmap.org", "test"
51 # create a way with pre-existing nodes
52 content "<osm><way><nd ref='#{nid1}'/><nd ref='#{nid2}'/><tag k='test' v='yes' /></way></osm>"
55 assert_response :success,
56 "way upload did not return success status"
57 # read id of created way and search for it
58 wayid = @response.body
59 checkway = Way.find(wayid)
60 assert_not_nil checkway,
61 "uploaded way not found in data base after upload"
63 assert_equal checkway.nds.length, 2,
64 "saved way does not contain exactly one node"
65 assert_equal checkway.nds[0], nid1,
66 "saved way does not contain the right node on pos 0"
67 assert_equal checkway.nds[1], nid2,
68 "saved way does not contain the right node on pos 1"
69 assert_equal users(:normal_user).id, checkway.user_id,
70 "saved way does not belong to user that created it"
71 assert_equal true, checkway.visible,
72 "saved way is not visible"
75 # -------------------------------------
76 # Test creating some invalid ways.
77 # -------------------------------------
79 def test_create_invalid
80 basic_authorization "test@openstreetmap.org", "test"
82 # create a way with non-existing node
83 content "<osm><way><nd ref='0'/><tag k='test' v='yes' /></way></osm>"
86 assert_response :precondition_failed,
87 "way upload with invalid node did not return 'precondition failed'"
89 # create a way with no nodes
90 content "<osm><way><tag k='test' v='yes' /></way></osm>"
93 assert_response :precondition_failed,
94 "way upload with no node did not return 'precondition failed'"
97 # -------------------------------------
99 # -------------------------------------
103 # first try to delete way without auth
104 delete :delete, :id => current_ways(:visible_way).id
105 assert_response :unauthorized
108 basic_authorization("test@openstreetmap.org", "test");
111 delete :delete, :id => current_ways(:visible_way).id
112 assert_response :success
114 # this won't work since the way is already deleted
115 delete :delete, :id => current_ways(:invisible_way).id
116 assert_response :gone
118 # this won't work since the way never existed
119 delete :delete, :id => 0
120 assert_response :not_found