logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

instance_test.exs (5699B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Instances.InstanceTest do
  5. alias Pleroma.Instances.Instance
  6. alias Pleroma.Repo
  7. use Pleroma.DataCase
  8. import ExUnit.CaptureLog
  9. import Pleroma.Factory
  10. setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
  11. describe "set_reachable/1" do
  12. test "clears `unreachable_since` of existing matching Instance record having non-nil `unreachable_since`" do
  13. unreachable_since = NaiveDateTime.to_iso8601(NaiveDateTime.utc_now())
  14. instance = insert(:instance, unreachable_since: unreachable_since)
  15. assert {:ok, instance} = Instance.set_reachable(instance.host)
  16. refute instance.unreachable_since
  17. end
  18. test "keeps nil `unreachable_since` of existing matching Instance record having nil `unreachable_since`" do
  19. instance = insert(:instance, unreachable_since: nil)
  20. assert {:ok, instance} = Instance.set_reachable(instance.host)
  21. refute instance.unreachable_since
  22. end
  23. test "does NOT create an Instance record in case of no existing matching record" do
  24. host = "domain.org"
  25. assert nil == Instance.set_reachable(host)
  26. assert [] = Repo.all(Ecto.Query.from(i in Instance))
  27. assert Instance.reachable?(host)
  28. end
  29. end
  30. describe "set_unreachable/1" do
  31. test "creates new record having `unreachable_since` to current time if record does not exist" do
  32. assert {:ok, instance} = Instance.set_unreachable("https://domain.com/path")
  33. instance = Repo.get(Instance, instance.id)
  34. assert instance.unreachable_since
  35. assert "domain.com" == instance.host
  36. end
  37. test "sets `unreachable_since` of existing record having nil `unreachable_since`" do
  38. instance = insert(:instance, unreachable_since: nil)
  39. refute instance.unreachable_since
  40. assert {:ok, _} = Instance.set_unreachable(instance.host)
  41. instance = Repo.get(Instance, instance.id)
  42. assert instance.unreachable_since
  43. end
  44. test "does NOT modify `unreachable_since` value of existing record in case it's present" do
  45. instance =
  46. insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
  47. assert instance.unreachable_since
  48. initial_value = instance.unreachable_since
  49. assert {:ok, _} = Instance.set_unreachable(instance.host)
  50. instance = Repo.get(Instance, instance.id)
  51. assert initial_value == instance.unreachable_since
  52. end
  53. end
  54. describe "set_unreachable/2" do
  55. test "sets `unreachable_since` value of existing record in case it's newer than supplied value" do
  56. instance =
  57. insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
  58. assert instance.unreachable_since
  59. past_value = NaiveDateTime.add(NaiveDateTime.utc_now(), -100)
  60. assert {:ok, _} = Instance.set_unreachable(instance.host, past_value)
  61. instance = Repo.get(Instance, instance.id)
  62. assert past_value == instance.unreachable_since
  63. end
  64. test "does NOT modify `unreachable_since` value of existing record in case it's equal to or older than supplied value" do
  65. instance =
  66. insert(:instance, unreachable_since: NaiveDateTime.add(NaiveDateTime.utc_now(), -10))
  67. assert instance.unreachable_since
  68. initial_value = instance.unreachable_since
  69. assert {:ok, _} = Instance.set_unreachable(instance.host, NaiveDateTime.utc_now())
  70. instance = Repo.get(Instance, instance.id)
  71. assert initial_value == instance.unreachable_since
  72. end
  73. end
  74. describe "get_or_update_favicon/1" do
  75. test "Scrapes favicon URLs" do
  76. Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
  77. %Tesla.Env{
  78. status: 200,
  79. body: ~s[<html><head><link rel="icon" href="/favicon.png"></head></html>]
  80. }
  81. end)
  82. assert "https://favicon.example.org/favicon.png" ==
  83. Instance.get_or_update_favicon(URI.parse("https://favicon.example.org/"))
  84. end
  85. test "Returns nil on too long favicon URLs" do
  86. long_favicon_url =
  87. "https://Lorem.ipsum.dolor.sit.amet/consecteturadipiscingelit/Praesentpharetrapurusutaliquamtempus/Mauriseulaoreetarcu/atfacilisisorci/Nullamporttitor/nequesedfeugiatmollis/dolormagnaefficiturlorem/nonpretiumsapienorcieurisus/Nullamveleratsem/Maecenassedaccumsanexnam/favicon.png"
  88. Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
  89. %Tesla.Env{
  90. status: 200,
  91. body:
  92. ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
  93. }
  94. end)
  95. assert capture_log(fn ->
  96. assert nil ==
  97. Instance.get_or_update_favicon(
  98. URI.parse("https://long-favicon.example.org/")
  99. )
  100. end) =~
  101. "Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
  102. end
  103. test "Handles not getting a favicon URL properly" do
  104. Tesla.Mock.mock(fn %{url: "https://no-favicon.example.org/"} ->
  105. %Tesla.Env{
  106. status: 200,
  107. body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
  108. }
  109. end)
  110. refute capture_log(fn ->
  111. assert nil ==
  112. Instance.get_or_update_favicon(
  113. URI.parse("https://no-favicon.example.org/")
  114. )
  115. end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
  116. end
  117. end
  118. end