Files
upstream-zulip/zerver/tests/test_redis_utils.py
upstream-mirror 64898b2c79
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
2026-09-15 15:05:30 +05:00

78 lines
2.6 KiB
Python

from unittest import mock
from redis import StrictRedis
from typing_extensions import override
from zerver.lib.redis_utils import (
MAX_KEY_LENGTH,
ZulipRedisKeyOfWrongFormatError,
ZulipRedisKeyTooLongError,
get_dict_from_redis,
get_redis_client,
put_dict_in_redis,
)
from zerver.lib.test_classes import ZulipTestCase
class RedisUtilsTest(ZulipTestCase):
key_format = "test_redis_utils_{token}"
expiration_seconds = 60
redis_client: "StrictRedis[bytes]"
@classmethod
@override
def setUpClass(cls) -> None:
cls.redis_client = get_redis_client()
return super().setUpClass()
def test_put_and_get_data(self) -> None:
data = {
"a": 1,
"b": "some value",
}
key = put_dict_in_redis(
self.redis_client, self.key_format, data, expiration_seconds=self.expiration_seconds
)
retrieved_data = get_dict_from_redis(self.redis_client, self.key_format, key)
self.assertEqual(data, retrieved_data)
def test_put_data_key_length_check(self) -> None:
data = {
"a": 1,
"b": "some value",
}
max_valid_token_length = MAX_KEY_LENGTH - (len(self.key_format) - len("{token}"))
key = put_dict_in_redis(
self.redis_client,
self.key_format,
data,
expiration_seconds=self.expiration_seconds,
token_length=max_valid_token_length,
)
retrieved_data = get_dict_from_redis(self.redis_client, self.key_format, key)
self.assertEqual(data, retrieved_data)
# Trying to put data under an overly long key should get stopped before even
# generating the random token.
with mock.patch("secrets.token_hex") as mock_generate:
with self.assertRaises(ZulipRedisKeyTooLongError):
put_dict_in_redis(
self.redis_client,
self.key_format,
data,
expiration_seconds=self.expiration_seconds,
token_length=max_valid_token_length + 1,
)
mock_generate.assert_not_called()
def test_get_data_key_length_check(self) -> None:
with self.assertRaises(ZulipRedisKeyTooLongError):
get_dict_from_redis(
self.redis_client, key_format="{token}", key="A" * (MAX_KEY_LENGTH + 1)
)
def test_get_data_key_format_validation(self) -> None:
with self.assertRaises(ZulipRedisKeyOfWrongFormatError):
get_dict_from_redis(self.redis_client, self.key_format, "nonmatching_format_1111")