Refactor some controller tests to use factories.

This commit is contained in:
Andy Allan 2017-05-31 14:51:53 +01:00
parent 938786e1ee
commit f735b07309
2 changed files with 22 additions and 15 deletions

View file

@ -1235,14 +1235,15 @@ EOF
assert_response :success assert_response :success
changeset_id = @response.body.to_i changeset_id = @response.body.to_i
old_way = current_ways(:visible_way) old_way = create(:way)
create(:way_node, :way => old_way, :node => create(:node, :lat => 1, :lon => 1))
diff = XML::Document.new diff = XML::Document.new
diff.root = XML::Node.new "osmChange" diff.root = XML::Node.new "osmChange"
modify = XML::Node.new "modify" modify = XML::Node.new "modify"
xml_old_way = old_way.to_xml_node xml_old_way = old_way.to_xml_node
nd_ref = XML::Node.new "nd" nd_ref = XML::Node.new "nd"
nd_ref["ref"] = current_nodes(:visible_node).id.to_s nd_ref["ref"] = create(:node, :lat => 3, :lon => 3).id.to_s
xml_old_way << nd_ref xml_old_way << nd_ref
xml_old_way["changeset"] = changeset_id.to_s xml_old_way["changeset"] = changeset_id.to_s
modify << xml_old_way modify << xml_old_way
@ -1510,6 +1511,9 @@ EOF
# check that the bounding box of a changeset gets updated correctly # check that the bounding box of a changeset gets updated correctly
# FIXME: This should really be moded to a integration test due to the with_controller # FIXME: This should really be moded to a integration test due to the with_controller
def test_changeset_bbox def test_changeset_bbox
way = create(:way)
create(:way_node, :way => way, :node => create(:node, :lat => 3, :lon => 3))
basic_authorization create(:user).email, "test" basic_authorization create(:user).email, "test"
# create a new changeset # create a new changeset
@ -1550,9 +1554,8 @@ EOF
# add (delete) a way to it, which contains a point at (3,3) # add (delete) a way to it, which contains a point at (3,3)
with_controller(WayController.new) do with_controller(WayController.new) do
content update_changeset(current_ways(:visible_way).to_xml, content update_changeset(way.to_xml, changeset_id)
changeset_id) put :delete, :id => way.id
put :delete, :id => current_ways(:visible_way).id
assert_response :success, "Couldn't delete a way." assert_response :success, "Couldn't delete a way."
end end

View file

@ -654,15 +654,19 @@ class RelationControllerTest < ActionController::TestCase
# add a member to a relation and check the bounding box is only that # add a member to a relation and check the bounding box is only that
# element. # element.
def test_add_member_bounding_box def test_add_member_bounding_box
relation_id = current_relations(:visible_relation).id relation = create(:relation)
node1 = create(:node, :lat => 4, :lon => 4)
node2 = create(:node, :lat => 7, :lon => 7)
way1 = create(:way)
create(:way_node, :way => way1, :node => create(:node, :lat => 8, :lon => 8))
way2 = create(:way)
create(:way_node, :way => way2, :node => create(:node, :lat => 9, :lon => 9), :sequence_id => 1)
create(:way_node, :way => way2, :node => create(:node, :lat => 10, :lon => 10), :sequence_id => 2)
[current_nodes(:used_node_1), [node1, node2, way1, way2].each do |element|
current_nodes(:used_node_2),
current_ways(:used_way),
current_ways(:way_with_versions)].each_with_index do |element, _version|
bbox = element.bbox.to_unscaled bbox = element.bbox.to_unscaled
check_changeset_modify(bbox) do |changeset_id| check_changeset_modify(bbox) do |changeset_id|
relation_xml = Relation.find(relation_id).to_xml relation_xml = Relation.find(relation.id).to_xml
relation_element = relation_xml.find("//osm/relation").first relation_element = relation_xml.find("//osm/relation").first
new_member = XML::Node.new("member") new_member = XML::Node.new("member")
new_member["ref"] = element.id.to_s new_member["ref"] = element.id.to_s
@ -675,11 +679,11 @@ class RelationControllerTest < ActionController::TestCase
# upload the change # upload the change
content relation_xml content relation_xml
put :update, :id => current_relations(:visible_relation).id put :update, :id => relation.id
assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}" assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}"
# get it back and check the ordering # get it back and check the ordering
get :read, :id => relation_id get :read, :id => relation.id
assert_response :success, "can't read back the relation: #{@response.body}" assert_response :success, "can't read back the relation: #{@response.body}"
check_ordering(relation_xml, @response.body) check_ordering(relation_xml, @response.body)
end end
@ -907,7 +911,7 @@ OSM
# that the changeset bounding box is +bbox+. # that the changeset bounding box is +bbox+.
def check_changeset_modify(bbox) def check_changeset_modify(bbox)
## First test with the private user to check that you get a forbidden ## First test with the private user to check that you get a forbidden
basic_authorization(users(:normal_user).email, "test") basic_authorization(create(:user, :data_public => false).email, "test")
# create a new changeset for this operation, so we are assured # create a new changeset for this operation, so we are assured
# that the bounding box will be newly-generated. # that the bounding box will be newly-generated.
@ -918,7 +922,7 @@ OSM
end end
## Now do the whole thing with the public user ## Now do the whole thing with the public user
basic_authorization(users(:public_user).email, "test") basic_authorization(create(:user).email, "test")
# create a new changeset for this operation, so we are assured # create a new changeset for this operation, so we are assured
# that the bounding box will be newly-generated. # that the bounding box will be newly-generated.