config_test.exs 8.13 KB
Newer Older
1 2 3 4 5 6 7 8 9
defmodule Pleroma.Web.AdminAPI.ConfigTest do
  use Pleroma.DataCase, async: true
  import Pleroma.Factory
  alias Pleroma.Web.AdminAPI.Config

  test "get_by_key/1" do
    config = insert(:config)
    insert(:config)

10
    assert config == Config.get_by_params(%{group: config.group, key: config.key})
11 12 13
  end

  test "create/1" do
14 15
    {:ok, config} = Config.create(%{group: "pleroma", key: "some_key", value: "some_value"})
    assert config == Config.get_by_params(%{group: "pleroma", key: "some_key"})
16 17 18 19 20
  end

  test "update/1" do
    config = insert(:config)
    {:ok, updated} = Config.update(config, %{value: "some_value"})
21
    loaded = Config.get_by_params(%{group: config.group, key: config.key})
22 23 24 25 26 27 28 29
    assert loaded == updated
  end

  test "update_or_create/1" do
    config = insert(:config)
    key2 = "another_key"

    params = [
30 31
      %{group: "pleroma", key: key2, value: "another_value"},
      %{group: config.group, key: config.key, value: "new_value"}
32 33 34 35 36 37 38 39
    ]

    assert Repo.all(Config) |> length() == 1

    Enum.each(params, &Config.update_or_create(&1))

    assert Repo.all(Config) |> length() == 2

40 41
    config1 = Config.get_by_params(%{group: config.group, key: config.key})
    config2 = Config.get_by_params(%{group: "pleroma", key: key2})
42 43 44 45 46 47 48

    assert config1.value == Config.transform("new_value")
    assert config2.value == Config.transform("another_value")
  end

  test "delete/1" do
    config = insert(:config)
49 50
    {:ok, _} = Config.delete(%{key: config.key, group: config.group})
    refute Config.get_by_params(%{key: config.key, group: config.group})
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
  end

  describe "transform/1" do
    test "string" do
      binary = Config.transform("value as string")
      assert binary == :erlang.term_to_binary("value as string")
      assert Config.from_binary(binary) == "value as string"
    end

    test "list of modules" do
      binary = Config.transform(["Pleroma.Repo", "Pleroma.Activity"])
      assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity])
      assert Config.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity]
    end

    test "list of strings" do
      binary = Config.transform(["string1", "string2"])
      assert binary == :erlang.term_to_binary(["string1", "string2"])
      assert Config.from_binary(binary) == ["string1", "string2"]
    end

    test "map" do
      binary =
        Config.transform(%{
          "types" => "Pleroma.PostgresTypes",
          "telemetry_event" => ["Pleroma.Repo.Instrumenter"],
          "migration_lock" => ""
        })

      assert binary ==
               :erlang.term_to_binary(
                 telemetry_event: [Pleroma.Repo.Instrumenter],
                 types: Pleroma.PostgresTypes
               )

      assert Config.from_binary(binary) == [
               telemetry_event: [Pleroma.Repo.Instrumenter],
               types: Pleroma.PostgresTypes
             ]
    end

    test "complex map with nested integers, lists and atoms" do
      binary =
        Config.transform(%{
          "uploader" => "Pleroma.Uploaders.Local",
          "filters" => ["Pleroma.Upload.Filter.Dedupe"],
          "link_name" => ":true",
          "proxy_remote" => ":false",
          "proxy_opts" => %{
            "redirect_on_failure" => ":false",
            "max_body_length" => "i:1048576",
            "http" => %{
              "follow_redirect" => ":true",
              "pool" => ":upload"
            }
          }
        })

      assert binary ==
               :erlang.term_to_binary(
                 filters: [Pleroma.Upload.Filter.Dedupe],
                 link_name: true,
                 proxy_opts: [
                   http: [
                     follow_redirect: true,
                     pool: :upload
                   ],
                   max_body_length: 1_048_576,
                   redirect_on_failure: false
                 ],
                 proxy_remote: false,
                 uploader: Pleroma.Uploaders.Local
               )

      assert Config.from_binary(binary) ==
               [
                 filters: [Pleroma.Upload.Filter.Dedupe],
                 link_name: true,
                 proxy_opts: [
                   http: [
                     follow_redirect: true,
                     pool: :upload
                   ],
                   max_body_length: 1_048_576,
                   redirect_on_failure: false
                 ],
                 proxy_remote: false,
                 uploader: Pleroma.Uploaders.Local
               ]
    end

    test "keyword" do
      binary =
        Config.transform(%{
          "level" => ":warn",
          "meta" => [":all"],
          "webhook_url" => "https://hooks.slack.com/services/YOUR-KEY-HERE"
        })

      assert binary ==
               :erlang.term_to_binary(
                 level: :warn,
                 meta: [:all],
                 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
               )

      assert Config.from_binary(binary) == [
               level: :warn,
               meta: [:all],
               webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
             ]
    end

    test "complex map with sigil" do
      binary =
        Config.transform(%{
          federated_timeline_removal: [],
          reject: [~r/comp[lL][aA][iI][nN]er/],
          replace: []
        })

      assert binary ==
               :erlang.term_to_binary(
                 federated_timeline_removal: [],
                 reject: [~r/comp[lL][aA][iI][nN]er/],
                 replace: []
               )

      assert Config.from_binary(binary) ==
               [federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []]
    end
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 245 246 247 248 249 250 251 252 253 254 255 256

    test "complex map with tuples with more than 2 values" do
      binary =
        Config.transform(%{
          "http" => %{
            "dispatch" => [
              %{
                "tuple" => [
                  ":_",
                  [
                    %{
                      "tuple" => [
                        "/api/v1/streaming",
                        "Pleroma.Web.MastodonAPI.WebsocketHandler",
                        []
                      ]
                    },
                    %{
                      "tuple" => [
                        "/websocket",
                        "Phoenix.Endpoint.CowboyWebSocket",
                        %{
                          "tuple" => [
                            "Phoenix.Transports.WebSocket",
                            %{"tuple" => ["Pleroma.Web.Endpoint", "Pleroma.Web.UserSocket", []]}
                          ]
                        }
                      ]
                    },
                    %{
                      "tuple" => [
                        ":_",
                        "Phoenix.Endpoint.Cowboy2Handler",
                        %{
                          "tuple" => ["Pleroma.Web.Endpoint", []]
                        }
                      ]
                    }
                  ]
                ]
              }
            ]
          }
        })

      assert binary ==
               :erlang.term_to_binary(
                 http: [
                   dispatch: [
                     _: [
                       {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
                       {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
                        {Phoenix.Transports.WebSocket,
                         {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
                       {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
                     ]
                   ]
                 ]
               )

      assert Config.from_binary(binary) == [
               http: [
                 dispatch: [
                   {:_,
                    [
                      {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
                      {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
                       {Phoenix.Transports.WebSocket,
                        {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
                      {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
                    ]}
                 ]
               ]
             ]
    end
257 258
  end
end