total rebase
[anni] / test / pleroma / docs / generator_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Docs.GeneratorTest do
6   use ExUnit.Case, async: true
7   alias Pleroma.Docs.Generator
8
9   @descriptions [
10     %{
11       group: :pleroma,
12       key: Pleroma.Upload,
13       type: :group,
14       description: "",
15       children: [
16         %{
17           key: :uploader,
18           type: :module,
19           description: "",
20           suggestions: {:list_behaviour_implementations, Pleroma.Upload.Filter}
21         },
22         %{
23           key: :filters,
24           type: {:list, :module},
25           description: "",
26           suggestions: {:list_behaviour_implementations, Pleroma.Web.ActivityPub.MRF.Policy}
27         },
28         %{
29           key: Pleroma.Upload,
30           type: :string,
31           description: "",
32           suggestions: [""]
33         },
34         %{
35           key: :some_key,
36           type: :keyword,
37           description: "",
38           suggestions: [],
39           children: [
40             %{
41               key: :another_key,
42               type: :integer,
43               description: "",
44               suggestions: [5]
45             },
46             %{
47               key: :another_key_with_label,
48               label: "Another label",
49               type: :integer,
50               description: "",
51               suggestions: [7]
52             }
53           ]
54         },
55         %{
56           key: :key1,
57           type: :atom,
58           description: "",
59           suggestions: [
60             :atom,
61             Pleroma.Upload,
62             {:tuple, "string", 8080},
63             [:atom, Pleroma.Upload, {:atom, Pleroma.Upload}]
64           ]
65         },
66         %{
67           key: Pleroma.Upload,
68           label: "Special Label",
69           type: :string,
70           description: "",
71           suggestions: [""]
72         },
73         %{
74           group: {:subgroup, Swoosh.Adapters.SMTP},
75           key: :auth,
76           type: :atom,
77           description: "`Swoosh.Adapters.SMTP` adapter specific setting",
78           suggestions: [:always, :never, :if_available]
79         },
80         %{
81           key: "application/xml",
82           type: {:list, :string},
83           suggestions: ["xml"]
84         },
85         %{
86           key: :versions,
87           type: {:list, :atom},
88           description: "List of TLS version to use",
89           suggestions: [:tlsv1, ":tlsv1.1", ":tlsv1.2", ":tlsv1.3"]
90         }
91       ]
92     },
93     %{
94       group: :tesla,
95       key: :adapter,
96       type: :group,
97       description: ""
98     },
99     %{
100       group: :cors_plug,
101       type: :group,
102       children: [%{key: :key1, type: :string, suggestions: [""]}]
103     },
104     %{group: "Some string group", key: "Some string key", type: :group}
105   ]
106
107   describe "convert_to_strings/1" do
108     test "group, key, label" do
109       [desc1, desc2 | _] = Generator.convert_to_strings(@descriptions)
110
111       assert desc1[:group] == ":pleroma"
112       assert desc1[:key] == "Pleroma.Upload"
113       assert desc1[:label] == "Pleroma.Upload"
114
115       assert desc2[:group] == ":tesla"
116       assert desc2[:key] == ":adapter"
117       assert desc2[:label] == "Adapter"
118     end
119
120     test "group without key" do
121       descriptions = Generator.convert_to_strings(@descriptions)
122       desc = Enum.at(descriptions, 2)
123
124       assert desc[:group] == ":cors_plug"
125       refute desc[:key]
126       assert desc[:label] == "Cors plug"
127     end
128
129     test "children key, label, type" do
130       [%{children: [child1, child2, child3, child4 | _]} | _] =
131         Generator.convert_to_strings(@descriptions)
132
133       assert child1[:key] == ":uploader"
134       assert child1[:label] == "Uploader"
135       assert child1[:type] == :module
136
137       assert child2[:key] == ":filters"
138       assert child2[:label] == "Filters"
139       assert child2[:type] == {:list, :module}
140
141       assert child3[:key] == "Pleroma.Upload"
142       assert child3[:label] == "Pleroma.Upload"
143       assert child3[:type] == :string
144
145       assert child4[:key] == ":some_key"
146       assert child4[:label] == "Some key"
147       assert child4[:type] == :keyword
148     end
149
150     test "child with predefined label" do
151       [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
152       child = Enum.at(children, 5)
153       assert child[:key] == "Pleroma.Upload"
154       assert child[:label] == "Special Label"
155     end
156
157     test "subchild" do
158       [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
159       child = Enum.at(children, 3)
160       %{children: [subchild | _]} = child
161
162       assert subchild[:key] == ":another_key"
163       assert subchild[:label] == "Another key"
164       assert subchild[:type] == :integer
165     end
166
167     test "subchild with predefined label" do
168       [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
169       child = Enum.at(children, 3)
170       %{children: subchildren} = child
171       subchild = Enum.at(subchildren, 1)
172
173       assert subchild[:key] == ":another_key_with_label"
174       assert subchild[:label] == "Another label"
175     end
176
177     test "module suggestions" do
178       [%{children: [%{suggestions: suggestions} | _]} | _] =
179         Generator.convert_to_strings(@descriptions)
180
181       Enum.each(suggestions, fn suggestion ->
182         assert String.starts_with?(suggestion, "Pleroma.")
183       end)
184     end
185
186     test "atoms in suggestions with leading `:`" do
187       [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
188       %{suggestions: suggestions} = Enum.at(children, 4)
189       assert Enum.at(suggestions, 0) == ":atom"
190       assert Enum.at(suggestions, 1) == "Pleroma.Upload"
191       assert Enum.at(suggestions, 2) == {":tuple", "string", 8080}
192       assert Enum.at(suggestions, 3) == [":atom", "Pleroma.Upload", {":atom", "Pleroma.Upload"}]
193
194       %{suggestions: suggestions} = Enum.at(children, 6)
195       assert Enum.at(suggestions, 0) == ":always"
196       assert Enum.at(suggestions, 1) == ":never"
197       assert Enum.at(suggestions, 2) == ":if_available"
198     end
199
200     test "group, key as string in main desc" do
201       descriptions = Generator.convert_to_strings(@descriptions)
202       desc = Enum.at(descriptions, 3)
203       assert desc[:group] == "Some string group"
204       assert desc[:key] == "Some string key"
205     end
206
207     test "key as string subchild" do
208       [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
209       child = Enum.at(children, 7)
210       assert child[:key] == "application/xml"
211     end
212
213     test "suggestion for tls versions" do
214       [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
215       child = Enum.at(children, 8)
216       assert child[:suggestions] == [":tlsv1", ":tlsv1.1", ":tlsv1.2", ":tlsv1.3"]
217     end
218
219     test "subgroup with module name" do
220       [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
221
222       %{group: subgroup} = Enum.at(children, 6)
223       assert subgroup == {":subgroup", "Swoosh.Adapters.SMTP"}
224     end
225   end
226 end