Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test: exercise real bearer resolvers and rate limiters (#43393)

Asuka Minatob9032c78b4cc
diff --git a/api/tests/unit_tests/libs/test_oauth_bearer.py b/api/tests/unit_tests/libs/test_oauth_bearer.py
index 1e71354dba3..ceed765bd4f 100644
--- a/api/tests/unit_tests/libs/test_oauth_bearer.py
+++ b/api/tests/unit_tests/libs/test_oauth_bearer.py
@@ -4,12 +4,13 @@ from __future__ import annotations
import uuid
from datetime import UTC, datetime
-from unittest.mock import MagicMock
import pytest
+from redis import Redis
+from sqlalchemy.orm import sessionmaker
from constants.oauth_bearer import Scope, SubjectType, TokenType
-from libs.oauth_bearer import ResolvedRow, _TokenTypeResolver
+from libs.oauth_bearer import OAuthAccessTokenResolver, ResolvedRow, _TokenTypeResolver
def _row(account_id: uuid.UUID | None) -> ResolvedRow:
@@ -26,7 +27,7 @@ def _row(account_id: uuid.UUID | None) -> ResolvedRow:
@pytest.mark.parametrize("token_type", list(TokenType), ids=lambda t: t.value)
def test_a_row_matches_its_subject_only_when_its_account_binding_agrees(token_type: TokenType) -> None:
"""Every token type, present or future, is held to the binding its subject declares."""
- resolver = _TokenTypeResolver(MagicMock(), token_type)
+ resolver = _TokenTypeResolver(OAuthAccessTokenResolver(sessionmaker(), Redis()), token_type)
bound = token_type.subject.bound_to_account
assert resolver._matches_subject(_row(uuid.uuid4())) is bound
assert resolver._matches_subject(_row(None)) is not bound
diff --git a/api/tests/unit_tests/libs/test_oauth_bearer_rate_limit_ordering.py b/api/tests/unit_tests/libs/test_oauth_bearer_rate_limit_ordering.py
index f345e7f146c..053a86cafde 100644
--- a/api/tests/unit_tests/libs/test_oauth_bearer_rate_limit_ordering.py
+++ b/api/tests/unit_tests/libs/test_oauth_bearer_rate_limit_ordering.py
@@ -1,24 +1,26 @@
from __future__ import annotations
-from unittest.mock import MagicMock, patch
+from unittest.mock import patch
import pytest
+from redis import Redis
+from sqlalchemy.orm import sessionmaker
from constants.oauth_bearer import TokenType
-from libs.oauth_bearer import BearerAuthenticator, InvalidBearerError
+from libs.oauth_bearer import BearerAuthenticator, InvalidBearerError, OAuthAccessTokenResolver, Resolver
-def _authenticator_with(resolver) -> BearerAuthenticator:
+def _authenticator_with(resolver: Resolver) -> BearerAuthenticator:
return BearerAuthenticator({TokenType.OAUTH_ACCOUNT: resolver})
@patch("libs.oauth_bearer.enforce_bearer_rate_limit")
-def test_rate_limit_called_before_resolve(rl):
+def test_rate_limit_called_before_resolve(rl, monkeypatch: pytest.MonkeyPatch):
call_order: list[str] = []
rl.side_effect = lambda _h: call_order.append("rl")
- resolver = MagicMock()
- resolver.resolve.side_effect = lambda _h: call_order.append("resolve") or None
- auth = _authenticator_with(resolver)
+ resolver = OAuthAccessTokenResolver(sessionmaker(), Redis())
+ monkeypatch.setattr(resolver, "cache_get", lambda _h: call_order.append("resolve") or "invalid")
+ auth = _authenticator_with(resolver.for_token_type(TokenType.OAUTH_ACCOUNT))
with pytest.raises(InvalidBearerError):
auth.authenticate("dfoa_xyz")
@@ -28,10 +30,10 @@ def test_rate_limit_called_before_resolve(rl):
@pytest.mark.parametrize("token", ["zzz_xyz", "dfoa_revoked"], ids=["unknown prefix", "revoked"])
@patch("libs.oauth_bearer.enforce_bearer_rate_limit")
-def test_every_refusal_raises_the_generic_invalid_bearer(rl, token: str):
- resolver = MagicMock()
- resolver.resolve.return_value = None
- auth = _authenticator_with(resolver)
+def test_every_refusal_raises_the_generic_invalid_bearer(rl, token: str, monkeypatch: pytest.MonkeyPatch):
+ resolver = OAuthAccessTokenResolver(sessionmaker(), Redis())
+ monkeypatch.setattr(resolver, "cache_get", lambda _h: "invalid")
+ auth = _authenticator_with(resolver.for_token_type(TokenType.OAUTH_ACCOUNT))
with pytest.raises(InvalidBearerError) as exc:
auth.authenticate(token)
assert str(exc.value) == "invalid_bearer"
diff --git a/api/tests/unit_tests/libs/test_rate_limit_bearer.py b/api/tests/unit_tests/libs/test_rate_limit_bearer.py
index f286bfd875d..3d20bff894e 100644
--- a/api/tests/unit_tests/libs/test_rate_limit_bearer.py
+++ b/api/tests/unit_tests/libs/test_rate_limit_bearer.py
@@ -3,9 +3,10 @@
from __future__ import annotations
from datetime import timedelta
-from unittest.mock import MagicMock, patch
+from unittest.mock import patch
import pytest
+from redis import Redis
from werkzeug.exceptions import TooManyRequests
from libs.helper import RateLimiter
@@ -18,8 +19,8 @@ from libs.rate_limit import (
@pytest.fixture
-def mock_redis():
- return MagicMock()
+def redis_client():
+ return Redis()
def test_limit_bearer_per_token_uses_60_per_minute_default():
@@ -27,48 +28,72 @@ def test_limit_bearer_per_token_uses_60_per_minute_default():
assert LIMIT_BEARER_PER_TOKEN.window == timedelta(minutes=1)
-def test_seconds_until_available_returns_remaining_window(mock_redis):
+def test_seconds_until_available_returns_remaining_window(redis_client, monkeypatch: pytest.MonkeyPatch):
"""ZSET oldest entry score = 100; window = 60s; now = 130s → remaining = 30s."""
- rl = RateLimiter("rl:bearer:token", max_attempts=60, time_window=60, redis_client=mock_redis)
- mock_redis.zrange.return_value = [(b"member-1", 100.0)]
+ rl = RateLimiter("rl:bearer:token", max_attempts=60, time_window=60, redis_client=redis_client)
+ monkeypatch.setattr(redis_client, "execute_command", lambda *_args, **_kwargs: [(b"member-1", 100.0)])
with patch("libs.helper.time.time", return_value=130):
assert rl.seconds_until_available("k1") == 30
-def test_seconds_until_available_floor_one_second(mock_redis):
+def test_seconds_until_available_floor_one_second(redis_client, monkeypatch: pytest.MonkeyPatch):
"""Even when math says <1s remaining, return at least 1 so client backs off measurably."""
- rl = RateLimiter("rl:bearer:token", max_attempts=60, time_window=60, redis_client=mock_redis)
- mock_redis.zrange.return_value = [(b"member-1", 119.5)]
+ rl = RateLimiter("rl:bearer:token", max_attempts=60, time_window=60, redis_client=redis_client)
+ monkeypatch.setattr(redis_client, "execute_command", lambda *_args, **_kwargs: [(b"member-1", 119.5)])
with patch("libs.helper.time.time", return_value=180):
# window expired (180 > 119.5+60=179.5 by 0.5s) — bucket is actually free now
# but this method only called when is_rate_limited() == True; defensive floor.
assert rl.seconds_until_available("k1") >= 1
-def test_seconds_until_available_empty_bucket(mock_redis):
+def test_seconds_until_available_empty_bucket(redis_client, monkeypatch: pytest.MonkeyPatch):
"""No entries → 1s sentinel (defensive; should not be reached when limited)."""
- rl = RateLimiter("rl:bearer:token", max_attempts=60, time_window=60, redis_client=mock_redis)
- mock_redis.zrange.return_value = []
+ rl = RateLimiter("rl:bearer:token", max_attempts=60, time_window=60, redis_client=redis_client)
+ monkeypatch.setattr(redis_client, "execute_command", lambda *_args, **_kwargs: [])
assert rl.seconds_until_available("k1") == 1
@patch("libs.rate_limit._build_limiter")
-def test_enforce_bearer_rate_limit_passes_under_limit(mock_build):
- limiter = MagicMock()
- limiter.is_rate_limited.return_value = False
+def test_enforce_bearer_rate_limit_passes_under_limit(mock_build, redis_client, monkeypatch: pytest.MonkeyPatch):
+ commands: list[tuple] = []
+
+ def execute_command(*args, **_kwargs):
+ commands.append(args)
+ return 0
+
+ monkeypatch.setattr(redis_client, "execute_command", execute_command)
+ limiter = RateLimiter(
+ "rl:bearer:token", max_attempts=60, time_window=60, member_factory=lambda: "member-1", redis_client=redis_client
+ )
mock_build.return_value = limiter
- enforce_bearer_rate_limit("hash-1")
- limiter.increment_rate_limit.assert_called_once_with("token:hash-1")
+ with patch("libs.helper.time.time", return_value=100):
+ enforce_bearer_rate_limit("hash-1")
+ assert commands == [
+ ("ZREMRANGEBYSCORE", "rl:bearer:token:token:hash-1", "-inf", 40),
+ ("ZCARD", "rl:bearer:token:token:hash-1"),
+ ("ZADD", "rl:bearer:token:token:hash-1", 100, "member-1"),
+ ("EXPIRE", "rl:bearer:token:token:hash-1", 120),
+ ]
@patch("libs.rate_limit._build_limiter")
-def test_enforce_bearer_rate_limit_raises_429_with_retry_after(mock_build):
- limiter = MagicMock()
- limiter.is_rate_limited.return_value = True
- limiter.seconds_until_available.return_value = 23
+def test_enforce_bearer_rate_limit_raises_429_with_retry_after(
+ mock_build, redis_client, monkeypatch: pytest.MonkeyPatch
+):
+ commands: list[tuple] = []
+
+ def execute_command(*args, **_kwargs):
+ commands.append(args)
+ if args[0] == "ZRANGE":
+ return [(b"member-1", 63.0)]
+ return 60 if args[0] == "ZCARD" else 0
+
+ monkeypatch.setattr(redis_client, "execute_command", execute_command)
+ limiter = RateLimiter("rl:bearer:token", max_attempts=60, time_window=60, redis_client=redis_client)
mock_build.return_value = limiter
- with pytest.raises(TooManyRequests) as exc:
+ with patch("libs.helper.time.time", return_value=100), pytest.raises(TooManyRequests) as exc:
enforce_bearer_rate_limit("hash-1")
+ assert [command[0] for command in commands] == ["ZREMRANGEBYSCORE", "ZCARD", "ZRANGE"]
# Header-only TooManyRequests: the canonical ErrorBody (code "too_many_requests") is built
# later by the openapi formatter; here we only assert the advisory header rides along.
assert dict(exc.value.headers).get("Retry-After") == "23"