Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.7KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2019 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. test "disabled" do
  9. Pleroma.Config.put(RemoteIp, enabled: false)
  10. %{remote_ip: remote_ip} = conn(:get, "/")
  11. conn =
  12. conn(:get, "/")
  13. |> put_req_header("x-forwarded-for", "1.1.1.1")
  14. |> RemoteIp.call(nil)
  15. assert conn.remote_ip == remote_ip
  16. end
  17. test "enabled" do
  18. Pleroma.Config.put(RemoteIp, enabled: true)
  19. conn =
  20. conn(:get, "/")
  21. |> put_req_header("x-forwarded-for", "1.1.1.1")
  22. |> RemoteIp.call(nil)
  23. assert conn.remote_ip == {1, 1, 1, 1}
  24. end
  25. test "custom headers" do
  26. Pleroma.Config.put(RemoteIp, enabled: true, headers: ["cf-connecting-ip"])
  27. conn =
  28. conn(:get, "/")
  29. |> put_req_header("x-forwarded-for", "1.1.1.1")
  30. |> RemoteIp.call(nil)
  31. refute conn.remote_ip == {1, 1, 1, 1}
  32. conn =
  33. conn(:get, "/")
  34. |> put_req_header("cf-connecting-ip", "1.1.1.1")
  35. |> RemoteIp.call(nil)
  36. assert conn.remote_ip == {1, 1, 1, 1}
  37. end
  38. test "custom proxies" do
  39. Pleroma.Config.put(RemoteIp, enabled: true)
  40. conn =
  41. conn(:get, "/")
  42. |> put_req_header("x-forwarded-for", "173.245.48.1, 1.1.1.1, 173.245.48.2")
  43. |> RemoteIp.call(nil)
  44. refute conn.remote_ip == {1, 1, 1, 1}
  45. Pleroma.Config.put([RemoteIp, :proxies], ["173.245.48.0/20"])
  46. conn =
  47. conn(:get, "/")
  48. |> put_req_header("x-forwarded-for", "173.245.48.1, 1.1.1.1, 173.245.48.2")
  49. |> RemoteIp.call(nil)
  50. assert conn.remote_ip == {1, 1, 1, 1}
  51. end
  52. end