config_controller_test.exs 48.1 KB
Newer Older
1
# Pleroma: A lightweight social networking server
Haelwenn's avatar
Haelwenn committed
2
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3
4
5
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do
lain's avatar
lain committed
6
  use Pleroma.Web.ConnCase
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

  import ExUnit.CaptureLog
  import Pleroma.Factory

  alias Pleroma.ConfigDB

  setup do
    admin = insert(:user, is_admin: true)
    token = insert(:oauth_admin_token, user: admin)

    conn =
      build_conn()
      |> assign(:user, admin)
      |> assign(:token, token)

    {:ok, %{admin: admin, token: token, conn: conn}}
  end

  describe "GET /api/pleroma/admin/config" do
    setup do: clear_config(:configurable_from_database, true)

    test "when configuration from database is off", %{conn: conn} do
29
      clear_config(:configurable_from_database, false)
30
31
      conn = get(conn, "/api/pleroma/admin/config")

32
      assert json_response_and_validate_schema(conn, 400) ==
33
               %{
feld's avatar
feld committed
34
                 "error" => "You must enable configurable_from_database in your config file."
35
36
37
38
39
40
41
               }
    end

    test "with settings only in db", %{conn: conn} do
      config1 = insert(:config)
      config2 = insert(:config)

42
      conn = get(conn, "/api/pleroma/admin/config?only_db=true")
43
44
45
46
47
48
49
50
51
52
53
54
55
56

      %{
        "configs" => [
          %{
            "group" => ":pleroma",
            "key" => key1,
            "value" => _
          },
          %{
            "group" => ":pleroma",
            "key" => key2,
            "value" => _
          }
        ]
57
      } = json_response_and_validate_schema(conn, 200)
58

59
60
      assert key1 == inspect(config1.key)
      assert key2 == inspect(config2.key)
61
62
63
    end

    test "db is added to settings that are in db", %{conn: conn} do
64
      _config = insert(:config, key: ":instance", value: [name: "Some name"])
65
66
67
68

      %{"configs" => configs} =
        conn
        |> get("/api/pleroma/admin/config")
69
        |> json_response_and_validate_schema(200)
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

      [instance_config] =
        Enum.filter(configs, fn %{"group" => group, "key" => key} ->
          group == ":pleroma" and key == ":instance"
        end)

      assert instance_config["db"] == [":name"]
    end

    test "merged default setting with db settings", %{conn: conn} do
      config1 = insert(:config)
      config2 = insert(:config)

      config3 =
        insert(:config,
85
          value: [k1: :v1, k2: :v2]
86
87
88
89
90
        )

      %{"configs" => configs} =
        conn
        |> get("/api/pleroma/admin/config")
91
        |> json_response_and_validate_schema(200)
92
93
94

      assert length(configs) > 3

95
96
97
      saved_configs = [config1, config2, config3]
      keys = Enum.map(saved_configs, &inspect(&1.key))

98
99
      received_configs =
        Enum.filter(configs, fn %{"group" => group, "key" => key} ->
100
          group == ":pleroma" and key in keys
101
102
103
104
105
106
107
        end)

      assert length(received_configs) == 3

      db_keys =
        config3.value
        |> Keyword.keys()
108
109
110
111
112
113
114
        |> ConfigDB.to_json_types()

      keys = Enum.map(saved_configs -- [config3], &inspect(&1.key))

      values = Enum.map(saved_configs, &ConfigDB.to_json_types(&1.value))

      mapset_keys = MapSet.new(keys ++ db_keys)
115
116

      Enum.each(received_configs, fn %{"value" => value, "db" => db} ->
117
118
        db = MapSet.new(db)
        assert MapSet.subset?(db, mapset_keys)
119

120
        assert value in values
121
122
123
124
      end)
    end

    test "subkeys with full update right merge", %{conn: conn} do
125
126
127
128
      insert(:config,
        key: ":emoji",
        value: [groups: [a: 1, b: 2], key: [a: 1]]
      )
129

