test/steal_emoji: reduce code duplication with mock macro
This commit is contained in:
parent
a4fa2ec9af
commit
d1ce5fd911
1 changed files with 24 additions and 33 deletions
|
@ -24,6 +24,25 @@ defp has_emoji?(shortcode) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defmacro mock_tesla(
|
||||||
|
url \\ "https://example.org/emoji/firedfox.png",
|
||||||
|
status \\ 200,
|
||||||
|
headers \\ [],
|
||||||
|
get_body \\ File.read!("test/fixtures/image.jpg")
|
||||||
|
) do
|
||||||
|
quote do
|
||||||
|
Tesla.Mock.mock(fn
|
||||||
|
%{method: :get, url: unquote(url)} ->
|
||||||
|
%Tesla.Env{
|
||||||
|
status: unquote(status),
|
||||||
|
body: unquote(get_body),
|
||||||
|
url: unquote(url),
|
||||||
|
headers: unquote(headers)
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
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")
|
||||||
|
|
||||||
|
@ -58,13 +77,7 @@ test "Steals emoji on unknown shortcode from allowed remote host", %{
|
||||||
refute "firedfox" in installed()
|
refute "firedfox" in installed()
|
||||||
refute has_pack?()
|
refute has_pack?()
|
||||||
|
|
||||||
Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
|
mock_tesla()
|
||||||
%Tesla.Env{
|
|
||||||
status: 200,
|
|
||||||
body: File.read!("test/fixtures/image.jpg"),
|
|
||||||
url: "https://example.org/emoji/firedfox.png"
|
|
||||||
}
|
|
||||||
end)
|
|
||||||
|
|
||||||
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
|
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
|
||||||
|
|
||||||
|
@ -85,13 +98,7 @@ test "rejects invalid shortcodes" do
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox"} ->
|
mock_tesla()
|
||||||
%Tesla.Env{
|
|
||||||
status: 200,
|
|
||||||
body: File.read!("test/fixtures/image.jpg"),
|
|
||||||
url: "https://example.org/emoji/firedfox.png"
|
|
||||||
}
|
|
||||||
end)
|
|
||||||
|
|
||||||
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
|
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
|
||||||
|
|
||||||
|
@ -113,14 +120,7 @@ test "prefers content-type header for extension" do
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.fud"} ->
|
mock_tesla("https://example.org/emoji/firedfox.fud", 200, [{"content-type", "image/gif"}])
|
||||||
%Tesla.Env{
|
|
||||||
status: 200,
|
|
||||||
body: File.read!("test/fixtures/image.jpg"),
|
|
||||||
url: "https://example.org/emoji/firedfox.wevp",
|
|
||||||
headers: [{"content-type", "image/gif"}]
|
|
||||||
}
|
|
||||||
end)
|
|
||||||
|
|
||||||
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
|
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
|
||||||
|
|
||||||
|
@ -161,13 +161,7 @@ test "reject string shortcode", %{message: message} do
|
||||||
test "reject if size is above the limit", %{message: message} do
|
test "reject if size is above the limit", %{message: message} do
|
||||||
refute "firedfox" in installed()
|
refute "firedfox" in installed()
|
||||||
|
|
||||||
Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
|
mock_tesla()
|
||||||
%Tesla.Env{
|
|
||||||
status: 200,
|
|
||||||
body: File.read!("test/fixtures/image.jpg"),
|
|
||||||
url: "https://example.org/emoji/firedfox.png"
|
|
||||||
}
|
|
||||||
end)
|
|
||||||
|
|
||||||
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 50_000)
|
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 50_000)
|
||||||
|
|
||||||
|
@ -179,10 +173,7 @@ test "reject if size is above the limit", %{message: message} do
|
||||||
test "reject if host returns error", %{message: message} do
|
test "reject if host returns error", %{message: message} do
|
||||||
refute "firedfox" in installed()
|
refute "firedfox" in installed()
|
||||||
|
|
||||||
Tesla.Mock.mock(fn %{method: :get, url: "https://example.org/emoji/firedfox.png"} ->
|
mock_tesla("https://example.org/emoji/firedfox.png", 404, [], "Not found")
|
||||||
{:ok,
|
|
||||||
%Tesla.Env{status: 404, body: "Not found", url: "https://example.org/emoji/firedfox.png"}}
|
|
||||||
end)
|
|
||||||
|
|
||||||
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
|
clear_config(:mrf_steal_emoji, hosts: ["example.org"], size_limit: 284_468)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue