Initial poll refresh support
[akkoma] / test / object_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ObjectTest do
6 use Pleroma.DataCase
7 import Pleroma.Factory
8 import Tesla.Mock
9 alias Pleroma.Object
10 alias Pleroma.Repo
11
12 setup do
13 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
14 :ok
15 end
16
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"])
20
21 assert object == found_object
22 end
23
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"]}})
28 assert cs.valid?
29
30 {:error, _result} = Repo.insert(cs)
31 end
32 end
33
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"])
38
39 assert object == found_object
40
41 Object.delete(found_object)
42
43 found_object = Object.get_by_ap_id(object.data["id"])
44
45 refute object == found_object
46
47 assert found_object.data["type"] == "Tombstone"
48 end
49
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"])
53
54 assert object == cached_object
55
56 Cachex.put(:web_resp_cache, URI.parse(object.data["id"]).path, "cofe")
57
58 Object.delete(cached_object)
59
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)
62
63 cached_object = Object.get_cached_by_ap_id(object.data["id"])
64
65 refute object == cached_object
66
67 assert cached_object.data["type"] == "Tombstone"
68 end
69 end
70
71 describe "normalizer" do
72 test "fetches unknown objects by default" do
73 %Object{} =
74 object = Object.normalize("http://mastodon.example.org/@admin/99541947525187367")
75
76 assert object.data["url"] == "http://mastodon.example.org/@admin/99541947525187367"
77 end
78
79 test "fetches unknown objects when fetch_remote is explicitly true" do
80 %Object{} =
81 object = Object.normalize("http://mastodon.example.org/@admin/99541947525187367", true)
82
83 assert object.data["url"] == "http://mastodon.example.org/@admin/99541947525187367"
84 end
85
86 test "does not fetch unknown objects when fetch_remote is false" do
87 assert is_nil(
88 Object.normalize("http://mastodon.example.org/@admin/99541947525187367", false)
89 )
90 end
91 end
92
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
95 mock(fn
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")}
98
99 env ->
100 apply(HttpRequestMock, :request, [env])
101 end)
102
103 %Object{} =
104 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
105
106 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
107 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
108
109 mock(fn
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")}
112
113 env ->
114 apply(HttpRequestMock, :request, [env])
115 end)
116
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
120 end
121
122 test "returns the old object if refetch fails" do
123 mock(fn
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")}
126
127 env ->
128 apply(HttpRequestMock, :request, [env])
129 end)
130
131 %Object{} =
132 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
133
134 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
135 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
136
137 mock(fn
138 %{method: :get, url: "https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d"} ->
139 %Tesla.Env{status: 404, body: ""}
140
141 env ->
142 apply(HttpRequestMock, :request, [env])
143 end)
144
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
148 end
149
150 test "does not refetch if the time since the last refetch is greater than the interval" do
151 mock(fn
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")}
154
155 env ->
156 apply(HttpRequestMock, :request, [env])
157 end)
158
159 %Object{} =
160 object = Object.normalize("https://patch.cx/objects/9a172665-2bc5-452d-8428-2361d4c33b1d")
161
162 assert Enum.at(object.data["oneOf"], 0)["replies"]["totalItems"] == 4
163 assert Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 0
164
165 mock(fn
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")}
168
169 env ->
170 apply(HttpRequestMock, :request, [env])
171 end)
172
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
176 end
177 end
178 end