logo

pleroma

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

instances_test.exs (4018B)


  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.InstancesTest do
  5. alias Pleroma.Instances
  6. use Pleroma.DataCase
  7. setup_all do: clear_config([:instance, :federation_reachability_timeout_days], 1)
  8. describe "reachable?/1" do
  9. test "returns `true` for host / url with unknown reachability status" do
  10. assert Instances.reachable?("unknown.site")
  11. assert Instances.reachable?("http://unknown.site")
  12. end
  13. test "returns `false` for host / url marked unreachable for at least `reachability_datetime_threshold()`" do
  14. host = "consistently-unreachable.name"
  15. Instances.set_consistently_unreachable(host)
  16. refute Instances.reachable?(host)
  17. refute Instances.reachable?("http://#{host}/path")
  18. end
  19. test "returns `true` for host / url marked unreachable for less than `reachability_datetime_threshold()`" do
  20. url = "http://eventually-unreachable.name/path"
  21. Instances.set_unreachable(url)
  22. assert Instances.reachable?(url)
  23. assert Instances.reachable?(URI.parse(url).host)
  24. end
  25. test "returns true on non-binary input" do
  26. assert Instances.reachable?(nil)
  27. assert Instances.reachable?(1)
  28. end
  29. end
  30. describe "filter_reachable/1" do
  31. setup do
  32. host = "consistently-unreachable.name"
  33. url1 = "http://eventually-unreachable.com/path"
  34. url2 = "http://domain.com/path"
  35. Instances.set_consistently_unreachable(host)
  36. Instances.set_unreachable(url1)
  37. result = Instances.filter_reachable([host, url1, url2, nil])
  38. %{result: result, url1: url1, url2: url2}
  39. end
  40. test "returns a map with keys containing 'not marked consistently unreachable' elements of supplied list",
  41. %{result: result, url1: url1, url2: url2} do
  42. assert is_map(result)
  43. assert Enum.sort([url1, url2]) == result |> Map.keys() |> Enum.sort()
  44. end
  45. test "returns a map with `unreachable_since` values for keys",
  46. %{result: result, url1: url1, url2: url2} do
  47. assert is_map(result)
  48. assert %NaiveDateTime{} = result[url1]
  49. assert is_nil(result[url2])
  50. end
  51. test "returns an empty map for empty list or list containing no hosts / url" do
  52. assert %{} == Instances.filter_reachable([])
  53. assert %{} == Instances.filter_reachable([nil])
  54. end
  55. end
  56. describe "set_reachable/1" do
  57. test "sets unreachable url or host reachable" do
  58. host = "domain.com"
  59. Instances.set_consistently_unreachable(host)
  60. refute Instances.reachable?(host)
  61. Instances.set_reachable(host)
  62. assert Instances.reachable?(host)
  63. end
  64. test "keeps reachable url or host reachable" do
  65. url = "https://site.name?q="
  66. assert Instances.reachable?(url)
  67. Instances.set_reachable(url)
  68. assert Instances.reachable?(url)
  69. end
  70. test "returns error status on non-binary input" do
  71. assert {:error, _} = Instances.set_reachable(nil)
  72. assert {:error, _} = Instances.set_reachable(1)
  73. end
  74. end
  75. # Note: implementation-specific (e.g. Instance) details of set_unreachable/1
  76. # should be tested in implementation-specific tests
  77. describe "set_unreachable/1" do
  78. test "returns error status on non-binary input" do
  79. assert {:error, _} = Instances.set_unreachable(nil)
  80. assert {:error, _} = Instances.set_unreachable(1)
  81. end
  82. end
  83. describe "set_consistently_unreachable/1" do
  84. test "sets reachable url or host unreachable" do
  85. url = "http://domain.com?q="
  86. assert Instances.reachable?(url)
  87. Instances.set_consistently_unreachable(url)
  88. refute Instances.reachable?(url)
  89. end
  90. test "keeps unreachable url or host unreachable" do
  91. host = "site.name"
  92. Instances.set_consistently_unreachable(host)
  93. refute Instances.reachable?(host)
  94. Instances.set_consistently_unreachable(host)
  95. refute Instances.reachable?(host)
  96. end
  97. end
  98. end