Merge branch 'fix/1658-invite-send' into 'develop'
[akkoma] / test / http / adapter_helper / gun_test.exs
index bc7e3f0e03d324102c005be4801d43e4010df832..2e961826ede9fb72fdbb05be211799b70c77d79b 100644 (file)
 defmodule Pleroma.HTTP.AdapterHelper.GunTest do
   use ExUnit.Case, async: true
   use Pleroma.Tests.Helpers
-  import ExUnit.CaptureLog
+
+  import Mox
+
   alias Pleroma.Config
   alias Pleroma.Gun.Conn
   alias Pleroma.HTTP.AdapterHelper.Gun
   alias Pleroma.Pool.Connections
 
-  setup_all do
-    {:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.Gun.API.Mock)
+  setup :verify_on_exit!
+
+  defp gun_mock(_) do
+    gun_mock()
     :ok
   end
 
+  defp gun_mock do
+    Pleroma.GunMock
+    |> stub(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(1000) end) end)
+    |> stub(:await_up, fn _, _ -> {:ok, :http} end)
+    |> stub(:set_owner, fn _, _ -> :ok end)
+  end
+
   describe "options/1" do
-    clear_config([:http, :adapter]) do
-      Config.put([:http, :adapter], a: 1, b: 2)
-    end
+    setup do: clear_config([:http, :adapter], a: 1, b: 2)
 
     test "https url with default port" do
       uri = URI.parse("https://example.com")
 
-      opts = Gun.options(uri)
+      opts = Gun.options([receive_conn: false], uri)
       assert opts[:certificates_verification]
-      tls_opts = opts[:tls_opts]
-      assert tls_opts[:verify] == :verify_peer
-      assert tls_opts[:depth] == 20
-      assert tls_opts[:reuse_sessions] == false
-
-      assert tls_opts[:verify_fun] ==
-               {&:ssl_verify_hostname.verify_fun/3, [check_hostname: 'example.com']}
-
-      assert File.exists?(tls_opts[:cacertfile])
-
-      assert opts[:original] == "example.com:443"
+      assert opts[:tls_opts][:log_level] == :warning
     end
 
     test "https ipv4 with default port" do
       uri = URI.parse("https://127.0.0.1")
 
-      opts = Gun.options(uri)
-
-      assert opts[:tls_opts][:verify_fun] ==
-               {&:ssl_verify_hostname.verify_fun/3, [check_hostname: '127.0.0.1']}
-
-      assert opts[:original] == "127.0.0.1:443"
+      opts = Gun.options([receive_conn: false], uri)
+      assert opts[:certificates_verification]
+      assert opts[:tls_opts][:log_level] == :warning
     end
 
     test "https ipv6 with default port" do
       uri = URI.parse("https://[2a03:2880:f10c:83:face:b00c:0:25de]")
 
-      opts = Gun.options(uri)
-
-      assert opts[:tls_opts][:verify_fun] ==
-               {&:ssl_verify_hostname.verify_fun/3,
-                [check_hostname: '2a03:2880:f10c:83:face:b00c:0:25de']}
-
-      assert opts[:original] == "2a03:2880:f10c:83:face:b00c:0:25de:443"
+      opts = Gun.options([receive_conn: false], uri)
+      assert opts[:certificates_verification]
+      assert opts[:tls_opts][:log_level] == :warning
     end
 
     test "https url with non standart port" do
       uri = URI.parse("https://example.com:115")
 
-      opts = Gun.options(uri)
+      opts = Gun.options([receive_conn: false], uri)
 
       assert opts[:certificates_verification]
-      assert opts[:transport] == :tls
-    end
-
-    test "receive conn by default" do
-      uri = URI.parse("http://another-domain.com")
-      :ok = Conn.open(uri, :gun_connections)
-
-      received_opts = Gun.options(uri)
-      assert received_opts[:close_conn] == false
-      assert is_pid(received_opts[:conn])
-    end
-
-    test "don't receive conn if receive_conn is false" do
-      uri = URI.parse("http://another-domain2.com")
-      :ok = Conn.open(uri, :gun_connections)
-
-      opts = [receive_conn: false]
-      received_opts = Gun.options(opts, uri)
-      assert received_opts[:close_conn] == nil
-      assert received_opts[:conn] == nil
     end
 
     test "get conn on next request" do
