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
8 fixtures :current_nodes, :nodes, :users, :current_segments, :segments
9 set_fixture_class :current_nodes => :Node
10 set_fixture_class :nodes => :OldNode
11 set_fixture_class :current_segments => :Segment
12 set_fixture_class :segments => :OldSegment
15 @controller = SegmentController.new
16 @request = ActionController::TestRequest.new
17 @response = ActionController::TestResponse.new
21 # cannot read password from fixture as it is stored as MD5 digest
22 basic_authorization("test@openstreetmap.org", "test");
23 na = current_nodes(:used_node_1).id
24 nb = current_nodes(:used_node_2).id
25 content("<osm><segment from='#{na}' to='#{nb}' /></osm>")
28 assert_response :success, "segment upload did not return success status"
29 # read id of created segment and search for it
30 segmentid = @response.body
31 checksegment = Segment.find(segmentid)
32 assert_not_nil checksegment, "uploaded segment not found in data base after upload"
34 assert_equal na, checksegment.node_a, "saved segment does not match requested from-node"
35 assert_equal nb, checksegment.node_b, "saved segment does not match requested to-node"
36 assert_equal users(:normal_user).id, checksegment.user_id, "saved segment does not belong to user that created it"
37 assert_equal true, checksegment.visible, "saved segment is not visible"
40 def test_create_invalid
41 basic_authorization("test@openstreetmap.org", "test");
42 # create a segment with one invalid node
43 na = current_nodes(:used_node_1).id
45 content("<osm><segment from='#{na}' to='#{nb}' /></osm>")
48 assert_response :precondition_failed, "upload of invalid segment did not return 'precondition failed'"
52 # check that a visible segment is returned properly
53 get :read, :id => current_segments(:visible_segment).id
54 assert_response :success
55 # TODO: check for <segment> tag in return data
57 # check that an invisible segment is not returned
58 get :read, :id => current_segments(:invisible_segment).id
61 # check chat a non-existent segment is not returned
63 assert_response :not_found
66 # this tests deletion restrictions - basic deletion is tested in the unit
70 # first try to delete segment without auth
71 delete :delete, :id => current_segments(:visible_segment).id
72 assert_response :unauthorized
75 basic_authorization("test@openstreetmap.org", "test");
78 delete :delete, :id => current_segments(:visible_segment).id
79 assert_response :success
81 # this won't work since the segment is already deleted
82 delete :delete, :id => current_segments(:invisible_segment).id
85 # this won't work since the segment never existed
86 delete :delete, :id => 0
87 assert_response :not_found
89 # this won't work since the segment is in use
90 delete :delete, :id => current_segments(:used_segment).id
91 assert_response :precondition_failed
95 def basic_authorization(user, pass)
96 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
100 @request.env["RAW_POST_DATA"] = c