aboutsummaryrefslogtreecommitdiff
path: root/test/pleroma/instances/instance_test.exs
blob: 6a718be21cb9b6a03ba3c6cfe6da17b25aa83c22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Instances.InstanceTest do
  alias Pleroma.Instances
  alias Pleroma.Instances.Instance
  alias Pleroma.Repo
  alias Pleroma.Tests.ObanHelpers
  alias Pleroma.Web.CommonAPI

  use Pleroma.DataCase

  import ExUnit.CaptureLog
  import Pleroma.Factory

  setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)

  describe "set_reachable/1" do
    test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
      unreachable_since = NaiveDateTime.to_iso8601(NaiveDateTime.utc_now())
      instance = insert(:instance, unreachable_since: unreachable_since)

      assert {:ok, instance} = Instance.set_reachable(instance.host)
      refute instance.unreachable_since
    end

    test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
      instance = insert(:instance, unreachable_since: nil)

      assert {:ok, instance} = Instance.set_reachable(instance.host)
      refute instance.unreachable_since
    end
  end

  describe "set_unreachable/1" do
    test "creates new record having `unreachable_since` to current time if record does not exist" do
      assert {:ok, instance} = Instance.set_unreachable("https://domain.com/path")

      instance = Repo.get(Instance, instance.id)
      assert instance.unreachable_since
      assert "domain.com" == instance.host
    end

    test "sets `unreachable_since` of existing record having nil `unreachable_since`" do
      instance = insert(:instance, unreachable_since: nil)
      refute instance.unreachable_since

      assert {:ok, _} = Instance.set_unreachable(instance.host)

      instance = Repo.get(Instance, instance.id)
      assert instance.unreachable_since
    end

    test "does NOT modify `unreachable_since` value of existing record in case it's present" do
      instance =
        insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))

      assert instance.unreachable_since
      initial_value = instance.unreachable_since

      assert {:ok, _} = Instance.set_unreachable(instance.host)

      instance = Repo.get(Instance, instance.id)
      assert initial_value == instance.unreachable_since
    end
  end

  describe "set_unreachable/2" do
    test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
      instance =
        insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))

      assert instance.unreachable_since

      past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
      assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)

      instance = Repo.get(Instance, instance.id)
      assert past_value == instance.unreachable_since
    end

    test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
      instance =
        insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))

      assert instance.unreachable_since
      initial_value = instance.unreachable_since

      assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())

      instance = Repo.get(Instance, instance.id)
      assert initial_value == instance.unreachable_since
    end
  end

  describe "get_or_update_favicon/1" do
    test "Scrapes favicon URLs" do
      Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
        %Tesla.Env{
          status: 200,
          body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
        }
      end)

      assert "https://favicon.example.org/favicon.png" ==
               Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
    end

    test "Returns nil on too long favicon URLs" do
      long_favicon_url =
        "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"

      Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
        %Tesla.Env{
          status: 200,
          body:
            ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
        }
      end)

      assert capture_log(fn ->
               assert nil ==
                        Instance.get_or_update_favicon(
                          URI.parse("https://long-favicon.example.org/")
                        )
             end) =~
               "Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
    end

    test "Handles not getting a favicon URL properly" do
      Tesla.Mock.mock(fn %{url: "https://no-favicon.example.org/"} ->
        %Tesla.Env{
          status: 200,
          body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
        }
      end)

      refute capture_log(fn ->
               assert nil ==
                        Instance.get_or_update_favicon(
                          URI.parse("https://no-favicon.example.org/")
                        )
             end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
    end

    test "Doesn't scrapes unreachable instances" do
      instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold())
      url = "https://" <> instance.host

      assert capture_log(fn -> assert nil == Instance.get_or_update_favicon(URI.parse(url)) end) =~
               "Instance.scrape_favicon(\"#{url}\") ignored unreachable host"
    end
  end

  describe "get_or_update_metadata/1" do
    test "Scrapes Wildebeest NodeInfo" do
      Tesla.Mock.mock(fn
        %{url: "https://wildebeest.example.org/.well-known/nodeinfo"} ->
          %Tesla.Env{
            status: 200,
            body: File.read!("test/fixtures/wildebeest-well-known-nodeinfo.json")
          }

        %{url: "https://wildebeest.example.org/nodeinfo/2.1"} ->
          %Tesla.Env{
            status: 200,
            body: File.read!("test/fixtures/wildebeest-nodeinfo21.json")
          }
      end)

      expected = %{
        software_name: "wildebeest",
        software_repository: "https://github.com/cloudflare/wildebeest",
        software_version: "0.0.1"
      }

      assert expected ==
               Instance.get_or_update_metadata(URI.parse("https://wildebeest.example.org/"))

      expected = %Pleroma.Instances.Instance.Pleroma.Instances.Metadata{
        software_name: "wildebeest",
        software_repository: "https://github.com/cloudflare/wildebeest",
        software_version: "0.0.1"
      }

      assert expected ==
               Repo.get_by(Pleroma.Instances.Instance, %{host: "wildebeest.example.org"}).metadata
    end

    test "Scrapes Mastodon NodeInfo" do
      Tesla.Mock.mock(fn
        %{url: "https://mastodon.example.org/.well-known/nodeinfo"} ->
          %Tesla.Env{
            status: 200,
            body: File.read!("test/fixtures/mastodon-well-known-nodeinfo.json")
          }

        %{url: "https://mastodon.example.org/nodeinfo/2.0"} ->
          %Tesla.Env{
            status: 200,
            body: File.read!("test/fixtures/mastodon-nodeinfo20.json")
          }
      end)

      expected = %{
        software_name: "mastodon",
        software_version: "4.1.0"
      }

      assert expected ==
               Instance.get_or_update_metadata(URI.parse("https://mastodon.example.org/"))
    end
  end

  test "delete_users_and_activities/1 deletes remote instance users and activities" do
    [mario, luigi, _peach, wario] =
      users = [
        insert(:user, nickname: "mario@mushroom.kingdom", name: "Mario"),
        insert(:user, nickname: "luigi@mushroom.kingdom", name: "Luigi"),
        insert(:user, nickname: "peach@mushroom.kingdom", name: "Peach"),
        insert(:user, nickname: "wario@greedville.biz", name: "Wario")
      ]

    {:ok, post1} = CommonAPI.post(mario, %{status: "letsa go!"})
    {:ok, post2} = CommonAPI.post(luigi, %{status: "itsa me... luigi"})
    {:ok, post3} = CommonAPI.post(wario, %{status: "WHA-HA-HA!"})

    {:ok, job} = Instance.delete_users_and_activities("mushroom.kingdom")
    :ok = ObanHelpers.perform(job)

    [mario, luigi, peach, wario] = Repo.reload(users)

    refute mario.is_active
    refute luigi.is_active
    refute peach.is_active
    refute peach.name == "Peach"

    assert wario.is_active
    assert wario.name == "Wario"

    assert [nil, nil, %{}] = Repo.reload([post1, post2, post3])
  end
end