1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.ObjectTest do
13 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 test "returns an object by it's AP id" do
18 object = insert(:note)
19 found_object = Object.get_by_ap_id(object.data["id"])
21 assert object == found_object
24 describe "generic changeset" do
25 test "it ensures uniqueness of the id" do
26 object = insert(:note)
27 cs = Object.change(%Object{}, %{data: %{id: object.data["id"]}})
30 {:error, _result} = Repo.insert(cs)
34 describe "deletion function" do
35 test "deletes an object" do
36 object = insert(:note)
37 found_object = Object.get_by_ap_id(object.data["id"])
39 assert object == found_object
41 Object.delete(found_object)
43 found_object = Object.get_by_ap_id(object.data["id"])
45 refute object == found_object
47 assert found_object.data["type"] == "Tombstone"
50 test "ensures cache is cleared for the object" do
51 object = insert(:note)
52 cached_object = Object.get_cached_by_ap_id(object.data["id"])
54 assert object == cached_object
56 Cachex.put(:web_resp_cache, URI.parse(object.data["id"]).path, "cofe")
58 Object.delete(cached_object)
60 {:ok, nil} = Cachex.get(:object_cache, "object:#{object.data["id"]}")
61 {:ok, nil} = Cachex.get(:web_resp_cache, URI.parse(object.data["id"]).path)
63 cached_object = Object.get_cached_by_ap_id(object.data["id"])
65 refute object == cached_object
67 assert cached_object.data["type"] == "Tombstone"
71 describe "normalizer" do
72 test "fetches unknown objects by default" do
74 object = Object.normalize("http://mastodon.example.org/@admin/99541947525187367")
76 assert object.data["url"] == "http://mastodon.example.org/@admin/99541947525187367"
79 test "fetches unknown objects when fetch_remote is explicitly true" do
81 object = Object.normalize("http://mastodon.example.org/@admin/99541947525187367", true)
83 assert object.data["url"] == "http://mastodon.example.org/@admin/99541947525187367"
86 test "does not fetch unknown objects when fetch_remote is false" do
88 Object.normalize("http://mastodon.example.org/@admin/99541947525187367", false)
93 describe "get_by_id_and_maybe_refetch" do
94 test "refetches if the time since the last refetch is greater than the interval" do
96 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
97 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/poll_original.json")}
100 apply(HttpRequestMock, :request, [env])
104 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
106 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
107 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
110 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
111 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/poll_modified.json")}
114 apply(HttpRequestMock, :request, [env])
117 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1)
118 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 8
119 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 3
122 test "returns the old object if refetch fails" do
124 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
125 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/poll_original.json")}
128 apply(HttpRequestMock, :request, [env])
132 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
134 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
135 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
138 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
139 %Tesla.Env{status: 404, body: ""}
142 apply(HttpRequestMock, :request, [env])
145 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: -1)
146 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 4
147 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 0
150 test "does not refetch if the time since the last refetch is greater than the interval" do
152 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
153 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/poll_original.json")}
156 apply(HttpRequestMock, :request, [env])
160 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
162 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
163 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
166 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
167 %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/poll_modified.json")}
170 apply(HttpRequestMock, :request, [env])
173 updated_object = Object.get_by_id_and_maybe_refetch(object.id, interval: 100)
174 assert Enum.at(updated_object.data["oneOf"], 0)["replies"]["totalItems"] == 4
175 assert Enum.at(updated_object.data["oneOf"], 1)["replies"]["totalItems"] == 0