summaryrefslogtreecommitdiff
path: root/test/plugs/rate_limit_plug_test.exs
blob: 2ec9a8fb7c3d2eaa6138dba681e64d594f28eda4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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