1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'segment_controller'
4 # Re-raise errors caught by the controller.
5 class SegmentController; def rescue_action(e) raise e end; end
7 class SegmentControllerTest < Test::Unit::TestCase
11 @controller = SegmentController.new
12 @request = ActionController::TestRequest.new
13 @response = ActionController::TestResponse.new
17 # cannot read password from fixture as it is stored as MD5 digest
18 basic_authorization("test@openstreetmap.org", "test");
19 na = current_nodes(:used_node_1).id
20 nb = current_nodes(:used_node_2).id
21 content("<osm><segment from='#{na}' to='#{nb}' /></osm>")
24 assert_response :success, "segment upload did not return success status"
25 # read id of created segment and search for it
26 segmentid = @response.body
27 checksegment = Segment.find(segmentid)
28 assert_not_nil checksegment, "uploaded segment not found in data base after upload"
30 assert_equal na, checksegment.node_a, "saved segment does not match requested from-node"
31 assert_equal nb, checksegment.node_b, "saved segment does not match requested to-node"
32 assert_equal users(:normal_user).id, checksegment.user_id, "saved segment does not belong to user that created it"
33 assert_equal true, checksegment.visible, "saved segment is not visible"
36 def test_create_invalid
37 basic_authorization("test@openstreetmap.org", "test");
38 # create a segment with one invalid node
39 na = current_nodes(:used_node_1).id
41 content("<osm><segment from='#{na}' to='#{nb}' /></osm>")
44 assert_response :precondition_failed, "upload of invalid segment did not return 'precondition failed'"
48 # check that a visible segment is returned properly
49 get :read, :id => current_segments(:visible_segment).id
50 assert_response :success
51 # TODO: check for <segment> tag in return data
53 # check that an invisible segment is not returned
54 get :read, :id => current_segments(:invisible_segment).id
57 # check chat a non-existent segment is not returned
59 assert_response :not_found
62 # this tests deletion restrictions - basic deletion is tested in the unit
66 # first try to delete segment without auth
67 delete :delete, :id => current_segments(:visible_segment).id
68 assert_response :unauthorized
71 basic_authorization("test@openstreetmap.org", "test");
74 delete :delete, :id => current_segments(:visible_segment).id
75 assert_response :success
77 # this won't work since the segment is already deleted
78 delete :delete, :id => current_segments(:invisible_segment).id
81 # this won't work since the segment never existed
82 delete :delete, :id => 0
83 assert_response :not_found
85 # this won't work since the segment is in use
86 delete :delete, :id => current_segments(:used_segment).id
87 assert_response :precondition_failed
91 def basic_authorization(user, pass)
92 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
96 @request.env["RAW_POST_DATA"] = c