summaryrefslogtreecommitdiff
path: root/test/plugs/remote_ip_test.exs
diff options
context:
space:
mode:
authorMaksim Pechnikov <parallel588@gmail.com>2019-09-28 10:32:03 +0300
committerMaksim Pechnikov <parallel588@gmail.com>2019-09-28 10:36:04 +0300
commit1053319cd64a0eab40fab6dc9ce3a1b78711069b (patch)
tree3d613f7c6129f667ea16aedfe46259d9ca6bde29 /test/plugs/remote_ip_test.exs
parent73ae38ca04df02656bfb239ceba4ffe64879e927 (diff)
parent50ab06435353144582f6afbf37402aef13c2b3f1 (diff)
downloadpleroma-1053319cd64a0eab40fab6dc9ce3a1b78711069b.tar.gz
pleroma-1053319cd64a0eab40fab6dc9ce3a1b78711069b.zip
Merge branch 'develop' into tests/mastodon_api_controller.ex
Diffstat (limited to 'test/plugs/remote_ip_test.exs')
-rw-r--r--test/plugs/remote_ip_test.exs72
1 files changed, 72 insertions, 0 deletions
diff --git a/test/plugs/remote_ip_test.exs b/test/plugs/remote_ip_test.exs
new file mode 100644
index 000000000..d120c588b
--- /dev/null
+++ b/test/plugs/remote_ip_test.exs
@@ -0,0 +1,72 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Plugs.RemoteIpTest do
+ use ExUnit.Case, async: true
+ use Plug.Test
+
+ alias Pleroma.Plugs.RemoteIp
+
+ test "disabled" do
+ Pleroma.Config.put(RemoteIp, enabled: false)
+
+ %{remote_ip: remote_ip} = conn(:get, "/")
+
+ conn =
+ conn(:get, "/")
+ |> put_req_header("x-forwarded-for", "1.1.1.1")
+ |> RemoteIp.call(nil)
+
+ assert conn.remote_ip == remote_ip
+ end
+
+ test "enabled" do
+ Pleroma.Config.put(RemoteIp, enabled: true)
+
+ conn =
+ conn(:get, "/")
+ |> put_req_header("x-forwarded-for", "1.1.1.1")
+ |> RemoteIp.call(nil)
+
+ assert conn.remote_ip == {1, 1, 1, 1}
+ end
+
+ test "custom headers" do
+ Pleroma.Config.put(RemoteIp, enabled: true, headers: ["cf-connecting-ip"])
+
+ conn =
+ conn(:get, "/")
+ |> put_req_header("x-forwarded-for", "1.1.1.1")
+ |> RemoteIp.call(nil)
+
+ refute conn.remote_ip == {1, 1, 1, 1}
+
+ conn =
+ conn(:get, "/")
+ |> put_req_header("cf-connecting-ip", "1.1.1.1")
+ |> RemoteIp.call(nil)
+
+ assert conn.remote_ip == {1, 1, 1, 1}
+ end
+
+ test "custom proxies" do
+ Pleroma.Config.put(RemoteIp, enabled: true)
+
+ conn =
+ conn(:get, "/")
+ |> put_req_header("x-forwarded-for", "173.245.48.1, 1.1.1.1, 173.245.48.2")
+ |> RemoteIp.call(nil)
+
+ refute conn.remote_ip == {1, 1, 1, 1}
+
+ Pleroma.Config.put([RemoteIp, :proxies], ["173.245.48.0/20"])
+
+ conn =
+ conn(:get, "/")
+ |> put_req_header("x-forwarded-for", "173.245.48.1, 1.1.1.1, 173.245.48.2")
+ |> RemoteIp.call(nil)
+
+ assert conn.remote_ip == {1, 1, 1, 1}
+ end
+end