130
131
132
133
      insert(:config,
        key: ":assets",
        value: [mascots: [a: 1, b: 2], key: [a: 1]]
      )
134
135
136
137

      %{"configs" => configs} =
        conn
        |> get("/api/pleroma/admin/config")
138
        |> json_response_and_validate_schema(200)
139
140
141

      vals =
        Enum.filter(configs, fn %{"group" => group, "key" => key} ->
142
          group == ":pleroma" and key in [":emoji", ":assets"]
143
144
145
146
147
        end)

      emoji = Enum.find(vals, fn %{"key" => key} -> key == ":emoji" end)
      assets = Enum.find(vals, fn %{"key" => key} -> key == ":assets" end)

148
149
      emoji_val = ConfigDB.to_elixir_types(emoji["value"])
      assets_val = ConfigDB.to_elixir_types(assets["value"])
150
151
152
153

      assert emoji_val[:groups] == [a: 1, b: 2]
      assert assets_val[:mascots] == [a: 1, b: 2]
    end
154
155
156
157
158
159
160
161

    test "with valid `admin_token` query parameter, skips OAuth scopes check" do
      clear_config([:admin_token], "password123")

      build_conn()
      |> get("/api/pleroma/admin/config?admin_token=password123")
      |> json_response_and_validate_schema(200)
    end
162
163
  end

164
165
166
  test "POST /api/pleroma/admin/config with configdb disabled", %{conn: conn} do
    clear_config(:configurable_from_database, false)

167
168
169
170
    conn =
      conn
      |> put_req_header("content-type", "application/json")
      |> post("/api/pleroma/admin/config", %{"configs" => []})
171

172
    assert json_response_and_validate_schema(conn, 400) ==
feld's avatar
feld committed
173
             %{"error" => "You must enable configurable_from_database in your config file."}
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
  end

  describe "POST /api/pleroma/admin/config" do
    setup do
      http = Application.get_env(:pleroma, :http)

      on_exit(fn ->
        Application.delete_env(:pleroma, :key1)
        Application.delete_env(:pleroma, :key2)
        Application.delete_env(:pleroma, :key3)
        Application.delete_env(:pleroma, :key4)
        Application.delete_env(:pleroma, :keyaa1)
        Application.delete_env(:pleroma, :keyaa2)
        Application.delete_env(:pleroma, Pleroma.Web.Endpoint.NotReal)
        Application.delete_env(:pleroma, Pleroma.Captcha.NotReal)
        Application.put_env(:pleroma, :http, http)
        Application.put_env(:tesla, :adapter, Tesla.Mock)
        Restarter.Pleroma.refresh()
      end)
    end

    setup do: clear_config(:configurable_from_database, true)

    @tag capture_log: true
    test "create new config setting in db", %{conn: conn} do
      ueberauth = Application.get_env(:ueberauth, Ueberauth)
      on_exit(fn -> Application.put_env(:ueberauth, Ueberauth, ueberauth) end)

      conn =
203
204
205
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/config", %{
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
          configs: [
            %{group: ":pleroma", key: ":key1", value: "value1"},
            %{
              group: ":ueberauth",
              key: "Ueberauth",
              value: [%{"tuple" => [":consumer_secret", "aaaa"]}]
            },
            %{
              group: ":pleroma",
              key: ":key2",
              value: %{
                ":nested_1" => "nested_value1",
                ":nested_2" => [
                  %{":nested_22" => "nested_value222"},
                  %{":nested_33" => %{":nested_44" => "nested_444"}}
                ]
              }
            },
            %{
              group: ":pleroma",
              key: ":key3",
              value: [
                %{"nested_3" => ":nested_3", "nested_33" => "nested_33"},
                %{"nested_4" => true}
              ]
            },
            %{
              group: ":pleroma",
              key: ":key4",
              value: %{":nested_5" => ":upload", "endpoint" => "https://example.com"}
            },
            %{
              group: ":idna",
              key: ":key5",
              value: %{"tuple" => ["string", "Pleroma.Captcha.NotReal", []]}
            }
          ]
        })

