diff options
author | kaniini <nenolod@gmail.com> | 2019-05-13 18:35:45 +0000 |
---|---|---|
committer | kaniini <nenolod@gmail.com> | 2019-05-13 18:35:45 +0000 |
commit | f3e8f5b1f208b10130c7123e68af1e38575f180b (patch) | |
tree | 523d1f1cfa399f4ee6d841ba3098ecd87d8e67e7 /test/plugs | |
parent | 5a4d55cf910f85b07f111972647a8b4410b5eb6b (diff) | |
parent | a2be420f940fb8f181feeb9b0fb9759d433dcae1 (diff) | |
download | pleroma-f3e8f5b1f208b10130c7123e68af1e38575f180b.tar.gz pleroma-f3e8f5b1f208b10130c7123e68af1e38575f180b.zip |
Merge branch 'features/mastoapi/2.7.0-registration' into 'develop'
Features/mastoapi/2.7.0 registration
Closes #773
See merge request pleroma/pleroma!1134
Diffstat (limited to 'test/plugs')
-rw-r--r-- | test/plugs/rate_limit_plug_test.exs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/test/plugs/rate_limit_plug_test.exs b/test/plugs/rate_limit_plug_test.exs new file mode 100644 index 000000000..2ec9a8fb7 --- /dev/null +++ b/test/plugs/rate_limit_plug_test.exs @@ -0,0 +1,50 @@ +defmodule Pleroma.Plugs.RateLimitPlugTest do + use ExUnit.Case, async: true + use Plug.Test + + alias Pleroma.Plugs.RateLimitPlug + + @opts RateLimitPlug.init(%{max_requests: 5, interval: 1}) + + setup do + enabled = Pleroma.Config.get([:app_account_creation, :enabled]) + + Pleroma.Config.put([:app_account_creation, :enabled], true) + + on_exit(fn -> + Pleroma.Config.put([:app_account_creation, :enabled], enabled) + end) + + :ok + end + + test "it restricts by opts" do + conn = conn(:get, "/") + bucket_name = conn.remote_ip |> Tuple.to_list() |> Enum.join(".") + ms = 1000 + + conn = RateLimitPlug.call(conn, @opts) + {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) + conn = RateLimitPlug.call(conn, @opts) + {2, 3, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) + conn = RateLimitPlug.call(conn, @opts) + {3, 2, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) + conn = RateLimitPlug.call(conn, @opts) + {4, 1, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) + conn = RateLimitPlug.call(conn, @opts) + {5, 0, to_reset, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) + conn = RateLimitPlug.call(conn, @opts) + assert conn.status == 403 + assert conn.halted + assert conn.resp_body == "{\"error\":\"Rate limit exceeded.\"}" + + Process.sleep(to_reset) + + conn = conn(:get, "/") + conn = RateLimitPlug.call(conn, @opts) + {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5) + refute conn.status == 403 + refute conn.halted + refute conn.resp_body + end +end |