Merge remote-tracking branch 'remotes/upstream/develop' into 1260-rate-limited-auth...
[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 end