245
      assert json_response_and_validate_schema(conn, 200) == %{
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
               "configs" => [
                 %{
                   "group" => ":pleroma",
                   "key" => ":key1",
                   "value" => "value1",
                   "db" => [":key1"]
                 },
                 %{
                   "group" => ":ueberauth",
                   "key" => "Ueberauth",
                   "value" => [%{"tuple" => [":consumer_secret", "aaaa"]}],
                   "db" => [":consumer_secret"]
                 },
                 %{
                   "group" => ":pleroma",
                   "key" => ":key2",
                   "value" => %{
                     ":nested_1" => "nested_value1",
                     ":nested_2" => [
                       %{":nested_22" => "nested_value222"},
                       %{":nested_33" => %{":nested_44" => "nested_444"}}
                     ]
                   },
                   "db" => [":key2"]
                 },
                 %{
                   "group" => ":pleroma",
                   "key" => ":key3",
                   "value" => [
                     %{"nested_3" => ":nested_3", "nested_33" => "nested_33"},
                     %{"nested_4" => true}
                   ],
                   "db" => [":key3"]
                 },
                 %{
                   "group" => ":pleroma",
                   "key" => ":key4",
                   "value" => %{"endpoint" => "https://example.com", ":nested_5" => ":upload"},
                   "db" => [":key4"]
                 },
                 %{
                   "group" => ":idna",
                   "key" => ":key5",
                   "value" => %{"tuple" => ["string", "Pleroma.Captcha.NotReal", []]},
                   "db" => [":key5"]
                 }
292
293
               ],
               "need_reboot" => false
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
             }

      assert Application.get_env(:pleroma, :key1) == "value1"

      assert Application.get_env(:pleroma, :key2) == %{
               nested_1: "nested_value1",
               nested_2: [
                 %{nested_22: "nested_value222"},
                 %{nested_33: %{nested_44: "nested_444"}}
               ]
             }

      assert Application.get_env(:pleroma, :key3) == [
               %{"nested_3" => :nested_3, "nested_33" => "nested_33"},
               %{"nested_4" => true}
             ]

      assert Application.get_env(:pleroma, :key4) == %{
               "endpoint" => "https://example.com",
               nested_5: :upload
             }

      assert Application.get_env(:idna, :key5) == {"string", Pleroma.Captcha.NotReal, []}
    end

    test "save configs setting without explicit key", %{conn: conn} do
      level = Application.get_env(:quack, :level)
      meta = Application.get_env(:quack, :meta)
      webhook_url = Application.get_env(:quack, :webhook_url)

      on_exit(fn ->
        Application.put_env(:quack, :level, level)
        Application.put_env(:quack, :meta, meta)
        Application.put_env(:quack, :webhook_url, webhook_url)
      end)

      conn =
331
332
333
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/config", %{
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
          configs: [
            %{
              group: ":quack",
              key: ":level",
              value: ":info"
            },
            %{
              group: ":quack",
              key: ":meta",
              value: [":none"]
            },
            %{
              group: ":quack",
              key: ":webhook_url",
              value: "https://hooks.slack.com/services/KEY"
            }
          ]
        })

353
      assert json_response_and_validate_schema(conn, 200) == %{
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
               "configs" => [
                 %{
                   "group" => ":quack",
                   "key" => ":level",
                   "value" => ":info",
                   "db" => [":level"]
                 },
                 %{
                   "group" => ":quack",
                   "key" => ":meta",
                   "value" => [":none"],
                   "db" => [":meta"]
                 },
                 %{
                   "group" => ":quack",
                   "key" => ":webhook_url",
                   "value" => "https://hooks.slack.com/services/KEY",
                   "db" => [":webhook_url"]
                 }
373
374
               ],
               "need_reboot" => false
375
376
377
378
379
380
381
382
             }

      assert Application.get_env(:quack, :level) == :info
      assert Application.get_env(:quack, :meta) == [:none]
      assert Application.get_env(:quack, :webhook_url) == "https://hooks.slack.com/services/KEY"
    end

    test "saving config with partial update", %{conn: conn} do
