Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel
[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 # Note: each example must work with separate buckets in order to prevent concurrency issues
14
15 describe "config" do
16 test "config is required for plug to work" do
17 limiter_name = :test_init
18 Pleroma.Config.put([:rate_limit, limiter_name], {1, 1})
19
20 assert %{limits: {1, 1}, name: :test_init, opts: [name: :test_init]} ==
21 RateLimiter.init(name: limiter_name)
22
23 assert nil == RateLimiter.init(name: :foo)
24 end
25
26 test "it restricts based on config values" do
27 limiter_name = :test_opts
28 scale = 80
29 limit = 5
30
31 Pleroma.Config.put([:rate_limit, limiter_name], {scale, limit})
32
33 opts = RateLimiter.init(name: limiter_name)
34 conn = conn(:get, "/")
35
36 for i <- 1..5 do
37 conn = RateLimiter.call(conn, opts)
38 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, opts)
39 Process.sleep(10)
40 end
41
42 conn = RateLimiter.call(conn, opts)
43 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
44 assert conn.halted
45
46 Process.sleep(50)
47
48 conn = conn(:get, "/")
49
50 conn = RateLimiter.call(conn, opts)
51 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, opts)
52
53 refute conn.status == Plug.Conn.Status.code(:too_many_requests)
54 refute conn.resp_body
55 refute conn.halted
56 end
57 end
58
59 describe "options" do
60 test "`bucket_name` option overrides default bucket name" do
61 limiter_name = :test_bucket_name
62
63 Pleroma.Config.put([:rate_limit, limiter_name], {1000, 5})
64
65 base_bucket_name = "#{limiter_name}:group1"
66 opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name)
67
68 conn = conn(:get, "/")
69
70 RateLimiter.call(conn, opts)
71 assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, opts)
72 assert {:err, :not_found} = RateLimiter.inspect_bucket(conn, limiter_name, opts)
73 end
74
75 test "`params` option allows different queries to be tracked independently" do
76 limiter_name = :test_params
77 Pleroma.Config.put([:rate_limit, limiter_name], {1000, 5})
78
79 opts = RateLimiter.init(name: limiter_name, params: ["id"])
80
81 conn = conn(:get, "/?id=1")
82 conn = Plug.Conn.fetch_query_params(conn)
83 conn_2 = conn(:get, "/?id=2")
84
85 RateLimiter.call(conn, opts)
86 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, opts)
87 assert {0, 5} = RateLimiter.inspect_bucket(conn_2, limiter_name, opts)
88 end
89
90 test "it supports combination of options modifying bucket name" do
91 limiter_name = :test_options_combo
92 Pleroma.Config.put([:rate_limit, limiter_name], {1000, 5})
93
94 base_bucket_name = "#{limiter_name}:group1"
95 opts = RateLimiter.init(name: limiter_name, bucket_name: base_bucket_name, params: ["id"])
96 id = "100"
97
98 conn = conn(:get, "/?id=#{id}")
99 conn = Plug.Conn.fetch_query_params(conn)
100 conn_2 = conn(:get, "/?id=#{101}")
101
102 RateLimiter.call(conn, opts)
103 assert {1, 4} = RateLimiter.inspect_bucket(conn, base_bucket_name, opts)
104 assert {0, 5} = RateLimiter.inspect_bucket(conn_2, base_bucket_name, opts)
105 end
106 end
107
108 describe "unauthenticated users" do
109 test "are restricted based on remote IP" do
110 limiter_name = :test_unauthenticated
111 Pleroma.Config.put([:rate_limit, limiter_name], [{1000, 5}, {1, 10}])
112
113 opts = RateLimiter.init(name: limiter_name)
114
115 conn = %{conn(:get, "/") | remote_ip: {127, 0, 0, 2}}
116 conn_2 = %{conn(:get, "/") | remote_ip: {127, 0, 0, 3}}
117
118 for i <- 1..5 do
119 conn = RateLimiter.call(conn, opts)
120 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, opts)
121 refute conn.halted
122 end
123
124 conn = RateLimiter.call(conn, opts)
125
126 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
127 assert conn.halted
128
129 conn_2 = RateLimiter.call(conn_2, opts)
130 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, opts)
131
132 refute conn_2.status == Plug.Conn.Status.code(:too_many_requests)
133 refute conn_2.resp_body
134 refute conn_2.halted
135 end
136 end
137
138 describe "authenticated users" do
139 setup do
140 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
141
142 :ok
143 end
144
145 test "can have limits seperate from unauthenticated connections" do
146 limiter_name = :test_authenticated
147
148 scale = 1000
149 limit = 5
150 Pleroma.Config.put([:rate_limit, limiter_name], [{1, 10}, {scale, limit}])
151
152 opts = RateLimiter.init(name: limiter_name)
153
154 user = insert(:user)
155 conn = conn(:get, "/") |> assign(:user, user)
156
157 for i <- 1..5 do
158 conn = RateLimiter.call(conn, opts)
159 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, opts)
160 refute conn.halted
161 end
162
163 conn = RateLimiter.call(conn, opts)
164
165 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
166 assert conn.halted
167
168 Process.sleep(1550)
169
170 conn = conn(:get, "/") |> assign(:user, user)
171 conn = RateLimiter.call(conn, opts)
172 assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, opts)
173
174 refute conn.status == Plug.Conn.Status.code(:too_many_requests)
175 refute conn.resp_body
176 refute conn.halted
177 end
178
179 test "diffrerent users are counted independently" do
180 limiter_name = :test_authenticated
181 Pleroma.Config.put([:rate_limit, limiter_name], [{1, 10}, {1000, 5}])
182
183 opts = RateLimiter.init(name: limiter_name)
184
185 user = insert(:user)
186 conn = conn(:get, "/") |> assign(:user, user)
187
188 user_2 = insert(:user)
189 conn_2 = conn(:get, "/") |> assign(:user, user_2)
190
191 for i <- 1..5 do
192 conn = RateLimiter.call(conn, opts)
193 assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, opts)
194 end
195
196 conn = RateLimiter.call(conn, opts)
197 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
198 assert conn.halted
199
200 conn_2 = RateLimiter.call(conn_2, opts)
201 assert {1, 4} = RateLimiter.inspect_bucket(conn_2, limiter_name, opts)
202 refute conn_2.status == Plug.Conn.Status.code(:too_many_requests)
203 refute conn_2.resp_body
204 refute conn_2.halted
205 end
206 end
207 end