summaryrefslogtreecommitdiff
path: root/test/pleroma/docs/generator_test.exs
blob: 8574c1d5e7b77e92a094ea3232e6b8dafef3e697 (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
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Docs.GeneratorTest do
  use ExUnit.Case, async: true
  alias Pleroma.Docs.Generator

  @descriptions [
    %{
      group: :pleroma,
      key: Pleroma.Upload,
      type: :group,
      description: "",
      children: [
        %{
          key: :uploader,
          type: :module,
          description: "",
          suggestions: {:list_behaviour_implementations, Pleroma.Upload.Filter}
        },
        %{
          key: :filters,
          type: {:list, :module},
          description: "",
          suggestions: {:list_behaviour_implementations, Pleroma.Web.ActivityPub.MRF.Policy}
        },
        %{
          key: Pleroma.Upload,
          type: :string,
          description: "",
          suggestions: [""]
        },
        %{
          key: :some_key,
          type: :keyword,
          description: "",
          suggestions: [],
          children: [
            %{
              key: :another_key,
              type: :integer,
              description: "",
              suggestions: [5]
            },
            %{
              key: :another_key_with_label,
              label: "Another label",
              type: :integer,
              description: "",
              suggestions: [7]
            }
          ]
        },
        %{
          key: :key1,
          type: :atom,
          description: "",
          suggestions: [
            :atom,
            Pleroma.Upload,
            {:tuple, "string", 8080},
            [:atom, Pleroma.Upload, {:atom, Pleroma.Upload}]
          ]
        },
        %{
          key: Pleroma.Upload,
          label: "Special Label",
          type: :string,
          description: "",
          suggestions: [""]
        },
        %{
          group: {:subgroup, Swoosh.Adapters.SMTP},
          key: :auth,
          type: :atom,
          description: "`Swoosh.Adapters.SMTP` adapter specific setting",
          suggestions: [:always, :never, :if_available]
        },
        %{
          key: "application/xml",
          type: {:list, :string},
          suggestions: ["xml"]
        },
        %{
          key: :versions,
          type: {:list, :atom},
          description: "List of TLS version to use",
          suggestions: [:tlsv1, ":tlsv1.1", ":tlsv1.2"]
        }
      ]
    },
    %{
      group: :tesla,
      key: :adapter,
      type: :group,
      description: ""
    },
    %{
      group: :cors_plug,
      type: :group,
      children: [%{key: :key1, type: :string, suggestions: [""]}]
    },
    %{group: "Some string group", key: "Some string key", type: :group}
  ]

  describe "convert_to_strings/1" do
    test "group, key, label" do
      [desc1, desc2 | _] = Generator.convert_to_strings(@descriptions)

      assert desc1[:group] == ":pleroma"
      assert desc1[:key] == "Pleroma.Upload"
      assert desc1[:label] == "Pleroma.Upload"

      assert desc2[:group] == ":tesla"
      assert desc2[:key] == ":adapter"
      assert desc2[:label] == "Adapter"
    end

    test "group without key" do
      descriptions = Generator.convert_to_strings(@descriptions)
      desc = Enum.at(descriptions, 2)

      assert desc[:group] == ":cors_plug"
      refute desc[:key]
      assert desc[:label] == "Cors plug"
    end

    test "children key, label, type" do
      [%{children: [child1, child2, child3, child4 | _]} | _] =
        Generator.convert_to_strings(@descriptions)

      assert child1[:key] == ":uploader"
      assert child1[:label] == "Uploader"
      assert child1[:type] == :module

      assert child2[:key] == ":filters"
      assert child2[:label] == "Filters"
      assert child2[:type] == {:list, :module}

      assert child3[:key] == "Pleroma.Upload"
      assert child3[:label] == "Pleroma.Upload"
      assert child3[:type] == :string

      assert child4[:key] == ":some_key"
      assert child4[:label] == "Some key"
      assert child4[:type] == :keyword
    end

    test "child with predefined label" do
      [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
      child = Enum.at(children, 5)
      assert child[:key] == "Pleroma.Upload"
      assert child[:label] == "Special Label"
    end

    test "subchild" do
      [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
      child = Enum.at(children, 3)
      %{children: [subchild | _]} = child

      assert subchild[:key] == ":another_key"
      assert subchild[:label] == "Another key"
      assert subchild[:type] == :integer
    end

    test "subchild with predefined label" do
      [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
      child = Enum.at(children, 3)
      %{children: subchildren} = child
      subchild = Enum.at(subchildren, 1)

      assert subchild[:key] == ":another_key_with_label"
      assert subchild[:label] == "Another label"
    end

    test "module suggestions" do
      [%{children: [%{suggestions: suggestions} | _]} | _] =
        Generator.convert_to_strings(@descriptions)

      Enum.each(suggestions, fn suggestion ->
        assert String.starts_with?(suggestion, "Pleroma.")
      end)
    end

    test "atoms in suggestions with leading `:`" do
      [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
      %{suggestions: suggestions} = Enum.at(children, 4)
      assert Enum.at(suggestions, 0) == ":atom"
      assert Enum.at(suggestions, 1) == "Pleroma.Upload"
      assert Enum.at(suggestions, 2) == {":tuple", "string", 8080}
      assert Enum.at(suggestions, 3) == [":atom", "Pleroma.Upload", {":atom", "Pleroma.Upload"}]

      %{suggestions: suggestions} = Enum.at(children, 6)
      assert Enum.at(suggestions, 0) == ":always"
      assert Enum.at(suggestions, 1) == ":never"
      assert Enum.at(suggestions, 2) == ":if_available"
    end

    test "group, key as string in main desc" do
      descriptions = Generator.convert_to_strings(@descriptions)
      desc = Enum.at(descriptions, 3)
      assert desc[:group] == "Some string group"
      assert desc[:key] == "Some string key"
    end

    test "key as string subchild" do
      [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
      child = Enum.at(children, 7)
      assert child[:key] == "application/xml"
    end

    test "suggestion for tls versions" do
      [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
      child = Enum.at(children, 8)
      assert child[:suggestions] == [":tlsv1", ":tlsv1.1", ":tlsv1.2"]
    end

    test "subgroup with module name" do
      [%{children: children} | _] = Generator.convert_to_strings(@descriptions)

      %{group: subgroup} = Enum.at(children, 6)
      assert subgroup == {":subgroup", "Swoosh.Adapters.SMTP"}
    end
  end
end