383
      insert(:config, key: ":key1", value: :erlang.term_to_binary(key1: 1, key2: 2))
384
385

      conn =
386
387
388
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/config", %{
389
          configs: [
390
            %{group: ":pleroma", key: ":key1", value: [%{"tuple" => [":key3", 3]}]}
391
392
393
          ]
        })

394
      assert json_response_and_validate_schema(conn, 200) == %{
395
396
397
398
399
400
401
402
403
404
405
               "configs" => [
                 %{
                   "group" => ":pleroma",
                   "key" => ":key1",
                   "value" => [
                     %{"tuple" => [":key1", 1]},
                     %{"tuple" => [":key2", 2]},
                     %{"tuple" => [":key3", 3]}
                   ],
                   "db" => [":key1", ":key2", ":key3"]
                 }
406
407
               ],
               "need_reboot" => false
408
409
410
411
             }
    end

    test "saving config which need pleroma reboot", %{conn: conn} do
412
      clear_config([:chat, :enabled], true)
413

414
415
416
      assert conn
             |> put_req_header("content-type", "application/json")
             |> post(
417
418
419
420
421
422
423
               "/api/pleroma/admin/config",
               %{
                 configs: [
                   %{group: ":pleroma", key: ":chat", value: [%{"tuple" => [":enabled", true]}]}
                 ]
               }
             )
424
             |> json_response_and_validate_schema(200) == %{
425
426
427
428
429
430
431
432
433
434
435
436
437
438
               "configs" => [
                 %{
                   "db" => [":enabled"],
                   "group" => ":pleroma",
                   "key" => ":chat",
                   "value" => [%{"tuple" => [":enabled", true]}]
                 }
               ],
               "need_reboot" => true
             }

      configs =
        conn
        |> get("/api/pleroma/admin/config")
439
        |> json_response_and_validate_schema(200)
440
441
442
443

      assert configs["need_reboot"]

      capture_log(fn ->
444
445
        assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) ==
                 %{}
446
447
448
449
450
      end) =~ "pleroma restarted"

      configs =
        conn
        |> get("/api/pleroma/admin/config")
451
        |> json_response_and_validate_schema(200)
452
453
454
455
456

      assert configs["need_reboot"] == false
    end

    test "update setting which need reboot, don't change reboot flag until reboot", %{conn: conn} do
457
      clear_config([:chat, :enabled], true)
458

459
460
461
      assert conn
             |> put_req_header("content-type", "application/json")
             |> post(
462
463
464
465
466
467
468
               "/api/pleroma/admin/config",
               %{
                 configs: [
                   %{group: ":pleroma", key: ":chat", value: [%{"tuple" => [":enabled", true]}]}
                 ]
               }
             )
469
             |> json_response_and_validate_schema(200) == %{
470
471
472
473
474
475
476
477
478
479
480
               "configs" => [
                 %{
                   "db" => [":enabled"],
                   "group" => ":pleroma",
                   "key" => ":chat",
                   "value" => [%{"tuple" => [":enabled", true]}]
                 }
               ],
               "need_reboot" => true
             }

481
482
483
      assert conn
             |> put_req_header("content-type", "application/json")
             |> post("/api/pleroma/admin/config", %{
484
485
486
487
               configs: [
                 %{group: ":pleroma", key: ":key1", value: [%{"tuple" => [":key3", 3]}]}
               ]
             })
488
             |> json_response_and_validate_schema(200) == %{
489
490
491
492
493
494
495
496
497
498
499
500
501
502
               "configs" => [
                 %{
                   "group" => ":pleroma",
                   "key" => ":key1",
                   "value" => [
                     %{"tuple" => [":key3", 3]}
                   ],
                   "db" => [":key3"]
                 }
               ],
               "need_reboot" => true
             }

      capture_log(fn ->
503
504
        assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) ==
                 %{}
