logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

keyword_policy_test.exs (8191B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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. Pleroma.Config.put([: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. Pleroma.Config.put([: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. Pleroma.Config.put([: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. Pleroma.Config.put([: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. Pleroma.Config.put([: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. end
  67. describe "delisting from ftl based on keywords" do
  68. test "delists if string matches in content" do
  69. Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], ["pun"])
  70. message = %{
  71. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  72. "type" => "Create",
  73. "object" => %{
  74. "content" => "just a daily reminder that compLAINer is a good pun",
  75. "summary" => ""
  76. }
  77. }
  78. {:ok, result} = KeywordPolicy.filter(message)
  79. assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
  80. refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
  81. end
  82. test "delists if string matches in summary" do
  83. Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], ["pun"])
  84. message = %{
  85. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  86. "type" => "Create",
  87. "object" => %{
  88. "summary" => "just a daily reminder that compLAINer is a good pun",
  89. "content" => ""
  90. }
  91. }
  92. {:ok, result} = KeywordPolicy.filter(message)
  93. assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"]
  94. refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"]
  95. end
  96. test "delists if regex matches in content" do
  97. Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
  98. assert true ==
  99. Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
  100. message = %{
  101. "type" => "Create",
  102. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  103. "object" => %{
  104. "content" => "just a daily reminder that #{content} is a good pun",
  105. "summary" => ""
  106. }
  107. }
  108. {:ok, result} = KeywordPolicy.filter(message)
  109. ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
  110. not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
  111. end)
  112. end
  113. test "delists if regex matches in summary" do
  114. Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/])
  115. assert true ==
  116. Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content ->
  117. message = %{
  118. "type" => "Create",
  119. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  120. "object" => %{
  121. "summary" => "just a daily reminder that #{content} is a good pun",
  122. "content" => ""
  123. }
  124. }
  125. {:ok, result} = KeywordPolicy.filter(message)
  126. ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] and
  127. not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"])
  128. end)
  129. end
  130. end
  131. describe "replacing keywords" do
  132. test "replaces keyword if string matches in content" do
  133. Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}])
  134. message = %{
  135. "type" => "Create",
  136. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  137. "object" => %{"content" => "ZFS is opensource", "summary" => ""}
  138. }
  139. {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
  140. assert result == "ZFS is free software"
  141. end
  142. test "replaces keyword if string matches in summary" do
  143. Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}])
  144. message = %{
  145. "type" => "Create",
  146. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  147. "object" => %{"summary" => "ZFS is opensource", "content" => ""}
  148. }
  149. {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
  150. assert result == "ZFS is free software"
  151. end
  152. test "replaces keyword if regex matches in content" do
  153. Pleroma.Config.put([:mrf_keyword, :replace], [
  154. {~r/open(-|\s)?source\s?(software)?/, "free software"}
  155. ])
  156. assert true ==
  157. Enum.all?(["opensource", "open-source", "open source"], fn content ->
  158. message = %{
  159. "type" => "Create",
  160. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  161. "object" => %{"content" => "ZFS is #{content}", "summary" => ""}
  162. }
  163. {:ok, %{"object" => %{"content" => result}}} = KeywordPolicy.filter(message)
  164. result == "ZFS is free software"
  165. end)
  166. end
  167. test "replaces keyword if regex matches in summary" do
  168. Pleroma.Config.put([:mrf_keyword, :replace], [
  169. {~r/open(-|\s)?source\s?(software)?/, "free software"}
  170. ])
  171. assert true ==
  172. Enum.all?(["opensource", "open-source", "open source"], fn content ->
  173. message = %{
  174. "type" => "Create",
  175. "to" => ["https://www.w3.org/ns/activitystreams#Public"],
  176. "object" => %{"summary" => "ZFS is #{content}", "content" => ""}
  177. }
  178. {:ok, %{"object" => %{"summary" => result}}} = KeywordPolicy.filter(message)
  179. result == "ZFS is free software"
  180. end)
  181. end
  182. end
  183. end