1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.RateLimiterTest do
6 use ExUnit.Case, async: true
9 alias Pleroma.Plugs.RateLimiter
11 import Pleroma.Factory
13 # Note: each example must work with separate buckets in order to prevent concurrency issues
16 limiter_name = :test_init
17 Pleroma.Config.put([:rate_limit, limiter_name], {1, 1})
19 assert {limiter_name, {1, 1}, []} == RateLimiter.init(limiter_name)
20 assert nil == RateLimiter.init(:foo)
24 assert "127.0.0.1" == RateLimiter.ip(%{remote_ip: {127, 0, 0, 1}})
27 test "it restricts by opts" do
28 limiter_name = :test_opts
32 Pleroma.Config.put([:rate_limit, limiter_name], {scale, limit})
34 opts = RateLimiter.init(limiter_name)
35 conn = conn(:get, "/")
36 bucket_name = "#{limiter_name}:#{RateLimiter.ip(conn)}"
38 conn = RateLimiter.call(conn, opts)
39 assert {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
41 conn = RateLimiter.call(conn, opts)
42 assert {2, 3, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
44 conn = RateLimiter.call(conn, opts)
45 assert {3, 2, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
47 conn = RateLimiter.call(conn, opts)
48 assert {4, 1, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
50 conn = RateLimiter.call(conn, opts)
51 assert {5, 0, to_reset, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
53 conn = RateLimiter.call(conn, opts)
55 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
58 Process.sleep(to_reset)
60 conn = conn(:get, "/")
62 conn = RateLimiter.call(conn, opts)
63 assert {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
65 refute conn.status == Plug.Conn.Status.code(:too_many_requests)
70 test "`bucket_name` option overrides default bucket name" do
71 limiter_name = :test_bucket_name
75 Pleroma.Config.put([:rate_limit, limiter_name], {scale, limit})
76 base_bucket_name = "#{limiter_name}:group1"
77 opts = RateLimiter.init({limiter_name, bucket_name: base_bucket_name})
79 conn = conn(:get, "/")
80 default_bucket_name = "#{limiter_name}:#{RateLimiter.ip(conn)}"
81 customized_bucket_name = "#{base_bucket_name}:#{RateLimiter.ip(conn)}"
83 RateLimiter.call(conn, opts)
84 assert {1, 4, _, _, _} = ExRated.inspect_bucket(customized_bucket_name, scale, limit)
85 assert {0, 5, _, _, _} = ExRated.inspect_bucket(default_bucket_name, scale, limit)
88 test "`params` option appends specified params' values to bucket name" do
89 limiter_name = :test_params
93 Pleroma.Config.put([:rate_limit, limiter_name], {scale, limit})
94 opts = RateLimiter.init({limiter_name, params: ["id"]})
97 conn = conn(:get, "/?id=#{id}")
98 conn = Plug.Conn.fetch_query_params(conn)
100 default_bucket_name = "#{limiter_name}:#{RateLimiter.ip(conn)}"
101 parametrized_bucket_name = "#{limiter_name}:#{id}:#{RateLimiter.ip(conn)}"
103 RateLimiter.call(conn, opts)
104 assert {1, 4, _, _, _} = ExRated.inspect_bucket(parametrized_bucket_name, scale, limit)
105 assert {0, 5, _, _, _} = ExRated.inspect_bucket(default_bucket_name, scale, limit)
108 test "it supports combination of options modifying bucket name" do
109 limiter_name = :test_options_combo
113 Pleroma.Config.put([:rate_limit, limiter_name], {scale, limit})
114 base_bucket_name = "#{limiter_name}:group1"
115 opts = RateLimiter.init({limiter_name, bucket_name: base_bucket_name, params: ["id"]})
118 conn = conn(:get, "/?id=#{id}")
119 conn = Plug.Conn.fetch_query_params(conn)
121 default_bucket_name = "#{limiter_name}:#{RateLimiter.ip(conn)}"
122 parametrized_bucket_name = "#{base_bucket_name}:#{id}:#{RateLimiter.ip(conn)}"
124 RateLimiter.call(conn, opts)
125 assert {1, 4, _, _, _} = ExRated.inspect_bucket(parametrized_bucket_name, scale, limit)
126 assert {0, 5, _, _, _} = ExRated.inspect_bucket(default_bucket_name, scale, limit)
129 test "optional limits for authenticated users" do
130 limiter_name = :test_authenticated
131 Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
135 Pleroma.Config.put([:rate_limit, limiter_name], [{1, 10}, {scale, limit}])
137 opts = RateLimiter.init(limiter_name)
140 conn = conn(:get, "/") |> assign(:user, user)
141 bucket_name = "#{limiter_name}:#{user.id}"
143 conn = RateLimiter.call(conn, opts)
144 assert {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
146 conn = RateLimiter.call(conn, opts)
147 assert {2, 3, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
149 conn = RateLimiter.call(conn, opts)
150 assert {3, 2, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
152 conn = RateLimiter.call(conn, opts)
153 assert {4, 1, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
155 conn = RateLimiter.call(conn, opts)
156 assert {5, 0, to_reset, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
158 conn = RateLimiter.call(conn, opts)
160 assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests)
163 Process.sleep(to_reset)
165 conn = conn(:get, "/") |> assign(:user, user)
167 conn = RateLimiter.call(conn, opts)
168 assert {1, 4, _, _, _} = ExRated.inspect_bucket(bucket_name, scale, limit)
170 refute conn.status == Plug.Conn.Status.code(:too_many_requests)
171 refute conn.resp_body