505
506
507
508
509
      end) =~ "pleroma restarted"

      configs =
        conn
        |> get("/api/pleroma/admin/config")
510
        |> json_response_and_validate_schema(200)
511
512
513
514
515

      assert configs["need_reboot"] == false
    end

    test "saving config with nested merge", %{conn: conn} do
516
      insert(:config, key: :key1, value: [key1: 1, key2: [k1: 1, k2: 2]])
517
518

      conn =
519
520
521
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/config", %{
522
523
          configs: [
            %{
524
525
              group: ":pleroma",
              key: ":key1",
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
              value: [
                %{"tuple" => [":key3", 3]},
                %{
                  "tuple" => [
                    ":key2",
                    [
                      %{"tuple" => [":k2", 1]},
                      %{"tuple" => [":k3", 3]}
                    ]
                  ]
                }
              ]
            }
          ]
        })

542
      assert json_response_and_validate_schema(conn, 200) == %{
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
               "configs" => [
                 %{
                   "group" => ":pleroma",
                   "key" => ":key1",
                   "value" => [
                     %{"tuple" => [":key1", 1]},
                     %{"tuple" => [":key3", 3]},
                     %{
                       "tuple" => [
                         ":key2",
                         [
                           %{"tuple" => [":k1", 1]},
                           %{"tuple" => [":k2", 1]},
                           %{"tuple" => [":k3", 3]}
                         ]
                       ]
                     }
                   ],
                   "db" => [":key1", ":key3", ":key2"]
                 }
563
564
               ],
               "need_reboot" => false
565
566
567
568
569
             }
    end

    test "saving special atoms", %{conn: conn} do
      conn =
570
571
572
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/config", %{
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
          "configs" => [
            %{
              "group" => ":pleroma",
              "key" => ":key1",
              "value" => [
                %{
                  "tuple" => [
                    ":ssl_options",
                    [%{"tuple" => [":versions", [":tlsv1", ":tlsv1.1", ":tlsv1.2"]]}]
                  ]
                }
              ]
            }
          ]
        })

589
      assert json_response_and_validate_schema(conn, 200) == %{
590
591
592
593
594
595
596
597
598
599
600
601
602
603
               "configs" => [
                 %{
                   "group" => ":pleroma",
                   "key" => ":key1",
                   "value" => [
                     %{
                       "tuple" => [
                         ":ssl_options",
                         [%{"tuple" => [":versions", [":tlsv1", ":tlsv1.1", ":tlsv1.2"]]}]
                       ]
                     }
                   ],
                   "db" => [":ssl_options"]
                 }
604
605
               ],
               "need_reboot" => false
606
607
608
609
610
611
612
613
614
615
616
             }

      assert Application.get_env(:pleroma, :key1) == [
               ssl_options: [versions: [:tlsv1, :"tlsv1.1", :"tlsv1.2"]]
             ]
    end

    test "saving full setting if value is in full_key_update list", %{conn: conn} do
      backends = Application.get_env(:logger, :backends)
      on_exit(fn -> Application.put_env(:logger, :backends, backends) end)

617
618
619
620
621
      insert(:config,
        group: :logger,
        key: :backends,
        value: []
      )
622
623
624
625
626
627

      Pleroma.Config.TransferTask.load_and_update_env([], false)

      assert Application.get_env(:logger, :backends) == []

      conn =
628
629
630
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/config", %{
631
632
          configs: [
            %{
633
634
              group: ":logger",
              key: ":backends",
635
636
637
638
639
              value: [":console"]
            }
          ]
        })

640
      assert json_response_and_validate_schema(conn, 200) == %{
641
642
643
644
645
646
647
648
649
               "configs" => [
                 %{
                   "group" => ":logger",
                   "key" => ":backends",
                   "value" => [
                     ":console"
                   ],
                   "db" => [":backends"]
                 }
650
651
               ],
               "need_reboot" => false
652
653
654
655
656
657
658
659
             }

      assert Application.get_env(:logger, :backends) == [
               :console
             ]
    end

    test "saving full setting if value is not keyword", %{conn: conn} do
