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
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!
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!
54 # -------------------------------------
55 # Test simple way creation.
56 # -------------------------------------
59 nid1 = current_nodes(:used_node_1).id
60 nid2 = current_nodes(:used_node_2).id
61 basic_authorization "test@openstreetmap.org", "test"
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>"
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"
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"
87 # -------------------------------------
88 # Test creating some invalid ways.
89 # -------------------------------------
91 def test_create_invalid
92 basic_authorization "test@openstreetmap.org", "test"
94 # create a way with non-existing node
95 content "<osm><way><nd ref='0'/><tag k='test' v='yes' /></way></osm>"
98 assert_response :precondition_failed,
99 "way upload with invalid node did not return 'precondition failed'"
101 # create a way with no nodes
102 content "<osm><way><tag k='test' v='yes' /></way></osm>"
105 assert_response :precondition_failed,
106 "way upload with no node did not return 'precondition failed'"
109 # -------------------------------------
110 # Test deleting ways.
111 # -------------------------------------
115 # first try to delete way without auth
116 delete :delete, :id => current_ways(:visible_way).id
117 assert_response :unauthorized
120 basic_authorization("test@openstreetmap.org", "test");
123 delete :delete, :id => current_ways(:visible_way).id
124 assert_response :success
126 # this won't work since the way is already deleted
127 delete :delete, :id => current_ways(:invisible_way).id
128 assert_response :gone
130 # this won't work since the way never existed
131 delete :delete, :id => 0
132 assert_response :not_found