From: Tom Hughes Date: Wed, 10 May 2017 16:01:06 +0000 (+0100) Subject: Merge remote-tracking branch 'openstreetmap/pull/1538' X-Git-Tag: live~3536 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/7d95a576cd28c51ea60ec498713838adda7be06c?hp=b1d44c77ad012bba00b3283b966f76638ceb81e7 Merge remote-tracking branch 'openstreetmap/pull/1538' --- diff --git a/test/controllers/api_controller_test.rb b/test/controllers/api_controller_test.rb index 7d249acf3..0715a64ad 100644 --- a/test/controllers/api_controller_test.rb +++ b/test/controllers/api_controller_test.rb @@ -52,8 +52,12 @@ class ApiControllerTest < ActionController::TestCase # ------------------------------------- def test_map - node = current_nodes(:used_node_1) + node = create(:node, :lat => 7, :lon => 7) tag = create(:node_tag, :node => node) + way1 = create(:way_node, :node => node).way + way2 = create(:way_node, :node => node).way + relation = create(:relation_member, :member => node).relation + # Need to split the min/max lat/lon out into their own variables here # so that we can test they are returned later. minlon = node.lon - 0.1 @@ -74,18 +78,22 @@ class ApiControllerTest < ActionController::TestCase assert_select "tag[k='#{tag.k}'][v='#{tag.v}']" end assert_select "way", :count => 2 - assert_select "way[id='1']", :count => 1 - assert_select "way[id='3']", :count => 1 + assert_select "way[id='#{way1.id}']", :count => 1 + assert_select "way[id='#{way2.id}']", :count => 1 assert_select "relation", :count => 1 - assert_select "relation[id='1']", :count => 1 + assert_select "relation[id='#{relation.id}']", :count => 1 end end # This differs from the above test in that we are making the bbox exactly # the same as the node we are looking at def test_map_inclusive - node = current_nodes(:used_node_1) + node = create(:node, :lat => 7, :lon => 7) tag = create(:node_tag, :node => node) + way1 = create(:way_node, :node => node).way + way2 = create(:way_node, :node => node).way + relation = create(:relation_member, :member => node).relation + bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}" get :map, :bbox => bbox assert_response :success, "The map call should have succeeded" @@ -96,29 +104,38 @@ class ApiControllerTest < ActionController::TestCase assert_select "tag[k='#{tag.k}'][v='#{tag.v}']" end assert_select "way", :count => 2 - assert_select "way[id='1']", :count => 1 - assert_select "way[id='3']", :count => 1 + assert_select "way[id='#{way1.id}']", :count => 1 + assert_select "way[id='#{way2.id}']", :count => 1 assert_select "relation", :count => 1 - assert_select "relation[id='1']", :count => 1 + assert_select "relation[id='#{relation.id}']", :count => 1 end end def test_map_complete_way - node = current_nodes(:used_node_2) + node = create(:node, :lat => 7, :lon => 7) + # create a couple of nodes well outside of the bbox + node2 = create(:node, :lat => 45, :lon => 45) + node3 = create(:node, :lat => 10, :lon => 10) + way1 = create(:way_node, :node => node).way + create(:way_node, :way => way1, :node => node2, :sequence_id => 2) + way2 = create(:way_node, :node => node).way + create(:way_node, :way => way2, :node => node3, :sequence_id => 2) + relation = create(:relation_member, :member => way1).relation + bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}" get :map, :bbox => bbox assert_response :success, "The map call should have succeeded" assert_select "osm[version='#{API_VERSION}'][generator='#{GENERATOR}']", :count => 1 do assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1 assert_select "node", :count => 3 - assert_select "node[id='4']", :count => 1 - assert_select "node[id='11']", :count => 1 - assert_select "node[id='15']", :count => 1 + assert_select "node[id='#{node.id}']", :count => 1 + assert_select "node[id='#{node2.id}']", :count => 1 + assert_select "node[id='#{node3.id}']", :count => 1 assert_select "way", :count => 2 - assert_select "way[id='5']", :count => 1 - assert_select "way[id='7']", :count => 1 + assert_select "way[id='#{way1.id}']", :count => 1 + assert_select "way[id='#{way2.id}']", :count => 1 assert_select "relation", :count => 1 - assert_select "relation[id='8']", :count => 1 + assert_select "relation[id='#{relation.id}']", :count => 1 end end diff --git a/test/controllers/old_node_controller_test.rb b/test/controllers/old_node_controller_test.rb index babb9df13..0fcc46310 100644 --- a/test/controllers/old_node_controller_test.rb +++ b/test/controllers/old_node_controller_test.rb @@ -33,13 +33,21 @@ class OldNodeControllerTest < ActionController::TestCase ## # FIXME: Move this test to being an integration test since it spans multiple controllers def test_version + private_user = create(:user, :data_public => false) + private_node = create(:node, :with_history, :version => 4, :changeset => create(:changeset, :user => private_user)) + user = create(:user) + node = create(:node, :with_history, :version => 4, :changeset => create(:changeset, :user => user)) + create_list(:node_tag, 2, :node => node) + # Ensure that the current tags are propagated to the history too + propagate_tags(node, node.old_nodes.last) + ## First try this with a non-public user - basic_authorization(users(:normal_user).email, "test") + basic_authorization(private_user.email, "test") # setup a simple XML node - xml_doc = current_nodes(:visible_node).to_xml + xml_doc = private_node.to_xml xml_node = xml_doc.find("//osm/node").first - nodeid = current_nodes(:visible_node).id + nodeid = private_node.id # keep a hash of the versions => string, as we'll need something # to test against later @@ -83,16 +91,13 @@ class OldNodeControllerTest < ActionController::TestCase # probably should check that they didn't get written to the database ## Now do it with the public user - basic_authorization(users(:public_user).email, "test") + basic_authorization(user.email, "test") # setup a simple XML node - create_list(:node_tag, 2, :node => current_nodes(:node_with_versions)) - xml_doc = current_nodes(:node_with_versions).to_xml - xml_node = xml_doc.find("//osm/node").first - nodeid = current_nodes(:node_with_versions).id - # Ensure that the current tags are propagated to the history too - propagate_tags(current_nodes(:node_with_versions), nodes(:node_with_versions_v4)) + xml_doc = node.to_xml + xml_node = xml_doc.find("//osm/node").first + nodeid = node.id # keep a hash of the versions => string, as we'll need something # to test against later @@ -150,8 +155,8 @@ class OldNodeControllerTest < ActionController::TestCase def test_not_found_version check_not_found_id_version(70000, 312344) check_not_found_id_version(-1, -13) - check_not_found_id_version(nodes(:visible_node).id, 24354) - check_not_found_id_version(24356, nodes(:visible_node).version) + check_not_found_id_version(create(:node).id, 24354) + check_not_found_id_version(24356, create(:node).version) end def check_not_found_id_version(id, version) @@ -165,29 +170,36 @@ class OldNodeControllerTest < ActionController::TestCase # Test that getting the current version is identical to picking # that version with the version URI call. def test_current_version - create(:node_tag, :node => current_nodes(:visible_node)) - create(:node_tag, :node => current_nodes(:used_node_1)) - create(:node_tag, :node => current_nodes(:used_node_2)) - create(:node_tag, :node => current_nodes(:node_used_by_relationship)) - create(:node_tag, :node => current_nodes(:node_with_versions)) - propagate_tags(current_nodes(:visible_node), nodes(:visible_node)) - propagate_tags(current_nodes(:used_node_1), nodes(:used_node_1)) - propagate_tags(current_nodes(:used_node_2), nodes(:used_node_2)) - propagate_tags(current_nodes(:node_used_by_relationship), nodes(:node_used_by_relationship)) - propagate_tags(current_nodes(:node_with_versions), nodes(:node_with_versions_v4)) - - check_current_version(current_nodes(:visible_node)) - check_current_version(current_nodes(:used_node_1)) - check_current_version(current_nodes(:used_node_2)) - check_current_version(current_nodes(:node_used_by_relationship)) - check_current_version(current_nodes(:node_with_versions)) + node = create(:node, :with_history) + used_node = create(:node, :with_history) + create(:way_node, :node => used_node) + node_used_by_relationship = create(:node, :with_history) + create(:relation_member, :member => node_used_by_relationship) + node_with_versions = create(:node, :with_history, :version => 4) + + create(:node_tag, :node => node) + create(:node_tag, :node => used_node) + create(:node_tag, :node => node_used_by_relationship) + create(:node_tag, :node => node_with_versions) + propagate_tags(node, node.old_nodes.last) + propagate_tags(used_node, used_node.old_nodes.last) + propagate_tags(node_used_by_relationship, node_used_by_relationship.old_nodes.last) + propagate_tags(node_with_versions, node_with_versions.old_nodes.last) + + check_current_version(node) + check_current_version(used_node) + check_current_version(node_used_by_relationship) + check_current_version(node_with_versions) end ## # test the redaction of an old version of a node, while not being # authorised. def test_redact_node_unauthorised - do_redact_node(nodes(:node_with_versions_v3), + node = create(:node, :with_history, :version => 4) + node_v3 = node.old_nodes.find_by(:version => 3) + + do_redact_node(node_v3, create(:redaction)) assert_response :unauthorized, "should need to be authenticated to redact." end @@ -198,7 +210,10 @@ class OldNodeControllerTest < ActionController::TestCase def test_redact_node_normal_user basic_authorization(create(:user).email, "test") - do_redact_node(nodes(:node_with_versions_v3), + node = create(:node, :with_history, :version => 4) + node_v3 = node.old_nodes.find_by(:version => 3) + + do_redact_node(node_v3, create(:redaction)) assert_response :forbidden, "should need to be moderator to redact." end @@ -209,7 +224,10 @@ class OldNodeControllerTest < ActionController::TestCase def test_redact_node_current_version basic_authorization(create(:moderator_user).email, "test") - do_redact_node(nodes(:node_with_versions_v4), + node = create(:node, :with_history, :version => 4) + node_v4 = node.old_nodes.find_by(:version => 4) + + do_redact_node(node_v4, create(:redaction)) assert_response :bad_request, "shouldn't be OK to redact current version as moderator." end @@ -218,88 +236,96 @@ class OldNodeControllerTest < ActionController::TestCase # test that redacted nodes aren't visible, regardless of # authorisation except as moderator... def test_version_redacted - node = nodes(:redacted_node_redacted_version) + node = create(:node, :with_history, :version => 2) + node_v1 = node.old_nodes.find_by(:version => 1) + node_v1.redact!(create(:redaction)) - get :version, :id => node.node_id, :version => node.version + get :version, :id => node_v1.node_id, :version => node_v1.version assert_response :forbidden, "Redacted node shouldn't be visible via the version API." # not even to a logged-in user - basic_authorization(users(:public_user).email, "test") - get :version, :id => node.node_id, :version => node.version + basic_authorization(create(:user).email, "test") + get :version, :id => node_v1.node_id, :version => node_v1.version assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in." end ## # test that redacted nodes aren't visible in the history def test_history_redacted - node = nodes(:redacted_node_redacted_version) + node = create(:node, :with_history, :version => 2) + node_v1 = node.old_nodes.find_by(:version => 1) + node_v1.redact!(create(:redaction)) - get :history, :id => node.node_id + get :history, :id => node_v1.node_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history." + assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0, "redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history." # not even to a logged-in user - basic_authorization(users(:public_user).email, "test") - get :history, :id => node.node_id + basic_authorization(create(:user).email, "test") + get :history, :id => node_v1.node_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history, even when logged in." + assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 0, "redacted node #{node_v1.node_id} version #{node_v1.version} shouldn't be present in the history, even when logged in." end ## # test the redaction of an old version of a node, while being # authorised as a moderator. def test_redact_node_moderator - node = nodes(:node_with_versions_v3) + node = create(:node, :with_history, :version => 4) + node_v3 = node.old_nodes.find_by(:version => 3) basic_authorization(create(:moderator_user).email, "test") - do_redact_node(node, create(:redaction)) + do_redact_node(node_v3, create(:redaction)) assert_response :success, "should be OK to redact old version as moderator." # check moderator can still see the redacted data, when passing # the appropriate flag - get :version, :id => node.node_id, :version => node.version + get :version, :id => node_v3.node_id, :version => node_v3.version assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed." - get :version, :id => node.node_id, :version => node.version, :show_redactions => "true" + get :version, :id => node_v3.node_id, :version => node_v3.version, :show_redactions => "true" assert_response :success, "After redaction, node should not be gone for moderator, when flag passed." # and when accessed via history - get :history, :id => node.node_id + get :history, :id => node_v3.node_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 0, "node #{node.node_id} version #{node.version} should not be present in the history for moderators when not passing flag." - get :history, :id => node.node_id, :show_redactions => "true" + assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0, "node #{node_v3.node_id} version #{node_v3.version} should not be present in the history for moderators when not passing flag." + get :history, :id => node_v3.node_id, :show_redactions => "true" assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 1, "node #{node.node_id} version #{node.version} should still be present in the history for moderators when passing flag." + assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 1, "node #{node_v3.node_id} version #{node_v3.version} should still be present in the history for moderators when passing flag." end # testing that if the moderator drops auth, he can't see the # redacted stuff any more. def test_redact_node_is_redacted - node = nodes(:node_with_versions_v3) + node = create(:node, :with_history, :version => 4) + node_v3 = node.old_nodes.find_by(:version => 3) basic_authorization(create(:moderator_user).email, "test") - do_redact_node(node, create(:redaction)) + do_redact_node(node_v3, create(:redaction)) assert_response :success, "should be OK to redact old version as moderator." # re-auth as non-moderator basic_authorization(create(:user).email, "test") # check can't see the redacted data - get :version, :id => node.node_id, :version => node.version + get :version, :id => node_v3.node_id, :version => node_v3.version assert_response :forbidden, "Redacted node shouldn't be visible via the version API." # and when accessed via history - get :history, :id => node.node_id + get :history, :id => node_v3.node_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 0, "redacted node #{node.node_id} version #{node.version} shouldn't be present in the history." + assert_select "osm node[id='#{node_v3.node_id}'][version='#{node_v3.version}']", 0, "redacted node #{node_v3.node_id} version #{node_v3.version} shouldn't be present in the history." end ## # test the unredaction of an old version of a node, while not being # authorised. def test_unredact_node_unauthorised - node = nodes(:redacted_node_redacted_version) + node = create(:node, :with_history, :version => 2) + node_v1 = node.old_nodes.find_by(:version => 1) + node_v1.redact!(create(:redaction)) - post :redact, :id => node.node_id, :version => node.version + post :redact, :id => node_v1.node_id, :version => node_v1.version assert_response :unauthorized, "should need to be authenticated to unredact." end @@ -308,10 +334,13 @@ class OldNodeControllerTest < ActionController::TestCase # authorised as a normal user. def test_unredact_node_normal_user user = create(:user) - node = nodes(:redacted_node_redacted_version) + node = create(:node, :with_history, :version => 2) + node_v1 = node.old_nodes.find_by(:version => 1) + node_v1.redact!(create(:redaction)) + basic_authorization(user.email, "test") - post :redact, :id => node.node_id, :version => node.version + post :redact, :id => node_v1.node_id, :version => node_v1.version assert_response :forbidden, "should need to be moderator to unredact." end @@ -320,32 +349,35 @@ class OldNodeControllerTest < ActionController::TestCase # authorised as a moderator. def test_unredact_node_moderator moderator_user = create(:moderator_user) - node = nodes(:redacted_node_redacted_version) + node = create(:node, :with_history, :version => 2) + node_v1 = node.old_nodes.find_by(:version => 1) + node_v1.redact!(create(:redaction)) + basic_authorization(moderator_user.email, "test") - post :redact, :id => node.node_id, :version => node.version - assert_response :success, "should be OK to redact old version as moderator." + post :redact, :id => node_v1.node_id, :version => node_v1.version + assert_response :success, "should be OK to unredact old version as moderator." # check moderator can now see the redacted data, when not # passing the aspecial flag - get :version, :id => node.node_id, :version => node.version + get :version, :id => node_v1.node_id, :version => node_v1.version assert_response :success, "After unredaction, node should not be gone for moderator." # and when accessed via history - get :history, :id => node.node_id + get :history, :id => node_v1.node_id assert_response :success, "Unredaction shouldn't have stopped history working." - assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 1, "node #{node.node_id} version #{node.version} should now be present in the history for moderators without passing flag." + assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1, "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for moderators without passing flag." basic_authorization(create(:user).email, "test") # check normal user can now see the redacted data - get :version, :id => node.node_id, :version => node.version + get :version, :id => node_v1.node_id, :version => node_v1.version assert_response :success, "After unredaction, node should be visible to normal users." # and when accessed via history - get :history, :id => node.node_id + get :history, :id => node_v1.node_id assert_response :success, "Unredaction shouldn't have stopped history working." - assert_select "osm node[id='#{node.node_id}'][version='#{node.version}']", 1, "node #{node.node_id} version #{node.version} should now be present in the history for normal users without passing flag." + assert_select "osm node[id='#{node_v1.node_id}'][version='#{node_v1.version}']", 1, "node #{node_v1.node_id} version #{node_v1.version} should now be present in the history for normal users without passing flag." end private diff --git a/test/controllers/old_relation_controller_test.rb b/test/controllers/old_relation_controller_test.rb index 1c7e691f4..f15acf849 100644 --- a/test/controllers/old_relation_controller_test.rb +++ b/test/controllers/old_relation_controller_test.rb @@ -26,7 +26,7 @@ class OldRelationControllerTest < ActionController::TestCase # ------------------------------------- def test_history # check that a visible relations is returned properly - get :history, :id => relations(:visible_relation).relation_id + get :history, :id => create(:relation, :with_history).id assert_response :success # check chat a non-existent relations is not returned @@ -38,8 +38,10 @@ class OldRelationControllerTest < ActionController::TestCase # test the redaction of an old version of a relation, while not being # authorised. def test_redact_relation_unauthorised - do_redact_relation(relations(:relation_with_versions_v3), - create(:redaction)) + relation = create(:relation, :with_history, :version => 4) + relation_v3 = relation.old_relations.find_by(:version => 3) + + do_redact_relation(relation_v3, create(:redaction)) assert_response :unauthorized, "should need to be authenticated to redact." end @@ -47,10 +49,12 @@ class OldRelationControllerTest < ActionController::TestCase # test the redaction of an old version of a relation, while being # authorised as a normal user. def test_redact_relation_normal_user + relation = create(:relation, :with_history, :version => 4) + relation_v3 = relation.old_relations.find_by(:version => 3) + basic_authorization(create(:user).email, "test") - do_redact_relation(relations(:relation_with_versions_v3), - create(:redaction)) + do_redact_relation(relation_v3, create(:redaction)) assert_response :forbidden, "should need to be moderator to redact." end @@ -58,10 +62,12 @@ class OldRelationControllerTest < ActionController::TestCase # test that, even as moderator, the current version of a relation # can't be redacted. def test_redact_relation_current_version + relation = create(:relation, :with_history, :version => 4) + relation_latest = relation.old_relations.last + basic_authorization(create(:moderator_user).email, "test") - do_redact_relation(relations(:relation_with_versions_v4), - create(:redaction)) + do_redact_relation(relation_latest, create(:redaction)) assert_response :bad_request, "shouldn't be OK to redact current version as moderator." end @@ -69,89 +75,99 @@ class OldRelationControllerTest < ActionController::TestCase # test that redacted relations aren't visible, regardless of # authorisation except as moderator... def test_version_redacted - relation = relations(:relation_with_redacted_versions_v2) + relation = create(:relation, :with_history, :version => 2) + relation_v1 = relation.old_relations.find_by(:version => 1) + relation_v1.redact!(create(:redaction)) - get :version, :id => relation.relation_id, :version => relation.version - assert_response :forbidden, "Redacted node shouldn't be visible via the version API." + get :version, :id => relation_v1.relation_id, :version => relation_v1.version + assert_response :forbidden, "Redacted relation shouldn't be visible via the version API." # not even to a logged-in user - basic_authorization(users(:public_user).email, "test") - get :version, :id => relation.relation_id, :version => relation.version - assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in." + basic_authorization(create(:user).email, "test") + get :version, :id => relation_v1.relation_id, :version => relation_v1.version + assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in." end ## - # test that redacted nodes aren't visible in the history + # test that redacted relations aren't visible in the history def test_history_redacted - relation = relations(:relation_with_redacted_versions_v2) + relation = create(:relation, :with_history, :version => 2) + relation_v1 = relation.old_relations.find_by(:version => 1) + relation_v1.redact!(create(:redaction)) - get :history, :id => relation.relation_id + get :history, :id => relation_v1.relation_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 0, "redacted relation #{relation.relation_id} version #{relation.version} shouldn't be present in the history." + assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0, "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history." # not even to a logged-in user - basic_authorization(users(:public_user).email, "test") - get :version, :id => relation.relation_id, :version => relation.version - get :history, :id => relation.relation_id + basic_authorization(create(:user).email, "test") + get :version, :id => relation_v1.relation_id, :version => relation_v1.version + get :history, :id => relation_v1.relation_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 0, "redacted node #{relation.relation_id} version #{relation.version} shouldn't be present in the history, even when logged in." + assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 0, "redacted relation #{relation_v1.relation_id} version #{relation_v1.version} shouldn't be present in the history, even when logged in." end ## # test the redaction of an old version of a relation, while being # authorised as a moderator. def test_redact_relation_moderator - relation = relations(:relation_with_versions_v3) + relation = create(:relation, :with_history, :version => 4) + relation_v3 = relation.old_relations.find_by(:version => 3) + basic_authorization(create(:moderator_user).email, "test") - do_redact_relation(relation, create(:redaction)) + do_redact_relation(relation_v3, create(:redaction)) assert_response :success, "should be OK to redact old version as moderator." # check moderator can still see the redacted data, when passing # the appropriate flag - get :version, :id => relation.relation_id, :version => relation.version - assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed." - get :version, :id => relation.relation_id, :version => relation.version, :show_redactions => "true" - assert_response :success, "After redaction, node should not be gone for moderator, when flag passed." + get :version, :id => relation_v3.relation_id, :version => relation_v3.version + assert_response :forbidden, "After redaction, relation should be gone for moderator, when flag not passed." + get :version, :id => relation_v3.relation_id, :version => relation_v3.version, :show_redactions => "true" + assert_response :success, "After redaction, relation should not be gone for moderator, when flag passed." # and when accessed via history - get :history, :id => relation.relation_id + get :history, :id => relation_v3.relation_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 0, "relation #{relation.relation_id} version #{relation.version} should not be present in the history for moderators when not passing flag." - get :history, :id => relation.relation_id, :show_redactions => "true" + assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0, "relation #{relation_v3.relation_id} version #{relation_v3.version} should not be present in the history for moderators when not passing flag." + get :history, :id => relation_v3.relation_id, :show_redactions => "true" assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 1, "relation #{relation.relation_id} version #{relation.version} should still be present in the history for moderators when passing flag." + assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 1, "relation #{relation_v3.relation_id} version #{relation_v3.version} should still be present in the history for moderators when passing flag." end # testing that if the moderator drops auth, he can't see the # redacted stuff any more. def test_redact_relation_is_redacted - relation = relations(:relation_with_versions_v3) + relation = create(:relation, :with_history, :version => 4) + relation_v3 = relation.old_relations.find_by(:version => 3) + basic_authorization(create(:moderator_user).email, "test") - do_redact_relation(relation, create(:redaction)) + do_redact_relation(relation_v3, create(:redaction)) assert_response :success, "should be OK to redact old version as moderator." # re-auth as non-moderator - basic_authorization(users(:public_user).email, "test") + basic_authorization(create(:user).email, "test") # check can't see the redacted data - get :version, :id => relation.relation_id, :version => relation.version - assert_response :forbidden, "Redacted node shouldn't be visible via the version API." + get :version, :id => relation_v3.relation_id, :version => relation_v3.version + assert_response :forbidden, "Redacted relation shouldn't be visible via the version API." # and when accessed via history - get :history, :id => relation.relation_id + get :history, :id => relation_v3.relation_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 0, "redacted relation #{relation.relation_id} version #{relation.version} shouldn't be present in the history." + assert_select "osm relation[id='#{relation_v3.relation_id}'][version='#{relation_v3.version}']", 0, "redacted relation #{relation_v3.relation_id} version #{relation_v3.version} shouldn't be present in the history." end ## # test the unredaction of an old version of a relation, while not being # authorised. def test_unredact_relation_unauthorised - relation = relations(:relation_with_redacted_versions_v3) + relation = create(:relation, :with_history, :version => 2) + relation_v1 = relation.old_relations.find_by(:version => 1) + relation_v1.redact!(create(:redaction)) - post :redact, :id => relation.relation_id, :version => relation.version + post :redact, :id => relation_v1.relation_id, :version => relation_v1.version assert_response :unauthorized, "should need to be authenticated to unredact." end @@ -159,10 +175,13 @@ class OldRelationControllerTest < ActionController::TestCase # test the unredaction of an old version of a relation, while being # authorised as a normal user. def test_unredact_relation_normal_user - relation = relations(:relation_with_redacted_versions_v3) - basic_authorization(users(:public_user).email, "test") + relation = create(:relation, :with_history, :version => 2) + relation_v1 = relation.old_relations.find_by(:version => 1) + relation_v1.redact!(create(:redaction)) + + basic_authorization(create(:user).email, "test") - post :redact, :id => relation.relation_id, :version => relation.version + post :redact, :id => relation_v1.relation_id, :version => relation_v1.version assert_response :forbidden, "should need to be moderator to unredact." end @@ -170,32 +189,35 @@ class OldRelationControllerTest < ActionController::TestCase # test the unredaction of an old version of a relation, while being # authorised as a moderator. def test_unredact_relation_moderator - relation = relations(:relation_with_redacted_versions_v3) - basic_authorization(users(:moderator_user).email, "test") + relation = create(:relation, :with_history, :version => 2) + relation_v1 = relation.old_relations.find_by(:version => 1) + relation_v1.redact!(create(:redaction)) - post :redact, :id => relation.relation_id, :version => relation.version + basic_authorization(create(:moderator_user).email, "test") + + post :redact, :id => relation_v1.relation_id, :version => relation_v1.version assert_response :success, "should be OK to unredact old version as moderator." # check moderator can still see the redacted data, without passing # the appropriate flag - get :version, :id => relation.relation_id, :version => relation.version - assert_response :success, "After redaction, node should not be gone for moderator, when flag passed." + get :version, :id => relation_v1.relation_id, :version => relation_v1.version + assert_response :success, "After unredaction, relation should not be gone for moderator." # and when accessed via history - get :history, :id => relation.relation_id + get :history, :id => relation_v1.relation_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 1, "relation #{relation.relation_id} version #{relation.version} should still be present in the history for moderators when passing flag." + assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1, "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for moderators." - basic_authorization(users(:normal_user).email, "test") + basic_authorization(create(:user).email, "test") # check normal user can now see the redacted data - get :version, :id => relation.relation_id, :version => relation.version - assert_response :success, "After redaction, node should not be gone for moderator, when flag passed." + get :version, :id => relation_v1.relation_id, :version => relation_v1.version + assert_response :success, "After redaction, node should not be gone for normal user." # and when accessed via history - get :history, :id => relation.relation_id + get :history, :id => relation_v1.relation_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm relation[id='#{relation.relation_id}'][version='#{relation.version}']", 1, "relation #{relation.relation_id} version #{relation.version} should still be present in the history for moderators when passing flag." + assert_select "osm relation[id='#{relation_v1.relation_id}'][version='#{relation_v1.version}']", 1, "relation #{relation_v1.relation_id} version #{relation_v1.version} should still be present in the history for normal users." end private diff --git a/test/controllers/old_way_controller_test.rb b/test/controllers/old_way_controller_test.rb index 9e779abb5..eae9d8a2e 100644 --- a/test/controllers/old_way_controller_test.rb +++ b/test/controllers/old_way_controller_test.rb @@ -27,13 +27,13 @@ class OldWayControllerTest < ActionController::TestCase def test_history_visible # check that a visible way is returned properly - get :history, :id => ways(:visible_way).way_id + get :history, :id => create(:way, :with_history).id assert_response :success end def test_history_invisible # check that an invisible way's history is returned properly - get :history, :id => ways(:invisible_way).way_id + get :history, :id => create(:way, :with_history, :deleted).id assert_response :success end @@ -46,33 +46,45 @@ class OldWayControllerTest < ActionController::TestCase ## # check that we can retrieve versions of a way def test_version - create(:way_tag, :way => current_ways(:visible_way)) - create(:way_tag, :way => current_ways(:used_way)) - create(:way_tag, :way => current_ways(:way_with_versions)) - propagate_tags(current_ways(:visible_way), ways(:visible_way)) - propagate_tags(current_ways(:used_way), ways(:used_way)) - propagate_tags(current_ways(:way_with_versions), ways(:way_with_versions_v4)) - - check_current_version(current_ways(:visible_way).id) - check_current_version(current_ways(:used_way).id) - check_current_version(current_ways(:way_with_versions).id) + way = create(:way, :with_history) + used_way = create(:way, :with_history) + create(:relation_member, :member => used_way) + way_with_versions = create(:way, :with_history, :version => 4) + + create(:way_tag, :way => way) + create(:way_tag, :way => used_way) + create(:way_tag, :way => way_with_versions) + propagate_tags(way, way.old_ways.last) + propagate_tags(used_way, used_way.old_ways.last) + propagate_tags(way_with_versions, way_with_versions.old_ways.last) + + check_current_version(way.id) + check_current_version(used_way.id) + check_current_version(way_with_versions.id) end ## # check that returned history is the same as getting all # versions of a way from the api. def test_history_equals_versions - check_history_equals_versions(current_ways(:visible_way).id) - check_history_equals_versions(current_ways(:used_way).id) - check_history_equals_versions(current_ways(:way_with_versions).id) + way = create(:way, :with_history) + used_way = create(:way, :with_history) + create(:relation_member, :member => used_way) + way_with_versions = create(:way, :with_history, :version => 4) + + check_history_equals_versions(way.id) + check_history_equals_versions(used_way.id) + check_history_equals_versions(way_with_versions.id) end ## # test the redaction of an old version of a way, while not being # authorised. def test_redact_way_unauthorised - do_redact_way(ways(:way_with_versions_v3), - create(:redaction)) + way = create(:way, :with_history, :version => 4) + way_v3 = way.old_ways.find_by(:version => 3) + + do_redact_way(way_v3, create(:redaction)) assert_response :unauthorized, "should need to be authenticated to redact." end @@ -81,9 +93,10 @@ class OldWayControllerTest < ActionController::TestCase # authorised as a normal user. def test_redact_way_normal_user basic_authorization(create(:user).email, "test") + way = create(:way, :with_history, :version => 4) + way_v3 = way.old_ways.find_by(:version => 3) - do_redact_way(ways(:way_with_versions_v3), - create(:redaction)) + do_redact_way(way_v3, create(:redaction)) assert_response :forbidden, "should need to be moderator to redact." end @@ -92,9 +105,10 @@ class OldWayControllerTest < ActionController::TestCase # can't be redacted. def test_redact_way_current_version basic_authorization(create(:moderator_user).email, "test") + way = create(:way, :with_history, :version => 4) + way_latest = way.old_ways.last - do_redact_way(ways(:way_with_versions_v4), - create(:redaction)) + do_redact_way(way_latest, create(:redaction)) assert_response :bad_request, "shouldn't be OK to redact current version as moderator." end @@ -102,89 +116,97 @@ class OldWayControllerTest < ActionController::TestCase # test that redacted ways aren't visible, regardless of # authorisation except as moderator... def test_version_redacted - way = ways(:way_with_redacted_versions_v2) + way = create(:way, :with_history, :version => 2) + way_v1 = way.old_ways.find_by(:version => 1) + way_v1.redact!(create(:redaction)) - get :version, :id => way.way_id, :version => way.version - assert_response :forbidden, "Redacted node shouldn't be visible via the version API." + get :version, :id => way_v1.way_id, :version => way_v1.version + assert_response :forbidden, "Redacted way shouldn't be visible via the version API." # not even to a logged-in user basic_authorization(create(:user).email, "test") - get :version, :id => way.way_id, :version => way.version - assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in." + get :version, :id => way_v1.way_id, :version => way_v1.version + assert_response :forbidden, "Redacted way shouldn't be visible via the version API, even when logged in." end ## - # test that redacted nodes aren't visible in the history + # test that redacted ways aren't visible in the history def test_history_redacted - way = ways(:way_with_redacted_versions_v2) + way = create(:way, :with_history, :version => 2) + way_v1 = way.old_ways.find_by(:version => 1) + way_v1.redact!(create(:redaction)) - get :history, :id => way.way_id + get :history, :id => way_v1.way_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 0, "redacted way #{way.way_id} version #{way.version} shouldn't be present in the history." + assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 0, "redacted way #{way_v1.way_id} version #{way_v1.version} shouldn't be present in the history." # not even to a logged-in user basic_authorization(create(:user).email, "test") - get :version, :id => way.way_id, :version => way.version - get :history, :id => way.way_id + get :version, :id => way_v1.way_id, :version => way_v1.version + get :history, :id => way_v1.way_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 0, "redacted node #{way.way_id} version #{way.version} shouldn't be present in the history, even when logged in." + assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 0, "redacted node #{way_v1.way_id} version #{way_v1.version} shouldn't be present in the history, even when logged in." end ## # test the redaction of an old version of a way, while being # authorised as a moderator. def test_redact_way_moderator - way = ways(:way_with_versions_v3) + way = create(:way, :with_history, :version => 4) + way_v3 = way.old_ways.find_by(:version => 3) basic_authorization(create(:moderator_user).email, "test") - do_redact_way(way, create(:redaction)) + do_redact_way(way_v3, create(:redaction)) assert_response :success, "should be OK to redact old version as moderator." # check moderator can still see the redacted data, when passing # the appropriate flag - get :version, :id => way.way_id, :version => way.version + get :version, :id => way_v3.way_id, :version => way_v3.version assert_response :forbidden, "After redaction, node should be gone for moderator, when flag not passed." - get :version, :id => way.way_id, :version => way.version, :show_redactions => "true" + get :version, :id => way_v3.way_id, :version => way_v3.version, :show_redactions => "true" assert_response :success, "After redaction, node should not be gone for moderator, when flag passed." # and when accessed via history - get :history, :id => way.way_id + get :history, :id => way_v3.way_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 0, "way #{way.way_id} version #{way.version} should not be present in the history for moderators when not passing flag." - get :history, :id => way.way_id, :show_redactions => "true" + assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0, "way #{way_v3.way_id} version #{way_v3.version} should not be present in the history for moderators when not passing flag." + get :history, :id => way_v3.way_id, :show_redactions => "true" assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 1, "way #{way.way_id} version #{way.version} should still be present in the history for moderators when passing flag." + assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 1, "way #{way_v3.way_id} version #{way_v3.version} should still be present in the history for moderators when passing flag." end # testing that if the moderator drops auth, he can't see the # redacted stuff any more. def test_redact_way_is_redacted - way = ways(:way_with_versions_v3) + way = create(:way, :with_history, :version => 4) + way_v3 = way.old_ways.find_by(:version => 3) basic_authorization(create(:moderator_user).email, "test") - do_redact_way(way, create(:redaction)) + do_redact_way(way_v3, create(:redaction)) assert_response :success, "should be OK to redact old version as moderator." # re-auth as non-moderator basic_authorization(create(:user).email, "test") # check can't see the redacted data - get :version, :id => way.way_id, :version => way.version + get :version, :id => way_v3.way_id, :version => way_v3.version assert_response :forbidden, "Redacted node shouldn't be visible via the version API." # and when accessed via history - get :history, :id => way.way_id + get :history, :id => way_v3.way_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 0, "redacted way #{way.way_id} version #{way.version} shouldn't be present in the history." + assert_select "osm way[id='#{way_v3.way_id}'][version='#{way_v3.version}']", 0, "redacted way #{way_v3.way_id} version #{way_v3.version} shouldn't be present in the history." end ## # test the unredaction of an old version of a way, while not being # authorised. def test_unredact_way_unauthorised - way = ways(:way_with_redacted_versions_v3) + way = create(:way, :with_history, :version => 2) + way_v1 = way.old_ways.find_by(:version => 1) + way_v1.redact!(create(:redaction)) - post :redact, :id => way.way_id, :version => way.version + post :redact, :id => way_v1.way_id, :version => way_v1.version assert_response :unauthorized, "should need to be authenticated to unredact." end @@ -192,10 +214,13 @@ class OldWayControllerTest < ActionController::TestCase # test the unredaction of an old version of a way, while being # authorised as a normal user. def test_unredact_way_normal_user - way = ways(:way_with_redacted_versions_v3) + way = create(:way, :with_history, :version => 2) + way_v1 = way.old_ways.find_by(:version => 1) + way_v1.redact!(create(:redaction)) + basic_authorization(create(:user).email, "test") - post :redact, :id => way.way_id, :version => way.version + post :redact, :id => way_v1.way_id, :version => way_v1.version assert_response :forbidden, "should need to be moderator to unredact." end @@ -204,32 +229,35 @@ class OldWayControllerTest < ActionController::TestCase # authorised as a moderator. def test_unredact_way_moderator moderator_user = create(:moderator_user) - way = ways(:way_with_redacted_versions_v3) + way = create(:way, :with_history, :version => 2) + way_v1 = way.old_ways.find_by(:version => 1) + way_v1.redact!(create(:redaction)) + basic_authorization(moderator_user.email, "test") - post :redact, :id => way.way_id, :version => way.version + post :redact, :id => way_v1.way_id, :version => way_v1.version assert_response :success, "should be OK to unredact old version as moderator." - # check moderator can still see the redacted data, without passing + # check moderator can still see the unredacted data, without passing # the appropriate flag - get :version, :id => way.way_id, :version => way.version - assert_response :success, "After redaction, node should not be gone for moderator, when flag passed." + get :version, :id => way_v1.way_id, :version => way_v1.version + assert_response :success, "After unredaction, node should not be gone for moderator." # and when accessed via history - get :history, :id => way.way_id - assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 1, "way #{way.way_id} version #{way.version} should still be present in the history for moderators when passing flag." + get :history, :id => way_v1.way_id + assert_response :success, "Unredaction shouldn't have stopped history working." + assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1, "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for moderators." - basic_authorization(users(:normal_user).email, "test") + basic_authorization(create(:user).email, "test") - # check normal user can now see the redacted data - get :version, :id => way.way_id, :version => way.version + # check normal user can now see the unredacted data + get :version, :id => way_v1.way_id, :version => way_v1.version assert_response :success, "After redaction, node should not be gone for moderator, when flag passed." # and when accessed via history - get :history, :id => way.way_id + get :history, :id => way_v1.way_id assert_response :success, "Redaction shouldn't have stopped history working." - assert_select "osm way[id='#{way.way_id}'][version='#{way.version}']", 1, "way #{way.way_id} version #{way.version} should still be present in the history for moderators when passing flag." + assert_select "osm way[id='#{way_v1.way_id}'][version='#{way_v1.version}']", 1, "way #{way_v1.way_id} version #{way_v1.version} should still be present in the history for normal users." end private diff --git a/test/factories/node.rb b/test/factories/node.rb index cbebbb009..bcc1fdbe8 100644 --- a/test/factories/node.rb +++ b/test/factories/node.rb @@ -16,7 +16,7 @@ FactoryGirl.define do trait :with_history do after(:create) do |node, _evaluator| (1..node.version).each do |n| - create(:old_node, :node_id => node.id, :version => n) + create(:old_node, :node_id => node.id, :version => n, :changeset => node.changeset) end # For deleted nodes, make sure the most recent old_node is also deleted. diff --git a/test/factories/relation.rb b/test/factories/relation.rb index 1631b92de..44f18f491 100644 --- a/test/factories/relation.rb +++ b/test/factories/relation.rb @@ -9,5 +9,20 @@ FactoryGirl.define do trait :deleted do visible false end + + trait :with_history do + after(:create) do |relation, _evaluator| + (1..relation.version).each do |n| + create(:old_relation, :relation_id => relation.id, :version => n, :changeset => relation.changeset) + end + + # For deleted relations, make sure the most recent old_relation is also deleted. + if relation.visible == false + latest = relation.old_relations.find_by(:version => relation.version) + latest.visible = false + latest.save + end + end + end end end diff --git a/test/factories/way.rb b/test/factories/way.rb index 89409e807..1c31de3d0 100644 --- a/test/factories/way.rb +++ b/test/factories/way.rb @@ -10,6 +10,21 @@ FactoryGirl.define do visible false end + trait :with_history do + after(:create) do |way, _evaluator| + (1..way.version).each do |n| + create(:old_way, :way_id => way.id, :version => n, :changeset => way.changeset) + end + + # For deleted ways, make sure the most recent old_way is also deleted. + if way.visible == false + latest = way.old_ways.find_by(:version => way.version) + latest.visible = false + latest.save + end + end + end + factory :way_with_nodes do transient do nodes_count 1