660
661
662
663
664
      insert(:config,
        group: :tesla,
        key: :adapter,
        value: Tesla.Adapter.Hackey
      )
665
666

      conn =
667
668
669
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/config", %{
670
          configs: [
671
            %{group: ":tesla", key: ":adapter", value: "Tesla.Adapter.Httpc"}
672
673
674
          ]
        })

675
      assert json_response_and_validate_schema(conn, 200) == %{
676
677
678
679
680
681
682
               "configs" => [
                 %{
                   "group" => ":tesla",
                   "key" => ":adapter",
                   "value" => "Tesla.Adapter.Httpc",
                   "db" => [":adapter"]
                 }
683
684
               ],
               "need_reboot" => false
685
686
687
688
689
690
691
692
693
             }
    end

    test "update config setting & delete with fallback to default value", %{
      conn: conn,
      admin: admin,
      token: token
    } do
      ueberauth = Application.get_env(:ueberauth, Ueberauth)
694
695
      insert(:config, key: :keyaa1)
      insert(:config, key: :keyaa2)
696
697
698

      config3 =
        insert(:config,
699
700
          group: :ueberauth,
          key: Ueberauth
701
702
703
        )

      conn =
704
705
706
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/config", %{
707
          configs: [
708
709
            %{group: ":pleroma", key: ":keyaa1", value: "another_value"},
            %{group: ":pleroma", key: ":keyaa2", value: "another_value"}
710
711
712
          ]
        })

713
      assert json_response_and_validate_schema(conn, 200) == %{
714
715
716
               "configs" => [
                 %{
                   "group" => ":pleroma",
717
                   "key" => ":keyaa1",
718
719
720
721
722
                   "value" => "another_value",
                   "db" => [":keyaa1"]
                 },
                 %{
                   "group" => ":pleroma",
723
                   "key" => ":keyaa2",
724
725
726
                   "value" => "another_value",
                   "db" => [":keyaa2"]
                 }
727
728
               ],
               "need_reboot" => false
729
730
731
732
             }

      assert Application.get_env(:pleroma, :keyaa1) == "another_value"
      assert Application.get_env(:pleroma, :keyaa2) == "another_value"
733
      assert Application.get_env(:ueberauth, Ueberauth) == config3.value
734
735
736
737
738

      conn =
        build_conn()
        |> assign(:user, admin)
        |> assign(:token, token)
739
        |> put_req_header("content-type", "application/json")
740
741
        |> post("/api/pleroma/admin/config", %{
          configs: [
742
            %{group: ":pleroma", key: ":keyaa2", delete: true},
743
744
745
746
747
748
749
750
            %{
              group: ":ueberauth",
              key: "Ueberauth",
              delete: true
            }
          ]
        })

751
      assert json_response_and_validate_schema(conn, 200) == %{
752
753
               "configs" => [],
               "need_reboot" => false
754
755
756
757
758
759
760
761
             }

      assert Application.get_env(:ueberauth, Ueberauth) == ueberauth
      refute Keyword.has_key?(Application.get_all_env(:pleroma), :keyaa2)
    end

    test "common config example", %{conn: conn} do
      conn =
762
763
764
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/config", %{
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
          configs: [
            %{
              "group" => ":pleroma",
              "key" => "Pleroma.Captcha.NotReal",
              "value" => [
                %{"tuple" => [":enabled", false]},
                %{"tuple" => [":method", "Pleroma.Captcha.Kocaptcha"]},
                %{"tuple" => [":seconds_valid", 60]},
                %{"tuple" => [":path", ""]},
                %{"tuple" => [":key1", nil]},
                %{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]},
                %{"tuple" => [":regex1", "~r/https:\/\/example.com/"]},
                %{"tuple" => [":regex2", "~r/https:\/\/example.com/u"]},
                %{"tuple" => [":regex3", "~r/https:\/\/example.com/i"]},
                %{"tuple" => [":regex4", "~r/https:\/\/example.com/s"]},
                %{"tuple" => [":name", "Pleroma"]}
              ]
            }
          ]
        })

      assert Config.get([Pleroma.Captcha.NotReal, :name]) == "Pleroma"

