summaryrefslogtreecommitdiff
path: root/test/pleroma/object/fetcher_test.exs
diff options
context:
space:
mode:
Diffstat (limited to 'test/pleroma/object/fetcher_test.exs')
-rw-r--r--test/pleroma/object/fetcher_test.exs279
1 files changed, 279 insertions, 0 deletions
diff --git a/test/pleroma/object/fetcher_test.exs b/test/pleroma/object/fetcher_test.exs
index 98130f434..c8ad66ddb 100644
--- a/test/pleroma/object/fetcher_test.exs
+++ b/test/pleroma/object/fetcher_test.exs
@@ -6,6 +6,7 @@ defmodule Pleroma.Object.FetcherTest do
use Pleroma.DataCase
alias Pleroma.Activity
+ alias Pleroma.Instances
alias Pleroma.Object
alias Pleroma.Object.Fetcher
@@ -159,6 +160,17 @@ defmodule Pleroma.Object.FetcherTest do
"https://patch.cx/media/03ca3c8b4ac3ddd08bf0f84be7885f2f88de0f709112131a22d83650819e36c2.json"
)
end
+
+ test "it resets instance reachability on successful fetch" do
+ id = "http://mastodon.example.org/@admin/99541947525187367"
+ Instances.set_consistently_unreachable(id)
+ refute Instances.reachable?(id)
+
+ {:ok, _object} =
+ Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
+
+ assert Instances.reachable?(id)
+ end
end
describe "implementation quirks" do
@@ -269,4 +281,271 @@ defmodule Pleroma.Object.FetcherTest do
refute called(Pleroma.Signature.sign(:_, :_))
end
end
+
+ describe "refetching" do
+ setup do
+ object1 = %{
+ "id" => "https://mastodon.social/1",
+ "actor" => "https://mastodon.social/users/emelie",
+ "attributedTo" => "https://mastodon.social/users/emelie",
+ "type" => "Note",
+ "content" => "test 1",
+ "bcc" => [],
+ "bto" => [],
+ "cc" => [],
+ "to" => [],
+ "summary" => ""
+ }
+
+ object2 = %{
+ "id" => "https://mastodon.social/2",
+ "actor" => "https://mastodon.social/users/emelie",
+ "attributedTo" => "https://mastodon.social/users/emelie",
+ "type" => "Note",
+ "content" => "test 2",
+ "bcc" => [],
+ "bto" => [],
+ "cc" => [],
+ "to" => [],
+ "summary" => "",
+ "formerRepresentations" => %{
+ "type" => "OrderedCollection",
+ "orderedItems" => [
+ %{
+ "type" => "Note",
+ "content" => "orig 2",
+ "actor" => "https://mastodon.social/users/emelie",
+ "attributedTo" => "https://mastodon.social/users/emelie",
+ "bcc" => [],
+ "bto" => [],
+ "cc" => [],
+ "to" => [],
+ "summary" => ""
+ }
+ ],
+ "totalItems" => 1
+ }
+ }
+
+ mock(fn
+ %{
+ method: :get,
+ url: "https://mastodon.social/1"
+ } ->
+ %Tesla.Env{
+ status: 200,
+ headers: [{"content-type", "application/activity+json"}],
+ body: Jason.encode!(object1)
+ }
+
+ %{
+ method: :get,
+ url: "https://mastodon.social/2"
+ } ->
+ %Tesla.Env{
+ status: 200,
+ headers: [{"content-type", "application/activity+json"}],
+ body: Jason.encode!(object2)
+ }
+
+ %{
+ method: :get,
+ url: "https://mastodon.social/users/emelie/collections/featured"
+ } ->
+ %Tesla.Env{
+ status: 200,
+ headers: [{"content-type", "application/activity+json"}],
+ body:
+ Jason.encode!(%{
+ "id" => "https://mastodon.social/users/emelie/collections/featured",
+ "type" => "OrderedCollection",
+ "actor" => "https://mastodon.social/users/emelie",
+ "attributedTo" => "https://mastodon.social/users/emelie",
+ "orderedItems" => [],
+ "totalItems" => 0
+ })
+ }
+
+ env ->
+ apply(HttpRequestMock, :request, [env])
+ end)
+
+ %{object1: object1, object2: object2}
+ end
+
+ test "it keeps formerRepresentations if remote does not have this attr", %{object1: object1} do
+ full_object1 =
+ object1
+ |> Map.merge(%{
+ "formerRepresentations" => %{
+ "type" => "OrderedCollection",
+ "orderedItems" => [
+ %{
+ "type" => "Note",
+ "content" => "orig 2",
+ "actor" => "https://mastodon.social/users/emelie",
+ "attributedTo" => "https://mastodon.social/users/emelie",
+ "bcc" => [],
+ "bto" => [],
+ "cc" => [],
+ "to" => [],
+ "summary" => ""
+ }
+ ],
+ "totalItems" => 1
+ }
+ })
+
+ {:ok, o} = Object.create(full_object1)
+
+ assert {:ok, refetched} = Fetcher.refetch_object(o)
+
+ assert %{"formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}} =
+ refetched.data
+ end
+
+ test "it uses formerRepresentations from remote if possible", %{object2: object2} do
+ {:ok, o} = Object.create(object2)
+
+ assert {:ok, refetched} = Fetcher.refetch_object(o)
+
+ assert %{"formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}} =
+ refetched.data
+ end
+
+ test "it replaces formerRepresentations with the one from remote", %{object2: object2} do
+ full_object2 =
+ object2
+ |> Map.merge(%{
+ "content" => "mew mew #def",
+ "formerRepresentations" => %{
+ "type" => "OrderedCollection",
+ "orderedItems" => [
+ %{"type" => "Note", "content" => "mew mew 2"}
+ ],
+ "totalItems" => 1
+ }
+ })
+
+ {:ok, o} = Object.create(full_object2)
+
+ assert {:ok, refetched} = Fetcher.refetch_object(o)
+
+ assert %{
+ "content" => "test 2",
+ "formerRepresentations" => %{"orderedItems" => [%{"content" => "orig 2"}]}
+ } = refetched.data
+ end
+
+ test "it adds to formerRepresentations if the remote does not have one and the object has changed",
+ %{object1: object1} do
+ full_object1 =
+ object1
+ |> Map.merge(%{
+ "content" => "mew mew #def",
+ "formerRepresentations" => %{
+ "type" => "OrderedCollection",
+ "orderedItems" => [
+ %{"type" => "Note", "content" => "mew mew 1"}
+ ],
+ "totalItems" => 1
+ }
+ })
+
+ {:ok, o} = Object.create(full_object1)
+
+ assert {:ok, refetched} = Fetcher.refetch_object(o)
+
+ assert %{
+ "content" => "test 1",
+ "formerRepresentations" => %{
+ "orderedItems" => [
+ %{"content" => "mew mew #def"},
+ %{"content" => "mew mew 1"}
+ ],
+ "totalItems" => 2
+ }
+ } = refetched.data
+ end
+ end
+
+ describe "fetch with history" do
+ setup do
+ object2 = %{
+ "id" => "https://mastodon.social/2",
+ "actor" => "https://mastodon.social/users/emelie",
+ "attributedTo" => "https://mastodon.social/users/emelie",
+ "type" => "Note",
+ "content" => "test 2",
+ "bcc" => [],
+ "bto" => [],
+ "cc" => ["https://mastodon.social/users/emelie/followers"],
+ "to" => [],
+ "summary" => "",
+ "formerRepresentations" => %{
+ "type" => "OrderedCollection",
+ "orderedItems" => [
+ %{
+ "type" => "Note",
+ "content" => "orig 2",
+ "actor" => "https://mastodon.social/users/emelie",
+ "attributedTo" => "https://mastodon.social/users/emelie",
+ "bcc" => [],
+ "bto" => [],
+ "cc" => ["https://mastodon.social/users/emelie/followers"],
+ "to" => [],
+ "summary" => ""
+ }
+ ],
+ "totalItems" => 1
+ }
+ }
+
+ mock(fn
+ %{
+ method: :get,
+ url: "https://mastodon.social/2"
+ } ->
+ %Tesla.Env{
+ status: 200,
+ headers: [{"content-type", "application/activity+json"}],
+ body: Jason.encode!(object2)
+ }
+
+ %{
+ method: :get,
+ url: "https://mastodon.social/users/emelie/collections/featured"
+ } ->
+ %Tesla.Env{
+ status: 200,
+ headers: [{"content-type", "application/activity+json"}],
+ body:
+ Jason.encode!(%{
+ "id" => "https://mastodon.social/users/emelie/collections/featured",
+ "type" => "OrderedCollection",
+ "actor" => "https://mastodon.social/users/emelie",
+ "attributedTo" => "https://mastodon.social/users/emelie",
+ "orderedItems" => [],
+ "totalItems" => 0
+ })
+ }
+
+ env ->
+ apply(HttpRequestMock, :request, [env])
+ end)
+
+ %{object2: object2}
+ end
+
+ test "it gets history", %{object2: object2} do
+ {:ok, object} = Fetcher.fetch_object_from_id(object2["id"])
+
+ assert %{
+ "formerRepresentations" => %{
+ "type" => "OrderedCollection",
+ "orderedItems" => [%{}]
+ }
+ } = object.data
+ end
+ end
end