logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

generator_test.exs (6936B)


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