+      gun_mock()
       level = Application.get_env(:logger, :level)
       Logger.configure(level: :debug)
       on_exit(fn -> Logger.configure(level: level) end)
       uri = URI.parse("http://some-domain2.com")
 
-      assert capture_log(fn ->
-               opts = Gun.options(uri)
+      opts = Gun.options(uri)
 
-               assert opts[:conn] == nil
-               assert opts[:close_conn] == nil
-             end) =~
-               "Gun connections pool checkin was not successful. Trying to open conn for next request."
+      assert opts[:conn] == nil
+      assert opts[:close_conn] == nil
 
+      Process.sleep(50)
       opts = Gun.options(uri)
 
       assert is_pid(opts[:conn])
@@ -111,12 +82,13 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
     end
 
     test "merges with defaul http adapter config" do
-      defaults = Gun.options(URI.parse("https://example.com"))
+      defaults = Gun.options([receive_conn: false], URI.parse("https://example.com"))
       assert Keyword.has_key?(defaults, :a)
       assert Keyword.has_key?(defaults, :b)
     end
 
     test "default ssl adapter opts with connection" do
+      gun_mock()
       uri = URI.parse("https://some-domain.com")
 
       :ok = Conn.open(uri, :gun_connections)
@@ -124,12 +96,8 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
       opts = Gun.options(uri)
 
       assert opts[:certificates_verification]
-      tls_opts = opts[:tls_opts]
-      assert tls_opts[:verify] == :verify_peer
-      assert tls_opts[:depth] == 20
-      assert tls_opts[:reuse_sessions] == false
+      refute opts[:tls_opts] == []
 
-      assert opts[:original] == "some-domain.com:443"
       assert opts[:close_conn] == false
       assert is_pid(opts[:conn])
     end
@@ -165,7 +133,32 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
     end
   end
 
+  describe "options/1 with receive_conn parameter" do
+    setup :gun_mock
+
+    test "receive conn by default" do
+      uri = URI.parse("http://another-domain.com")
+      :ok = Conn.open(uri, :gun_connections)
+
+      received_opts = Gun.options(uri)
+      assert received_opts[:close_conn] == false
+      assert is_pid(received_opts[:conn])
+    end
+
+    test "don't receive conn if receive_conn is false" do
+      uri = URI.parse("http://another-domain.com")
+      :ok = Conn.open(uri, :gun_connections)
+
+      opts = [receive_conn: false]
+      received_opts = Gun.options(opts, uri)
+      assert received_opts[:close_conn] == nil
+      assert received_opts[:conn] == nil
+    end
+  end
+
   describe "after_request/1" do
+    setup :gun_mock
+
     test "body_as not chunks" do
       uri = URI.parse("http://some-domain.com")
       :ok = Conn.open(uri, :gun_connections)
@@ -230,7 +223,6 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
       uri = URI.parse("http://127.0.0.1")
       :ok = Conn.open(uri, :gun_connections)
       opts = Gun.options(uri)
-      send(:gun_connections, {:gun_up, opts[:conn], :http})
       :ok = Gun.after_request(opts)
       conn = opts[:conn]
 
@@ -249,7 +241,6 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
       uri = URI.parse("http://[2a03:2880:f10c:83:face:b00c:0:25de]")
       :ok = Conn.open(uri, :gun_connections)
       opts = Gun.options(uri)
-      send(:gun_connections, {:gun_up, opts[:conn], :http})
       :ok = Gun.after_request(opts)
       conn = opts[:conn]
 
@@ -264,23 +255,4 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
              } = Connections.get_state(:gun_connections)
     end
   end
-
-  describe "format_host/1" do
-    test "with domain" do
-      assert Gun.format_host("example.com") == 'example.com'
-    end
-
-    test "with idna domain" do
-      assert Gun.format_host("ですexample.com") == 'xn--example-183fne.com'
-    end
-
-    test "with ipv4" do
-      assert Gun.format_host("127.0.0.1") == '127.0.0.1'
-    end
-
-    test "with ipv6" do
-      assert Gun.format_host("2a03:2880:f10c:83:face:b00c:0:25de") ==
-               '2a03:2880:f10c:83:face:b00c:0:25de'
-    end
-  end
 end