tests changes

This commit is contained in:
Alexander Strizhakov 2020-03-07 11:01:37 +03:00
parent 14678a7708
commit 9f884a2639
No known key found for this signature in database
GPG Key ID: 022896A53AEF1381
4 changed files with 27 additions and 52 deletions

View File

@ -24,8 +24,8 @@ defp gun_mock(_) do
defp gun_mock do defp gun_mock do
Pleroma.GunMock Pleroma.GunMock
|> stub(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(1000) end) end) |> stub(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(1000) end) end)
|> expect(:await_up, fn _, _ -> {:ok, :http} end) |> stub(:await_up, fn _, _ -> {:ok, :http} end)
|> expect(:set_owner, fn _, _ -> :ok end) |> stub(:set_owner, fn _, _ -> :ok end)
end end
describe "options/1" do describe "options/1" do

View File

@ -7,13 +7,10 @@ defmodule Pleroma.HTTP.ConnectionTest do
use Pleroma.Tests.Helpers use Pleroma.Tests.Helpers
import ExUnit.CaptureLog import ExUnit.CaptureLog
import Mox
alias Pleroma.Config alias Pleroma.Config
alias Pleroma.HTTP.Connection alias Pleroma.HTTP.Connection
setup :verify_on_exit!
describe "parse_host/1" do describe "parse_host/1" do
test "as atom to charlist" do test "as atom to charlist" do
assert Connection.parse_host(:localhost) == 'localhost' assert Connection.parse_host(:localhost) == 'localhost'
@ -115,30 +112,5 @@ test "passed opts have more weight than defaults" do
assert opts[:proxy] == {'example.com', 4321} assert opts[:proxy] == {'example.com', 4321}
end end
test "default ssl adapter opts with connection" do
adapter = Application.get_env(:tesla, :adapter)
Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun)
on_exit(fn -> Application.put_env(:tesla, :adapter, adapter) end)
uri = URI.parse("https://some-domain.com")
Pleroma.GunMock
|> expect(:open, fn 'some-domain.com', 443, _ ->
Task.start_link(fn -> Process.sleep(1000) end)
end)
|> expect(:await_up, fn _, _ -> {:ok, :http2} end)
|> expect(:set_owner, fn _, _ -> :ok end)
:ok = Pleroma.Gun.Conn.open(uri, :gun_connections)
opts = Connection.options(uri)
assert opts[:certificates_verification]
refute opts[:tls_opts] == []
assert opts[:close_conn] == false
assert is_pid(opts[:conn])
end
end end
end end

View File

@ -3,7 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTPTest do defmodule Pleroma.HTTPTest do
use ExUnit.Case use ExUnit.Case, async: true
use Pleroma.Tests.Helpers use Pleroma.Tests.Helpers
import Tesla.Mock import Tesla.Mock
alias Pleroma.HTTP alias Pleroma.HTTP

View File

