logo

pleroma

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

config_test.exs (4838B)


  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.ConfigTest do
  5. use ExUnit.Case
  6. test "get/1 with an atom" do
  7. assert Pleroma.Config.get(:instance) == Application.get_env(:pleroma, :instance)
  8. assert Pleroma.Config.get(:azertyuiop) == nil
  9. assert Pleroma.Config.get(:azertyuiop, true) == true
  10. end
  11. test "get/1 with a list of keys" do
  12. assert Pleroma.Config.get([:instance, :public]) ==
  13. Keyword.get(Application.get_env(:pleroma, :instance), :public)
  14. assert Pleroma.Config.get([Pleroma.Web.Endpoint, :render_errors, :view]) ==
  15. get_in(
  16. Application.get_env(
  17. :pleroma,
  18. Pleroma.Web.Endpoint
  19. ),
  20. [:render_errors, :view]
  21. )
  22. assert Pleroma.Config.get([:azerty, :uiop]) == nil
  23. assert Pleroma.Config.get([:azerty, :uiop], true) == true
  24. end
  25. describe "nil values" do
  26. setup do
  27. Pleroma.Config.put(:lorem, nil)
  28. Pleroma.Config.put(:ipsum, %{dolor: [sit: nil]})
  29. Pleroma.Config.put(:dolor, sit: %{amet: nil})
  30. on_exit(fn -> Enum.each(~w(lorem ipsum dolor)a, &Pleroma.Config.delete/1) end)
  31. end
  32. test "get/1 with an atom for nil value" do
  33. assert Pleroma.Config.get(:lorem) == nil
  34. end
  35. test "get/2 with an atom for nil value" do
  36. assert Pleroma.Config.get(:lorem, true) == nil
  37. end
  38. test "get/1 with a list of keys for nil value" do
  39. assert Pleroma.Config.get([:ipsum, :dolor, :sit]) == nil
  40. assert Pleroma.Config.get([:dolor, :sit, :amet]) == nil
  41. end
  42. test "get/2 with a list of keys for nil value" do
  43. assert Pleroma.Config.get([:ipsum, :dolor, :sit], true) == nil
  44. assert Pleroma.Config.get([:dolor, :sit, :amet], true) == nil
  45. end
  46. end
  47. test "get/1 when value is false" do
  48. Pleroma.Config.put([:instance, :false_test], false)
  49. Pleroma.Config.put([:instance, :nested], [])
  50. Pleroma.Config.put([:instance, :nested, :false_test], false)
  51. assert Pleroma.Config.get([:instance, :false_test]) == false
  52. assert Pleroma.Config.get([:instance, :nested, :false_test]) == false
  53. end
  54. test "get!/1" do
  55. assert Pleroma.Config.get!(:instance) == Application.get_env(:pleroma, :instance)
  56. assert Pleroma.Config.get!([:instance, :public]) ==
  57. Keyword.get(Application.get_env(:pleroma, :instance), :public)
  58. assert_raise(Pleroma.Config.Error, fn ->
  59. Pleroma.Config.get!(:azertyuiop)
  60. end)
  61. assert_raise(Pleroma.Config.Error, fn ->
  62. Pleroma.Config.get!([:azerty, :uiop])
  63. end)
  64. end
  65. test "get!/1 when value is false" do
  66. Pleroma.Config.put([:instance, :false_test], false)
  67. Pleroma.Config.put([:instance, :nested], [])
  68. Pleroma.Config.put([:instance, :nested, :false_test], false)
  69. assert Pleroma.Config.get!([:instance, :false_test]) == false
  70. assert Pleroma.Config.get!([:instance, :nested, :false_test]) == false
  71. end
  72. test "put/2 with a key" do
  73. Pleroma.Config.put(:config_test, true)
  74. assert Pleroma.Config.get(:config_test) == true
  75. end
  76. test "put/2 with a list of keys" do
  77. Pleroma.Config.put([:instance, :config_test], true)
  78. Pleroma.Config.put([:instance, :config_nested_test], [])
  79. Pleroma.Config.put([:instance, :config_nested_test, :x], true)
  80. assert Pleroma.Config.get([:instance, :config_test]) == true
  81. assert Pleroma.Config.get([:instance, :config_nested_test, :x]) == true
  82. end
  83. test "delete/1 with a key" do
  84. Pleroma.Config.put([:delete_me], :delete_me)
  85. Pleroma.Config.delete([:delete_me])
  86. assert Pleroma.Config.get([:delete_me]) == nil
  87. end
  88. test "delete/2 with a list of keys" do
  89. Pleroma.Config.put([:delete_me], hello: "world", world: "Hello")
  90. Pleroma.Config.delete([:delete_me, :world])
  91. assert Pleroma.Config.get([:delete_me]) == [hello: "world"]
  92. Pleroma.Config.put([:delete_me, :delete_me], hello: "world", world: "Hello")
  93. Pleroma.Config.delete([:delete_me, :delete_me, :world])
  94. assert Pleroma.Config.get([:delete_me, :delete_me]) == [hello: "world"]
  95. assert Pleroma.Config.delete([:this_key_does_not_exist])
  96. assert Pleroma.Config.delete([:non, :existing, :key])
  97. end
  98. test "fetch/1" do
  99. Pleroma.Config.put([:lorem], :ipsum)
  100. Pleroma.Config.put([:ipsum], dolor: :sit)
  101. assert Pleroma.Config.fetch([:lorem]) == {:ok, :ipsum}
  102. assert Pleroma.Config.fetch(:lorem) == {:ok, :ipsum}
  103. assert Pleroma.Config.fetch([:ipsum, :dolor]) == {:ok, :sit}
  104. assert Pleroma.Config.fetch([:lorem, :ipsum]) == :error
  105. assert Pleroma.Config.fetch([:loremipsum]) == :error
  106. assert Pleroma.Config.fetch(:loremipsum) == :error
  107. Pleroma.Config.delete([:lorem])
  108. Pleroma.Config.delete([:ipsum])
  109. end
  110. end