2020-04-30 06:14:00 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-04-30 06:14:00 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.Transmogrifier.DeleteHandlingTest do
|
|
|
|
use Oban.Testing, repo: Pleroma.Repo
|
|
|
|
use Pleroma.DataCase
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.Tests.ObanHelpers
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
|
|
|
setup_all do
|
|
|
|
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it works for incoming deletes" do
|
|
|
|
activity = insert(:note_activity)
|
|
|
|
deleting_user = insert(:user)
|
|
|
|
|
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/mastodon-delete.json")
|
2020-11-23 12:28:55 -07:00
|
|
|
|> Jason.decode!()
|
2020-04-30 06:14:00 -06:00
|
|
|
|> Map.put("actor", deleting_user.ap_id)
|
2020-05-03 04:51:28 -06:00
|
|
|
|> put_in(["object", "id"], activity.data["object"])
|
2020-04-30 06:14:00 -06:00
|
|
|
|
|
|
|
{:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
|
|
|
|
Transmogrifier.handle_incoming(data)
|
|
|
|
|
|
|
|
assert id == data["id"]
|
|
|
|
|
2020-05-03 04:51:28 -06:00
|
|
|
# We delete the Create activity because we base our timelines on it.
|
2020-04-30 06:14:00 -06:00
|
|
|
# This should be changed after we unify objects and activities
|
|
|
|
refute Activity.get_by_id(activity.id)
|
|
|
|
assert actor == deleting_user.ap_id
|
|
|
|
|
|
|
|
# Objects are replaced by a tombstone object.
|
2021-01-04 05:38:31 -07:00
|
|
|
object = Object.normalize(activity.data["object"], fetch: false)
|
2020-04-30 06:14:00 -06:00
|
|
|
assert object.data["type"] == "Tombstone"
|
|
|
|
end
|
|
|
|
|
2020-05-11 07:06:23 -06:00
|
|
|
test "it works for incoming when the object has been pruned" do
|
|
|
|
activity = insert(:note_activity)
|
|
|
|
|
|
|
|
{:ok, object} =
|
2021-01-04 05:38:31 -07:00
|
|
|
Object.normalize(activity.data["object"], fetch: false)
|
2020-05-11 07:06:23 -06:00
|
|
|
|> Repo.delete()
|
|
|
|
|
2020-12-18 05:18:17 -07:00
|
|
|
# TODO: mock cachex
|
2020-05-11 07:06:23 -06:00
|
|
|
Cachex.del(:object_cache, "object:#{object.data["id"]}")
|
|
|
|
|
|
|
|
deleting_user = insert(:user)
|
|
|
|
|
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/mastodon-delete.json")
|
2020-11-23 12:28:55 -07:00
|
|
|
|> Jason.decode!()
|
2020-05-11 07:06:23 -06:00
|
|
|
|> Map.put("actor", deleting_user.ap_id)
|
|
|
|
|> put_in(["object", "id"], activity.data["object"])
|
|
|
|
|
|
|
|
{:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
|
|
|
|
Transmogrifier.handle_incoming(data)
|
|
|
|
|
|
|
|
assert id == data["id"]
|
|
|
|
|
|
|
|
# We delete the Create activity because we base our timelines on it.
|
|
|
|
# This should be changed after we unify objects and activities
|
|
|
|
refute Activity.get_by_id(activity.id)
|
|
|
|
assert actor == deleting_user.ap_id
|
|
|
|
end
|
|
|
|
|
2020-04-30 06:14:00 -06:00
|
|
|
test "it fails for incoming deletes with spoofed origin" do
|
|
|
|
activity = insert(:note_activity)
|
2020-05-03 04:51:28 -06:00
|
|
|
%{ap_id: ap_id} = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
|
2020-04-30 06:14:00 -06:00
|
|
|
|
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/mastodon-delete.json")
|
2020-11-23 12:28:55 -07:00
|
|
|
|> Jason.decode!()
|
2020-05-03 04:51:28 -06:00
|
|
|
|> Map.put("actor", ap_id)
|
|
|
|
|> put_in(["object", "id"], activity.data["object"])
|
2020-04-30 06:14:00 -06:00
|
|
|
|
2020-05-03 04:51:28 -06:00
|
|
|
assert match?({:error, _}, Transmogrifier.handle_incoming(data))
|
2020-04-30 06:14:00 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
@tag capture_log: true
|
|
|
|
test "it works for incoming user deletes" do
|
|
|
|
%{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
|
|
|
|
|
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/mastodon-delete-user.json")
|
2020-11-23 12:28:55 -07:00
|
|
|
|> Jason.decode!()
|
2020-04-30 06:14:00 -06:00
|
|
|
|
|
|
|
{:ok, _} = Transmogrifier.handle_incoming(data)
|
|
|
|
ObanHelpers.perform_all()
|
|
|
|
|
2020-05-03 05:01:19 -06:00
|
|
|
assert User.get_cached_by_ap_id(ap_id).deactivated
|
2020-04-30 06:14:00 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it fails for incoming user deletes with spoofed origin" do
|
|
|
|
%{ap_id: ap_id} = insert(:user)
|
|
|
|
|
|
|
|
data =
|
|
|
|
File.read!("test/fixtures/mastodon-delete-user.json")
|
2020-11-23 12:28:55 -07:00
|
|
|
|> Jason.decode!()
|
2020-04-30 06:14:00 -06:00
|
|
|
|> Map.put("actor", ap_id)
|
|
|
|
|
2020-04-30 07:26:23 -06:00
|
|
|
assert match?({:error, _}, Transmogrifier.handle_incoming(data))
|
2020-04-30 06:14:00 -06:00
|
|
|
|
|
|
|
assert User.get_cached_by_ap_id(ap_id)
|
|
|
|
end
|
|
|
|
end
|