source: subversion/sites/rails_port/test/functional/segment_controller_test.rb @ 3797

Revision 3797, 3.8 KB checked in by tomhughes, 7 years ago (diff)

Load way fixtures so segment delete tests will fail when segments
are in use.

Line 
1require File.dirname(__FILE__) + '/../test_helper'
2require 'segment_controller'
3
4# Re-raise errors caught by the controller.
5class SegmentController; def rescue_action(e) raise e end; end
6
7class SegmentControllerTest < Test::Unit::TestCase
8  fixtures :current_nodes, :nodes, :users, :current_segments, :segments, :current_ways, :ways, :current_way_segments, :way_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
13  set_fixture_class :current_ways => :Way
14  set_fixture_class :ways => :OldWay
15  set_fixture_class :current_way_segments => :WaySegment
16  set_fixture_class :way_segments => :OldWaySegment
17
18  def setup
19    @controller = SegmentController.new
20    @request    = ActionController::TestRequest.new
21    @response   = ActionController::TestResponse.new
22  end
23
24  def test_create
25    # cannot read password from fixture as it is stored as MD5 digest
26    basic_authorization("test@openstreetmap.org", "test"); 
27    na = current_nodes(:used_node_1).id
28    nb = current_nodes(:used_node_2).id
29    content("<osm><segment from='#{na}' to='#{nb}' /></osm>")
30    put :create
31    # hope for success
32    assert_response :success, "segment upload did not return success status"
33    # read id of created segment and search for it
34    segmentid = @response.body
35    checksegment = Segment.find(segmentid)
36    assert_not_nil checksegment, "uploaded segment not found in data base after upload"
37    # compare values
38    assert_equal na, checksegment.node_a, "saved segment does not match requested from-node"
39    assert_equal nb, checksegment.node_b, "saved segment does not match requested to-node"
40    assert_equal users(:normal_user).id, checksegment.user_id, "saved segment does not belong to user that created it"
41    assert_equal true, checksegment.visible, "saved segment is not visible"
42  end
43
44  def test_create_invalid
45    basic_authorization("test@openstreetmap.org", "test"); 
46    # create a segment with one invalid node
47    na = current_nodes(:used_node_1).id
48    nb = 0
49    content("<osm><segment from='#{na}' to='#{nb}' /></osm>")
50    put :create
51    # expect failure
52    assert_response :precondition_failed, "upload of invalid segment did not return 'precondition failed'"
53  end
54
55  def test_read
56    # check that a visible segment is returned properly
57    get :read, :id => current_segments(:visible_segment).id
58    assert_response :success
59    # TODO: check for <segment> tag in return data
60
61    # check that an invisible segment is not returned
62    get :read, :id => current_segments(:invisible_segment).id
63    assert_response :gone
64
65    # check chat a non-existent segment is not returned
66    get :read, :id => 0
67    assert_response :not_found
68  end
69
70  # this tests deletion restrictions - basic deletion is tested in the unit
71  # tests for segment!
72  def test_delete
73
74    # first try to delete segment without auth
75    delete :delete, :id => current_segments(:visible_segment).id
76    assert_response :unauthorized
77
78    # now set auth
79    basic_authorization("test@openstreetmap.org", "test"); 
80
81    # this should work
82    delete :delete, :id => current_segments(:visible_segment).id
83    assert_response :success
84
85    # this won't work since the segment is already deleted
86    delete :delete, :id => current_segments(:invisible_segment).id
87    assert_response :gone
88
89    # this won't work since the segment never existed
90    delete :delete, :id => 0
91    assert_response :not_found
92
93    # this won't work since the segment is in use
94    delete :delete, :id => current_segments(:used_segment).id
95    assert_response :precondition_failed
96  end
97
98
99  def basic_authorization(user, pass)
100    @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
101  end
102
103  def content(c)
104    @request.env["RAW_POST_DATA"] = c
105  end
106end
Note: See TracBrowser for help on using the repository browser.