parsing proxy url setting
[akkoma] / test / web / admin_api / config_test.exs
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 describe "update_or_create/1" do
30 test "common" do
31 config = insert(:config)
32 key2 = "another_key"
33
34 params = [
35 %{group: "pleroma", key: key2, value: "another_value"},
36 %{group: config.group, key: config.key, value: "new_value"}
37 ]
38
39 assert Repo.all(Config) |> length() == 1
40
41 Enum.each(params, &Config.update_or_create(&1))
42
43 assert Repo.all(Config) |> length() == 2
44
45 config1 = Config.get_by_params(%{group: config.group, key: config.key})
46 config2 = Config.get_by_params(%{group: "pleroma", key: key2})
47
48 assert config1.value == Config.transform("new_value")
49 assert config2.value == Config.transform("another_value")
50 end
51
52 test "partial update" do
53 config = insert(:config, value: Config.to_binary(key1: "val1", key2: :val2))
54
55 {:ok, _config} =
56 Config.update_or_create(%{
57 group: config.group,
58 key: config.key,
59 value: [key1: :val1, key3: :val3]
60 })
61
62 updated = Config.get_by_params(%{group: config.group, key: config.key})
63
64 value = Config.from_binary(updated.value)
65 assert length(value) == 3
66 assert value[:key1] == :val1
67 assert value[:key2] == :val2
68 assert value[:key3] == :val3
69 end
70
71 test "only full update for some keys" do
72 config1 = insert(:config, key: ":ecto_repos", value: Config.to_binary(repo: Pleroma.Repo))
73 config2 = insert(:config, group: ":cors_plug", key: ":max_age", value: Config.to_binary(18))
74
75 {:ok, _config} =
76 Config.update_or_create(%{
77 group: config1.group,
78 key: config1.key,
79 value: [another_repo: [Pleroma.Repo]]
80 })
81
82 {:ok, _config} =
83 Config.update_or_create(%{
84 group: config2.group,
85 key: config2.key,
86 value: 777
87 })
88
89 updated1 = Config.get_by_params(%{group: config1.group, key: config1.key})
90 updated2 = Config.get_by_params(%{group: config2.group, key: config2.key})
91
92 assert Config.from_binary(updated1.value) == [another_repo: [Pleroma.Repo]]
93 assert Config.from_binary(updated2.value) == 777
94 end
95
96 test "full update if value is not keyword" do
97 config =
98 insert(:config,
99 group: ":tesla",
100 key: ":adapter",
101 value: Config.to_binary(Tesla.Adapter.Hackney)
102 )
103
104 {:ok, _config} =
105 Config.update_or_create(%{
106 group: config.group,
107 key: config.key,
108 value: Tesla.Adapter.Httpc
109 })
110
111 updated = Config.get_by_params(%{group: config.group, key: config.key})
112
113 assert Config.from_binary(updated.value) == Tesla.Adapter.Httpc
114 end
115 end
116
117 test "delete/1" do
118 config = insert(:config)
119 {:ok, _} = Config.delete(%{key: config.key, group: config.group})
120 refute Config.get_by_params(%{key: config.key, group: config.group})
121 end
122
123 describe "transform/1" do
124 test "string" do
125 binary = Config.transform("value as string")
126 assert binary == :erlang.term_to_binary("value as string")
127 assert Config.from_binary(binary) == "value as string"
128 end
129
130 test "boolean" do
131 binary = Config.transform(false)
132 assert binary == :erlang.term_to_binary(false)
133 assert Config.from_binary(binary) == false
134 end
135
136 test "nil" do
137 binary = Config.transform(nil)
138 assert binary == :erlang.term_to_binary(nil)
139 assert Config.from_binary(binary) == nil
140 end
141
142 test "integer" do
143 binary = Config.transform(150)
144 assert binary == :erlang.term_to_binary(150)
145 assert Config.from_binary(binary) == 150
146 end
147
148 test "atom" do
149 binary = Config.transform(":atom")
150 assert binary == :erlang.term_to_binary(:atom)
151 assert Config.from_binary(binary) == :atom
152 end
153
154 test "pleroma module" do
155 binary = Config.transform("Pleroma.Bookmark")
156 assert binary == :erlang.term_to_binary(Pleroma.Bookmark)
157 assert Config.from_binary(binary) == Pleroma.Bookmark
158 end
159
160 test "pleroma string" do
161 binary = Config.transform("Pleroma")
162 assert binary == :erlang.term_to_binary("Pleroma")
163 assert Config.from_binary(binary) == "Pleroma"
164 end
165
166 test "phoenix module" do
167 binary = Config.transform("Phoenix.Socket.V1.JSONSerializer")
168 assert binary == :erlang.term_to_binary(Phoenix.Socket.V1.JSONSerializer)
169 assert Config.from_binary(binary) == Phoenix.Socket.V1.JSONSerializer
170 end
171
172 test "tesla module" do
173 binary = Config.transform("Tesla.Adapter.Hackney")
174 assert binary == :erlang.term_to_binary(Tesla.Adapter.Hackney)
175 assert Config.from_binary(binary) == Tesla.Adapter.Hackney
176 end
177
178 test "sigil" do
179 binary = Config.transform("~r[comp[lL][aA][iI][nN]er]")
180 assert binary == :erlang.term_to_binary(~r/comp[lL][aA][iI][nN]er/)
181 assert Config.from_binary(binary) == ~r/comp[lL][aA][iI][nN]er/
182 end
183
184 test "link sigil" do
185 binary = Config.transform("~r/https:\/\/example.com/")
186 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/)
187 assert Config.from_binary(binary) == ~r/https:\/\/example.com/
188 end
189
190 test "link sigil with um modifiers" do
191 binary = Config.transform("~r/https:\/\/example.com/um")
192 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/um)
193 assert Config.from_binary(binary) == ~r/https:\/\/example.com/um
194 end
195
196 test "link sigil with i modifier" do
197 binary = Config.transform("~r/https:\/\/example.com/i")
198 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/i)
199 assert Config.from_binary(binary) == ~r/https:\/\/example.com/i
200 end
201
202 test "link sigil with s modifier" do
203 binary = Config.transform("~r/https:\/\/example.com/s")
204 assert binary == :erlang.term_to_binary(~r/https:\/\/example.com/s)
205 assert Config.from_binary(binary) == ~r/https:\/\/example.com/s
206 end
207
208 test "raise if valid delimiter not found" do
209 assert_raise ArgumentError, "valid delimiter for Regex expression not found", fn ->
210 Config.transform("~r/https://[]{}<>\"'()|example.com/s")
211 end
212 end
213
214 test "2 child tuple" do
215 binary = Config.transform(%{"tuple" => ["v1", ":v2"]})
216 assert binary == :erlang.term_to_binary({"v1", :v2})
217 assert Config.from_binary(binary) == {"v1", :v2}
218 end
219
220 test "proxy tuple with localhost" do
221 binary =
222 Config.transform(%{
223 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]
224 })
225
226 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, :localhost, 1234}})
227 assert Config.from_binary(binary) == {:proxy_url, {:socks5, :localhost, 1234}}
228 end
229
230 test "proxy tuple with domain" do
231 binary =
232 Config.transform(%{
233 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]
234 })
235
236 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, 'domain.com', 1234}})
237 assert Config.from_binary(binary) == {:proxy_url, {:socks5, 'domain.com', 1234}}
238 end
239
240 test "proxy tuple with ip" do
241 binary =
242 Config.transform(%{
243 "tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]
244 })
245
246 assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}})
247 assert Config.from_binary(binary) == {:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}}
248 end
249
250 test "tuple with n childs" do
251 binary =
252 Config.transform(%{
253 "tuple" => [
254 "v1",
255 ":v2",
256 "Pleroma.Bookmark",
257 150,
258 false,
259 "Phoenix.Socket.V1.JSONSerializer"
260 ]
261 })
262
263 assert binary ==
264 :erlang.term_to_binary(
265 {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
266 )
267
268 assert Config.from_binary(binary) ==
269 {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
270 end
271
272 test "tuple with dispatch key" do
273 binary = Config.transform(%{"tuple" => [":dispatch", ["{:_,
274 [
275 {\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
276 {\"/websocket\", Phoenix.Endpoint.CowboyWebSocket,
277 {Phoenix.Transports.WebSocket,
278 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}},
279 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
280 ]}"]]})
281
282 assert binary ==
283 :erlang.term_to_binary(
284 {:dispatch,
285 [
286 {:_,
287 [
288 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
289 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
290 {Phoenix.Transports.WebSocket,
291 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
292 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
293 ]}
294 ]}
295 )
296
297 assert Config.from_binary(binary) ==
298 {:dispatch,
299 [
300 {:_,
301 [
302 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
303 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
304 {Phoenix.Transports.WebSocket,
305 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
306 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
307 ]}
308 ]}
309 end
310
311 test "map with string key" do
312 binary = Config.transform(%{"key" => "value"})
313 assert binary == :erlang.term_to_binary(%{"key" => "value"})
314 assert Config.from_binary(binary) == %{"key" => "value"}
315 end
316
317 test "map with atom key" do
318 binary = Config.transform(%{":key" => "value"})
319 assert binary == :erlang.term_to_binary(%{key: "value"})
320 assert Config.from_binary(binary) == %{key: "value"}
321 end
322
323 test "list of strings" do
324 binary = Config.transform(["v1", "v2", "v3"])
325 assert binary == :erlang.term_to_binary(["v1", "v2", "v3"])
326 assert Config.from_binary(binary) == ["v1", "v2", "v3"]
327 end
328
329 test "list of modules" do
330 binary = Config.transform(["Pleroma.Repo", "Pleroma.Activity"])
331 assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity])
332 assert Config.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity]
333 end
334
335 test "list of atoms" do
336 binary = Config.transform([":v1", ":v2", ":v3"])
337 assert binary == :erlang.term_to_binary([:v1, :v2, :v3])
338 assert Config.from_binary(binary) == [:v1, :v2, :v3]
339 end
340
341 test "list of mixed values" do
342 binary =
343 Config.transform([
344 "v1",
345 ":v2",
346 "Pleroma.Repo",
347 "Phoenix.Socket.V1.JSONSerializer",
348 15,
349 false
350 ])
351
352 assert binary ==
353 :erlang.term_to_binary([
354 "v1",
355 :v2,
356 Pleroma.Repo,
357 Phoenix.Socket.V1.JSONSerializer,
358 15,
359 false
360 ])
361
362 assert Config.from_binary(binary) == [
363 "v1",
364 :v2,
365 Pleroma.Repo,
366 Phoenix.Socket.V1.JSONSerializer,
367 15,
368 false
369 ]
370 end
371
372 test "simple keyword" do
373 binary = Config.transform([%{"tuple" => [":key", "value"]}])
374 assert binary == :erlang.term_to_binary([{:key, "value"}])
375 assert Config.from_binary(binary) == [{:key, "value"}]
376 assert Config.from_binary(binary) == [key: "value"]
377 end
378
379 test "keyword with partial_chain key" do
380 binary =
381 Config.transform([%{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]}])
382
383 assert binary == :erlang.term_to_binary(partial_chain: &:hackney_connect.partial_chain/1)
384 assert Config.from_binary(binary) == [partial_chain: &:hackney_connect.partial_chain/1]
385 end
386
387 test "keyword" do
388 binary =
389 Config.transform([
390 %{"tuple" => [":types", "Pleroma.PostgresTypes"]},
391 %{"tuple" => [":telemetry_event", ["Pleroma.Repo.Instrumenter"]]},
392 %{"tuple" => [":migration_lock", nil]},
393 %{"tuple" => [":key1", 150]},
394 %{"tuple" => [":key2", "string"]}
395 ])
396
397 assert binary ==
398 :erlang.term_to_binary(
399 types: Pleroma.PostgresTypes,
400 telemetry_event: [Pleroma.Repo.Instrumenter],
401 migration_lock: nil,
402 key1: 150,
403 key2: "string"
404 )
405
406 assert Config.from_binary(binary) == [
407 types: Pleroma.PostgresTypes,
408 telemetry_event: [Pleroma.Repo.Instrumenter],
409 migration_lock: nil,
410 key1: 150,
411 key2: "string"
412 ]
413 end
414
415 test "complex keyword with nested mixed childs" do
416 binary =
417 Config.transform([
418 %{"tuple" => [":uploader", "Pleroma.Uploaders.Local"]},
419 %{"tuple" => [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
420 %{"tuple" => [":link_name", true]},
421 %{"tuple" => [":proxy_remote", false]},
422 %{"tuple" => [":common_map", %{":key" => "value"}]},
423 %{
424 "tuple" => [
425 ":proxy_opts",
426 [
427 %{"tuple" => [":redirect_on_failure", false]},
428 %{"tuple" => [":max_body_length", 1_048_576]},
429 %{
430 "tuple" => [
431 ":http",
432 [%{"tuple" => [":follow_redirect", true]}, %{"tuple" => [":pool", ":upload"]}]
433 ]
434 }
435 ]
436 ]
437 }
438 ])
439
440 assert binary ==
441 :erlang.term_to_binary(
442 uploader: Pleroma.Uploaders.Local,
443 filters: [Pleroma.Upload.Filter.Dedupe],
444 link_name: true,
445 proxy_remote: false,
446 common_map: %{key: "value"},
447 proxy_opts: [
448 redirect_on_failure: false,
449 max_body_length: 1_048_576,
450 http: [
451 follow_redirect: true,
452 pool: :upload
453 ]
454 ]
455 )
456
457 assert Config.from_binary(binary) ==
458 [
459 uploader: Pleroma.Uploaders.Local,
460 filters: [Pleroma.Upload.Filter.Dedupe],
461 link_name: true,
462 proxy_remote: false,
463 common_map: %{key: "value"},
464 proxy_opts: [
465 redirect_on_failure: false,
466 max_body_length: 1_048_576,
467 http: [
468 follow_redirect: true,
469 pool: :upload
470 ]
471 ]
472 ]
473 end
474
475 test "common keyword" do
476 binary =
477 Config.transform([
478 %{"tuple" => [":level", ":warn"]},
479 %{"tuple" => [":meta", [":all"]]},
480 %{"tuple" => [":path", ""]},
481 %{"tuple" => [":val", nil]},
482 %{"tuple" => [":webhook_url", "https://hooks.slack.com/services/YOUR-KEY-HERE"]}
483 ])
484
485 assert binary ==
486 :erlang.term_to_binary(
487 level: :warn,
488 meta: [:all],
489 path: "",
490 val: nil,
491 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
492 )
493
494 assert Config.from_binary(binary) == [
495 level: :warn,
496 meta: [:all],
497 path: "",
498 val: nil,
499 webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
500 ]
501 end
502
503 test "complex keyword with sigil" do
504 binary =
505 Config.transform([
506 %{"tuple" => [":federated_timeline_removal", []]},
507 %{"tuple" => [":reject", ["~r/comp[lL][aA][iI][nN]er/"]]},
508 %{"tuple" => [":replace", []]}
509 ])
510
511 assert binary ==
512 :erlang.term_to_binary(
513 federated_timeline_removal: [],
514 reject: [~r/comp[lL][aA][iI][nN]er/],
515 replace: []
516 )
517
518 assert Config.from_binary(binary) ==
519 [federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []]
520 end
521
522 test "complex keyword with tuples with more than 2 values" do
523 binary =
524 Config.transform([
525 %{
526 "tuple" => [
527 ":http",
528 [
529 %{
530 "tuple" => [
531 ":key1",
532 [
533 %{
534 "tuple" => [
535 ":_",
536 [
537 %{
538 "tuple" => [
539 "/api/v1/streaming",
540 "Pleroma.Web.MastodonAPI.WebsocketHandler",
541 []
542 ]
543 },
544 %{
545 "tuple" => [
546 "/websocket",
547 "Phoenix.Endpoint.CowboyWebSocket",
548 %{
549 "tuple" => [
550 "Phoenix.Transports.WebSocket",
551 %{
552 "tuple" => [
553 "Pleroma.Web.Endpoint",
554 "Pleroma.Web.UserSocket",
555 []
556 ]
557 }
558 ]
559 }
560 ]
561 },
562 %{
563 "tuple" => [
564 ":_",
565 "Phoenix.Endpoint.Cowboy2Handler",
566 %{"tuple" => ["Pleroma.Web.Endpoint", []]}
567 ]
568 }
569 ]
570 ]
571 }
572 ]
573 ]
574 }
575 ]
576 ]
577 }
578 ])
579
580 assert binary ==
581 :erlang.term_to_binary(
582 http: [
583 key1: [
584 _: [
585 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
586 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
587 {Phoenix.Transports.WebSocket,
588 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
589 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
590 ]
591 ]
592 ]
593 )
594
595 assert Config.from_binary(binary) == [
596 http: [
597 key1: [
598 {:_,
599 [
600 {"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
601 {"/websocket", Phoenix.Endpoint.CowboyWebSocket,
602 {Phoenix.Transports.WebSocket,
603 {Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, []}}},
604 {:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
605 ]}
606 ]
607 ]
608 ]
609 end
610 end
611 end