1 defmodule Pleroma.Plugs.RateLimitPlugTest do
2 use ExUnit.Case, async: true
5 alias Pleroma.Plugs.RateLimitPlug
7 @opts RateLimitPlug.init(%{max_requests: 5, interval: 1})
10 enabled = Pleroma.Config.get([:app_account_creation, :enabled])
12 Pleroma.Config.put([:app_account_creation, :enabled], true)
15 Pleroma.Config.put([:app_account_creation, :enabled], enabled)
21 test "it restricts by opts" do
22 conn = conn(:get, "/")
23 bucket_name = conn.remote_ip |> Tuple.to_list() |> Enum.join(".")
26 conn = RateLimitPlug.call(conn, @opts)
27 {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5)
28 conn = RateLimitPlug.call(conn, @opts)
29 {2, 3, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5)
30 conn = RateLimitPlug.call(conn, @opts)
31 {3, 2, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5)
32 conn = RateLimitPlug.call(conn, @opts)
33 {4, 1, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5)
34 conn = RateLimitPlug.call(conn, @opts)
35 {5, 0, to_reset, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5)
36 conn = RateLimitPlug.call(conn, @opts)
37 assert conn.status == 403
39 assert conn.resp_body == "{\"error\":\"Rate limit exceeded.\"}"
41 Process.sleep(to_reset)
43 conn = conn(:get, "/")
44 conn = RateLimitPlug.call(conn, @opts)
45 {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, ms, 5)
46 refute conn.status == 403