mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 21:43:21 +00:00
rate_limit: Rename key_fragment() method to key().
This commit is contained in:
committed by
Tim Abbott
parent
9c9f8100e7
commit
2c6b1fd575
@@ -308,7 +308,7 @@ class RateLimitedPasswordResetByEmail(RateLimitedObject):
|
|||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return "Email: {}".format(self.email)
|
return "Email: {}".format(self.email)
|
||||||
|
|
||||||
def key_fragment(self) -> str:
|
def key(self) -> str:
|
||||||
return "{}:{}".format(type(self), self.email)
|
return "{}:{}".format(type(self), self.email)
|
||||||
|
|
||||||
def rules(self) -> List[Tuple[int, int]]:
|
def rules(self) -> List[Tuple[int, int]]:
|
||||||
|
|||||||
@@ -444,7 +444,7 @@ class RateLimitedRealmMirror(RateLimitedObject):
|
|||||||
self.realm = realm
|
self.realm = realm
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def key_fragment(self) -> str:
|
def key(self) -> str:
|
||||||
return "emailmirror:{}:{}".format(type(self.realm), self.realm.id)
|
return "emailmirror:{}:{}".format(type(self.realm), self.realm.id)
|
||||||
|
|
||||||
def rules(self) -> List[Tuple[int, int]]:
|
def rules(self) -> List[Tuple[int, int]]:
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ class RateLimitedObject(ABC):
|
|||||||
self.backend = RedisRateLimiterBackend
|
self.backend = RedisRateLimiterBackend
|
||||||
|
|
||||||
def get_keys(self) -> List[str]:
|
def get_keys(self) -> List[str]:
|
||||||
key_fragment = self.key_fragment()
|
key = self.key()
|
||||||
return ["{}ratelimit:{}:{}".format(KEY_PREFIX, key_fragment, keytype)
|
return ["{}ratelimit:{}:{}".format(KEY_PREFIX, key, keytype)
|
||||||
for keytype in ['list', 'zset', 'block']]
|
for keytype in ['list', 'zset', 'block']]
|
||||||
|
|
||||||
def rate_limit(self) -> Tuple[bool, float]:
|
def rate_limit(self) -> Tuple[bool, float]:
|
||||||
@@ -92,7 +92,7 @@ class RateLimitedObject(ABC):
|
|||||||
return self.backend.get_api_calls_left(self, max_window, max_calls)
|
return self.backend.get_api_calls_left(self, max_window, max_calls)
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def key_fragment(self) -> str:
|
def key(self) -> str:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
@@ -112,7 +112,7 @@ class RateLimitedUser(RateLimitedObject):
|
|||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return "Id: {}".format(self.user.id)
|
return "Id: {}".format(self.user.id)
|
||||||
|
|
||||||
def key_fragment(self) -> str:
|
def key(self) -> str:
|
||||||
return "{}:{}:{}".format(type(self.user), self.user.id, self.domain)
|
return "{}:{}:{}".format(type(self.user), self.user.id, self.domain)
|
||||||
|
|
||||||
def rules(self) -> List[Tuple[int, int]]:
|
def rules(self) -> List[Tuple[int, int]]:
|
||||||
|
|||||||
@@ -484,20 +484,20 @@ class RateLimitAuthenticationTests(ZulipTestCase):
|
|||||||
Optional[UserProfile]],
|
Optional[UserProfile]],
|
||||||
username: str, correct_password: str, wrong_password: str,
|
username: str, correct_password: str, wrong_password: str,
|
||||||
expected_user_profile: UserProfile) -> None:
|
expected_user_profile: UserProfile) -> None:
|
||||||
# We have to mock RateLimitedAuthenticationByUsername.key_fragment to avoid key collisions
|
# We have to mock RateLimitedAuthenticationByUsername.key to avoid key collisions
|
||||||
# if tests run in parallel.
|
# if tests run in parallel.
|
||||||
original_key_fragment_method = RateLimitedAuthenticationByUsername.key_fragment
|
original_key_method = RateLimitedAuthenticationByUsername.key
|
||||||
salt = generate_random_token(32)
|
salt = generate_random_token(32)
|
||||||
|
|
||||||
def _mock_key_fragment(self: RateLimitedAuthenticationByUsername) -> str:
|
def _mock_key(self: RateLimitedAuthenticationByUsername) -> str:
|
||||||
return "{}:{}".format(salt, original_key_fragment_method(self))
|
return "{}:{}".format(salt, original_key_method(self))
|
||||||
|
|
||||||
def attempt_authentication(username: str, password: str) -> Optional[UserProfile]:
|
def attempt_authentication(username: str, password: str) -> Optional[UserProfile]:
|
||||||
request = HttpRequest()
|
request = HttpRequest()
|
||||||
return attempt_authentication_func(request, username, password)
|
return attempt_authentication_func(request, username, password)
|
||||||
|
|
||||||
add_ratelimit_rule(10, 2, domain='authenticate_by_username')
|
add_ratelimit_rule(10, 2, domain='authenticate_by_username')
|
||||||
with mock.patch.object(RateLimitedAuthenticationByUsername, 'key_fragment', new=_mock_key_fragment):
|
with mock.patch.object(RateLimitedAuthenticationByUsername, 'key', new=_mock_key):
|
||||||
try:
|
try:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
with mock.patch('time.time', return_value=start_time):
|
with mock.patch('time.time', return_value=start_time):
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ class RateLimitedAuthenticationByUsername(RateLimitedObject):
|
|||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return "Username: {}".format(self.username)
|
return "Username: {}".format(self.username)
|
||||||
|
|
||||||
def key_fragment(self) -> str:
|
def key(self) -> str:
|
||||||
return "{}:{}".format(type(self), self.username)
|
return "{}:{}".format(type(self), self.username)
|
||||||
|
|
||||||
def rules(self) -> List[Tuple[int, int]]:
|
def rules(self) -> List[Tuple[int, int]]:
|
||||||
|
|||||||
Reference in New Issue
Block a user