From efc4c6f85de12eab4fc5c7c621d73f779570d958 Mon Sep 17 00:00:00 2001 From: Frederik Ramm Date: Thu, 26 Jul 2007 22:05:51 +0000 Subject: [PATCH] added more tests --- test/fixtures/current_nodes.yml | 33 +++++- test/fixtures/current_segments.yml | 27 +++++ test/fixtures/current_way_segments.yml | 9 ++ test/fixtures/current_way_tags.yml | 9 ++ test/fixtures/current_ways.yml | 11 ++ test/fixtures/nodes.yml | 32 ++++- test/fixtures/segments.yml | 27 +++++ test/fixtures/way_segments.yml | 11 ++ test/fixtures/way_tags.yml | 11 ++ test/fixtures/ways.yml | 13 ++ test/functional/node_controller_test.rb | 33 +++++- test/functional/segment_controller_test.rb | 102 ++++++++++++++++ test/functional/way_controller_test.rb | 132 +++++++++++++++++++++ test/unit/segment_test.rb | 104 ++++++++++++++++ 14 files changed, 549 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/current_segments.yml create mode 100644 test/fixtures/current_way_segments.yml create mode 100644 test/fixtures/current_way_tags.yml create mode 100644 test/fixtures/current_ways.yml create mode 100644 test/fixtures/segments.yml create mode 100644 test/fixtures/way_segments.yml create mode 100644 test/fixtures/way_tags.yml create mode 100644 test/fixtures/ways.yml create mode 100644 test/functional/segment_controller_test.rb create mode 100644 test/functional/way_controller_test.rb create mode 100644 test/unit/segment_test.rb diff --git a/test/fixtures/current_nodes.yml b/test/fixtures/current_nodes.yml index 3de38ab15..dd3bd2487 100644 --- a/test/fixtures/current_nodes.yml +++ b/test/fixtures/current_nodes.yml @@ -5,12 +5,41 @@ visible_node: longitude: 1 user_id: 1 visible: 1 + tags: test=yes timestamp: 2007-01-01 00:00:00 invisible_node: id: 2 - latitude: 1 - longitude: 1 + latitude: 2 + longitude: 2 user_id: 1 visible: 0 + tags: test=yes + timestamp: 2007-01-01 00:00:00 + +used_node_1: + id: 3 + latitude: 3 + longitude: 3 + user_id: 1 + visible: 1 + tags: test=yes + timestamp: 2007-01-01 00:00:00 + +used_node_2: + id: 4 + latitude: 4 + longitude: 4 + user_id: 1 + visible: 1 + tags: test=yes + timestamp: 2007-01-01 00:00:00 + +node_used_by_relationship: + id: 5 + latitude: 5 + longitude: 5 + user_id: 1 + visible: 1 + tags: test=yes timestamp: 2007-01-01 00:00:00 diff --git a/test/fixtures/current_segments.yml b/test/fixtures/current_segments.yml new file mode 100644 index 000000000..ce3758360 --- /dev/null +++ b/test/fixtures/current_segments.yml @@ -0,0 +1,27 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +visible_segment: + id: 1 + node_a: 3 + node_b: 4 + user_id: 1 + visible: 1 + tags: test=yes + timestamp: 2007-01-01 00:00:00 + +invisible_segment: + id: 2 + node_a: 3 + node_b: 4 + user_id: 1 + visible: 0 + tags: test=yes + timestamp: 2007-01-01 00:00:00 + +used_segment: + id: 3 + node_a: 3 + node_b: 4 + user_id: 1 + visible: 1 + tags: test=yes + timestamp: 2007-01-01 00:00:00 diff --git a/test/fixtures/current_way_segments.yml b/test/fixtures/current_way_segments.yml new file mode 100644 index 000000000..db82de5fc --- /dev/null +++ b/test/fixtures/current_way_segments.yml @@ -0,0 +1,9 @@ +t1: + id: 1 + segment_id: 3 + sequence_id: 1 + +t2: + id: 2 + segment_id: 3 + sequence_id: 1 diff --git a/test/fixtures/current_way_tags.yml b/test/fixtures/current_way_tags.yml new file mode 100644 index 000000000..05d876fd8 --- /dev/null +++ b/test/fixtures/current_way_tags.yml @@ -0,0 +1,9 @@ +t1: + id: 1 + k: test + v: yes + +t1: + id: 2 + k: test + v: yes diff --git a/test/fixtures/current_ways.yml b/test/fixtures/current_ways.yml new file mode 100644 index 000000000..97bc485ba --- /dev/null +++ b/test/fixtures/current_ways.yml @@ -0,0 +1,11 @@ +visible_way: + id: 1 + user_id: 1 + timestamp: 2007-01-01 00:00:00 + visible: 1 + +invisible_way: + id: 2 + user_id: 1 + timestamp: 2007-01-01 00:00:00 + visible: 0 diff --git a/test/fixtures/nodes.yml b/test/fixtures/nodes.yml index 5bdd7a52a..37152c4d3 100644 --- a/test/fixtures/nodes.yml +++ b/test/fixtures/nodes.yml @@ -10,9 +10,37 @@ visible_node: invisible_node: id: 2 - latitude: 1 - longitude: 1 + latitude: 2 + longitude: 2 user_id: 1 visible: 0 tags: test=yes timestamp: 2007-01-01 00:00:00 + +used_node_1: + id: 3 + latitude: 3 + longitude: 3 + user_id: 1 + visible: 1 + tags: test=yes + timestamp: 2007-01-01 00:00:00 + +used_node_2: + id: 4 + latitude: 4 + longitude: 4 + user_id: 1 + visible: 1 + tags: test=yes + timestamp: 2007-01-01 00:00:00 + +node_used_by_relationship: + id: 5 + latitude: 5 + longitude: 5 + user_id: 1 + visible: 1 + tags: test=yes + timestamp: 2007-01-01 00:00:00 + diff --git a/test/fixtures/segments.yml b/test/fixtures/segments.yml new file mode 100644 index 000000000..ce3758360 --- /dev/null +++ b/test/fixtures/segments.yml @@ -0,0 +1,27 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +visible_segment: + id: 1 + node_a: 3 + node_b: 4 + user_id: 1 + visible: 1 + tags: test=yes + timestamp: 2007-01-01 00:00:00 + +invisible_segment: + id: 2 + node_a: 3 + node_b: 4 + user_id: 1 + visible: 0 + tags: test=yes + timestamp: 2007-01-01 00:00:00 + +used_segment: + id: 3 + node_a: 3 + node_b: 4 + user_id: 1 + visible: 1 + tags: test=yes + timestamp: 2007-01-01 00:00:00 diff --git a/test/fixtures/way_segments.yml b/test/fixtures/way_segments.yml new file mode 100644 index 000000000..bbefff6dc --- /dev/null +++ b/test/fixtures/way_segments.yml @@ -0,0 +1,11 @@ +t1: + id: 1 + segment_id: 3 + sequence_id: 1 + version: 1 + +t2: + id: 2 + segment_id: 3 + sequence_id: 1 + version: 1 diff --git a/test/fixtures/way_tags.yml b/test/fixtures/way_tags.yml new file mode 100644 index 000000000..d1c69a6d4 --- /dev/null +++ b/test/fixtures/way_tags.yml @@ -0,0 +1,11 @@ +t1: + id: 1 + k: test + v: yes + version: 1 + +t2: + id: 2 + k: test + v: yes + version: 1 diff --git a/test/fixtures/ways.yml b/test/fixtures/ways.yml new file mode 100644 index 000000000..14489850a --- /dev/null +++ b/test/fixtures/ways.yml @@ -0,0 +1,13 @@ +visible_way: + id: 1 + user_id: 1 + timestamp: 2007-01-01 00:00:00 + visible: 1 + version: 1 + +invisible_way: + id: 2 + user_id: 1 + timestamp: 2007-01-01 00:00:00 + visible: 0 + version: 1 diff --git a/test/functional/node_controller_test.rb b/test/functional/node_controller_test.rb index afe09c91b..8d6d9b035 100644 --- a/test/functional/node_controller_test.rb +++ b/test/functional/node_controller_test.rb @@ -5,9 +5,11 @@ require 'node_controller' class NodeController; def rescue_action(e) raise e end; end class NodeControllerTest < Test::Unit::TestCase - fixtures :current_nodes, :nodes, :users + fixtures :current_nodes, :nodes, :users, :current_segments, :segments set_fixture_class :current_nodes => :Node set_fixture_class :nodes => :OldNode + set_fixture_class :current_segments => :Segment + set_fixture_class :segments => :OldSegment def setup @controller = NodeController.new @@ -50,6 +52,35 @@ class NodeControllerTest < Test::Unit::TestCase assert_response :not_found end + # this tests deletion restrictions - basic deletion is tested in the unit + # tests for node! + def test_delete + + # first try to delete node without auth + delete :delete, :id => current_nodes(:visible_node).id + assert_response :unauthorized + + # now set auth + basic_authorization("test@openstreetmap.org", "test"); + + # this should work + delete :delete, :id => current_nodes(:visible_node).id + assert_response :success + + # this won't work since the node is already deleted + delete :delete, :id => current_nodes(:invisible_node).id + assert_response :gone + + # this won't work since the node never existed + delete :delete, :id => 0 + assert_response :not_found + + # this won't work since the node is in use + delete :delete, :id => current_nodes(:used_node_1).id + assert_response :precondition_failed + end + + def basic_authorization(user, pass) @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}") end diff --git a/test/functional/segment_controller_test.rb b/test/functional/segment_controller_test.rb new file mode 100644 index 000000000..48c3142a6 --- /dev/null +++ b/test/functional/segment_controller_test.rb @@ -0,0 +1,102 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'segment_controller' + +# Re-raise errors caught by the controller. +class SegmentController; def rescue_action(e) raise e end; end + +class SegmentControllerTest < Test::Unit::TestCase + fixtures :current_nodes, :nodes, :users, :current_segments, :segments + set_fixture_class :current_nodes => :Node + set_fixture_class :nodes => :OldNode + set_fixture_class :current_segments => :Segment + set_fixture_class :segments => :OldSegment + + def setup + @controller = SegmentController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + def test_create + # cannot read password from fixture as it is stored as MD5 digest + basic_authorization("test@openstreetmap.org", "test"); + na = current_nodes(:used_node_1).id + nb = current_nodes(:used_node_2).id + content("") + put :create + # hope for success + assert_response :success, "segment upload did not return success status" + # read id of created segment and search for it + segmentid = @response.body + checksegment = Segment.find(segmentid) + assert_not_nil checksegment, "uploaded segment not found in data base after upload" + # compare values + assert_equal na, checksegment.node_a, "saved segment does not match requested from-node" + assert_equal nb, checksegment.node_b, "saved segment does not match requested to-node" + assert_equal users(:normal_user).id, checksegment.user_id, "saved segment does not belong to user that created it" + assert_equal true, checksegment.visible, "saved segment is not visible" + end + + def test_create_invalid + basic_authorization("test@openstreetmap.org", "test"); + # create a segment with one invalid node + na = current_nodes(:used_node_1).id + nb = 0 + content("") + put :create + # expect failure + assert_response :precondition_failed, "upload of invalid segment did not return 'precondition failed'" + end + + def test_read + # check that a visible segment is returned properly + get :read, :id => current_segments(:visible_segment).id + assert_response :success + # TODO: check for tag in return data + + # check that an invisible segment is not returned + get :read, :id => current_segments(:invisible_segment).id + assert_response :gone + + # check chat a non-existent segment is not returned + get :read, :id => 0 + assert_response :not_found + end + + # this tests deletion restrictions - basic deletion is tested in the unit + # tests for segment! + def test_delete + + # first try to delete segment without auth + delete :delete, :id => current_segments(:visible_segment).id + assert_response :unauthorized + + # now set auth + basic_authorization("test@openstreetmap.org", "test"); + + # this should work + delete :delete, :id => current_segments(:visible_segment).id + assert_response :success + + # this won't work since the segment is already deleted + delete :delete, :id => current_segments(:invisible_segment).id + assert_response :gone + + # this won't work since the segment never existed + delete :delete, :id => 0 + assert_response :not_found + + # this won't work since the segment is in use + delete :delete, :id => current_segments(:used_segment).id + assert_response :precondition_failed + end + + + def basic_authorization(user, pass) + @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}") + end + + def content(c) + @request.env["RAW_POST_DATA"] = c + end +end diff --git a/test/functional/way_controller_test.rb b/test/functional/way_controller_test.rb new file mode 100644 index 000000000..c5a3f0b8e --- /dev/null +++ b/test/functional/way_controller_test.rb @@ -0,0 +1,132 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'way_controller' + +# Re-raise errors caught by the controller. +class WayController; def rescue_action(e) raise e end; end + +class WayControllerTest < Test::Unit::TestCase + fixtures :current_nodes, :nodes, :users, :current_segments, :segments, :ways, :current_ways, :way_tags, :current_way_tags, :way_segments, :current_way_segments + set_fixture_class :current_ways => :Way + set_fixture_class :ways => :OldWay + set_fixture_class :current_segments => :Segment + set_fixture_class :segments => :OldSegment + + def setup + @controller = WayController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + def basic_authorization(user, pass) + @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}") + end + + def content(c) + @request.env["RAW_POST_DATA"] = c + end + + # ------------------------------------- + # Test reading ways. + # ------------------------------------- + + def test_read + # check that a visible way is returned properly + get :read, :id => current_ways(:visible_way).id + assert_response :success + + # check that an invisible way is not returned + get :read, :id => current_ways(:invisible_way).id + assert_response :gone + + # check chat a non-existent way is not returned + get :read, :id => 0 + assert_response :not_found + end + + # ------------------------------------- + # Test simple way creation. + # ------------------------------------- + + def test_create + sid = current_segments(:used_segment).id + basic_authorization "test@openstreetmap.org", "test" + + # create a way with pre-existing segment + content "" + put :create + # hope for success + assert_response :success, + "way upload did not return success status" + # read id of created way and search for it + wayid = @response.body + checkway = Way.find(wayid) + assert_not_nil checkway, + "uploaded way not found in data base after upload" + # compare values + assert_equal checkway.segs.length, 1, + "saved way does not contain exactly one segment" + assert_equal checkway.segs[0], sid, + "saved way does not contain the right segment" + assert_equal users(:normal_user).id, checkway.user_id, + "saved way does not belong to user that created it" + assert_equal true, checkway.visible, + "saved way is not visible" + end + + # ------------------------------------- + # Test creating some invalid ways. + # ------------------------------------- + + def test_create_invalid + basic_authorization "test@openstreetmap.org", "test" + + # create a way with non-existing segment + content "" + put :create + # expect failure + assert_response :precondition_failed, + "way upload with invalid segment did not return 'precondition failed'" + + # create a way with no segments + content "" + put :create + # expect failure + assert_response :precondition_failed, + "way upload with no segments did not return 'precondition failed'" + + # create a way that has the same segment, twice + # (commented out - this is currently allowed!) + #sid = current_segments(:used_segment).id + #content "" + #put :create + #assert_response :internal_server_error, + # "way upload with double segment did not return 'internal server error'" + end + + # ------------------------------------- + # Test deleting ways. + # ------------------------------------- + + def test_delete + + # first try to delete way without auth + delete :delete, :id => current_ways(:visible_way).id + assert_response :unauthorized + + # now set auth + basic_authorization("test@openstreetmap.org", "test"); + + # this should work + delete :delete, :id => current_ways(:visible_way).id + assert_response :success + + # this won't work since the way is already deleted + delete :delete, :id => current_ways(:invisible_way).id + assert_response :gone + + # this won't work since the way never existed + delete :delete, :id => 0 + assert_response :not_found + end + +end diff --git a/test/unit/segment_test.rb b/test/unit/segment_test.rb new file mode 100644 index 000000000..e8af3df6d --- /dev/null +++ b/test/unit/segment_test.rb @@ -0,0 +1,104 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class SegmentTest < Test::Unit::TestCase + fixtures :current_nodes, :nodes, :current_segments, :segments, :users + set_fixture_class :current_segments => :Segment + set_fixture_class :segments => :OldSegment + set_fixture_class :current_nodes => :Node + set_fixture_class :nodes => :OldNode + + def test_create + + segment_template = Segment.new(:node_a => nodes(:used_node_1).id, + :node_b => nodes(:used_node_2).id, + :user_id => users(:normal_user).id, + :visible => 1, + :tags => "") + assert segment_template.save_with_history + + segment = Segment.find(segment_template.id) + assert_not_nil segment + assert_equal segment_template.node_a, segment.node_a + assert_equal segment_template.node_b, segment.node_b + assert_equal segment_template.user_id, segment.user_id + assert_equal segment_template.visible, segment.visible + assert_equal segment_template.tags, segment.tags + assert_equal segment_template.timestamp.to_i, segment.timestamp.to_i + + assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 1 + old_segment = OldSegment.find(:first, :conditions => [ "id = ?", segment_template.id ]) + assert_not_nil old_segment + assert_equal segment_template.node_a, old_segment.node_a + assert_equal segment_template.node_b, old_segment.node_b + assert_equal segment_template.user_id, old_segment.user_id + assert_equal segment_template.visible, old_segment.visible + assert_equal segment_template.tags, old_segment.tags + assert_equal segment_template.timestamp.to_i, old_segment.timestamp.to_i + end + + def test_update + segment_template = Segment.find(1) + assert_not_nil segment_template + + assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 1 + old_segment_template = OldSegment.find(:first, :conditions => [ "id = ?", segment_template.id ]) + assert_not_nil old_segment_template + + segment_template.node_a = nodes(:used_node_2).id + segment_template.node_b = nodes(:used_node_1).id + segment_template.tags = "updated=yes" + assert segment_template.save_with_history + + segment = Segment.find(segment_template.id) + assert_not_nil segment + assert_equal segment_template.node_a, segment.node_a + assert_equal segment_template.node_b, segment.node_b + assert_equal segment_template.user_id, segment.user_id + assert_equal segment_template.visible, segment.visible + assert_equal segment_template.tags, segment.tags + assert_equal segment_template.timestamp.to_i, segment.timestamp.to_i + + assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 2 + assert_equal OldSegment.find(:all, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ]).length, 1 + old_segment = OldSegment.find(:first, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ]) + assert_not_nil old_segment + assert_equal segment_template.node_a, old_segment.node_a + assert_equal segment_template.node_b, old_segment.node_b + assert_equal segment_template.user_id, old_segment.user_id + assert_equal segment_template.visible, old_segment.visible + assert_equal segment_template.tags, old_segment.tags + assert_equal segment_template.timestamp.to_i, old_segment.timestamp.to_i + end + + def test_delete + segment_template = Segment.find(1) + assert_not_nil segment_template + + assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 1 + old_segment_template = OldSegment.find(:first, :conditions => [ "id = ?", segment_template.id ]) + assert_not_nil old_segment_template + + segment_template.visible = 0 + assert segment_template.save_with_history + + segment = Segment.find(segment_template.id) + assert_not_nil segment + assert_equal segment_template.node_a, segment.node_a + assert_equal segment_template.node_b, segment.node_b + assert_equal segment_template.user_id, segment.user_id + assert_equal segment_template.visible, segment.visible + assert_equal segment_template.tags, segment.tags + assert_equal segment_template.timestamp.to_i, segment.timestamp.to_i + + assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 2 + assert_equal OldSegment.find(:all, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ]).length, 1 + old_segment = OldSegment.find(:first, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ]) + assert_not_nil old_segment + assert_equal segment_template.node_a, old_segment.node_a + assert_equal segment_template.node_b, old_segment.node_b + assert_equal segment_template.user_id, old_segment.user_id + assert_equal segment_template.visible, old_segment.visible + assert_equal segment_template.tags, old_segment.tags + assert_equal segment_template.timestamp.to_i, old_segment.timestamp.to_i + end +end -- 2.39.5