logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

instance_test.exs (8847B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Instances.InstanceTest do
  5. alias Pleroma.Instances
  6. alias Pleroma.Instances.Instance
  7. alias Pleroma.Repo
  8. alias Pleroma.Tests.ObanHelpers
  9. alias Pleroma.Web.CommonAPI
  10. use Pleroma.DataCase
  11. import ExUnit.CaptureLog
  12. import Pleroma.Factory
  13. setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
  14. describe "set_reachable/1" do
  15. test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
  16. unreachable_since = NaiveDateTime.to_iso8601(NaiveDateTime.utc_now())
  17. instance = insert(:instance, unreachable_since: unreachable_since)
  18. assert {:ok, instance} = Instance.set_reachable(instance.host)
  19. refute instance.unreachable_since
  20. end
  21. test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
  22. instance = insert(:instance, unreachable_since: nil)
  23. assert {:ok, instance} = Instance.set_reachable(instance.host)
  24. refute instance.unreachable_since
  25. end
  26. end
  27. describe "set_unreachable/1" do
  28. test "creates new record having `unreachable_since` to current time if record does not exist" do
  29. assert {:ok, instance} = Instance.set_unreachable("https://domain.com/path")
  30. instance = Repo.get(Instance, instance.id)
  31. assert instance.unreachable_since
  32. assert "domain.com" == instance.host
  33. end
  34. test "sets `unreachable_since` of existing record having nil `unreachable_since`" do
  35. instance = insert(:instance, unreachable_since: nil)
  36. refute instance.unreachable_since
  37. assert {:ok, _} = Instance.set_unreachable(instance.host)
  38. instance = Repo.get(Instance, instance.id)
  39. assert instance.unreachable_since
  40. end
  41. test "does NOT modify `unreachable_since` value of existing record in case it's present" do
  42. instance =
  43. insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
  44. assert instance.unreachable_since
  45. initial_value = instance.unreachable_since
  46. assert {:ok, _} = Instance.set_unreachable(instance.host)
  47. instance = Repo.get(Instance, instance.id)
  48. assert initial_value == instance.unreachable_since
  49. end
  50. end
  51. describe "set_unreachable/2" do
  52. test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
  53. instance =
  54. insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
  55. assert instance.unreachable_since
  56. past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
  57. assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
  58. instance = Repo.get(Instance, instance.id)
  59. assert past_value == instance.unreachable_since
  60. end
  61. test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
  62. instance =
  63. insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
  64. assert instance.unreachable_since
  65. initial_value = instance.unreachable_since
  66. assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
  67. instance = Repo.get(Instance, instance.id)
  68. assert initial_value == instance.unreachable_since
  69. end
  70. end
  71. describe "get_or_update_favicon/1" do
  72. test "Scrapes favicon URLs" do
  73. Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
  74. %Tesla.Env{
  75. status: 200,
  76. body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
  77. }
  78. end)
  79. assert "https://favicon.example.org/favicon.png" ==
  80. Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
  81. end
  82. test "Returns nil on too long favicon URLs" do
  83. long_favicon_url =
  84. "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
  85. Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
  86. %Tesla.Env{
  87. status: 200,
  88. body:
  89. ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
  90. }
  91. end)
  92. assert capture_log(fn ->
  93. assert nil ==
  94. Instance.get_or_update_favicon(
  95. URI.parse("https://long-favicon.example.org/")
  96. )
  97. end) =~
  98. "Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
  99. end
  100. test "Handles not getting a favicon URL properly" do
  101. Tesla.Mock.mock(fn %{url: "https://no-favicon.example.org/"} ->
  102. %Tesla.Env{
  103. status: 200,
  104. body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
  105. }
  106. end)
  107. refute capture_log(fn ->
  108. assert nil ==
  109. Instance.get_or_update_favicon(
  110. URI.parse("https://no-favicon.example.org/")
  111. )
  112. end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
  113. end
  114. test "Doesn't scrapes unreachable instances" do
  115. instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold())
  116. url = "https://" <> instance.host
  117. assert capture_log(fn -> assert nil == Instance.get_or_update_favicon(URI.parse(url)) end) =~
  118. "Instance.scrape_favicon(\"#{url}\") ignored unreachable host"
  119. end
  120. end
  121. describe "get_or_update_metadata/1" do
  122. test "Scrapes Wildebeest NodeInfo" do
  123. Tesla.Mock.mock(fn
  124. %{url: "https://wildebeest.example.org/.well-known/nodeinfo"} ->
  125. %Tesla.Env{
  126. status: 200,
  127. body: File.read!("test/fixtures/wildebeest-well-known-nodeinfo.json")
  128. }
  129. %{url: "https://wildebeest.example.org/nodeinfo/2.1"} ->
  130. %Tesla.Env{
  131. status: 200,
  132. body: File.read!("test/fixtures/wildebeest-nodeinfo21.json")
  133. }
  134. end)
  135. expected = %{
  136. software_name: "wildebeest",
  137. software_repository: "https://github.com/cloudflare/wildebeest",
  138. software_version: "0.0.1"
  139. }
  140. assert expected ==
  141. Instance.get_or_update_metadata(URI.parse("https://wildebeest.example.org/"))
  142. expected = %Pleroma.Instances.Instance.Pleroma.Instances.Metadata{
  143. software_name: "wildebeest",
  144. software_repository: "https://github.com/cloudflare/wildebeest",
  145. software_version: "0.0.1"
  146. }
  147. assert expected ==
  148. Repo.get_by(Pleroma.Instances.Instance, %{host: "wildebeest.example.org"}).metadata
  149. end
  150. test "Scrapes Mastodon NodeInfo" do
  151. Tesla.Mock.mock(fn
  152. %{url: "https://mastodon.example.org/.well-known/nodeinfo"} ->
  153. %Tesla.Env{
  154. status: 200,
  155. body: File.read!("test/fixtures/mastodon-well-known-nodeinfo.json")
  156. }
  157. %{url: "https://mastodon.example.org/nodeinfo/2.0"} ->
  158. %Tesla.Env{
  159. status: 200,
  160. body: File.read!("test/fixtures/mastodon-nodeinfo20.json")
  161. }
  162. end)
  163. expected = %{
  164. software_name: "mastodon",
  165. software_version: "4.1.0"
  166. }
  167. assert expected ==
  168. Instance.get_or_update_metadata(URI.parse("https://mastodon.example.org/"))
  169. end
  170. end
  171. test "delete_users_and_activities/1 deletes remote instance users and activities" do
  172. [mario, luigi, _peach, wario] =
  173. users = [
  174. insert(:user, nickname: "mario@mushroom.kingdom", name: "Mario"),
  175. insert(:user, nickname: "luigi@mushroom.kingdom", name: "Luigi"),
  176. insert(:user, nickname: "peach@mushroom.kingdom", name: "Peach"),
  177. insert(:user, nickname: "wario@greedville.biz", name: "Wario")
  178. ]
  179. {:ok, post1} = CommonAPI.post(mario, %{status: "letsa go!"})
  180. {:ok, post2} = CommonAPI.post(luigi, %{status: "itsa me... luigi"})
  181. {:ok, post3} = CommonAPI.post(wario, %{status: "WHA-HA-HA!"})
  182. {:ok, job} = Instance.delete_users_and_activities("mushroom.kingdom")
  183. :ok = ObanHelpers.perform(job)
  184. [mario, luigi, peach, wario] = Repo.reload(users)
  185. refute mario.is_active
  186. refute luigi.is_active
  187. refute peach.is_active
  188. refute peach.name == "Peach"
  189. assert wario.is_active
  190. assert wario.name == "Wario"
  191. assert [nil, nil, %{}] = Repo.reload([post1, post2, post3])
  192. end
  193. end