diff options
Diffstat (limited to 'test/plugs')
| -rw-r--r-- | test/plugs/cache_control_test.exs | 2 | ||||
| -rw-r--r-- | test/plugs/ensure_authenticated_plug_test.exs | 66 | ||||
| -rw-r--r-- | test/plugs/oauth_plug_test.exs | 2 | ||||
| -rw-r--r-- | test/plugs/rate_limiter_test.exs | 76 | 
4 files changed, 82 insertions, 64 deletions
diff --git a/test/plugs/cache_control_test.exs b/test/plugs/cache_control_test.exs index 005912ffb..6b567e81d 100644 --- a/test/plugs/cache_control_test.exs +++ b/test/plugs/cache_control_test.exs @@ -9,7 +9,7 @@ defmodule Pleroma.Web.CacheControlTest do    test "Verify Cache-Control header on static assets", %{conn: conn} do      conn = get(conn, "/index.html") -    assert Conn.get_resp_header(conn, "cache-control") == ["public max-age=86400 must-revalidate"] +    assert Conn.get_resp_header(conn, "cache-control") == ["public, no-cache"]    end    test "Verify Cache-Control header on the API", %{conn: conn} do diff --git a/test/plugs/ensure_authenticated_plug_test.exs b/test/plugs/ensure_authenticated_plug_test.exs index 18be5edd0..7f3559b83 100644 --- a/test/plugs/ensure_authenticated_plug_test.exs +++ b/test/plugs/ensure_authenticated_plug_test.exs @@ -8,24 +8,62 @@ defmodule Pleroma.Plugs.EnsureAuthenticatedPlugTest do    alias Pleroma.Plugs.EnsureAuthenticatedPlug    alias Pleroma.User -  test "it halts if no user is assigned", %{conn: conn} do -    conn = -      conn -      |> EnsureAuthenticatedPlug.call(%{}) +  describe "without :if_func / :unless_func options" do +    test "it halts if user is NOT assigned", %{conn: conn} do +      conn = EnsureAuthenticatedPlug.call(conn, %{}) -    assert conn.status == 403 -    assert conn.halted == true +      assert conn.status == 403 +      assert conn.halted == true +    end + +    test "it continues if a user is assigned", %{conn: conn} do +      conn = assign(conn, :user, %User{}) +      ret_conn = EnsureAuthenticatedPlug.call(conn, %{}) + +      assert ret_conn == conn +    end    end -  test "it continues if a user is assigned", %{conn: conn} do -    conn = -      conn -      |> assign(:user, %User{}) +  describe "with :if_func / :unless_func options" do +    setup do +      %{ +        true_fn: fn -> true end, +        false_fn: fn -> false end +      } +    end + +    test "it continues if a user is assigned", %{conn: conn, true_fn: true_fn, false_fn: false_fn} do +      conn = assign(conn, :user, %User{}) +      assert EnsureAuthenticatedPlug.call(conn, if_func: true_fn) == conn +      assert EnsureAuthenticatedPlug.call(conn, if_func: false_fn) == conn +      assert EnsureAuthenticatedPlug.call(conn, unless_func: true_fn) == conn +      assert EnsureAuthenticatedPlug.call(conn, unless_func: false_fn) == conn +    end + +    test "it continues if a user is NOT assigned but :if_func evaluates to `false`", +         %{conn: conn, false_fn: false_fn} do +      assert EnsureAuthenticatedPlug.call(conn, if_func: false_fn) == conn +    end + +    test "it continues if a user is NOT assigned but :unless_func evaluates to `true`", +         %{conn: conn, true_fn: true_fn} do +      assert EnsureAuthenticatedPlug.call(conn, unless_func: true_fn) == conn +    end + +    test "it halts if a user is NOT assigned and :if_func evaluates to `true`", +         %{conn: conn, true_fn: true_fn} do +      conn = EnsureAuthenticatedPlug.call(conn, if_func: true_fn) + +      assert conn.status == 403 +      assert conn.halted == true +    end -    ret_conn = -      conn -      |> EnsureAuthenticatedPlug.call(%{}) +    test "it halts if a user is NOT assigned and :unless_func evaluates to `false`", +         %{conn: conn, false_fn: false_fn} do +      conn = EnsureAuthenticatedPlug.call(conn, unless_func: false_fn) -    assert ret_conn == conn +      assert conn.status == 403 +      assert conn.halted == true +    end    end  end diff --git a/test/plugs/oauth_plug_test.exs b/test/plugs/oauth_plug_test.exs index 8534a5c13..f74c068cd 100644 --- a/test/plugs/oauth_plug_test.exs +++ b/test/plugs/oauth_plug_test.exs @@ -38,7 +38,7 @@ defmodule Pleroma.Plugs.OAuthPlugTest do      assert conn.assigns[:user] == opts[:user]    end -  test "with valid token(downcase) in url parameters, it assings the user", opts do +  test "with valid token(downcase) in url parameters, it assigns the user", opts do      conn =        :get        |> build_conn("/?access_token=#{opts[:token]}") diff --git a/test/plugs/rate_limiter_test.exs b/test/plugs/rate_limiter_test.exs index 8023271e4..81e2009c8 100644 --- a/test/plugs/rate_limiter_test.exs +++ b/test/plugs/rate_limiter_test.exs @@ -3,8 +3,7 @@  # SPDX-License-Identifier: AGPL-3.0-only  defmodule Pleroma.Plugs.RateLimiterTest do -  use ExUnit.Case, async: true -  use Plug.Test +  use Pleroma.Web.ConnCase    alias Pleroma.Config    alias Pleroma.Plugs.RateLimiter @@ -36,63 +35,44 @@ defmodule Pleroma.Plugs.RateLimiterTest do                 |> RateLimiter.init()                 |> RateLimiter.action_settings()      end +  end -    test "it is disabled for localhost" do -      Config.put([:rate_limit, @limiter_name], {1, 1}) -      Config.put([Pleroma.Web.Endpoint, :http, :ip], {127, 0, 0, 1}) -      Config.put([Pleroma.Plugs.RemoteIp, :enabled], false) - -      assert RateLimiter.disabled?() == true -    end +  test "it is disabled if it remote ip plug is enabled but no remote ip is found" do +    Config.put([Pleroma.Web.Endpoint, :http, :ip], {127, 0, 0, 1}) +    assert RateLimiter.disabled?(Plug.Conn.assign(build_conn(), :remote_ip_found, false)) +  end -    test "it is disabled for socket" do -      Config.put([:rate_limit, @limiter_name], {1, 1}) -      Config.put([Pleroma.Web.Endpoint, :http, :ip], {:local, "/path/to/pleroma.sock"}) -      Config.put([Pleroma.Plugs.RemoteIp, :enabled], false) +  test "it restricts based on config values" do +    limiter_name = :test_plug_opts +    scale = 80 +    limit = 5 -      assert RateLimiter.disabled?() == true -    end +    Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8}) +    Config.put([:rate_limit, limiter_name], {scale, limit}) -    test "it is enabled for socket when remote ip is enabled" do -      Config.put([:rate_limit, @limiter_name], {1, 1}) -      Config.put([Pleroma.Web.Endpoint, :http, :ip], {:local, "/path/to/pleroma.sock"}) -      Config.put([Pleroma.Plugs.RemoteIp, :enabled], true) +    plug_opts = RateLimiter.init(name: limiter_name) +    conn = conn(:get, "/") -      assert RateLimiter.disabled?() == false +    for i <- 1..5 do +      conn = RateLimiter.call(conn, plug_opts) +      assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts) +      Process.sleep(10)      end -    test "it restricts based on config values" do -      limiter_name = :test_plug_opts -      scale = 80 -      limit = 5 - -      Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8}) -      Config.put([:rate_limit, limiter_name], {scale, limit}) - -      plug_opts = RateLimiter.init(name: limiter_name) -      conn = conn(:get, "/") - -      for i <- 1..5 do -        conn = RateLimiter.call(conn, plug_opts) -        assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts) -        Process.sleep(10) -      end +    conn = RateLimiter.call(conn, plug_opts) +    assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests) +    assert conn.halted -      conn = RateLimiter.call(conn, plug_opts) -      assert %{"error" => "Throttled"} = Phoenix.ConnTest.json_response(conn, :too_many_requests) -      assert conn.halted +    Process.sleep(50) -      Process.sleep(50) +    conn = conn(:get, "/") -      conn = conn(:get, "/") +    conn = RateLimiter.call(conn, plug_opts) +    assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts) -      conn = RateLimiter.call(conn, plug_opts) -      assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts) - -      refute conn.status == Plug.Conn.Status.code(:too_many_requests) -      refute conn.resp_body -      refute conn.halted -    end +    refute conn.status == Plug.Conn.Status.code(:too_many_requests) +    refute conn.resp_body +    refute conn.halted    end    describe "options" do  | 
