test: use pack functions to check for emoji
The hardocded path and filenames assumptions will be broken with the next commit.
This commit is contained in:
parent
d1c4d07404
commit
ee5ce87825
1 changed files with 25 additions and 19 deletions
|
@ -7,8 +7,23 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do
|
||||||
|
|
||||||
alias Pleroma.Config
|
alias Pleroma.Config
|
||||||
alias Pleroma.Emoji
|
alias Pleroma.Emoji
|
||||||
|
alias Pleroma.Emoji.Pack
|
||||||
alias Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy
|
alias Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy
|
||||||
|
|
||||||
|
defp has_pack?() do
|
||||||
|
case Pack.load_pack("stolen") do
|
||||||
|
{:ok, _pack} -> true
|
||||||
|
{:error, :enoent} -> false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp has_emoji?(shortcode) do
|
||||||
|
case Pack.load_pack("stolen") do
|
||||||
|
{:ok, pack} -> Map.has_key?(pack.files, shortcode)
|
||||||
|
{:error, :enoent} -> false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
emoji_path = [:instance, :static_dir] |> Config.get() |> Path.join("emoji/stolen")
|
emoji_path = [:instance, :static_dir] |> Config.get() |> Path.join("emoji/stolen")
|
||||||
|
|
||||||
|
@ -26,7 +41,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do
|
||||||
File.rm_rf!(emoji_path)
|
File.rm_rf!(emoji_path)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
[message: message, path: emoji_path]
|
[message: message]
|
||||||
end
|
end
|
||||||
|
|
||||||
test "does nothing by default", %{message: message} do
|
test "does nothing by default", %{message: message} do
|
||||||
|
@ -38,11 +53,10 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "Steals emoji on unknown shortcode from allowed remote host", %{
|
test "Steals emoji on unknown shortcode from allowed remote host", %{
|
||||||
message: message,
|
message: message
|
||||||
path: path
|
|
||||||
} do
|
} do
|
||||||
refute "firedfox" in installed()
|
refute "firedfox" in installed()
|
||||||
refute File.exists?(path)
|
refute has_pack?()
|
||||||
|
|
||||||
Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
|
Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
|
||||||
%Tesla.Env{
|
%Tesla.Env{
|
||||||
|
@ -57,14 +71,12 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do
|
||||||
assert {:ok, _message} = StealEmojiPolicy.filter(message)
|
assert {:ok, _message} = StealEmojiPolicy.filter(message)
|
||||||
|
|
||||||
assert "firedfox" in installed()
|
assert "firedfox" in installed()
|
||||||
assert File.exists?(path)
|
assert has_pack?()
|
||||||
|
|
||||||
assert path
|
assert has_emoji?("firedfox")
|
||||||
|> Path.join("firedfox.png")
|
|
||||||
|> File.exists?()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "rejects invalid shortcodes", %{path: path} do
|
test "rejects invalid shortcodes" do
|
||||||
message = %{
|
message = %{
|
||||||
"type" => "Create",
|
"type" => "Create",
|
||||||
"object" => %{
|
"object" => %{
|
||||||
|
@ -73,8 +85,6 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fullpath = Path.join(path, "fired/fox.png")
|
|
||||||
|
|
||||||
Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox"} ->
|
Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox"} ->
|
||||||
%Tesla.Env{
|
%Tesla.Env{
|
||||||
status: 200,
|
status: 200,
|
||||||
|
@ -86,15 +96,15 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do
|
||||||
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
|
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
|
||||||
|
|
||||||
refute "firedfox" in installed()
|
refute "firedfox" in installed()
|
||||||
refute File.exists?(path)
|
refute has_pack?()
|
||||||
|
|
||||||
assert {:ok, _message} = StealEmojiPolicy.filter(message)
|
assert {:ok, _message} = StealEmojiPolicy.filter(message)
|
||||||
|
|
||||||
refute "fired/fox" in installed()
|
refute "fired/fox" in installed()
|
||||||
refute File.exists?(fullpath)
|
refute has_emoji?("fired/fox")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "prefers content-type header for extension", %{path: path} do
|
test "prefers content-type header for extension" do
|
||||||
message = %{
|
message = %{
|
||||||
"type" => "Create",
|
"type" => "Create",
|
||||||
"object" => %{
|
"object" => %{
|
||||||
|
@ -117,11 +127,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do
|
||||||
assert {:ok, _message} = StealEmojiPolicy.filter(message)
|
assert {:ok, _message} = StealEmojiPolicy.filter(message)
|
||||||
|
|
||||||
assert "firedfox" in installed()
|
assert "firedfox" in installed()
|
||||||
assert File.exists?(path)
|
assert has_emoji?("firedfox")
|
||||||
|
|
||||||
assert path
|
|
||||||
|> Path.join("firedfox.gif")
|
|
||||||
|> File.exists?()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "reject regex shortcode", %{message: message} do
|
test "reject regex shortcode", %{message: message} do
|
||||||
|
|
Loading…
Reference in a new issue