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
Pleroma.GunMock
|> stub(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(1000) end) end)
|> expect(:await_up, fn _, _ -> {:ok, :http} end)
|> expect(:set_owner, fn _, _ -> :ok end)
|> stub(:await_up, fn _, _ -> {:ok, :http} end)
|> stub(:set_owner, fn _, _ -> :ok end)
end
describe "options/1" do

View file

@ -7,13 +7,10 @@ defmodule Pleroma.HTTP.ConnectionTest do
use Pleroma.Tests.Helpers
import ExUnit.CaptureLog
import Mox
alias Pleroma.Config
alias Pleroma.HTTP.Connection
setup :verify_on_exit!
describe "parse_host/1" do
test "as atom to charlist" do
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}
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

View file

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

View file

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