788
      assert json_response_and_validate_schema(conn, 200) == %{
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
               "configs" => [
                 %{
                   "group" => ":pleroma",
                   "key" => "Pleroma.Captcha.NotReal",
                   "value" => [
                     %{"tuple" => [":enabled", false]},
                     %{"tuple" => [":method", "Pleroma.Captcha.Kocaptcha"]},
                     %{"tuple" => [":seconds_valid", 60]},
                     %{"tuple" => [":path", ""]},
                     %{"tuple" => [":key1", nil]},
                     %{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]},
                     %{"tuple" => [":regex1", "~r/https:\\/\\/example.com/"]},
                     %{"tuple" => [":regex2", "~r/https:\\/\\/example.com/u"]},
                     %{"tuple" => [":regex3", "~r/https:\\/\\/example.com/i"]},
                     %{"tuple" => [":regex4", "~r/https:\\/\\/example.com/s"]},
                     %{"tuple" => [":name", "Pleroma"]}
                   ],
                   "db" => [
                     ":enabled",
                     ":method",
                     ":seconds_valid",
                     ":path",
                     ":key1",
                     ":partial_chain",
                     ":regex1",
                     ":regex2",
                     ":regex3",
                     ":regex4",
                     ":name"
                   ]
                 }
820
821
               ],
               "need_reboot" => false
822
823
824
825
826
             }
    end

    test "tuples with more than two values", %{conn: conn} do
      conn =
827
828
829
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/config", %{
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
          configs: [
            %{
              "group" => ":pleroma",
              "key" => "Pleroma.Web.Endpoint.NotReal",
              "value" => [
                %{
                  "tuple" => [
                    ":http",
                    [
                      %{
                        "tuple" => [
                          ":key2",
                          [
                            %{
                              "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", []]}
                                    ]
                                  }
                                ]
                              ]
                            }
                          ]
                        ]
                      }
                    ]
                  ]
                }
              ]
            }
          ]
        })

893
      assert json_response_and_validate_schema(conn, 200) == %{
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
               "configs" => [
                 %{
                   "group" => ":pleroma",
                   "key" => "Pleroma.Web.Endpoint.NotReal",
                   "value" => [
                     %{
                       "tuple" => [
                         ":http",
                         [
                           %{
                             "tuple" => [
                               ":key2",
                               [
                                 %{
                                   "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", []]}
                                         ]
                                       }
                                     ]
                                   ]
                                 }
                               ]
                             ]
                           }
                         ]
                       ]
                     }
                   ],
                   "db" => [":http"]
                 }
955
956
               ],
               "need_reboot" => false
957
958
959
960
961
             }
    end

    test "settings with nesting map", %{conn: conn} do
      conn =
962
963
964
        conn
        |> put_req_header("content-type", "application/json")
        |> post("/api/pleroma/admin/config", %{
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
          configs: [
            %{
              "group" => ":pleroma",
              "key" => ":key1",
              "value" => [
                %{"tuple" => [":key2", "some_val"]},
                %{
                  "tuple" => [
                    ":key3",
                    %{
                      ":max_options" => 20,
                      ":max_option_chars" => 200,
                      ":min_expiration" => 0,
                      ":max_expiration" => 31_536_000,
                      "nested" => %{
                        ":max_options" => 20,
                        ":max_option_chars" => 200,
                        ":min_expiration" => 0,
                        ":max_expiration" => 31_536_000
                      }
                    }
                  ]
                }
              ]
            }
          ]
        })

993
      assert json_response_and_validate_schema(conn, 200) ==
994
995
996
997
998
999
1000
               %{
                 "configs" => [
                   %{
                     "group" => ":pleroma",
                     "key" => ":key1",
                     "value" => [
                       %{"tuple" => [":key2", "some_val"]},
For faster browsing, not all history is shown. View entire blame