@ -3,14 +3,17 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ReverseProxyTest do defmodule Pleroma.ReverseProxyTest do
use Pleroma.Web.ConnCase use Pleroma.Web.ConnCase, async: true
import ExUnit.CaptureLog import ExUnit.CaptureLog
import Mox import Mox
alias Pleroma.ReverseProxy alias Pleroma.ReverseProxy
alias Pleroma.ReverseProxy.ClientMock alias Pleroma.ReverseProxy.ClientMock
alias Plug.Conn
setup_all do setup_all do
{:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.ReverseProxy.ClientMock) {:ok, _} = Registry.start_link(keys: :unique, name: ClientMock)
:ok :ok
end end
@ -21,7 +24,7 @@ defp user_agent_mock(user_agent, invokes) do
ClientMock ClientMock
|> expect(:request, fn :get, url, _, _, _ -> |> expect(:request, fn :get, url, _, _, _ ->
Registry.register(Pleroma.ReverseProxy.ClientMock, url, 0) Registry.register(ClientMock, url, 0)
{:ok, 200, {:ok, 200,
[ [
@ -30,13 +33,13 @@ defp user_agent_mock(user_agent, invokes) do
], %{url: url}} ], %{url: url}}
end) end)
|> expect(:stream_body, invokes, fn %{url: url} = client -> |> expect(:stream_body, invokes, fn %{url: url} = client ->
case Registry.lookup(Pleroma.ReverseProxy.ClientMock, url) do case Registry.lookup(ClientMock, url) do
[{_, 0}] -> [{_, 0}] ->
Registry.update_value(Pleroma.ReverseProxy.ClientMock, url, &(&1 + 1)) Registry.update_value(ClientMock, url, &(&1 + 1))
{:ok, json, client} {:ok, json, client}
[{_, 1}] -> [{_, 1}] ->
Registry.unregister(Pleroma.ReverseProxy.ClientMock, url) Registry.unregister(ClientMock, url)
:done :done
end end
end) end)
@ -81,7 +84,7 @@ test "closed connection", %{conn: conn} do
defp stream_mock(invokes, with_close? \\ false) do defp stream_mock(invokes, with_close? \\ false) do
ClientMock ClientMock
|> expect(:request, fn :get, "/stream-bytes/" <> length, _, _, _ -> |> expect(:request, fn :get, "/stream-bytes/" <> length, _, _, _ ->
Registry.register(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length, 0) Registry.register(ClientMock, "/stream-bytes/" <> length, 0)
{:ok, 200, [{"content-type", "application/octet-stream"}], {:ok, 200, [{"content-type", "application/octet-stream"}],
%{url: "/stream-bytes/" <> length}} %{url: "/stream-bytes/" <> length}}
@ -89,10 +92,10 @@ defp stream_mock(invokes, with_close? \\ false) do
|> expect(:stream_body, invokes, fn %{url: "/stream-bytes/" <> length} = client -> |> expect(:stream_body, invokes, fn %{url: "/stream-bytes/" <> length} = client ->
max = String.to_integer(length) max = String.to_integer(length)
case Registry.lookup(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length) do case Registry.lookup(ClientMock, "/stream-bytes/" <> length) do
[{_, current}] when current < max -> [{_, current}] when current < max ->
Registry.update_value( Registry.update_value(
Pleroma.ReverseProxy.ClientMock, ClientMock,
"/stream-bytes/" <> length, "/stream-bytes/" <> length,
&(&1 + 10) &(&1 + 10)
) )
@ -100,7 +103,7 @@ defp stream_mock(invokes, with_close? \\ false) do
{:ok, "0123456789", client} {:ok, "0123456789", client}
[{_, ^max}] -> [{_, ^max}] ->
Registry.unregister(Pleroma.ReverseProxy.ClientMock, "/stream-bytes/" <> length) Registry.unregister(ClientMock, "/stream-bytes/" <> length)
:done :done
end end
end) end)
@ -214,24 +217,24 @@ test "streaming", %{conn: conn} do
conn = ReverseProxy.call(conn, "/stream-bytes/200") conn = ReverseProxy.call(conn, "/stream-bytes/200")
assert conn.state == :chunked assert conn.state == :chunked
assert byte_size(conn.resp_body) == 200 assert byte_size(conn.resp_body) == 200
assert Plug.Conn.get_resp_header(conn, "content-type") == ["application/octet-stream"] assert Conn.get_resp_header(conn, "content-type") == ["application/octet-stream"]
end end
defp headers_mock(_) do defp headers_mock(_) do
ClientMock ClientMock
|> expect(:request, fn :get, "/headers", headers, _, _ -> |> expect(:request, fn :get, "/headers", headers, _, _ ->
Registry.register(Pleroma.ReverseProxy.ClientMock, "/headers", 0) Registry.register(ClientMock, "/headers", 0)
{:ok, 200, [{"content-type", "application/json"}], %{url: "/headers", headers: headers}} {:ok, 200, [{"content-type", "application/json"}], %{url: "/headers", headers: headers}}
end) end)
|> expect(:stream_body, 2, fn %{url: url, headers: headers} = client -> |> expect(:stream_body, 2, fn %{url: url, headers: headers} = client ->
case Registry.lookup(Pleroma.ReverseProxy.ClientMock, url) do case Registry.lookup(ClientMock, url) do
[{_, 0}] -> [{_, 0}] ->
Registry.update_value(Pleroma.ReverseProxy.ClientMock, url, &(&1 + 1)) Registry.update_value(ClientMock, url, &(&1 + 1))
headers = for {k, v} <- headers, into: %{}, do: {String.capitalize(k), v} headers = for {k, v} <- headers, into: %{}, do: {String.capitalize(k), v}
{:ok, Jason.encode!(%{headers: headers}), client} {:ok, Jason.encode!(%{headers: headers}), client}
[{_, 1}] -> [{_, 1}] ->
Registry.unregister(Pleroma.ReverseProxy.ClientMock, url) Registry.unregister(ClientMock, url)
:done :done
end end
end) end)
@ -244,7 +247,7 @@ defp headers_mock(_) do
test "header passes", %{conn: conn} do test "header passes", %{conn: conn} do
conn = conn =
Plug.Conn.put_req_header( Conn.put_req_header(
conn, conn,
"accept", "accept",
"text/html" "text/html"
@ -257,7 +260,7 @@ test "header passes", %{conn: conn} do
test "header is filtered", %{conn: conn} do test "header is filtered", %{conn: conn} do
conn = conn =
Plug.Conn.put_req_header( Conn.put_req_header(
conn, conn,
"accept-language", "accept-language",
"en-US" "en-US"
@ -301,18 +304,18 @@ test "add cache-control", %{conn: conn} do
defp disposition_headers_mock(headers) do defp disposition_headers_mock(headers) do
ClientMock ClientMock
|> expect(:request, fn :get, "/disposition", _, _, _ -> |> expect(:request, fn :get, "/disposition", _, _, _ ->
Registry.register(Pleroma.ReverseProxy.ClientMock, "/disposition", 0) Registry.register(ClientMock, "/disposition", 0)
{:ok, 200, headers, %{url: "/disposition"}} {:ok, 200, headers, %{url: "/disposition"}}
end) end)
|> expect(:stream_body, 2, fn %{url: "/disposition"} = client -> |> expect(:stream_body, 2, fn %{url: "/disposition"} = client ->
case Registry.lookup(Pleroma.ReverseProxy.ClientMock, "/disposition") do case Registry.lookup(ClientMock, "/disposition") do
[{_, 0}] -> [{_, 0}] ->
Registry.update_value(Pleroma.ReverseProxy.ClientMock, "/disposition", &(&1 + 1)) Registry.update_value(ClientMock, "/disposition", &(&1 + 1))
{:ok, "", client} {:ok, "", client}
[{_, 1}] -> [{_, 1}] ->
Registry.unregister(Pleroma.ReverseProxy.ClientMock, "/disposition") Registry.unregister(ClientMock, "/disposition")
:done :done
end end
end) end)