Test states of old elements after unsuccessful redactions

This commit is contained in:
Anton Khorev 2025-02-10 19:31:29 +03:00
parent 06cca3717a
commit 03ebd2ac5e
3 changed files with 174 additions and 176 deletions

View file

@ -197,16 +197,14 @@ module Api
# test the redaction of an old version of a node, while not being # test the redaction of an old version of a node, while not being
# authorised. # authorised.
def test_redact_node_unauthorised def test_redact_node_unauthorised
do_redact_redactable_node node = create(:node, :with_history, :version => 2)
assert_response :unauthorized, "should need to be authenticated to redact." old_node = node.old_nodes.find_by(:version => 1)
end redaction = create(:redaction)
## post node_version_redact_path(*old_node.id), :params => { :redaction => redaction.id }
# test the redaction of an old version of a node, while being
# authorised as a normal user. assert_response :unauthorized, "should need to be authenticated to redact."
def test_redact_node_normal_user assert_nil old_node.reload.redaction
do_redact_redactable_node bearer_authorization_header
assert_response :forbidden, "should need to be moderator to redact."
end end
## ##
@ -214,53 +212,62 @@ module Api
# can't be redacted. # can't be redacted.
def test_redact_node_current_version def test_redact_node_current_version
node = create(:node, :with_history, :version => 2) node = create(:node, :with_history, :version => 2)
redaction = create(:redaction) old_node = node.old_nodes.find_by(:version => 2)
auth_header = bearer_authorization_header create(:moderator_user)
post node_version_redact_path(node, 2), :params => { :redaction => redaction.id }, :headers => auth_header
assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
end
def test_redact_node_by_regular_without_write_redactions_scope
auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
do_redact_redactable_node(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_node_by_regular_with_write_redactions_scope
auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
do_redact_redactable_node(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_node_by_moderator_without_write_redactions_scope
auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
do_redact_redactable_node(auth_header)
assert_response :forbidden, "should need to have write_redactions scope to redact."
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) redaction = create(:redaction)
auth_header = bearer_authorization_header create(:moderator_user) auth_header = bearer_authorization_header create(:moderator_user)
post node_version_redact_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header 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." assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
old_node.reload assert_nil old_node.reload.redaction
assert_predicate old_node, :redacted? end
assert_equal redaction, old_node.redaction
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])
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])
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])
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
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[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 with write_redactions scope."
assert_equal redaction, old_node.reload.redaction
end end
## ##
@ -313,13 +320,6 @@ module Api
private 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) def check_not_found_id_version(id, version)
get api_node_version_path(id, version) get api_node_version_path(id, version)
assert_response :not_found assert_response :not_found

View file

