logo

pleroma

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

keyword_policy_test.exs (12091B)


  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.Web.ActivityPub.MRF.KeywordPolicyTest do
  5. use Pleroma.DataCase
  6. alias Pleroma.Web.ActivityPub.MRF.KeywordPolicy
  7. setup do: clear_config(:mrf_keyword)
  8. setup do
  9. clear_config([:mrf_keyword], %{reject: [], federated_timeline_removal: [], replace: []})
  10. end
  11. describe "rejecting based on keywords" do
  12. test "rejects if string matches in content" do
  13. clear_config([:mrf_keyword, :reject], ["pun"])
  14. message = %{
  15. "type" => "Create",
  16. "object" => %{
  17. "content" => "just a daily reminder that compLAINer is a good pun",
  18. "summary" => ""
  19. }
  20. }
  21. assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
  22. KeywordPolicy.filter(message)
  23. end
  24. test "rejects if string matches in summary" do
  25. clear_config([:mrf_keyword, :reject], ["pun"])
  26. message = %{
  27. "type" => "Create",
  28. "object" => %{
  29. "summary" => "just a daily reminder that compLAINer is a good pun",
  30. "content" => ""
  31. }
  32. }
  33. assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
  34. KeywordPolicy.filter(message)
  35. end
  36. test "rejects if regex matches in content" do
  37. clear_config([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
  38. assert true ==
  39. Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
  40. message = %{
  41. "type" => "Create",
  42. "object" => %{
  43. "content" => "just a daily reminder that #{content} is a good pun",
  44. "summary" => ""
  45. }
  46. }
  47. {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
  48. KeywordPolicy.filter(message)
  49. end)
  50. end
  51. test "rejects if regex matches in summary" do
  52. clear_config([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/])
  53. assert true ==
  54. Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
  55. message = %{
  56. "type" => "Create",
  57. "object" => %{
  58. "summary" => "just a daily reminder that #{content} is a good pun",
  59. "content" => ""
  60. }
  61. }
  62. {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
  63. KeywordPolicy.filter(message)
  64. end)
  65. end
  66. test "rejects if string matches in history" do
  67. clear_config([:mrf_keyword, :reject], ["pun"])
  68. message = %{
  69. "type" => "Create",
  70. "object" => %{
  71. "content" => "just a daily reminder that compLAINer is a good",
  72. "summary" => "",
  73. "formerRepresentations" => %{
  74. "type" => "OrderedCollection",
  75. "orderedItems" => [
  76. %{
  77. "content" => "just a daily reminder that compLAINer is a good pun",
  78. "summary" => ""
  79. }
  80. ]
  81. }
  82. }
  83. }
  84. assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
  85. KeywordPolicy.filter(message)
  86. end
  87. test "rejects Updates" do
  88. clear_config([:mrf_keyword, :reject], ["pun"])
  89. message = %{
  90. "type" => "Update",
  91. "object" => %{
  92. "content" => "just a daily reminder that compLAINer is a good",
  93. "summary" => "",
  94. "formerRepresentations" => %{
  95. "type" => "OrderedCollection",
  96. "orderedItems" => [
  97. %{
  98. "content" => "just a daily reminder that compLAINer is a good pun",
  99. "summary" => ""
  100. }
  101. ]
  102. }
  103. }
  104. }
  105. assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} =
  106. KeywordPolicy.filter(message)
  107. end
  108. end
  109. describe "delisting from ftl based on keywords" do
  110. test "delists if string matches in content" do
  111. clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"])
  112. message = %{
  113. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  114. "type" => "Create",
  115. "object" => %{
  116. "content" => "just a daily reminder that compLAINer is a good pun",
  117. "summary" => ""
  118. }
  119. }
  120. {:ok, result} = KeywordPolicy.filter(message)
  121. assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
  122. refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
  123. end
  124. test "delists if string matches in summary" do
  125. clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"])
  126. message = %{
  127. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  128. "type" => "Create",
  129. "object" => %{
  130. "summary" => "just a daily reminder that compLAINer is a good pun",
  131. "content" => ""
  132. }
  133. }
  134. {:ok, result} = KeywordPolicy.filter(message)
  135. assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
  136. refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
  137. end
  138. test "delists if regex matches in content" do
  139. clear_config([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
  140. assert true ==
  141. Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
  142. message = %{
  143. "type" => "Create",
  144. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  145. "object" => %{
  146. "content" => "just a daily reminder that #{content} is a good pun",
  147. "summary" => ""
  148. }
  149. }
  150. {:ok, result} = KeywordPolicy.filter(message)
  151. ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
  152. not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
  153. end)
  154. end
  155. test "delists if regex matches in summary" do
  156. clear_config([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
  157. assert true ==
  158. Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
  159. message = %{
  160. "type" => "Create",
  161. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  162. "object" => %{
  163. "summary" => "just a daily reminder that #{content} is a good pun",
  164. "content" => ""
  165. }
  166. }
  167. {:ok, result} = KeywordPolicy.filter(message)
  168. ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
  169. not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
  170. end)
  171. end
  172. test "delists if string matches in history" do
  173. clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"])
  174. message = %{
  175. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  176. "type" => "Create",
  177. "object" => %{
  178. "content" => "just a daily reminder that compLAINer is a good",
  179. "summary" => "",
  180. "formerRepresentations" => %{
  181. "orderedItems" => [
  182. %{
  183. "content" => "just a daily reminder that compLAINer is a good pun",
  184. "summary" => ""
  185. }
  186. ]
  187. }
  188. }
  189. }
  190. {:ok, result} = KeywordPolicy.filter(message)
  191. assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
  192. refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
  193. end
  194. end
  195. describe "replacing keywords" do
  196. test "replaces keyword if string matches in content" do
  197. clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
  198. message = %{
  199. "type" => "Create",
  200. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  201. "object" => %{"content" => "ZFS is opensource", "summary" => ""}
  202. }
  203. {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
  204. assert result == "ZFS is free software"
  205. end
  206. test "replaces keyword if string matches in summary" do
  207. clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
  208. message = %{
  209. "type" => "Create",
  210. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  211. "object" => %{"summary" => "ZFS is opensource", "content" => ""}
  212. }
  213. {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
  214. assert result == "ZFS is free software"
  215. end
  216. test "replaces keyword if regex matches in content" do
  217. clear_config([:mrf_keyword, :replace], [
  218. {~r/open(-|\s)?source\s?(software)?/, "free software"}
  219. ])
  220. assert true ==
  221. Enum.all?(["opensource", "open-source", "open source"], fn content ->
  222. message = %{
  223. "type" => "Create",
  224. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  225. "object" => %{"content" => "ZFS is #{content}", "summary" => ""}
  226. }
  227. {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
  228. result == "ZFS is free software"
  229. end)
  230. end
  231. test "replaces keyword if regex matches in summary" do
  232. clear_config([:mrf_keyword, :replace], [
  233. {~r/open(-|\s)?source\s?(software)?/, "free software"}
  234. ])
  235. assert true ==
  236. Enum.all?(["opensource", "open-source", "open source"], fn content ->
  237. message = %{
  238. "type" => "Create",
  239. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  240. "object" => %{"summary" => "ZFS is #{content}", "content" => ""}
  241. }
  242. {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
  243. result == "ZFS is free software"
  244. end)
  245. end
  246. test "replaces keyword if string matches in history" do
  247. clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
  248. message = %{
  249. "type" => "Create",
  250. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  251. "object" => %{
  252. "content" => "ZFS is opensource",
  253. "summary" => "",
  254. "formerRepresentations" => %{
  255. "type" => "OrderedCollection",
  256. "orderedItems" => [
  257. %{"content" => "ZFS is opensource mew mew", "summary" => ""}
  258. ]
  259. }
  260. }
  261. }
  262. {:ok,
  263. %{
  264. "object" => %{
  265. "content" => "ZFS is free software",
  266. "formerRepresentations" => %{
  267. "orderedItems" => [%{"content" => "ZFS is free software mew mew"}]
  268. }
  269. }
  270. }} = KeywordPolicy.filter(message)
  271. end
  272. test "replaces keyword in Updates" do
  273. clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
  274. message = %{
  275. "type" => "Update",
  276. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  277. "object" => %{
  278. "content" => "ZFS is opensource",
  279. "summary" => "",
  280. "formerRepresentations" => %{
  281. "type" => "OrderedCollection",
  282. "orderedItems" => [
  283. %{"content" => "ZFS is opensource mew mew", "summary" => ""}
  284. ]
  285. }
  286. }
  287. }
  288. {:ok,
  289. %{
  290. "object" => %{
  291. "content" => "ZFS is free software",
  292. "formerRepresentations" => %{
  293. "orderedItems" => [%{"content" => "ZFS is free software mew mew"}]
  294. }
  295. }
  296. }} = KeywordPolicy.filter(message)
  297. end
  298. end
  299. end