From: Anton Khorev Date: Mon, 10 Feb 2025 16:31:29 +0000 (+0300) Subject: Test states of old elements after unsuccessful redactions X-Git-Tag: live~67^2~2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/03ebd2ac5e8048fcf7c5dea1cbdde6d90f606402?hp=06cca3717a91dcc089205bda5aab30c0f10ba582 Test states of old elements after unsuccessful redactions --- diff --git a/test/controllers/api/old_nodes_controller_test.rb b/test/controllers/api/old_nodes_controller_test.rb index 04e76dac5..fb1ee16bc 100644 --- a/test/controllers/api/old_nodes_controller_test.rb +++ b/test/controllers/api/old_nodes_controller_test.rb @@ -197,16 +197,14 @@ module Api # test the redaction of an old version of a node, while not being # authorised. def test_redact_node_unauthorised - do_redact_redactable_node - assert_response :unauthorized, "should need to be authenticated to redact." - end + node = create(:node, :with_history, :version => 2) + old_node = node.old_nodes.find_by(:version => 1) + redaction = create(:redaction) - ## - # test the redaction of an old version of a node, while being - # authorised as a normal user. - def test_redact_node_normal_user - do_redact_redactable_node bearer_authorization_header - assert_response :forbidden, "should need to be moderator to redact." + post node_version_redact_path(*old_node.id), :params => { :redaction => redaction.id } + + assert_response :unauthorized, "should need to be authenticated to redact." + assert_nil old_node.reload.redaction end ## @@ -214,53 +212,62 @@ module Api # can't be redacted. def test_redact_node_current_version node = create(:node, :with_history, :version => 2) + old_node = node.old_nodes.find_by(:version => 2) redaction = create(:redaction) auth_header = bearer_authorization_header create(:moderator_user) - post node_version_redact_path(node, 2), :params => { :redaction => redaction.id }, :headers => auth_header + post node_version_redact_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header assert_response :bad_request, "shouldn't be OK to redact current version as moderator." + assert_nil old_node.reload.redaction end def test_redact_node_by_regular_without_write_redactions_scope + node = create(:node, :with_history, :version => 2) + old_node = node.old_nodes.find_by(:version => 1) + redaction = create(:redaction) auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api]) - do_redact_redactable_node(auth_header) + + post node_version_redact_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header + assert_response :forbidden, "should need to be moderator to redact." + assert_nil old_node.reload.redaction end def test_redact_node_by_regular_with_write_redactions_scope + node = create(:node, :with_history, :version => 2) + old_node = node.old_nodes.find_by(:version => 1) + redaction = create(:redaction) auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions]) - do_redact_redactable_node(auth_header) + + post node_version_redact_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header + assert_response :forbidden, "should need to be moderator to redact." + assert_nil old_node.reload.redaction end def test_redact_node_by_moderator_without_write_redactions_scope + node = create(:node, :with_history, :version => 2) + old_node = node.old_nodes.find_by(:version => 1) + redaction = create(:redaction) auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api]) - do_redact_redactable_node(auth_header) + + post node_version_redact_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header + assert_response :forbidden, "should need to have write_redactions scope to redact." + assert_nil old_node.reload.redaction end def test_redact_node_by_moderator_with_write_redactions_scope - auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions]) - do_redact_redactable_node(auth_header) - assert_response :success, "should be OK to redact old version as moderator with write_redactions scope." - end - - ## - # test the redaction of an old version of a node, while being - # authorised as a moderator. - def test_redact_node_moderator node = create(:node, :with_history, :version => 2) old_node = node.old_nodes.find_by(:version => 1) redaction = create(:redaction) - auth_header = bearer_authorization_header create(:moderator_user) + auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions]) post node_version_redact_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header - assert_response :success, "should be OK to redact old version as moderator." - old_node.reload - assert_predicate old_node, :redacted? - assert_equal redaction, old_node.redaction + assert_response :success, "should be OK to redact old version as moderator with write_redactions scope." + assert_equal redaction, old_node.reload.redaction end ## @@ -313,13 +320,6 @@ module Api private - def do_redact_redactable_node(headers = {}) - node = create(:node, :with_history, :version => 2) - redaction = create(:redaction) - - post node_version_redact_path(node, 1), :params => { :redaction => redaction.id }, :headers => headers - end - def check_not_found_id_version(id, version) get api_node_version_path(id, version) assert_response :not_found diff --git a/test/controllers/api/old_relations_controller_test.rb b/test/controllers/api/old_relations_controller_test.rb index 8d98359ea..ecd9107b4 100644 --- a/test/controllers/api/old_relations_controller_test.rb +++ b/test/controllers/api/old_relations_controller_test.rb @@ -188,16 +188,14 @@ module Api # test the redaction of an old version of a relation, while not being # authorised. def test_redact_relation_unauthorised - do_redact_redactable_relation - assert_response :unauthorized, "should need to be authenticated to redact." - end + relation = create(:relation, :with_history, :version => 2) + old_relation = relation.old_relations.find_by(:version => 1) + redaction = create(:redaction) - ## - # test the redaction of an old version of a relation, while being - # authorised as a normal user. - def test_redact_relation_normal_user - do_redact_redactable_relation bearer_authorization_header - assert_response :forbidden, "should need to be moderator to redact." + post relation_version_redact_path(*old_relation.id), :params => { :redaction => redaction.id } + + assert_response :unauthorized, "should need to be authenticated to redact." + assert_nil old_relation.reload.redaction end ## @@ -205,53 +203,62 @@ module Api # can't be redacted. def test_redact_relation_current_version relation = create(:relation, :with_history, :version => 2) + old_relation = relation.old_relations.find_by(:version => 2) redaction = create(:redaction) auth_header = bearer_authorization_header create(:moderator_user) - post relation_version_redact_path(relation, 2), :params => { :redaction => redaction.id }, :headers => auth_header + post relation_version_redact_path(*old_relation.id), :params => { :redaction => redaction.id }, :headers => auth_header assert_response :bad_request, "shouldn't be OK to redact current version as moderator." + assert_nil old_relation.reload.redaction end def test_redact_relation_by_regular_without_write_redactions_scope + relation = create(:relation, :with_history, :version => 2) + old_relation = relation.old_relations.find_by(:version => 1) + redaction = create(:redaction) auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api]) - do_redact_redactable_relation(auth_header) + + post relation_version_redact_path(*old_relation.id), :params => { :redaction => redaction.id }, :headers => auth_header + assert_response :forbidden, "should need to be moderator to redact." + assert_nil old_relation.reload.redaction end def test_redact_relation_by_regular_with_write_redactions_scope + relation = create(:relation, :with_history, :version => 2) + old_relation = relation.old_relations.find_by(:version => 1) + redaction = create(:redaction) auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions]) - do_redact_redactable_relation(auth_header) + + post relation_version_redact_path(*old_relation.id), :params => { :redaction => redaction.id }, :headers => auth_header + assert_response :forbidden, "should need to be moderator to redact." + assert_nil old_relation.reload.redaction end def test_redact_relation_by_moderator_without_write_redactions_scope + relation = create(:relation, :with_history, :version => 2) + old_relation = relation.old_relations.find_by(:version => 1) + redaction = create(:redaction) auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api]) - do_redact_redactable_relation(auth_header) + + post relation_version_redact_path(*old_relation.id), :params => { :redaction => redaction.id }, :headers => auth_header + assert_response :forbidden, "should need to have write_redactions scope to redact." + assert_nil old_relation.reload.redaction end def test_redact_relation_by_moderator_with_write_redactions_scope - auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions]) - do_redact_redactable_relation(auth_header) - assert_response :success, "should be OK to redact old version as moderator with write_redactions scope." - end - - ## - # test the redaction of an old version of a relation, while being - # authorised as a moderator. - def test_redact_relation_moderator relation = create(:relation, :with_history, :version => 2) old_relation = relation.old_relations.find_by(:version => 1) redaction = create(:redaction) - auth_header = bearer_authorization_header create(:moderator_user) + auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions]) post relation_version_redact_path(*old_relation.id), :params => { :redaction => redaction.id }, :headers => auth_header - assert_response :success, "should be OK to redact old version as moderator." - old_relation.reload - assert_predicate old_relation, :redacted? - assert_equal redaction, old_relation.redaction + assert_response :success, "should be OK to redact old version as moderator with write_redactions scope." + assert_equal redaction, old_relation.reload.redaction end ## @@ -299,14 +306,5 @@ module Api assert_response :success, "should be OK to unredact old version as moderator." assert_nil old_relation.reload.redaction end - - private - - def do_redact_redactable_relation(headers = {}) - relation = create(:relation, :with_history, :version => 2) - redaction = create(:redaction) - - post relation_version_redact_path(relation, 1), :params => { :redaction => redaction.id }, :headers => headers - end end end diff --git a/test/controllers/api/old_ways_controller_test.rb b/test/controllers/api/old_ways_controller_test.rb index 95f3f2bd0..d695311fc 100644 --- a/test/controllers/api/old_ways_controller_test.rb +++ b/test/controllers/api/old_ways_controller_test.rb @@ -199,16 +199,14 @@ module Api # test the redaction of an old version of a way, while not being # authorised. def test_redact_way_unauthorised - do_redact_redactable_way - assert_response :unauthorized, "should need to be authenticated to redact." - end + way = create(:way, :with_history, :version => 2) + old_way = way.old_ways.find_by(:version => 1) + redaction = create(:redaction) - ## - # test the redaction of an old version of a way, while being - # authorised as a normal user. - def test_redact_way_normal_user - do_redact_redactable_way bearer_authorization_header - assert_response :forbidden, "should need to be moderator to redact." + post way_version_redact_path(*old_way.id), :params => { :redaction => redaction.id } + + assert_response :unauthorized, "should need to be authenticated to redact." + assert_nil old_way.reload.redaction end ## @@ -216,53 +214,62 @@ module Api # can't be redacted. def test_redact_way_current_version way = create(:way, :with_history, :version => 2) + old_way = way.old_ways.find_by(:version => 2) redaction = create(:redaction) auth_header = bearer_authorization_header create(:moderator_user) - post way_version_redact_path(way, 2), :params => { :redaction => redaction.id }, :headers => auth_header + post way_version_redact_path(*old_way.id), :params => { :redaction => redaction.id }, :headers => auth_header assert_response :bad_request, "shouldn't be OK to redact current version as moderator." + assert_nil old_way.reload.redaction end def test_redact_way_by_regular_without_write_redactions_scope + way = create(:way, :with_history, :version => 2) + old_way = way.old_ways.find_by(:version => 1) + redaction = create(:redaction) auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api]) - do_redact_redactable_way(auth_header) + + post way_version_redact_path(*old_way.id), :params => { :redaction => redaction.id }, :headers => auth_header + assert_response :forbidden, "should need to be moderator to redact." + assert_nil old_way.reload.redaction end def test_redact_way_by_regular_with_write_redactions_scope + way = create(:way, :with_history, :version => 2) + old_way = way.old_ways.find_by(:version => 1) + redaction = create(:redaction) auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions]) - do_redact_redactable_way(auth_header) + + post way_version_redact_path(*old_way.id), :params => { :redaction => redaction.id }, :headers => auth_header + assert_response :forbidden, "should need to be moderator to redact." + assert_nil old_way.reload.redaction end def test_redact_way_by_moderator_without_write_redactions_scope + way = create(:way, :with_history, :version => 2) + old_way = way.old_ways.find_by(:version => 1) + redaction = create(:redaction) auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api]) - do_redact_redactable_way(auth_header) + + post way_version_redact_path(*old_way.id), :params => { :redaction => redaction.id }, :headers => auth_header + assert_response :forbidden, "should need to have write_redactions scope to redact." + assert_nil old_way.reload.redaction end def test_redact_way_by_moderator_with_write_redactions_scope - auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions]) - do_redact_redactable_way(auth_header) - assert_response :success, "should be OK to redact old version as moderator with write_redactions scope." - end - - ## - # test the redaction of an old version of a way, while being - # authorised as a moderator. - def test_redact_way_moderator way = create(:way, :with_history, :version => 2) old_way = way.old_ways.find_by(:version => 1) redaction = create(:redaction) - auth_header = bearer_authorization_header create(:moderator_user) + auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[write_redactions]) post way_version_redact_path(*old_way.id), :params => { :redaction => redaction.id }, :headers => auth_header - assert_response :success, "should be OK to redact old version as moderator." - old_way.reload - assert_predicate old_way, :redacted? - assert_equal redaction, old_way.redaction + assert_response :success, "should be OK to redact old version as moderator with write_redactions scope." + assert_equal redaction, old_way.reload.redaction end ## @@ -335,12 +342,5 @@ module Api assert_ways_are_equal history_way, version_way end end - - def do_redact_redactable_way(headers = {}) - way = create(:way, :with_history, :version => 2) - redaction = create(:redaction) - - post way_version_redact_path(way.id, 1), :params => { :redaction => redaction.id }, :headers => headers - end end end