@ -188,16 +188,14 @@ module Api
# test the redaction of an old version of a relation, while not being # test the redaction of an old version of a relation, while not being
# authorised. # authorised.
def test_redact_relation_unauthorised def test_redact_relation_unauthorised
do_redact_redactable_relation relation = create(:relation, :with_history, :version => 2)
assert_response :unauthorized, "should need to be authenticated to redact." old_relation = relation.old_relations.find_by(:version => 1)
end redaction = create(:redaction)
## post relation_version_redact_path(*old_relation.id), :params => { :redaction => redaction.id }
# test the redaction of an old version of a relation, while being
# authorised as a normal user. assert_response :unauthorized, "should need to be authenticated to redact."
def test_redact_relation_normal_user assert_nil old_relation.reload.redaction
do_redact_redactable_relation bearer_authorization_header
assert_response :forbidden, "should need to be moderator to redact."
end end
## ##
@ -205,53 +203,62 @@ module Api
# can't be redacted. # can't be redacted.
def test_redact_relation_current_version def test_redact_relation_current_version
relation = create(:relation, :with_history, :version => 2) relation = create(:relation, :with_history, :version => 2)
redaction = create(:redaction) old_relation = relation.old_relations.find_by(:version => 2)
auth_header = bearer_authorization_header create(:moderator_user)
post relation_version_redact_path(relation, 2), :params => { :redaction => redaction.id }, :headers => auth_header
assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
end
def test_redact_relation_by_regular_without_write_redactions_scope
auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
do_redact_redactable_relation(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_relation_by_regular_with_write_redactions_scope
auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
do_redact_redactable_relation(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_relation_by_moderator_without_write_redactions_scope
auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
do_redact_redactable_relation(auth_header)
assert_response :forbidden, "should need to have write_redactions scope to redact."
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) redaction = create(:redaction)
auth_header = bearer_authorization_header create(:moderator_user) auth_header = bearer_authorization_header create(:moderator_user)
post relation_version_redact_path(*old_relation.id), :params => { :redaction => redaction.id }, :headers => auth_header 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." assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
old_relation.reload assert_nil old_relation.reload.redaction
assert_predicate old_relation, :redacted? end
assert_equal redaction, old_relation.redaction
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])
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])
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])
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
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[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 with write_redactions scope."
assert_equal redaction, old_relation.reload.redaction
end end
## ##
@ -299,14 +306,5 @@ module Api
assert_response :success, "should be OK to unredact old version as moderator." assert_response :success, "should be OK to unredact old version as moderator."
assert_nil old_relation.reload.redaction assert_nil old_relation.reload.redaction
end 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
end end

View file

@ -199,16 +199,14 @@ module Api
# test the redaction of an old version of a way, while not being # test the redaction of an old version of a way, while not being
# authorised. # authorised.
def test_redact_way_unauthorised def test_redact_way_unauthorised
do_redact_redactable_way way = create(:way, :with_history, :version => 2)
assert_response :unauthorized, "should need to be authenticated to redact." old_way = way.old_ways.find_by(:version => 1)
end redaction = create(:redaction)
## post way_version_redact_path(*old_way.id), :params => { :redaction => redaction.id }
# test the redaction of an old version of a way, while being
# authorised as a normal user. assert_response :unauthorized, "should need to be authenticated to redact."
def test_redact_way_normal_user assert_nil old_way.reload.redaction
do_redact_redactable_way bearer_authorization_header
assert_response :forbidden, "should need to be moderator to redact."
end end
## ##
@ -216,53 +214,62 @@ module Api
# can't be redacted. # can't be redacted.
def test_redact_way_current_version def test_redact_way_current_version
way = create(:way, :with_history, :version => 2) way = create(:way, :with_history, :version => 2)
redaction = create(:redaction) old_way = way.old_ways.find_by(:version => 2)
auth_header = bearer_authorization_header create(:moderator_user)
post way_version_redact_path(way, 2), :params => { :redaction => redaction.id }, :headers => auth_header
assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
end
def test_redact_way_by_regular_without_write_redactions_scope
auth_header = bearer_authorization_header(create(:user), :scopes => %w[read_prefs write_api])
do_redact_redactable_way(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_way_by_regular_with_write_redactions_scope
auth_header = bearer_authorization_header(create(:user), :scopes => %w[write_redactions])
do_redact_redactable_way(auth_header)
assert_response :forbidden, "should need to be moderator to redact."
end
def test_redact_way_by_moderator_without_write_redactions_scope
auth_header = bearer_authorization_header(create(:moderator_user), :scopes => %w[read_prefs write_api])
do_redact_redactable_way(auth_header)
assert_response :forbidden, "should need to have write_redactions scope to redact."
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) redaction = create(:redaction)
auth_header = bearer_authorization_header create(:moderator_user) auth_header = bearer_authorization_header create(:moderator_user)
post way_version_redact_path(*old_way.id), :params => { :redaction => redaction.id }, :headers => auth_header 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." assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
old_way.reload assert_nil old_way.reload.redaction
assert_predicate old_way, :redacted? end
assert_equal redaction, old_way.redaction
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])
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])
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])
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
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[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 with write_redactions scope."
assert_equal redaction, old_way.reload.redaction
end end
## ##
@ -335,12 +342,5 @@ module Api
assert_ways_are_equal history_way, version_way assert_ways_are_equal history_way, version_way
end end
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
end end