logo

pleroma

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

config_test.exs (17032B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 defmodule Pleroma.Web.AdminAPI.ConfigTest do
      6   use Pleroma.DataCase, async: true
      7   import Pleroma.Factory
      8   alias Pleroma.Web.AdminAPI.Config
      9 
     10   test "get_by_key/1" do
     11     config = insert(:config)
     12     insert(:config)
     13 
     14     assert config == Config.get_by_params(%{group: config.group, key: config.key})
     15   end
     16 
     17   test "create/1" do
     18     {:ok, config} = Config.create(%{group: "pleroma", key: "some_key", value: "some_value"})
     19     assert config == Config.get_by_params(%{group: "pleroma", key: "some_key"})
     20   end
     21 
     22   test "update/1" do
     23     config = insert(:config)
     24     {:ok, updated} = Config.update(config, %{value: "some_value"})
     25     loaded = Config.get_by_params(%{group: config.group, key: config.key})
     26     assert loaded == updated
     27   end
     28 
     29   test "update_or_create/1" do
     30     config = insert(:config)
     31     key2 = "another_key"
     32 
     33     params = [
     34       %{group: "pleroma", key: key2, value: "another_value"},
     35       %{group: config.group, key: config.key, value: "new_value"}
     36     ]
     37 
     38     assert Repo.all(Config) |> length() == 1
     39 
     40     Enum.each(params, &Config.update_or_create(&1))
     41 
     42     assert Repo.all(Config) |> length() == 2
     43 
     44     config1 = Config.get_by_params(%{group: config.group, key: config.key})
     45     config2 = Config.get_by_params(%{group: "pleroma", key: key2})
     46 
     47     assert config1.value == Config.transform("new_value")
     48     assert config2.value == Config.transform("another_value")
     49   end
     50 
     51   test "delete/1" do
     52     config = insert(:config)
     53     {:ok, _} = Config.delete(%{key: config.key, group: config.group})
     54     refute Config.get_by_params(%{key: config.key, group: config.group})
     55   end
     56 
     57   describe "transform/1" do
     58     test "string" do
     59       binary = Config.transform("value as string")
     60       assert binary == :erlang.term_to_binary("value as string")
     61       assert Config.from_binary(binary) == "value as string"
     62     end
     63 
     64     test "boolean" do
     65       binary = Config.transform(false)
     66       assert binary == :erlang.term_to_binary(false)
     67       assert Config.from_binary(binary) == false
     68     end
     69 
     70     test "nil" do
     71       binary = Config.transform(nil)
     72       assert binary == :erlang.term_to_binary(nil)
     73       assert Config.from_binary(binary) == nil
     74     end
     75 
     76     test "integer" do
     77       binary = Config.transform(150)
     78       assert binary == :erlang.term_to_binary(150)
     79       assert Config.from_binary(binary) == 150
     80     end
     81 
     82     test "atom" do
     83       binary = Config.transform(":atom")
     84       assert binary == :erlang.term_to_binary(:atom)
     85       assert Config.from_binary(binary) == :atom
     86     end
     87 
     88     test "pleroma module" do
     89       binary = Config.transform("Pleroma.Bookmark")
     90       assert binary == :erlang.term_to_binary(Pleroma.Bookmark)
     91       assert Config.from_binary(binary) == Pleroma.Bookmark
     92     end
     93 
     94     test "phoenix module" do
     95       binary = Config.transform("Phoenix.Socket.V1.JSONSerializer")
     96       assert binary == :erlang.term_to_binary(Phoenix.Socket.V1.JSONSerializer)
     97       assert Config.from_binary(binary) == Phoenix.Socket.V1.JSONSerializer
     98     end
     99 
    100     test "sigil" do
    101       binary = Config.transform("~r/comp[lL][aA][iI][nN]er/")
    102       assert binary == :erlang.term_to_binary(~r/comp[lL][aA][iI][nN]er/)
    103       assert Config.from_binary(binary) == ~r/comp[lL][aA][iI][nN]er/
    104     end
    105 
    106     test "link sigil" do
    107       binary = Config.transform("~r/https:\/\/example.com/")
    108       assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/)
    109       assert Config.from_binary(binary) == ~r/https:\/\/example.com/
    110     end
    111 
    112     test "link sigil with u modifier" do
    113       binary = Config.transform("~r/https:\/\/example.com/u")
    114       assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/u)
    115       assert Config.from_binary(binary) == ~r/https:\/\/example.com/u
    116     end
    117 
    118     test "link sigil with i modifier" do
    119       binary = Config.transform("~r/https:\/\/example.com/i")
    120       assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/i)
    121       assert Config.from_binary(binary) == ~r/https:\/\/example.com/i
    122     end
    123 
    124     test "link sigil with s modifier" do
    125       binary = Config.transform("~r/https:\/\/example.com/s")
    126       assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/s)
    127       assert Config.from_binary(binary) == ~r/https:\/\/example.com/s
    128     end
    129 
    130     test "2 child tuple" do
    131       binary = Config.transform(%{"tuple" => ["v1", ":v2"]})
    132       assert binary == :erlang.term_to_binary({"v1", :v2})
    133       assert Config.from_binary(binary) == {"v1", :v2}
    134     end
    135 
    136     test "tuple with n childs" do
    137       binary =
    138         Config.transform(%{
    139           "tuple" => [
    140             "v1",
    141             ":v2",
    142             "Pleroma.Bookmark",
    143             150,
    144             false,
    145             "Phoenix.Socket.V1.JSONSerializer"
    146           ]
    147         })
    148 
    149       assert binary ==
    150                :erlang.term_to_binary(
    151                  {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
    152                )
    153 
    154       assert Config.from_binary(binary) ==
    155                {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
    156     end
    157 
    158     test "tuple with dispatch key" do
    159       binary = Config.transform(%{"tuple" => [":dispatch", ["{:_,
    160        [
    161          {\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
    162          {\"/websocket\", Phoenix.Endpoint.CowboyWebSocket,
    163           {Phoenix.Transports.WebSocket,
    164            {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}},
    165          {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
    166        ]}"]]})
    167 
    168       assert binary ==
    169                :erlang.term_to_binary(
    170                  {:dispatch,
    171                   [
    172                     {:_,
    173                      [
    174                        {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
    175                        {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
    176                         {Phoenix.Transports.WebSocket,
    177                          {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
    178                        {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
    179                      ]}
    180                   ]}
    181                )
    182 
    183       assert Config.from_binary(binary) ==
    184                {:dispatch,
    185                 [
    186                   {:_,
    187                    [
    188                      {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
    189                      {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
    190                       {Phoenix.Transports.WebSocket,
    191                        {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
    192                      {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
    193                    ]}
    194                 ]}
    195     end
    196 
    197     test "map with string key" do
    198       binary = Config.transform(%{"key" => "value"})
    199       assert binary == :erlang.term_to_binary(%{"key" => "value"})
    200       assert Config.from_binary(binary) == %{"key" => "value"}
    201     end
    202 
    203     test "map with atom key" do
    204       binary = Config.transform(%{":key" => "value"})
    205       assert binary == :erlang.term_to_binary(%{key: "value"})
    206       assert Config.from_binary(binary) == %{key: "value"}
    207     end
    208 
    209     test "list of strings" do
    210       binary = Config.transform(["v1", "v2", "v3"])
    211       assert binary == :erlang.term_to_binary(["v1", "v2", "v3"])
    212       assert Config.from_binary(binary) == ["v1", "v2", "v3"]
    213     end
    214 
    215     test "list of modules" do
    216       binary = Config.transform(["Pleroma.Repo", "Pleroma.Activity"])
    217       assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity])
    218       assert Config.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity]
    219     end
    220 
    221     test "list of atoms" do
    222       binary = Config.transform([":v1", ":v2", ":v3"])
    223       assert binary == :erlang.term_to_binary([:v1, :v2, :v3])
    224       assert Config.from_binary(binary) == [:v1, :v2, :v3]
    225     end
    226 
    227     test "list of mixed values" do
    228       binary =
    229         Config.transform([
    230           "v1",
    231           ":v2",
    232           "Pleroma.Repo",
    233           "Phoenix.Socket.V1.JSONSerializer",
    234           15,
    235           false
    236         ])
    237 
    238       assert binary ==
    239                :erlang.term_to_binary([
    240                  "v1",
    241                  :v2,
    242                  Pleroma.Repo,
    243                  Phoenix.Socket.V1.JSONSerializer,
    244                  15,
    245                  false
    246                ])
    247 
    248       assert Config.from_binary(binary) == [
    249                "v1",
    250                :v2,
    251                Pleroma.Repo,
    252                Phoenix.Socket.V1.JSONSerializer,
    253                15,
    254                false
    255              ]
    256     end
    257 
    258     test "simple keyword" do
    259       binary = Config.transform([%{"tuple" => [":key", "value"]}])
    260       assert binary == :erlang.term_to_binary([{:key, "value"}])
    261       assert Config.from_binary(binary) == [{:key, "value"}]
    262       assert Config.from_binary(binary) == [key: "value"]
    263     end
    264 
    265     test "keyword with partial_chain key" do
    266       binary =
    267         Config.transform([%{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]}])
    268 
    269       assert binary == :erlang.term_to_binary(partial_chain: &:hackney_connect.partial_chain/1)
    270       assert Config.from_binary(binary) == [partial_chain: &:hackney_connect.partial_chain/1]
    271     end
    272 
    273     test "keyword" do
    274       binary =
    275         Config.transform([
    276           %{"tuple" => [":types", "Pleroma.PostgresTypes"]},
    277           %{"tuple" => [":telemetry_event", ["Pleroma.Repo.Instrumenter"]]},
    278           %{"tuple" => [":migration_lock", nil]},
    279           %{"tuple" => [":key1", 150]},
    280           %{"tuple" => [":key2", "string"]}
    281         ])
    282 
    283       assert binary ==
    284                :erlang.term_to_binary(
    285                  types: Pleroma.PostgresTypes,
    286                  telemetry_event: [Pleroma.Repo.Instrumenter],
    287                  migration_lock: nil,
    288                  key1: 150,
    289                  key2: "string"
    290                )
    291 
    292       assert Config.from_binary(binary) == [
    293                types: Pleroma.PostgresTypes,
    294                telemetry_event: [Pleroma.Repo.Instrumenter],
    295                migration_lock: nil,
    296                key1: 150,
    297                key2: "string"
    298              ]
    299     end
    300 
    301     test "complex keyword with nested mixed childs" do
    302       binary =
    303         Config.transform([
    304           %{"tuple" => [":uploader", "Pleroma.Uploaders.Local"]},
    305           %{"tuple" => [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
    306           %{"tuple" => [":link_name", true]},
    307           %{"tuple" => [":proxy_remote", false]},
    308           %{"tuple" => [":common_map", %{":key" => "value"}]},
    309           %{
    310             "tuple" => [
    311               ":proxy_opts",
    312               [
    313                 %{"tuple" => [":redirect_on_failure", false]},
    314                 %{"tuple" => [":max_body_length", 1_048_576]},
    315                 %{
    316                   "tuple" => [
    317                     ":http",
    318                     [%{"tuple" => [":follow_redirect", true]}, %{"tuple" => [":pool", ":upload"]}]
    319                   ]
    320                 }
    321               ]
    322             ]
    323           }
    324         ])
    325 
    326       assert binary ==
    327                :erlang.term_to_binary(
    328                  uploader: Pleroma.Uploaders.Local,
    329                  filters: [Pleroma.Upload.Filter.Dedupe],
    330                  link_name: true,
    331                  proxy_remote: false,
    332                  common_map: %{key: "value"},
    333                  proxy_opts: [
    334                    redirect_on_failure: false,
    335                    max_body_length: 1_048_576,
    336                    http: [
    337                      follow_redirect: true,
    338                      pool: :upload
    339                    ]
    340                  ]
    341                )
    342 
    343       assert Config.from_binary(binary) ==
    344                [
    345                  uploader: Pleroma.Uploaders.Local,
    346                  filters: [Pleroma.Upload.Filter.Dedupe],
    347                  link_name: true,
    348                  proxy_remote: false,
    349                  common_map: %{key: "value"},
    350                  proxy_opts: [
    351                    redirect_on_failure: false,
    352                    max_body_length: 1_048_576,
    353                    http: [
    354                      follow_redirect: true,
    355                      pool: :upload
    356                    ]
    357                  ]
    358                ]
    359     end
    360 
    361     test "common keyword" do
    362       binary =
    363         Config.transform([
    364           %{"tuple" => [":level", ":warn"]},
    365           %{"tuple" => [":meta", [":all"]]},
    366           %{"tuple" => [":path", ""]},
    367           %{"tuple" => [":val", nil]},
    368           %{"tuple" => [":webhook_url", "https://hooks.slack.com/services/YOUR-KEY-HERE"]}
    369         ])
    370 
    371       assert binary ==
    372                :erlang.term_to_binary(
    373                  level: :warn,
    374                  meta: [:all],
    375                  path: "",
    376                  val: nil,
    377                  webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
    378                )
    379 
    380       assert Config.from_binary(binary) == [
    381                level: :warn,
    382                meta: [:all],
    383                path: "",
    384                val: nil,
    385                webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
    386              ]
    387     end
    388 
    389     test "complex keyword with sigil" do
    390       binary =
    391         Config.transform([
    392           %{"tuple" => [":federated_timeline_removal", []]},
    393           %{"tuple" => [":reject", ["~r/comp[lL][aA][iI][nN]er/"]]},
    394           %{"tuple" => [":replace", []]}
    395         ])
    396 
    397       assert binary ==
    398                :erlang.term_to_binary(
    399                  federated_timeline_removal: [],
    400                  reject: [~r/comp[lL][aA][iI][nN]er/],
    401                  replace: []
    402                )
    403 
    404       assert Config.from_binary(binary) ==
    405                [federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []]
    406     end
    407 
    408     test "complex keyword with tuples with more than 2 values" do
    409       binary =
    410         Config.transform([
    411           %{
    412             "tuple" => [
    413               ":http",
    414               [
    415                 %{
    416                   "tuple" => [
    417                     ":key1",
    418                     [
    419                       %{
    420                         "tuple" => [
    421                           ":_",
    422                           [
    423                             %{
    424                               "tuple" => [
    425                                 "/api/v1/streaming",
    426                                 "Pleroma.Web.MastodonAPI.WebsocketHandler",
    427                                 []
    428                               ]
    429                             },
    430                             %{
    431                               "tuple" => [
    432                                 "/websocket",
    433                                 "Phoenix.Endpoint.CowboyWebSocket",
    434                                 %{
    435                                   "tuple" => [
    436                                     "Phoenix.Transports.WebSocket",
    437                                     %{
    438                                       "tuple" => [
    439                                         "Pleroma.Web.Endpoint",
    440                                         "Pleroma.Web.UserSocket",
    441                                         []
    442                                       ]
    443                                     }
    444                                   ]
    445                                 }
    446                               ]
    447                             },
    448                             %{
    449                               "tuple" => [
    450                                 ":_",
    451                                 "Phoenix.Endpoint.Cowboy2Handler",
    452                                 %{"tuple" => ["Pleroma.Web.Endpoint", []]}
    453                               ]
    454                             }
    455                           ]
    456                         ]
    457                       }
    458                     ]
    459                   ]
    460                 }
    461               ]
    462             ]
    463           }
    464         ])
    465 
    466       assert binary ==
    467                :erlang.term_to_binary(
    468                  http: [
    469                    key1: [
    470                      _: [
    471                        {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
    472                        {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
    473                         {Phoenix.Transports.WebSocket,
    474                          {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
    475                        {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
    476                      ]
    477                    ]
    478                  ]
    479                )
    480 
    481       assert Config.from_binary(binary) == [
    482                http: [
    483                  key1: [
    484                    {:_,
    485                     [
    486                       {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
    487                       {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
    488                        {Phoenix.Transports.WebSocket,
    489                         {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
    490                       {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
    491                     ]}
    492                  ]
    493                ]
    494              ]
    495     end
    496   end
    497 end