logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

remote_ip_test.exs (1860B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Plugs.RemoteIpTest do
  5. use ExUnit.Case, async: true
  6. use Plug.Test
  7. alias Pleroma.Plugs.RemoteIp
  8. import Pleroma.Tests.Helpers, only: [clear_config: 1, clear_config: 2]
  9. setup do: clear_config(RemoteIp)
  10. test "disabled" do
  11. Pleroma.Config.put(RemoteIp, enabled: false)
  12. %{remote_ip: remote_ip} = conn(:get, "/")
  13. conn =
  14. conn(:get, "/")
  15. |> put_req_header("x-forwarded-for", "1.1.1.1")
  16. |> RemoteIp.call(nil)
  17. assert conn.remote_ip == remote_ip
  18. end
  19. test "enabled" do
  20. Pleroma.Config.put(RemoteIp, enabled: true)
  21. conn =
  22. conn(:get, "/")
  23. |> put_req_header("x-forwarded-for", "1.1.1.1")
  24. |> RemoteIp.call(nil)
  25. assert conn.remote_ip == {1, 1, 1, 1}
  26. end
  27. test "custom headers" do
  28. Pleroma.Config.put(RemoteIp, enabled: true, headers: ["cf-connecting-ip"])
  29. conn =
  30. conn(:get, "/")
  31. |> put_req_header("x-forwarded-for", "1.1.1.1")
  32. |> RemoteIp.call(nil)
  33. refute conn.remote_ip == {1, 1, 1, 1}
  34. conn =
  35. conn(:get, "/")
  36. |> put_req_header("cf-connecting-ip", "1.1.1.1")
  37. |> RemoteIp.call(nil)
  38. assert conn.remote_ip == {1, 1, 1, 1}
  39. end
  40. test "custom proxies" do
  41. Pleroma.Config.put(RemoteIp, enabled: true)
  42. conn =
  43. conn(:get, "/")
  44. |> put_req_header("x-forwarded-for", "173.245.48.1, 1.1.1.1, 173.245.48.2")
  45. |> RemoteIp.call(nil)
  46. refute conn.remote_ip == {1, 1, 1, 1}
  47. Pleroma.Config.put([RemoteIp, :proxies], ["173.245.48.0/20"])
  48. conn =
  49. conn(:get, "/")
  50. |> put_req_header("x-forwarded-for", "173.245.48.1, 1.1.1.1, 173.245.48.2")
  51. |> RemoteIp.call(nil)
  52. assert conn.remote_ip == {1, 1, 1, 1}
  53. end
  54. end