X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=test%2Fpleroma%2Fweb%2Factivity_pub%2Fmrf%2Fkeyword_policy_test.exs;h=9bc8c835503bbc9e2ec9f4ae25f1b6fc3c5911ca;hb=1f863f0a36ebb0648c3d39ecb7ea9e3d01deab60;hp=b3d0f3d90620b39b104cb0dbf8ff7c5bc6d41d89;hpb=b3d6cf90229ff3e9a5bd0028d4a6cbd3b68da9fe;p=akkoma diff --git a/test/pleroma/web/activity_pub/mrf/keyword_policy_test.exs b/test/pleroma/web/activity_pub/mrf/keyword_policy_test.exs index b3d0f3d90..9bc8c8355 100644 --- a/test/pleroma/web/activity_pub/mrf/keyword_policy_test.exs +++ b/test/pleroma/web/activity_pub/mrf/keyword_policy_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors +# Copyright © 2017-2021 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do @@ -10,12 +10,12 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do setup do: clear_config(:mrf_keyword) setup do - Pleroma.Config.put([:mrf_keyword], %{reject: [], federated_timeline_removal: [], replace: []}) + clear_config([:mrf_keyword], %{reject: [], federated_timeline_removal: [], replace: []}) end describe "rejecting based on keywords" do test "rejects if string matches in content" do - Pleroma.Config.put([:mrf_keyword, :reject], ["pun"]) + clear_config([:mrf_keyword, :reject], ["pun"]) message = %{ "type" => "Create", @@ -30,7 +30,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do end test "rejects if string matches in summary" do - Pleroma.Config.put([:mrf_keyword, :reject], ["pun"]) + clear_config([:mrf_keyword, :reject], ["pun"]) message = %{ "type" => "Create", @@ -45,7 +45,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do end test "rejects if regex matches in content" do - Pleroma.Config.put([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/]) + clear_config([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/]) assert true == Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content -> @@ -63,7 +63,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do end test "rejects if regex matches in summary" do - Pleroma.Config.put([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/]) + clear_config([:mrf_keyword, :reject], [~r/comp[lL][aA][iI][nN]er/]) assert true == Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content -> @@ -79,11 +79,59 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do KeywordPolicy.filter(message) end) end + + test "rejects if string matches in history" do + clear_config([:mrf_keyword, :reject], ["pun"]) + + message = %{ + "type" => "Create", + "object" => %{ + "content" => "just a daily reminder that compLAINer is a good", + "summary" => "", + "formerRepresentations" => %{ + "type" => "OrderedCollection", + "orderedItems" => [ + %{ + "content" => "just a daily reminder that compLAINer is a good pun", + "summary" => "" + } + ] + } + } + } + + assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} = + KeywordPolicy.filter(message) + end + + test "rejects Updates" do + clear_config([:mrf_keyword, :reject], ["pun"]) + + message = %{ + "type" => "Update", + "object" => %{ + "content" => "just a daily reminder that compLAINer is a good", + "summary" => "", + "formerRepresentations" => %{ + "type" => "OrderedCollection", + "orderedItems" => [ + %{ + "content" => "just a daily reminder that compLAINer is a good pun", + "summary" => "" + } + ] + } + } + } + + assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} = + KeywordPolicy.filter(message) + end end describe "delisting from ftl based on keywords" do test "delists if string matches in content" do - Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], ["pun"]) + clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"]) message = %{ "to" => ["https://www.w3.org/ns/activitystreams#Public"], @@ -100,7 +148,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do end test "delists if string matches in summary" do - Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], ["pun"]) + clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"]) message = %{ "to" => ["https://www.w3.org/ns/activitystreams#Public"], @@ -117,7 +165,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do end test "delists if regex matches in content" do - Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/]) + clear_config([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/]) assert true == Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content -> @@ -138,7 +186,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do end test "delists if regex matches in summary" do - Pleroma.Config.put([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/]) + clear_config([:mrf_keyword, :federated_timeline_removal], [~r/comp[lL][aA][iI][nN]er/]) assert true == Enum.all?(["complainer", "compLainer", "compLAiNer", "compLAINer"], fn content -> @@ -157,11 +205,36 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do not (["https://www.w3.org/ns/activitystreams#Public"] == result["to"]) end) end + + test "delists if string matches in history" do + clear_config([:mrf_keyword, :federated_timeline_removal], ["pun"]) + + message = %{ + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "type" => "Create", + "object" => %{ + "content" => "just a daily reminder that compLAINer is a good", + "summary" => "", + "formerRepresentations" => %{ + "orderedItems" => [ + %{ + "content" => "just a daily reminder that compLAINer is a good pun", + "summary" => "" + } + ] + } + } + } + + {:ok, result} = KeywordPolicy.filter(message) + assert ["https://www.w3.org/ns/activitystreams#Public"] == result["cc"] + refute ["https://www.w3.org/ns/activitystreams#Public"] == result["to"] + end end describe "replacing keywords" do test "replaces keyword if string matches in content" do - Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}]) + clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}]) message = %{ "type" => "Create", @@ -174,7 +247,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do end test "replaces keyword if string matches in summary" do - Pleroma.Config.put([:mrf_keyword, :replace], [{"opensource", "free software"}]) + clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}]) message = %{ "type" => "Create", @@ -187,7 +260,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do end test "replaces keyword if regex matches in content" do - Pleroma.Config.put([:mrf_keyword, :replace], [ + clear_config([:mrf_keyword, :replace], [ {~r/open(-|\s)?source\s?(software)?/, "free software"} ]) @@ -205,7 +278,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do end test "replaces keyword if regex matches in summary" do - Pleroma.Config.put([:mrf_keyword, :replace], [ + clear_config([:mrf_keyword, :replace], [ {~r/open(-|\s)?source\s?(software)?/, "free software"} ]) @@ -221,5 +294,63 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do result == "ZFS is free software" end) end + + test "replaces keyword if string matches in history" do + clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}]) + + message = %{ + "type" => "Create", + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "object" => %{ + "content" => "ZFS is opensource", + "summary" => "", + "formerRepresentations" => %{ + "type" => "OrderedCollection", + "orderedItems" => [ + %{"content" => "ZFS is opensource mew mew", "summary" => ""} + ] + } + } + } + + {:ok, + %{ + "object" => %{ + "content" => "ZFS is free software", + "formerRepresentations" => %{ + "orderedItems" => [%{"content" => "ZFS is free software mew mew"}] + } + } + }} = KeywordPolicy.filter(message) + end + + test "replaces keyword in Updates" do + clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}]) + + message = %{ + "type" => "Update", + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "object" => %{ + "content" => "ZFS is opensource", + "summary" => "", + "formerRepresentations" => %{ + "type" => "OrderedCollection", + "orderedItems" => [ + %{"content" => "ZFS is opensource mew mew", "summary" => ""} + ] + } + } + } + + {:ok, + %{ + "object" => %{ + "content" => "ZFS is free software", + "formerRepresentations" => %{ + "orderedItems" => [%{"content" => "ZFS is free software mew mew"}] + } + } + }} = KeywordPolicy.filter(message) + end end end