Extend Pleroma.Pagination to support offset-based pagination, use async/await to...
[akkoma] / test / plugs / rate_limiter_test.exs
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
5 defmodule Pleroma.Plugs.RateLimiterTest do
6 use ExUnit.Case, async: true
7 use Plug.Test
8
9 alias Pleroma.Plugs.RateLimiter
10
11 import Pleroma.Factory
12
13 @limiter_name :testing
14
15 test "init/1" do
16 Pleroma.Config.put([:rate_limit, @limiter_name], {1, 1})
17
18 assert {@limiter_name, {1, 1}} == RateLimiter.init(@limiter_name)
19 assert nil == RateLimiter.init(:foo)
20 end
21
22 test "ip/1" do
23 assert "127.0.0.1" == RateLimiter.ip(%{remote_ip: {127, 0, 0, 1}})
24 end
25
26 test "it restricts by opts" do
27 scale = 1000
28 limit = 5
29
30 Pleroma.Config.put([:rate_limit, @limiter_name], {scale, limit})
31
32 opts = RateLimiter.init(@limiter_name)
33 conn = conn(:get, "/")
34 bucket_name = "#{@limiter_name}:#{RateLimiter.ip(conn)}"
35
36 conn = RateLimiter.call(conn, opts)
37 assert {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
38
39 conn = RateLimiter.call(conn, opts)
40 assert {2, 3, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
41
42 conn = RateLimiter.call(conn, opts)
43 assert {3, 2, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
44
45 conn = RateLimiter.call(conn, opts)
46 assert {4, 1, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
47
48 conn = RateLimiter.call(conn, opts)
49 assert {5, 0, to_reset, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
50
51 conn = RateLimiter.call(conn, opts)
52
53 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
54 assert conn.halted
55
56 Process.sleep(to_reset)
57
58 conn = conn(:get, "/")
59
60 conn = RateLimiter.call(conn, opts)
61 assert {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
62
63 refute conn.status == Plug.Conn.Status.code(:too_many_requests)
64 refute conn.resp_body
65 refute conn.halted
66 end
67
68 test "optional limits for authenticated users" do
69 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
70
71 scale = 1000
72 limit = 5
73 Pleroma.Config.put([:rate_limit, @limiter_name], [{1, 10}, {scale, limit}])
74
75 opts = RateLimiter.init(@limiter_name)
76
77 user = insert(:user)
78 conn = conn(:get, "/") |> assign(:user, user)
79 bucket_name = "#{@limiter_name}:#{user.id}"
80
81 conn = RateLimiter.call(conn, opts)
82 assert {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
83
84 conn = RateLimiter.call(conn, opts)
85 assert {2, 3, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
86
87 conn = RateLimiter.call(conn, opts)
88 assert {3, 2, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
89
90 conn = RateLimiter.call(conn, opts)
91 assert {4, 1, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
92
93 conn = RateLimiter.call(conn, opts)
94 assert {5, 0, to_reset, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
95
96 conn = RateLimiter.call(conn, opts)
97
98 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
99 assert conn.halted
100
101 Process.sleep(to_reset)
102
103 conn = conn(:get, "/") |> assign(:user, user)
104
105 conn = RateLimiter.call(conn, opts)
106 assert {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
107
108 refute conn.status == Plug.Conn.Status.code(:too_many_requests)
109 refute conn.resp_body
110 refute conn.halted
111 end
112 end