mirror of https://github.com/zulip/zulip.git
rate_limit: Rename key_fragment() method to key().
This commit is contained in:
parent
9c9f8100e7
commit
2c6b1fd575
|
@ -308,7 +308,7 @@ class RateLimitedPasswordResetByEmail(RateLimitedObject):
|
|||
def __str__(self) -> str:
|
||||
return "Email: {}".format(self.email)
|
||||
|
||||
def key_fragment(self) -> str:
|
||||
def key(self) -> str:
|
||||
return "{}:{}".format(type(self), self.email)
|
||||
|
||||
def rules(self) -> List[Tuple[int, int]]:
|
||||
|
|
|
@ -444,7 +444,7 @@ class RateLimitedRealmMirror(RateLimitedObject):
|
|||
self.realm = realm
|
||||
super().__init__()
|
||||
|
||||
def key_fragment(self) -> str:
|
||||
def key(self) -> str:
|
||||
return "emailmirror:{}:{}".format(type(self.realm), self.realm.id)
|
||||
|
||||
def rules(self) -> List[Tuple[int, int]]:
|
||||
|
|
|
@ -33,8 +33,8 @@ class RateLimitedObject(ABC):
|
|||
self.backend = RedisRateLimiterBackend
|
||||
|
||||
def get_keys(self) -> List[str]:
|
||||
key_fragment = self.key_fragment()
|
||||
return ["{}ratelimit:{}:{}".format(KEY_PREFIX, key_fragment, keytype)
|
||||
key = self.key()
|
||||
return ["{}ratelimit:{}:{}".format(KEY_PREFIX, key, keytype)
|
||||
for keytype in ['list', 'zset', 'block']]
|
||||
|
||||
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)
|
||||
|
||||
@abstractmethod
|
||||
def key_fragment(self) -> str:
|
||||
def key(self) -> str:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
|
@ -112,7 +112,7 @@ class RateLimitedUser(RateLimitedObject):
|
|||
def __str__(self) -> str:
|
||||
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)
|
||||
|
||||
def rules(self) -> List[Tuple[int, int]]:
|
||||
|
|
|
@ -484,20 +484,20 @@ class RateLimitAuthenticationTests(ZulipTestCase):
|
|||
Optional[UserProfile]],
|
||||
username: str, correct_password: str, wrong_password: str,
|
||||
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.
|
||||
original_key_fragment_method = RateLimitedAuthenticationByUsername.key_fragment
|
||||
original_key_method = RateLimitedAuthenticationByUsername.key
|
||||
salt = generate_random_token(32)
|
||||
|
||||
def _mock_key_fragment(self: RateLimitedAuthenticationByUsername) -> str:
|
||||
return "{}:{}".format(salt, original_key_fragment_method(self))
|
||||
def _mock_key(self: RateLimitedAuthenticationByUsername) -> str:
|
||||
return "{}:{}".format(salt, original_key_method(self))
|
||||
|
||||
def attempt_authentication(username: str, password: str) -> Optional[UserProfile]:
|
||||
request = HttpRequest()
|
||||
return attempt_authentication_func(request, username, password)
|
||||
|
||||
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:
|
||||
start_time = time.time()
|
||||
with mock.patch('time.time', return_value=start_time):
|
||||
|
|
|
@ -185,7 +185,7 @@ class RateLimitedAuthenticationByUsername(RateLimitedObject):
|
|||
def __str__(self) -> str:
|
||||
return "Username: {}".format(self.username)
|
||||
|
||||
def key_fragment(self) -> str:
|
||||
def key(self) -> str:
|
||||
return "{}:{}".format(type(self), self.username)
|
||||
|
||||
def rules(self) -> List[Tuple[int, int]]:
|
||||
|
|
Loading…
Reference in New Issue