X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=test%2Fpleroma%2Fweb%2Factivity_pub%2Fmrf%2Fsteal_emoji_policy_test.exs;h=bae57f29ad517c5addca7ebf9f2810487ffd2ed8;hb=694d98be550b0afe5b110c7f3c63e9cb382f2c90;hp=3f8222736ec591dbcc449aedcedc46280b27b1b1;hpb=d8d4e962304c5424de4d171d25445b7135a67a09;p=akkoma
diff --git a/test/pleroma/web/activity_pub/mrf/steal_emoji_policy_test.exs b/test/pleroma/web/activity_pub/mrf/steal_emoji_policy_test.exs
index 3f8222736..bae57f29a 100644
--- a/test/pleroma/web/activity_pub/mrf/steal_emoji_policy_test.exs
+++ b/test/pleroma/web/activity_pub/mrf/steal_emoji_policy_test.exs
@@ -1,11 +1,12 @@
# 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.StealEmojiPolicyTest do
use Pleroma.DataCase
alias Pleroma.Config
+ alias Pleroma.Emoji
alias Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy
setup_all do
@@ -14,22 +15,9 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do
end
setup do
- emoji_path = Path.join(Config.get([:instance, :static_dir]), "emoji/stolen")
- File.rm_rf!(emoji_path)
- File.mkdir!(emoji_path)
+ emoji_path = [:instance, :static_dir] |> Config.get() |> Path.join("emoji/stolen")
- Pleroma.Emoji.reload()
-
- on_exit(fn ->
- File.rm_rf!(emoji_path)
- end)
-
- :ok
- end
-
- test "does nothing by default" do
- installed_emoji = Pleroma.Emoji.get_all() |> Enum.map(fn {k, _} -> k end)
- refute "firedfox" in installed_emoji
+ Emoji.reload()
message = %{
"type" => "Create",
@@ -39,30 +27,79 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do
}
}
- assert {:ok, message} == StealEmojiPolicy.filter(message)
+ on_exit(fn ->
+ File.rm_rf!(emoji_path)
+ end)
- installed_emoji = Pleroma.Emoji.get_all() |> Enum.map(fn {k, _} -> k end)
- refute "firedfox" in installed_emoji
+ [message: message, path: emoji_path]
end
- test "Steals emoji on unknown shortcode from allowed remote host" do
- installed_emoji = Pleroma.Emoji.get_all() |> Enum.map(fn {k, _} -> k end)
- refute "firedfox" in installed_emoji
+ test "does nothing by default", %{message: message} do
+ refute "firedfox" in installed()
- message = %{
- "type" => "Create",
- "object" => %{
- "emoji" => [{"firedfox", "https://example.org/emoji/firedfox.png"}],
- "actor" => "https://example.org/users/admin"
- }
- }
+ assert {:ok, _message} = StealEmojiPolicy.filter(message)
+
+ refute "firedfox" in installed()
+ end
- clear_config([:mrf_steal_emoji, :hosts], ["example.org"])
- clear_config([:mrf_steal_emoji, :size_limit], 284_468)
+ test "Steals emoji on unknown shortcode from allowed remote host", %{
+ message: message,
+ path: path
+ } do
+ refute "firedfox" in installed()
+ refute File.exists?(path)
- assert {:ok, message} == StealEmojiPolicy.filter(message)
+ clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
- installed_emoji = Pleroma.Emoji.get_all() |> Enum.map(fn {k, _} -> k end)
- assert "firedfox" in installed_emoji
+ assert {:ok, _message} = StealEmojiPolicy.filter(message)
+
+ assert "firedfox" in installed()
+ assert File.exists?(path)
+
+ assert path
+ |> Path.join("firedfox.png")
+ |> File.exists?()
+ end
+
+ test "reject shortcode", %{message: message} do
+ refute "firedfox" in installed()
+
+ clear_config(:mrf_steal_emoji,
+ hosts: ["example.org"],
+ size_limit: 284_468,
+ rejected_shortcodes: [~r/firedfox/]
+ )
+
+ assert {:ok, _message} = StealEmojiPolicy.filter(message)
+
+ refute "firedfox" in installed()
+ end
+
+ test "reject if size is above the limit", %{message: message} do
+ refute "firedfox" in installed()
+
+ clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 50_000)
+
+ assert {:ok, _message} = StealEmojiPolicy.filter(message)
+
+ refute "firedfox" in installed()
+ end
+
+ test "reject if host returns error", %{message: message} do
+ refute "firedfox" in installed()
+
+ Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
+ {:ok, %Tesla.Env{status: 404, body: "Not found"}}
+ end)
+
+ clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
+
+ ExUnit.CaptureLog.capture_log(fn ->
+ assert {:ok, _message} = StealEmojiPolicy.filter(message)
+ end) =~ "MRF.StealEmojiPolicy: Failed to fetch https://example.org/emoji/firedfox.png"
+
+ refute "firedfox" in installed()
end
+
+ defp installed, do: Emoji.get_all() |> Enum.map(fn {k, _} -> k end)
end