2020-05-26 07:16:25 +02:00
|
|
|
from unittest import mock
|
2020-01-26 17:01:23 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.redis_utils import (
|
|
|
|
MAX_KEY_LENGTH,
|
|
|
|
ZulipRedisKeyOfWrongFormatError,
|
|
|
|
ZulipRedisKeyTooLongError,
|
|
|
|
get_dict_from_redis,
|
|
|
|
get_redis_client,
|
|
|
|
put_dict_in_redis,
|
|
|
|
)
|
2020-01-26 17:01:23 +01:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2020-01-26 17:01:23 +01:00
|
|
|
|
|
|
|
class RedisUtilsTest(ZulipTestCase):
|
|
|
|
key_format = "test_redis_utils_{token}"
|
|
|
|
expiration_seconds = 60
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls) -> None:
|
|
|
|
cls.redis_client = get_redis_client()
|
|
|
|
return super().setUpClass()
|
|
|
|
|
|
|
|
def test_put_and_get_data(self) -> None:
|
|
|
|
data = {
|
|
|
|
"a": 1,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
"b": "some value",
|
2020-01-26 17:01:23 +01:00
|
|
|
}
|
|
|
|
key = put_dict_in_redis(self.redis_client, self.key_format, data,
|
|
|
|
expiration_seconds=self.expiration_seconds)
|
2020-01-26 19:01:56 +01:00
|
|
|
retrieved_data = get_dict_from_redis(self.redis_client, self.key_format, key)
|
2020-01-26 17:01:23 +01:00
|
|
|
self.assertEqual(data, retrieved_data)
|
|
|
|
|
|
|
|
def test_put_data_key_length_check(self) -> None:
|
|
|
|
data = {
|
|
|
|
"a": 1,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
"b": "some value",
|
2020-01-26 17:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-01-26 19:01:56 +01:00
|
|
|
retrieved_data = get_dict_from_redis(self.redis_client, self.key_format, key)
|
2020-01-26 17:01:23 +01:00
|
|
|
self.assertEqual(data, retrieved_data)
|
|
|
|
|
|
|
|
# Trying to put data under an overly long key should get stopped before even
|
|
|
|
# generating the random token.
|
2020-09-05 04:02:13 +02:00
|
|
|
with mock.patch("secrets.token_hex") as mock_generate:
|
2020-01-26 17:01:23 +01:00
|
|
|
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):
|
2020-01-26 19:01:56 +01:00
|
|
|
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')
|