Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPP-3932: Add flag 'developer_mode', use to simulate complaint and log notifications #5090

Merged
merged 19 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions emails/tests/utils_tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import base64
import json
import random
import zlib
from base64 import b64encode
from typing import Literal, TypedDict
from unittest.mock import patch
Expand All @@ -10,6 +13,8 @@

from emails.utils import (
InvalidFromHeader,
decode_dict_gza85,
encode_dict_gza85,
generate_from_header,
get_domains_from_settings,
get_email_domain_from_settings,
Expand Down Expand Up @@ -391,3 +396,58 @@ def test_simple_strict_tracker_found(self):
assert changed_content == content
assert general_removed == 0
assert general_count == 0


def test_encode_dict_gza85() -> None:
data = {"key": "value"}
encoded = encode_dict_gza85(data)
assert encoded == "Gatg8b\"f'<Z;OLK9?\\hZ<N63&/$B+B"
decoded = decode_dict_gza85(encoded)
assert decoded == data


def test_encode_dict_gza85_large_value() -> None:
data = {
"key": "value",
"random_strings": [
base64.encodebytes(random.randbytes(32)).decode() for _ in range(100)
],
}
encoded = encode_dict_gza85(data)
assert len(encoded) > 1024
assert "\n" in encoded
decoded = decode_dict_gza85(encoded)
assert decoded == data


DECODE_DICT_GZA85_ERROR_CASES = {
"invalid_zlib": ("ascii85_garbage", zlib.error, "incorrect header check"),
"invalid_a85": ("v_is_invalid_ASCII85", ValueError, "Non-Ascii85 digit found: v"),
"not_json": (
base64.a85encode(zlib.compress(b"[This] is {not} JSON"), pad=True).decode(),
ValueError,
"Expecting value: line 1 column 2",
),
"not_json_dict": (
base64.a85encode(zlib.compress(b'["A", "list"]'), pad=True).decode(),
ValueError,
"Encoded data is not a dict",
),
"non_string_key": (
base64.a85encode(zlib.compress(b'{1: "One"}'), pad=True).decode(),
ValueError,
"Expecting property name enclosed in double quotes",
),
}


@pytest.mark.parametrize(
"invalid_encoded, expected_error, expected_regex",
DECODE_DICT_GZA85_ERROR_CASES.values(),
ids=DECODE_DICT_GZA85_ERROR_CASES.keys(),
)
def test_decode_dict_gza85_invalid_encoded_raises(
invalid_encoded, expected_error, expected_regex
):
with pytest.raises(expected_error, match=expected_regex):
decode_dict_gza85(invalid_encoded)
Loading
Loading