summaryrefslogtreecommitdiff
path: root/test/pleroma/config_db_test.exs
blob: d42123fb46865ce3186bd5f6f6e0d116ac25c829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.ConfigDBTest do
  use Pleroma.DataCase, async: true
  import Pleroma.Factory
  alias Pleroma.ConfigDB

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

    assert config == ConfigDB.get_by_params(%{group: config.group, key: config.key})
  end

  test "get_all_as_keyword/0" do
    saved = insert(:config)
    insert(:config, group: ":quack", key: ":level", value: :info)
    insert(:config, group: ":quack", key: ":meta", value: [:none])

    insert(:config,
      group: ":quack",
      key: ":webhook_url",
      value: "https://hooks.slack.com/services/KEY/some_val"
    )

    config = ConfigDB.get_all_as_keyword()

    assert config[:pleroma] == [
             {saved.key, saved.value}
           ]

    assert config[:quack][:level] == :info
    assert config[:quack][:meta] == [:none]
    assert config[:quack][:webhook_url] == "https://hooks.slack.com/services/KEY/some_val"
  end

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

      params = [
        %{group: :pleroma, key: key2, value: "another_value"},
        %{group: :pleroma, key: config.key, value: [a: 1, b: 2, c: "new_value"]}
      ]

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

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

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

      config1 = ConfigDB.get_by_params(%{group: config.group, key: config.key})
      config2 = ConfigDB.get_by_params(%{group: :pleroma, key: key2})

      assert config1.value == [a: 1, b: 2, c: "new_value"]
      assert config2.value == "another_value"
    end

    test "partial update" do
      config = insert(:config, value: [key1: "val1", key2: :val2])

      {:ok, config} =
        ConfigDB.update_or_create(%{
          group: config.group,
          key: config.key,
          value: [key1: :val1, key3: :val3]
        })

      updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})

      assert config.value == updated.value
      assert updated.value[:key1] == :val1
      assert updated.value[:key2] == :val2
      assert updated.value[:key3] == :val3
    end

    test "deep merge" do
      config = insert(:config, value: [key1: "val1", key2: [k1: :v1, k2: "v2"]])

      {:ok, config} =
        ConfigDB.update_or_create(%{
          group: config.group,
          key: config.key,
          value: [key1: :val1, key2: [k2: :v2, k3: :v3], key3: :val3]
        })

      updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})

      assert config.value == updated.value
      assert updated.value[:key1] == :val1
      assert updated.value[:key2] == [k1: :v1, k2: :v2, k3: :v3]
      assert updated.value[:key3] == :val3
    end

    test "only full update for some keys" do
      config1 = insert(:config, key: :ecto_repos, value: [repo: Pleroma.Repo])

      config2 = insert(:config, group: :cors_plug, key: :max_age, value: 18)

      {:ok, _config} =
        ConfigDB.update_or_create(%{
          group: config1.group,
          key: config1.key,
          value: [another_repo: [Pleroma.Repo]]
        })

      {:ok, _config} =
        ConfigDB.update_or_create(%{
          group: config2.group,
          key: config2.key,
          value: 777
        })

      updated1 = ConfigDB.get_by_params(%{group: config1.group, key: config1.key})
      updated2 = ConfigDB.get_by_params(%{group: config2.group, key: config2.key})

      assert updated1.value == [another_repo: [Pleroma.Repo]]
      assert updated2.value == 777
    end

    test "full update if value is not keyword" do
      config =
        insert(:config,
          group: ":tesla",
          key: ":adapter",
          value: Tesla.Adapter.Hackney
        )

      {:ok, _config} =
        ConfigDB.update_or_create(%{
          group: config.group,
          key: config.key,
          value: Tesla.Adapter.Httpc
        })

      updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})

      assert updated.value == Tesla.Adapter.Httpc
    end

    test "only full update for some subkeys" do
      config1 =
        insert(:config,
          key: ":emoji",
          value: [groups: [a: 1, b: 2], key: [a: 1]]
        )

      config2 =
        insert(:config,
          key: ":assets",
          value: [mascots: [a: 1, b: 2], key: [a: 1]]
        )

      {:ok, _config} =
        ConfigDB.update_or_create(%{
          group: config1.group,
          key: config1.key,
          value: [groups: [c: 3, d: 4], key: [b: 2]]
        })

      {:ok, _config} =
        ConfigDB.update_or_create(%{
          group: config2.group,
          key: config2.key,
          value: [mascots: [c: 3, d: 4], key: [b: 2]]
        })

      updated1 = ConfigDB.get_by_params(%{group: config1.group, key: config1.key})
      updated2 = ConfigDB.get_by_params(%{group: config2.group, key: config2.key})

      assert updated1.value == [groups: [c: 3, d: 4], key: [a: 1, b: 2]]
      assert updated2.value == [mascots: [c: 3, d: 4], key: [a: 1, b: 2]]
    end
  end

  describe "delete/1" do
    test "error on deleting non existing setting" do
      {:error, error} = ConfigDB.delete(%{group: ":pleroma", key: ":key"})
      assert error =~ "Config with params %{group: \":pleroma\", key: \":key\"} not found"
    end

    test "full delete" do
      config = insert(:config)
      {:ok, deleted} = ConfigDB.delete(%{group: config.group, key: config.key})
      assert Ecto.get_meta(deleted, :state) == :deleted
      refute ConfigDB.get_by_params(%{group: config.group, key: config.key})
    end

    test "partial subkeys delete" do
      config = insert(:config, value: [groups: [a: 1, b: 2], key: [a: 1]])

      {:ok, deleted} =
        ConfigDB.delete(%{group: config.group, key: config.key, subkeys: [":groups"]})

      assert Ecto.get_meta(deleted, :state) == :loaded

      assert deleted.value == [key: [a: 1]]

      updated = ConfigDB.get_by_params(%{group: config.group, key: config.key})

      assert updated.value == deleted.value
    end

    test "full delete if remaining value after subkeys deletion is empty list" do
      config = insert(:config, value: [groups: [a: 1, b: 2]])

      {:ok, deleted} =
        ConfigDB.delete(%{group: config.group, key: config.key, subkeys: [":groups"]})

      assert Ecto.get_meta(deleted, :state) == :deleted

      refute ConfigDB.get_by_params(%{group: config.group, key: config.key})
    end
  end

  describe "to_elixir_types/1" do
    test "string" do
      assert ConfigDB.to_elixir_types("value as string") == "value as string"
    end

    test "boolean" do
      assert ConfigDB.to_elixir_types(false) == false
    end

    test "nil" do
      assert ConfigDB.to_elixir_types(nil) == nil
    end

    test "integer" do
      assert ConfigDB.to_elixir_types(150) == 150
    end

    test "atom" do
      assert ConfigDB.to_elixir_types(":atom") == :atom
    end

    test "ssl options" do
      assert ConfigDB.to_elixir_types([":tlsv1", ":tlsv1.1", ":tlsv1.2"]) == [
               :tlsv1,
               :"tlsv1.1",
               :"tlsv1.2"
             ]
    end

    test "pleroma module" do
      assert ConfigDB.to_elixir_types("Pleroma.Bookmark") == Pleroma.Bookmark
    end

    test "pleroma string" do
      assert ConfigDB.to_elixir_types("Pleroma") == "Pleroma"
    end

    test "phoenix module" do
      assert ConfigDB.to_elixir_types("Phoenix.Socket.V1.JSONSerializer") ==
               Phoenix.Socket.V1.JSONSerializer
    end

    test "tesla module" do
      assert ConfigDB.to_elixir_types("Tesla.Adapter.Hackney") == Tesla.Adapter.Hackney
    end

    test "ExSyslogger module" do
      assert ConfigDB.to_elixir_types("ExSyslogger") == ExSyslogger
    end

    test "Quack.Logger module" do
      assert ConfigDB.to_elixir_types("Quack.Logger") == Quack.Logger
    end

    test "Swoosh.Adapters modules" do
      assert ConfigDB.to_elixir_types("Swoosh.Adapters.SMTP") == Swoosh.Adapters.SMTP
      assert ConfigDB.to_elixir_types("Swoosh.Adapters.AmazonSES") == Swoosh.Adapters.AmazonSES
    end

    test "sigil" do
      assert ConfigDB.to_elixir_types("~r[comp[lL][aA][iI][nN]er]") == ~r/comp[lL][aA][iI][nN]er/
    end

    test "link sigil" do
      assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/") == ~r/https:\/\/example.com/
    end

    test "link sigil with um modifiers" do
      assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/um") ==
               ~r/https:\/\/example.com/um
    end

    test "link sigil with i modifier" do
      assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/i") == ~r/https:\/\/example.com/i
    end

    test "link sigil with s modifier" do
      assert ConfigDB.to_elixir_types("~r/https:\/\/example.com/s") == ~r/https:\/\/example.com/s
    end

    test "raise if valid delimiter not found" do
      assert_raise ArgumentError, "valid delimiter for Regex expression not found", fn ->
        ConfigDB.to_elixir_types("~r/https://[]{}<>\"'()|example.com/s")
      end
    end

    test "2 child tuple" do
      assert ConfigDB.to_elixir_types(%{"tuple" => ["v1", ":v2"]}) == {"v1", :v2}
    end

    test "proxy tuple with localhost" do
      assert ConfigDB.to_elixir_types(%{
               "tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]
             }) == {:proxy_url, {:socks5, :localhost, 1234}}
    end

    test "proxy tuple with domain" do
      assert ConfigDB.to_elixir_types(%{
               "tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]
             }) == {:proxy_url, {:socks5, 'domain.com', 1234}}
    end

    test "proxy tuple with ip" do
      assert ConfigDB.to_elixir_types(%{
               "tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]
             }) == {:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}}
    end

    test "tuple with n childs" do
      assert ConfigDB.to_elixir_types(%{
               "tuple" => [
                 "v1",
                 ":v2",
                 "Pleroma.Bookmark",
                 150,
                 false,
                 "Phoenix.Socket.V1.JSONSerializer"
               ]
             }) == {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
    end

    test "map with string key" do
      assert ConfigDB.to_elixir_types(%{"key" => "value"}) == %{"key" => "value"}
    end

    test "map with atom key" do
      assert ConfigDB.to_elixir_types(%{":key" => "value"}) == %{key: "value"}
    end

    test "list of strings" do
      assert ConfigDB.to_elixir_types(["v1", "v2", "v3"]) == ["v1", "v2", "v3"]
    end

    test "list of modules" do
      assert ConfigDB.to_elixir_types(["Pleroma.Repo", "Pleroma.Activity"]) == [
               Pleroma.Repo,
               Pleroma.Activity
             ]
    end

    test "list of atoms" do
      assert ConfigDB.to_elixir_types([":v1", ":v2", ":v3"]) == [:v1, :v2, :v3]
    end

    test "list of mixed values" do
      assert ConfigDB.to_elixir_types([
               "v1",
               ":v2",
               "Pleroma.Repo",
               "Phoenix.Socket.V1.JSONSerializer",
               15,
               false
             ]) == [
               "v1",
               :v2,
               Pleroma.Repo,
               Phoenix.Socket.V1.JSONSerializer,
               15,
               false
             ]
    end

    test "simple keyword" do
      assert ConfigDB.to_elixir_types([%{"tuple" => [":key", "value"]}]) == [key: "value"]
    end

    test "keyword" do
      assert ConfigDB.to_elixir_types([
               %{"tuple" => [":types", "Pleroma.PostgresTypes"]},
               %{"tuple" => [":telemetry_event", ["Pleroma.Repo.Instrumenter"]]},
               %{"tuple" => [":migration_lock", nil]},
               %{"tuple" => [":key1", 150]},
               %{"tuple" => [":key2", "string"]}
             ]) == [
               types: Pleroma.PostgresTypes,
               telemetry_event: [Pleroma.Repo.Instrumenter],
               migration_lock: nil,
               key1: 150,
               key2: "string"
             ]
    end

    test "trandformed keyword" do
      assert ConfigDB.to_elixir_types(a: 1, b: 2, c: "string") == [a: 1, b: 2, c: "string"]
    end

    test "complex keyword with nested mixed childs" do
      assert ConfigDB.to_elixir_types([
               %{"tuple" => [":uploader", "Pleroma.Uploaders.Local"]},
               %{"tuple" => [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
               %{"tuple" => [":link_name", true]},
               %{"tuple" => [":proxy_remote", false]},
               %{"tuple" => [":common_map", %{":key" => "value"}]},
               %{
                 "tuple" => [
                   ":proxy_opts",
                   [
                     %{"tuple" => [":redirect_on_failure", false]},
                     %{"tuple" => [":max_body_length", 1_048_576]},
                     %{
                       "tuple" => [
                         ":http",
                         [
                           %{"tuple" => [":follow_redirect", true]},
                           %{"tuple" => [":pool", ":upload"]}
                         ]
                       ]
                     }
                   ]
                 ]
               }
             ]) == [
               uploader: Pleroma.Uploaders.Local,
               filters: [Pleroma.Upload.Filter.Dedupe],
               link_name: true,
               proxy_remote: false,
               common_map: %{key: "value"},
               proxy_opts: [
                 redirect_on_failure: false,
                 max_body_length: 1_048_576,
                 http: [
                   follow_redirect: true,
                   pool: :upload
                 ]
               ]
             ]
    end

    test "common keyword" do
      assert ConfigDB.to_elixir_types([
               %{"tuple" => [":level", ":warn"]},
               %{"tuple" => [":meta", [":all"]]},
               %{"tuple" => [":path", ""]},
               %{"tuple" => [":val", nil]},
               %{"tuple" => [":webhook_url", "https://hooks.slack.com/services/YOUR-KEY-HERE"]}
             ]) == [
               level: :warn,
               meta: [:all],
               path: "",
               val: nil,
               webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
             ]
    end

    test "complex keyword with sigil" do
      assert ConfigDB.to_elixir_types([
               %{"tuple" => [":federated_timeline_removal", []]},
               %{"tuple" => [":reject", ["~r/comp[lL][aA][iI][nN]er/"]]},
               %{"tuple" => [":replace", []]}
             ]) == [
               federated_timeline_removal: [],
               reject: [~r/comp[lL][aA][iI][nN]er/],
               replace: []
             ]
    end

    test "complex keyword with tuples with more than 2 values" do
      assert ConfigDB.to_elixir_types([
               %{
                 "tuple" => [
                   ":http",
                   [
                     %{
                       "tuple" => [
                         ":key1",
                         [
                           %{
                             "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", []]}
                                   ]
                                 }
                               ]
                             ]
                           }
                         ]
                       ]
                     }
                   ]
                 ]
               }
             ]) == [
               http: [
                 key1: [
                   {:_,
                    [
                      {"/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
  end
end