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!
46 # Needs to be updated when changing fixtures
47 # The generator should probably be defined in the environment.rb file
48 # in the same place as the api version
49 assert_select "osm[version=#{API_VERSION}][generator=\"OpenStreetMap server\"]", 1
50 assert_select "osm way", 3
51 assert_select "osm way nd", 3
52 assert_select "osm way tag", 3
54 # check the "full" mode
55 get :full, :id => current_ways(:visible_way).id
56 assert_response :success
57 # FIXME check whether this contains the stuff we want!
59 # Check the way is correctly returned
60 way = current_ways(:visible_way)
61 assert_select "osm way[id=#{way.id}][version=#{way.version}][visible=#{way.visible}]", 1
62 assert_select "osm way nd[ref=#{way.way_nodes[0].node_id}]", 1
63 # Check that the node is correctly returned
64 nd = current_ways(:visible_way).nodes
65 assert_equal 1, nd.count
67 assert_select "osm node[id=#{nda.id}][version=#{nda.version}][lat=#{nda.lat}][lon=#{nda.lon}]", 1
70 # -------------------------------------
71 # Test simple way creation.
72 # -------------------------------------
75 nid1 = current_nodes(:used_node_1).id
76 nid2 = current_nodes(:used_node_2).id
77 basic_authorization "test@openstreetmap.org", "test"
79 # FIXME create a new changeset and use the id that is returned for the next step
81 # create a way with pre-existing nodes
82 content "<osm><way><nd ref='#{nid1}'/><nd ref='#{nid2}'/><tag k='test' v='yes' /></way></osm>"
85 assert_response :success,
86 "way upload did not return success status"
87 # read id of created way and search for it
88 wayid = @response.body
89 checkway = Way.find(wayid)
90 assert_not_nil checkway,
91 "uploaded way not found in data base after upload"
93 assert_equal checkway.nds.length, 2,
94 "saved way does not contain exactly one node"
95 assert_equal checkway.nds[0], nid1,
96 "saved way does not contain the right node on pos 0"
97 assert_equal checkway.nds[1], nid2,
98 "saved way does not contain the right node on pos 1"
99 assert_equal users(:normal_user).id, checkway.user_id,
100 "saved way does not belong to user that created it"
101 assert_equal true, checkway.visible,
102 "saved way is not visible"
105 # -------------------------------------
106 # Test creating some invalid ways.
107 # -------------------------------------
109 def test_create_invalid
110 basic_authorization "test@openstreetmap.org", "test"
112 # create a way with non-existing node
113 content "<osm><way><nd ref='0'/><tag k='test' v='yes' /></way></osm>"
116 assert_response :precondition_failed,
117 "way upload with invalid node did not return 'precondition failed'"
119 # create a way with no nodes
120 content "<osm><way><tag k='test' v='yes' /></way></osm>"
123 assert_response :precondition_failed,
124 "way upload with no node did not return 'precondition failed'"
127 # -------------------------------------
128 # Test deleting ways.
129 # -------------------------------------
133 # first try to delete way without auth
134 delete :delete, :id => current_ways(:visible_way).id
135 assert_response :unauthorized
138 basic_authorization("test@openstreetmap.org", "test");
141 delete :delete, :id => current_ways(:visible_way).id
142 assert_response :success
144 # this won't work since the way is already deleted
145 delete :delete, :id => current_ways(:invisible_way).id
146 assert_response :gone
148 # this won't work since the way never existed
149 delete :delete, :id => 0
150 assert_response :not_found