2020-06-30 18:38:42 +02:00
|
|
|
"""
|
|
|
|
This is new module that we intend to GROW from test_events.py.
|
|
|
|
|
|
|
|
It will contain schemas (aka validators) for Zulip events.
|
|
|
|
|
|
|
|
Right now it's only intended to be used by test code.
|
|
|
|
"""
|
2020-08-05 12:31:09 +02:00
|
|
|
from typing import Dict, List, Sequence, Tuple, Union
|
2020-07-30 18:11:19 +02:00
|
|
|
|
|
|
|
from zerver.lib.data_types import (
|
|
|
|
DictType,
|
|
|
|
EnumType,
|
|
|
|
Equals,
|
|
|
|
ListType,
|
|
|
|
OptionalType,
|
|
|
|
UnionType,
|
|
|
|
UrlType,
|
|
|
|
check_data,
|
|
|
|
event_dict_type,
|
|
|
|
make_checker,
|
2020-07-08 02:14:11 +02:00
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
from zerver.lib.topic import ORIG_TOPIC, TOPIC_LINKS, TOPIC_NAME
|
|
|
|
from zerver.lib.validator import Validator, check_dict_only, check_int
|
2020-07-15 16:02:35 +02:00
|
|
|
from zerver.models import Realm, Stream, Subscription, UserProfile
|
2020-06-30 18:38:42 +02:00
|
|
|
|
2020-07-08 12:53:52 +02:00
|
|
|
# These fields are used for "stream" events, and are included in the
|
|
|
|
# larger "subscription" events that also contain personal settings.
|
|
|
|
basic_stream_fields = [
|
2020-07-30 18:11:19 +02:00
|
|
|
("description", str),
|
|
|
|
("first_message_id", OptionalType(int)),
|
|
|
|
("history_public_to_subscribers", bool),
|
|
|
|
("invite_only", bool),
|
|
|
|
("is_announcement_only", bool),
|
|
|
|
("is_web_public", bool),
|
2020-08-05 11:57:45 +02:00
|
|
|
("message_retention_days", OptionalType(int)),
|
2020-07-30 18:11:19 +02:00
|
|
|
("name", str),
|
|
|
|
("rendered_description", str),
|
|
|
|
("stream_id", int),
|
|
|
|
("stream_post_policy", int),
|
|
|
|
("date_created", int),
|
2020-07-08 12:53:52 +02:00
|
|
|
]
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
subscription_fields: Sequence[Tuple[str, object]] = [
|
2020-07-08 14:13:16 +02:00
|
|
|
*basic_stream_fields,
|
2020-07-30 18:11:19 +02:00
|
|
|
("audible_notifications", OptionalType(bool)),
|
|
|
|
("color", str),
|
|
|
|
("desktop_notifications", OptionalType(bool)),
|
|
|
|
("email_address", str),
|
|
|
|
("email_notifications", OptionalType(bool)),
|
|
|
|
("in_home_view", bool),
|
|
|
|
("is_muted", bool),
|
|
|
|
("pin_to_top", bool),
|
|
|
|
("push_notifications", OptionalType(bool)),
|
|
|
|
("role", EnumType(Subscription.ROLE_TYPES)),
|
|
|
|
("stream_weekly_traffic", OptionalType(int)),
|
|
|
|
("wildcard_mentions_notify", OptionalType(bool)),
|
2020-07-08 14:13:16 +02:00
|
|
|
]
|
|
|
|
|
2020-06-30 18:38:42 +02:00
|
|
|
|
|
|
|
def check_events_dict(
|
|
|
|
required_keys: Sequence[Tuple[str, Validator[object]]],
|
|
|
|
optional_keys: Sequence[Tuple[str, Validator[object]]] = [],
|
|
|
|
) -> Validator[Dict[str, object]]:
|
|
|
|
"""
|
2020-07-30 18:11:19 +02:00
|
|
|
This is just a tiny wrapper on check_dict_only, but it provides
|
2020-06-30 18:38:42 +02:00
|
|
|
some minor benefits:
|
|
|
|
|
|
|
|
- mark clearly that the schema is for a Zulip event
|
|
|
|
- make sure there's a type field
|
|
|
|
- add id field automatically
|
|
|
|
- sanity check that we have no duplicate keys (we
|
2020-07-30 18:11:19 +02:00
|
|
|
should just make check_dict_only do that, eventually)
|
2020-06-30 18:38:42 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
rkeys = [key[0] for key in required_keys]
|
|
|
|
okeys = [key[0] for key in optional_keys]
|
|
|
|
keys = rkeys + okeys
|
|
|
|
assert len(keys) == len(set(keys))
|
|
|
|
assert "type" in rkeys
|
|
|
|
assert "id" not in keys
|
|
|
|
return check_dict_only(
|
2020-09-02 06:59:07 +02:00
|
|
|
required_keys=[*required_keys, ("id", check_int)],
|
2020-06-30 18:38:42 +02:00
|
|
|
optional_keys=optional_keys,
|
|
|
|
)
|
2020-07-08 02:14:11 +02:00
|
|
|
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
equals_add_or_remove = UnionType(
|
2020-07-17 09:13:10 +02:00
|
|
|
[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
Equals("add"),
|
|
|
|
Equals("remove"),
|
2020-07-17 09:13:10 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
value_type = UnionType(
|
2020-07-08 02:14:11 +02:00
|
|
|
[
|
|
|
|
# force vertical formatting
|
2020-07-30 18:11:19 +02:00
|
|
|
bool,
|
|
|
|
int,
|
|
|
|
str,
|
2020-07-08 02:14:11 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
optional_value_type = UnionType(
|
2020-07-08 13:35:37 +02:00
|
|
|
[
|
|
|
|
# force vertical formatting
|
2020-07-30 18:11:19 +02:00
|
|
|
bool,
|
|
|
|
int,
|
|
|
|
str,
|
|
|
|
Equals(None),
|
2020-07-08 13:35:37 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
alert_words_event = event_dict_type(
|
2020-07-18 17:11:41 +02:00
|
|
|
required_keys=[
|
|
|
|
# force vertical formatting
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("alert_words")),
|
|
|
|
("alert_words", ListType(str)),
|
2020-07-18 17:11:41 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_alert_words = make_checker(alert_words_event)
|
2020-07-18 17:11:41 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
custom_profile_field_type = DictType(
|
2020-07-18 17:02:28 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("id", int),
|
|
|
|
("type", int),
|
|
|
|
("name", str),
|
|
|
|
("hint", str),
|
|
|
|
("field_data", str),
|
|
|
|
("order", int),
|
|
|
|
],
|
2020-07-18 17:02:28 +02:00
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
custom_profile_fields_event = event_dict_type(
|
2020-07-18 17:02:28 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("custom_profile_fields")),
|
|
|
|
("op", Equals("add")),
|
|
|
|
("fields", ListType(custom_profile_field_type)),
|
2020-07-18 17:02:28 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_custom_profile_fields = make_checker(custom_profile_fields_event)
|
2020-07-18 17:02:28 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_stream_group = DictType(
|
2020-08-01 14:33:03 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("name", str),
|
|
|
|
("id", int),
|
|
|
|
("description", str),
|
|
|
|
("streams", ListType(DictType(basic_stream_fields))),
|
2020-08-01 14:33:03 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
default_stream_groups_event = event_dict_type(
|
2020-08-01 14:33:03 +02:00
|
|
|
required_keys=[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("default_stream_groups")),
|
|
|
|
("default_stream_groups", ListType(_check_stream_group)),
|
2020-08-01 14:33:03 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_default_stream_groups = make_checker(default_stream_groups_event)
|
2020-08-01 14:33:03 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
default_streams_event = event_dict_type(
|
2020-08-01 14:36:13 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("default_streams")),
|
|
|
|
("default_streams", ListType(DictType(basic_stream_fields))),
|
2020-08-01 14:36:13 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_default_streams = make_checker(default_streams_event)
|
2020-08-01 14:36:13 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
invites_changed_event = event_dict_type(
|
2020-07-18 16:33:03 +02:00
|
|
|
required_keys=[
|
|
|
|
# the most boring event...no metadata
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("invites_changed")),
|
2020-07-18 16:33:03 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_invites_changed = make_checker(invites_changed_event)
|
2020-07-18 16:33:03 +02:00
|
|
|
|
2020-07-10 16:10:58 +02:00
|
|
|
message_fields = [
|
2020-07-30 18:11:19 +02:00
|
|
|
("avatar_url", OptionalType(str)),
|
|
|
|
("client", str),
|
|
|
|
("content", str),
|
|
|
|
("content_type", Equals("text/html")),
|
|
|
|
("display_recipient", str),
|
|
|
|
("id", int),
|
|
|
|
("is_me_message", bool),
|
|
|
|
("reactions", ListType(dict)),
|
|
|
|
("recipient_id", int),
|
|
|
|
("sender_realm_str", str),
|
|
|
|
("sender_email", str),
|
|
|
|
("sender_full_name", str),
|
|
|
|
("sender_id", int),
|
|
|
|
("stream_id", int),
|
|
|
|
(TOPIC_NAME, str),
|
|
|
|
(TOPIC_LINKS, ListType(str)),
|
|
|
|
("submessages", ListType(dict)),
|
|
|
|
("timestamp", int),
|
|
|
|
("type", str),
|
2020-07-10 16:10:58 +02:00
|
|
|
]
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
message_event = event_dict_type(
|
2020-07-10 16:10:58 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("message")),
|
|
|
|
("flags", ListType(str)),
|
|
|
|
("message", DictType(message_fields)),
|
2020-07-10 16:10:58 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_message = make_checker(message_event)
|
2020-07-10 16:10:58 +02:00
|
|
|
|
2020-07-17 09:23:12 +02:00
|
|
|
# We will eventually just send user_ids.
|
2020-07-30 18:11:19 +02:00
|
|
|
reaction_user_type = DictType(
|
2020-07-17 09:23:12 +02:00
|
|
|
required_keys=[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
("email", str),
|
|
|
|
("full_name", str),
|
|
|
|
("user_id", int),
|
2020-07-17 09:23:12 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
reaction_event = event_dict_type(
|
2020-07-17 09:23:12 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("reaction")),
|
|
|
|
("op", equals_add_or_remove),
|
|
|
|
("message_id", int),
|
|
|
|
("emoji_name", str),
|
|
|
|
("emoji_code", str),
|
|
|
|
("reaction_type", str),
|
|
|
|
("user_id", int),
|
|
|
|
("user", reaction_user_type),
|
2020-07-17 09:23:12 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_reaction = make_checker(reaction_event)
|
2020-07-17 09:23:12 +02:00
|
|
|
|
|
|
|
|
2020-08-08 13:41:21 +02:00
|
|
|
def check_reaction(var_name: str, event: Dict[str, object], op: str) -> None:
|
2020-07-17 09:23:12 +02:00
|
|
|
_check_reaction(var_name, event)
|
|
|
|
assert event["op"] == op
|
|
|
|
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
bot_services_outgoing_type = DictType(
|
2020-07-08 17:07:29 +02:00
|
|
|
required_keys=[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
("base_url", UrlType()),
|
|
|
|
("interface", int),
|
|
|
|
("token", str),
|
2020-07-08 17:07:29 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
# We use a strict check here, because our tests
|
|
|
|
# don't specifically focus on seeing how
|
|
|
|
# flexible we can make the types be for config_data.
|
2020-07-30 18:11:19 +02:00
|
|
|
_ad_hoc_config_data_schema = Equals(dict(foo="bar"))
|
2020-07-08 17:07:29 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
bot_services_embedded_type = DictType(
|
2020-07-08 17:07:29 +02:00
|
|
|
required_keys=[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
("service_name", str),
|
2020-07-08 17:07:29 +02:00
|
|
|
("config_data", _ad_hoc_config_data_schema),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Note that regular bots just get an empty list of services,
|
2020-07-30 18:11:19 +02:00
|
|
|
# so the sub_validator for ListType won't matter for them.
|
|
|
|
bot_services_type = ListType(
|
|
|
|
UnionType(
|
2020-07-08 17:07:29 +02:00
|
|
|
[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
bot_services_outgoing_type,
|
|
|
|
bot_services_embedded_type,
|
2020-07-08 17:07:29 +02:00
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
bot_type = DictType(
|
2020-07-08 17:07:29 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("user_id", int),
|
|
|
|
("api_key", str),
|
|
|
|
("avatar_url", str),
|
|
|
|
("bot_type", int),
|
|
|
|
("default_all_public_streams", bool),
|
|
|
|
("default_events_register_stream", OptionalType(str)),
|
|
|
|
("default_sending_stream", OptionalType(str)),
|
|
|
|
("email", str),
|
|
|
|
("full_name", str),
|
|
|
|
("is_active", bool),
|
|
|
|
("owner_id", int),
|
|
|
|
("services", bot_services_type),
|
2020-07-08 17:07:29 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
realm_bot_add_event = event_dict_type(
|
2020-07-08 17:07:29 +02:00
|
|
|
required_keys=[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("realm_bot")),
|
|
|
|
("op", Equals("add")),
|
|
|
|
("bot", bot_type),
|
2020-07-08 17:07:29 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_realm_bot_add = make_checker(realm_bot_add_event)
|
2020-07-08 17:07:29 +02:00
|
|
|
|
|
|
|
|
2020-08-08 13:41:21 +02:00
|
|
|
def check_realm_bot_add(var_name: str, event: Dict[str, object],) -> None:
|
2020-07-08 17:07:29 +02:00
|
|
|
_check_realm_bot_add(var_name, event)
|
|
|
|
|
2020-08-08 13:41:21 +02:00
|
|
|
assert isinstance(event["bot"], dict)
|
2020-07-08 17:07:29 +02:00
|
|
|
bot_type = event["bot"]["bot_type"]
|
|
|
|
|
|
|
|
services_field = f"{var_name}['bot']['services']"
|
|
|
|
services = event["bot"]["services"]
|
|
|
|
|
|
|
|
if bot_type == UserProfile.DEFAULT_BOT:
|
2020-07-30 18:11:19 +02:00
|
|
|
check_data(Equals([]), services_field, services)
|
2020-07-08 17:07:29 +02:00
|
|
|
elif bot_type == UserProfile.OUTGOING_WEBHOOK_BOT:
|
2020-07-30 18:11:19 +02:00
|
|
|
check_data(
|
|
|
|
ListType(bot_services_outgoing_type, length=1), services_field, services
|
|
|
|
)
|
2020-07-08 17:07:29 +02:00
|
|
|
elif bot_type == UserProfile.EMBEDDED_BOT:
|
2020-07-30 18:11:19 +02:00
|
|
|
check_data(
|
|
|
|
ListType(bot_services_embedded_type, length=1), services_field, services
|
|
|
|
)
|
2020-07-08 17:07:29 +02:00
|
|
|
else:
|
|
|
|
raise AssertionError(f"Unknown bot_type: {bot_type}")
|
|
|
|
|
2020-07-08 02:14:11 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
bot_type_for_delete = DictType(
|
2020-07-08 21:06:22 +02:00
|
|
|
required_keys=[
|
|
|
|
# for legacy reasons we have a dict here
|
|
|
|
# with only one key
|
2020-07-30 18:11:19 +02:00
|
|
|
("user_id", int),
|
2020-07-08 21:06:22 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
realm_bot_delete_event = event_dict_type(
|
2020-07-08 21:06:22 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("realm_bot")),
|
|
|
|
("op", Equals("delete")),
|
|
|
|
("bot", bot_type_for_delete),
|
2020-07-08 21:06:22 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_realm_bot_delete = make_checker(realm_bot_delete_event)
|
2020-07-08 21:06:22 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
bot_type_for_remove = DictType(
|
2020-07-08 21:06:22 +02:00
|
|
|
required_keys=[
|
|
|
|
# Why does remove have full_name but delete doesn't?
|
|
|
|
# Why do we have both a remove and a delete event
|
|
|
|
# for bots? I don't know the answer as I write this.
|
2020-07-30 18:11:19 +02:00
|
|
|
("full_name", str),
|
|
|
|
("user_id", int),
|
2020-07-08 21:06:22 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
realm_bot_remove_event = event_dict_type(
|
2020-07-08 21:06:22 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("realm_bot")),
|
|
|
|
("op", Equals("remove")),
|
|
|
|
("bot", bot_type_for_remove),
|
2020-07-08 21:06:22 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_realm_bot_remove = make_checker(realm_bot_remove_event)
|
2020-07-08 21:06:22 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
bot_type_for_update = DictType(
|
2020-07-08 17:47:56 +02:00
|
|
|
required_keys=[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
("user_id", int),
|
2020-07-08 17:47:56 +02:00
|
|
|
],
|
|
|
|
optional_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("api_key", str),
|
|
|
|
("avatar_url", str),
|
|
|
|
("default_all_public_streams", bool),
|
|
|
|
("default_events_register_stream", OptionalType(str)),
|
|
|
|
("default_sending_stream", OptionalType(str)),
|
|
|
|
("full_name", str),
|
|
|
|
("owner_id", int),
|
|
|
|
("services", bot_services_type),
|
2020-07-08 17:47:56 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
realm_bot_update_event = event_dict_type(
|
2020-07-08 17:47:56 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("realm_bot")),
|
|
|
|
("op", Equals("update")),
|
|
|
|
("bot", bot_type_for_update),
|
2020-07-08 17:47:56 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_realm_bot_update = make_checker(realm_bot_update_event)
|
2020-07-08 17:47:56 +02:00
|
|
|
|
|
|
|
|
2020-08-08 13:41:21 +02:00
|
|
|
def check_realm_bot_update(
|
|
|
|
# Check schema plus the field.
|
|
|
|
var_name: str,
|
|
|
|
event: Dict[str, object],
|
|
|
|
field: str,
|
|
|
|
) -> None:
|
2020-07-08 17:47:56 +02:00
|
|
|
# Check the overall schema first.
|
|
|
|
_check_realm_bot_update(var_name, event)
|
|
|
|
|
2020-08-08 13:41:21 +02:00
|
|
|
assert isinstance(event["bot"], dict)
|
2020-07-08 17:47:56 +02:00
|
|
|
assert {"user_id", field} == set(event["bot"].keys())
|
|
|
|
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
plan_type_extra_data_type = DictType(
|
2020-07-23 18:01:27 +02:00
|
|
|
required_keys=[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
("upload_quota", int),
|
2020-07-23 18:01:27 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-08 02:14:11 +02:00
|
|
|
"""
|
|
|
|
realm/update events are flexible for values;
|
|
|
|
we will use a more strict checker to check
|
|
|
|
types in a context-specific manner
|
|
|
|
"""
|
2020-07-30 18:11:19 +02:00
|
|
|
realm_update_event = event_dict_type(
|
2020-07-08 02:14:11 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("realm")),
|
|
|
|
("op", Equals("update")),
|
|
|
|
("property", str),
|
|
|
|
("value", value_type),
|
2020-07-23 18:01:27 +02:00
|
|
|
],
|
|
|
|
optional_keys=[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
("extra_data", plan_type_extra_data_type),
|
2020-07-23 18:01:27 +02:00
|
|
|
],
|
2020-07-08 02:14:11 +02:00
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_realm_update = make_checker(realm_update_event)
|
2020-07-08 02:14:11 +02:00
|
|
|
|
|
|
|
|
2020-08-08 13:41:21 +02:00
|
|
|
def check_realm_update(var_name: str, event: Dict[str, object], prop: str,) -> None:
|
2020-07-08 02:14:11 +02:00
|
|
|
"""
|
|
|
|
Realm updates have these two fields:
|
|
|
|
|
|
|
|
property
|
|
|
|
value
|
|
|
|
|
|
|
|
We check not only the basic schema, but also that
|
|
|
|
the value people actually matches the type from
|
|
|
|
Realm.property_types that we have configured
|
|
|
|
for the property.
|
|
|
|
"""
|
|
|
|
_check_realm_update(var_name, event)
|
2020-07-23 17:38:53 +02:00
|
|
|
|
|
|
|
assert prop == event["property"]
|
2020-07-08 02:14:11 +02:00
|
|
|
value = event["value"]
|
|
|
|
|
2020-07-23 18:01:27 +02:00
|
|
|
if prop == "plan_type":
|
|
|
|
assert isinstance(value, int)
|
|
|
|
assert "extra_data" in event.keys()
|
|
|
|
return
|
|
|
|
|
|
|
|
assert "extra_data" not in event.keys()
|
|
|
|
|
2020-07-23 17:38:53 +02:00
|
|
|
if prop in ["notifications_stream_id", "signup_notifications_stream_id"]:
|
|
|
|
assert isinstance(value, int)
|
|
|
|
return
|
|
|
|
|
2020-07-08 02:14:11 +02:00
|
|
|
property_type = Realm.property_types[prop]
|
2020-07-23 17:38:53 +02:00
|
|
|
|
2020-07-08 02:14:11 +02:00
|
|
|
if property_type in (bool, int, str):
|
|
|
|
assert isinstance(value, property_type)
|
|
|
|
elif property_type == (int, type(None)):
|
|
|
|
assert isinstance(value, int)
|
|
|
|
elif property_type == (str, type(None)):
|
|
|
|
assert isinstance(value, str)
|
|
|
|
else:
|
|
|
|
raise AssertionError(f"Unexpected property type {property_type}")
|
2020-07-08 12:53:52 +02:00
|
|
|
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
custom_profile_field_type = DictType(
|
event_schema: Extract check_realm_user_update.
This a pretty big commit, but I really wanted it
to be atomic.
All realm_user/update events look the same from
the top:
_check_realm_user_update = check_events_dict(
required_keys=[
("type", equals("realm_user")),
("op", equals("update")),
("person", _check_realm_user_person),
]
)
And then we have a bunch of fields for person that
are optional, and we usually only send user_id plus
one other field, with the exception of avatar-related
events:
_check_realm_user_person = check_dict_only(
required_keys=[
# vertical formatting
("user_id", check_int),
],
optional_keys=[
("avatar_source", check_string),
("avatar_url", check_none_or(check_string)),
("avatar_url_medium", check_none_or(check_string)),
("avatar_version", check_int),
("bot_owner_id", check_int),
("custom_profile_field", _check_custom_profile_field),
("delivery_email", check_string),
("full_name", check_string),
("role", check_int_in(UserProfile.ROLE_TYPES)),
("email", check_string),
("user_id", check_int),
("timezone", check_string),
],
)
I would start the code review by just skimming the changes
to event_schema.py, to get the big picture of the complexity
here. Basically the schema is just the combined superset of
all the individual schemas that we remove from test_events.
Then I would read test_events.py.
The simplest diffs are basically of this form:
- schema_checker = check_events_dict([
- ('type', equals('realm_user')),
- ('op', equals('update')),
- ('person', check_dict_only([
- ('role', check_int_in(UserProfile.ROLE_TYPES)),
- ('user_id', check_int),
- ])),
- ])
# ...
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
Instead of a custom schema checker, we use the "superset"
schema checker, but then we pass in the set of fields that we
expect to be there. Note that 'user_id' is always there.
So most of the heavy lifting happens in this new function
in event_schema.py:
def check_realm_user_update(
var_name: str, event: Dict[str, Any], optional_fields: Set[str],
) -> None:
_check_realm_user_update(var_name, event)
keys = set(event["person"].keys()) - {"user_id"}
assert optional_fields == keys
But we still do some more custom checks in test_events.py.
custom profile fields: check keys of custom_profile_field
def test_custom_profile_field_data_events(self) -> None:
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value", "rendered_value"}
+ )
+ check_realm_user_update('events[0]', events[0], {"custom_profile_field"})
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value"}
+ )
avatar fields: check more specific types, since the superset
schema has check_none_or(check_string)
def test_change_avatar_fields(self) -> None:
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ assert isinstance(events[0]['person']['avatar_url'], str)
+ assert isinstance(events[0]['person']['avatar_url_medium'], str)
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ self.assertEqual(events[0]['person']['avatar_url'], None)
+ self.assertEqual(events[0]['person']['avatar_url_medium'], None)
Also note that avatar_fields is a set of four fields that
are set in event_schema.
full name: no extra work!
def test_change_full_name(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'full_name'})
test_change_user_delivery_email_email_address_visibilty_admins:
no extra work for delivery_email
check avatar fields more directly
roles (several examples) -- actually check the specific role
def test_change_realm_authentication_methods(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
+ self.assertEqual(events[0]['person']['role'], role)
bot_owner_id: no extra work!
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
timezone: no extra work!
- timezone_schema_checker('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"email", "timezone"})
2020-07-23 16:04:06 +02:00
|
|
|
required_keys=[
|
|
|
|
# vertical formatting
|
2020-07-30 18:11:19 +02:00
|
|
|
("id", int),
|
|
|
|
("value", str),
|
event_schema: Extract check_realm_user_update.
This a pretty big commit, but I really wanted it
to be atomic.
All realm_user/update events look the same from
the top:
_check_realm_user_update = check_events_dict(
required_keys=[
("type", equals("realm_user")),
("op", equals("update")),
("person", _check_realm_user_person),
]
)
And then we have a bunch of fields for person that
are optional, and we usually only send user_id plus
one other field, with the exception of avatar-related
events:
_check_realm_user_person = check_dict_only(
required_keys=[
# vertical formatting
("user_id", check_int),
],
optional_keys=[
("avatar_source", check_string),
("avatar_url", check_none_or(check_string)),
("avatar_url_medium", check_none_or(check_string)),
("avatar_version", check_int),
("bot_owner_id", check_int),
("custom_profile_field", _check_custom_profile_field),
("delivery_email", check_string),
("full_name", check_string),
("role", check_int_in(UserProfile.ROLE_TYPES)),
("email", check_string),
("user_id", check_int),
("timezone", check_string),
],
)
I would start the code review by just skimming the changes
to event_schema.py, to get the big picture of the complexity
here. Basically the schema is just the combined superset of
all the individual schemas that we remove from test_events.
Then I would read test_events.py.
The simplest diffs are basically of this form:
- schema_checker = check_events_dict([
- ('type', equals('realm_user')),
- ('op', equals('update')),
- ('person', check_dict_only([
- ('role', check_int_in(UserProfile.ROLE_TYPES)),
- ('user_id', check_int),
- ])),
- ])
# ...
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
Instead of a custom schema checker, we use the "superset"
schema checker, but then we pass in the set of fields that we
expect to be there. Note that 'user_id' is always there.
So most of the heavy lifting happens in this new function
in event_schema.py:
def check_realm_user_update(
var_name: str, event: Dict[str, Any], optional_fields: Set[str],
) -> None:
_check_realm_user_update(var_name, event)
keys = set(event["person"].keys()) - {"user_id"}
assert optional_fields == keys
But we still do some more custom checks in test_events.py.
custom profile fields: check keys of custom_profile_field
def test_custom_profile_field_data_events(self) -> None:
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value", "rendered_value"}
+ )
+ check_realm_user_update('events[0]', events[0], {"custom_profile_field"})
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value"}
+ )
avatar fields: check more specific types, since the superset
schema has check_none_or(check_string)
def test_change_avatar_fields(self) -> None:
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ assert isinstance(events[0]['person']['avatar_url'], str)
+ assert isinstance(events[0]['person']['avatar_url_medium'], str)
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ self.assertEqual(events[0]['person']['avatar_url'], None)
+ self.assertEqual(events[0]['person']['avatar_url_medium'], None)
Also note that avatar_fields is a set of four fields that
are set in event_schema.
full name: no extra work!
def test_change_full_name(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'full_name'})
test_change_user_delivery_email_email_address_visibilty_admins:
no extra work for delivery_email
check avatar fields more directly
roles (several examples) -- actually check the specific role
def test_change_realm_authentication_methods(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
+ self.assertEqual(events[0]['person']['role'], role)
bot_owner_id: no extra work!
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
timezone: no extra work!
- timezone_schema_checker('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"email", "timezone"})
2020-07-23 16:04:06 +02:00
|
|
|
],
|
|
|
|
optional_keys=[
|
|
|
|
# vertical formatting
|
2020-07-30 18:11:19 +02:00
|
|
|
("rendered_value", str),
|
event_schema: Extract check_realm_user_update.
This a pretty big commit, but I really wanted it
to be atomic.
All realm_user/update events look the same from
the top:
_check_realm_user_update = check_events_dict(
required_keys=[
("type", equals("realm_user")),
("op", equals("update")),
("person", _check_realm_user_person),
]
)
And then we have a bunch of fields for person that
are optional, and we usually only send user_id plus
one other field, with the exception of avatar-related
events:
_check_realm_user_person = check_dict_only(
required_keys=[
# vertical formatting
("user_id", check_int),
],
optional_keys=[
("avatar_source", check_string),
("avatar_url", check_none_or(check_string)),
("avatar_url_medium", check_none_or(check_string)),
("avatar_version", check_int),
("bot_owner_id", check_int),
("custom_profile_field", _check_custom_profile_field),
("delivery_email", check_string),
("full_name", check_string),
("role", check_int_in(UserProfile.ROLE_TYPES)),
("email", check_string),
("user_id", check_int),
("timezone", check_string),
],
)
I would start the code review by just skimming the changes
to event_schema.py, to get the big picture of the complexity
here. Basically the schema is just the combined superset of
all the individual schemas that we remove from test_events.
Then I would read test_events.py.
The simplest diffs are basically of this form:
- schema_checker = check_events_dict([
- ('type', equals('realm_user')),
- ('op', equals('update')),
- ('person', check_dict_only([
- ('role', check_int_in(UserProfile.ROLE_TYPES)),
- ('user_id', check_int),
- ])),
- ])
# ...
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
Instead of a custom schema checker, we use the "superset"
schema checker, but then we pass in the set of fields that we
expect to be there. Note that 'user_id' is always there.
So most of the heavy lifting happens in this new function
in event_schema.py:
def check_realm_user_update(
var_name: str, event: Dict[str, Any], optional_fields: Set[str],
) -> None:
_check_realm_user_update(var_name, event)
keys = set(event["person"].keys()) - {"user_id"}
assert optional_fields == keys
But we still do some more custom checks in test_events.py.
custom profile fields: check keys of custom_profile_field
def test_custom_profile_field_data_events(self) -> None:
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value", "rendered_value"}
+ )
+ check_realm_user_update('events[0]', events[0], {"custom_profile_field"})
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value"}
+ )
avatar fields: check more specific types, since the superset
schema has check_none_or(check_string)
def test_change_avatar_fields(self) -> None:
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ assert isinstance(events[0]['person']['avatar_url'], str)
+ assert isinstance(events[0]['person']['avatar_url_medium'], str)
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ self.assertEqual(events[0]['person']['avatar_url'], None)
+ self.assertEqual(events[0]['person']['avatar_url_medium'], None)
Also note that avatar_fields is a set of four fields that
are set in event_schema.
full name: no extra work!
def test_change_full_name(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'full_name'})
test_change_user_delivery_email_email_address_visibilty_admins:
no extra work for delivery_email
check avatar fields more directly
roles (several examples) -- actually check the specific role
def test_change_realm_authentication_methods(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
+ self.assertEqual(events[0]['person']['role'], role)
bot_owner_id: no extra work!
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
timezone: no extra work!
- timezone_schema_checker('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"email", "timezone"})
2020-07-23 16:04:06 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2020-08-05 12:31:09 +02:00
|
|
|
realm_user_person_types = dict(
|
|
|
|
# Note that all flavors of person include user_id.
|
|
|
|
avatar_fields=DictType(
|
|
|
|
required_keys=[
|
|
|
|
("user_id", int),
|
|
|
|
("avatar_source", str),
|
|
|
|
("avatar_url", OptionalType(str)),
|
|
|
|
("avatar_url_medium", OptionalType(str)),
|
|
|
|
("avatar_version", int),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
bot_owner_id=DictType(
|
|
|
|
required_keys=[
|
|
|
|
# vertical formatting
|
|
|
|
("user_id", int),
|
|
|
|
("bot_owner_id", int),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
custom_profile_field=DictType(
|
|
|
|
required_keys=[
|
|
|
|
# vertical formatting
|
|
|
|
("user_id", int),
|
|
|
|
("custom_profile_field", custom_profile_field_type),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
delivery_email=DictType(
|
|
|
|
required_keys=[
|
|
|
|
# vertical formatting
|
|
|
|
("user_id", int),
|
|
|
|
("delivery_email", str),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
full_name=DictType(
|
|
|
|
required_keys=[
|
|
|
|
# vertical formatting
|
|
|
|
("user_id", int),
|
|
|
|
("full_name", str),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
role=DictType(
|
|
|
|
required_keys=[
|
|
|
|
# vertical formatting
|
|
|
|
("user_id", int),
|
|
|
|
("role", EnumType(UserProfile.ROLE_TYPES)),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
timezone=DictType(
|
|
|
|
required_keys=[
|
|
|
|
# we should probably eliminate email here
|
|
|
|
("user_id", int),
|
|
|
|
("email", str),
|
|
|
|
("timezone", str),
|
|
|
|
],
|
|
|
|
),
|
event_schema: Extract check_realm_user_update.
This a pretty big commit, but I really wanted it
to be atomic.
All realm_user/update events look the same from
the top:
_check_realm_user_update = check_events_dict(
required_keys=[
("type", equals("realm_user")),
("op", equals("update")),
("person", _check_realm_user_person),
]
)
And then we have a bunch of fields for person that
are optional, and we usually only send user_id plus
one other field, with the exception of avatar-related
events:
_check_realm_user_person = check_dict_only(
required_keys=[
# vertical formatting
("user_id", check_int),
],
optional_keys=[
("avatar_source", check_string),
("avatar_url", check_none_or(check_string)),
("avatar_url_medium", check_none_or(check_string)),
("avatar_version", check_int),
("bot_owner_id", check_int),
("custom_profile_field", _check_custom_profile_field),
("delivery_email", check_string),
("full_name", check_string),
("role", check_int_in(UserProfile.ROLE_TYPES)),
("email", check_string),
("user_id", check_int),
("timezone", check_string),
],
)
I would start the code review by just skimming the changes
to event_schema.py, to get the big picture of the complexity
here. Basically the schema is just the combined superset of
all the individual schemas that we remove from test_events.
Then I would read test_events.py.
The simplest diffs are basically of this form:
- schema_checker = check_events_dict([
- ('type', equals('realm_user')),
- ('op', equals('update')),
- ('person', check_dict_only([
- ('role', check_int_in(UserProfile.ROLE_TYPES)),
- ('user_id', check_int),
- ])),
- ])
# ...
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
Instead of a custom schema checker, we use the "superset"
schema checker, but then we pass in the set of fields that we
expect to be there. Note that 'user_id' is always there.
So most of the heavy lifting happens in this new function
in event_schema.py:
def check_realm_user_update(
var_name: str, event: Dict[str, Any], optional_fields: Set[str],
) -> None:
_check_realm_user_update(var_name, event)
keys = set(event["person"].keys()) - {"user_id"}
assert optional_fields == keys
But we still do some more custom checks in test_events.py.
custom profile fields: check keys of custom_profile_field
def test_custom_profile_field_data_events(self) -> None:
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value", "rendered_value"}
+ )
+ check_realm_user_update('events[0]', events[0], {"custom_profile_field"})
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value"}
+ )
avatar fields: check more specific types, since the superset
schema has check_none_or(check_string)
def test_change_avatar_fields(self) -> None:
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ assert isinstance(events[0]['person']['avatar_url'], str)
+ assert isinstance(events[0]['person']['avatar_url_medium'], str)
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ self.assertEqual(events[0]['person']['avatar_url'], None)
+ self.assertEqual(events[0]['person']['avatar_url_medium'], None)
Also note that avatar_fields is a set of four fields that
are set in event_schema.
full name: no extra work!
def test_change_full_name(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'full_name'})
test_change_user_delivery_email_email_address_visibilty_admins:
no extra work for delivery_email
check avatar fields more directly
roles (several examples) -- actually check the specific role
def test_change_realm_authentication_methods(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
+ self.assertEqual(events[0]['person']['role'], role)
bot_owner_id: no extra work!
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
timezone: no extra work!
- timezone_schema_checker('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"email", "timezone"})
2020-07-23 16:04:06 +02:00
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
realm_user_update_event = event_dict_type(
|
event_schema: Extract check_realm_user_update.
This a pretty big commit, but I really wanted it
to be atomic.
All realm_user/update events look the same from
the top:
_check_realm_user_update = check_events_dict(
required_keys=[
("type", equals("realm_user")),
("op", equals("update")),
("person", _check_realm_user_person),
]
)
And then we have a bunch of fields for person that
are optional, and we usually only send user_id plus
one other field, with the exception of avatar-related
events:
_check_realm_user_person = check_dict_only(
required_keys=[
# vertical formatting
("user_id", check_int),
],
optional_keys=[
("avatar_source", check_string),
("avatar_url", check_none_or(check_string)),
("avatar_url_medium", check_none_or(check_string)),
("avatar_version", check_int),
("bot_owner_id", check_int),
("custom_profile_field", _check_custom_profile_field),
("delivery_email", check_string),
("full_name", check_string),
("role", check_int_in(UserProfile.ROLE_TYPES)),
("email", check_string),
("user_id", check_int),
("timezone", check_string),
],
)
I would start the code review by just skimming the changes
to event_schema.py, to get the big picture of the complexity
here. Basically the schema is just the combined superset of
all the individual schemas that we remove from test_events.
Then I would read test_events.py.
The simplest diffs are basically of this form:
- schema_checker = check_events_dict([
- ('type', equals('realm_user')),
- ('op', equals('update')),
- ('person', check_dict_only([
- ('role', check_int_in(UserProfile.ROLE_TYPES)),
- ('user_id', check_int),
- ])),
- ])
# ...
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
Instead of a custom schema checker, we use the "superset"
schema checker, but then we pass in the set of fields that we
expect to be there. Note that 'user_id' is always there.
So most of the heavy lifting happens in this new function
in event_schema.py:
def check_realm_user_update(
var_name: str, event: Dict[str, Any], optional_fields: Set[str],
) -> None:
_check_realm_user_update(var_name, event)
keys = set(event["person"].keys()) - {"user_id"}
assert optional_fields == keys
But we still do some more custom checks in test_events.py.
custom profile fields: check keys of custom_profile_field
def test_custom_profile_field_data_events(self) -> None:
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value", "rendered_value"}
+ )
+ check_realm_user_update('events[0]', events[0], {"custom_profile_field"})
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value"}
+ )
avatar fields: check more specific types, since the superset
schema has check_none_or(check_string)
def test_change_avatar_fields(self) -> None:
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ assert isinstance(events[0]['person']['avatar_url'], str)
+ assert isinstance(events[0]['person']['avatar_url_medium'], str)
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ self.assertEqual(events[0]['person']['avatar_url'], None)
+ self.assertEqual(events[0]['person']['avatar_url_medium'], None)
Also note that avatar_fields is a set of four fields that
are set in event_schema.
full name: no extra work!
def test_change_full_name(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'full_name'})
test_change_user_delivery_email_email_address_visibilty_admins:
no extra work for delivery_email
check avatar fields more directly
roles (several examples) -- actually check the specific role
def test_change_realm_authentication_methods(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
+ self.assertEqual(events[0]['person']['role'], role)
bot_owner_id: no extra work!
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
timezone: no extra work!
- timezone_schema_checker('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"email", "timezone"})
2020-07-23 16:04:06 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("realm_user")),
|
|
|
|
("op", Equals("update")),
|
2020-08-05 12:31:09 +02:00
|
|
|
("person", UnionType(list(realm_user_person_types.values()))),
|
|
|
|
],
|
event_schema: Extract check_realm_user_update.
This a pretty big commit, but I really wanted it
to be atomic.
All realm_user/update events look the same from
the top:
_check_realm_user_update = check_events_dict(
required_keys=[
("type", equals("realm_user")),
("op", equals("update")),
("person", _check_realm_user_person),
]
)
And then we have a bunch of fields for person that
are optional, and we usually only send user_id plus
one other field, with the exception of avatar-related
events:
_check_realm_user_person = check_dict_only(
required_keys=[
# vertical formatting
("user_id", check_int),
],
optional_keys=[
("avatar_source", check_string),
("avatar_url", check_none_or(check_string)),
("avatar_url_medium", check_none_or(check_string)),
("avatar_version", check_int),
("bot_owner_id", check_int),
("custom_profile_field", _check_custom_profile_field),
("delivery_email", check_string),
("full_name", check_string),
("role", check_int_in(UserProfile.ROLE_TYPES)),
("email", check_string),
("user_id", check_int),
("timezone", check_string),
],
)
I would start the code review by just skimming the changes
to event_schema.py, to get the big picture of the complexity
here. Basically the schema is just the combined superset of
all the individual schemas that we remove from test_events.
Then I would read test_events.py.
The simplest diffs are basically of this form:
- schema_checker = check_events_dict([
- ('type', equals('realm_user')),
- ('op', equals('update')),
- ('person', check_dict_only([
- ('role', check_int_in(UserProfile.ROLE_TYPES)),
- ('user_id', check_int),
- ])),
- ])
# ...
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
Instead of a custom schema checker, we use the "superset"
schema checker, but then we pass in the set of fields that we
expect to be there. Note that 'user_id' is always there.
So most of the heavy lifting happens in this new function
in event_schema.py:
def check_realm_user_update(
var_name: str, event: Dict[str, Any], optional_fields: Set[str],
) -> None:
_check_realm_user_update(var_name, event)
keys = set(event["person"].keys()) - {"user_id"}
assert optional_fields == keys
But we still do some more custom checks in test_events.py.
custom profile fields: check keys of custom_profile_field
def test_custom_profile_field_data_events(self) -> None:
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value", "rendered_value"}
+ )
+ check_realm_user_update('events[0]', events[0], {"custom_profile_field"})
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value"}
+ )
avatar fields: check more specific types, since the superset
schema has check_none_or(check_string)
def test_change_avatar_fields(self) -> None:
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ assert isinstance(events[0]['person']['avatar_url'], str)
+ assert isinstance(events[0]['person']['avatar_url_medium'], str)
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ self.assertEqual(events[0]['person']['avatar_url'], None)
+ self.assertEqual(events[0]['person']['avatar_url_medium'], None)
Also note that avatar_fields is a set of four fields that
are set in event_schema.
full name: no extra work!
def test_change_full_name(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'full_name'})
test_change_user_delivery_email_email_address_visibilty_admins:
no extra work for delivery_email
check avatar fields more directly
roles (several examples) -- actually check the specific role
def test_change_realm_authentication_methods(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
+ self.assertEqual(events[0]['person']['role'], role)
bot_owner_id: no extra work!
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
timezone: no extra work!
- timezone_schema_checker('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"email", "timezone"})
2020-07-23 16:04:06 +02:00
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_realm_user_update = make_checker(realm_user_update_event)
|
event_schema: Extract check_realm_user_update.
This a pretty big commit, but I really wanted it
to be atomic.
All realm_user/update events look the same from
the top:
_check_realm_user_update = check_events_dict(
required_keys=[
("type", equals("realm_user")),
("op", equals("update")),
("person", _check_realm_user_person),
]
)
And then we have a bunch of fields for person that
are optional, and we usually only send user_id plus
one other field, with the exception of avatar-related
events:
_check_realm_user_person = check_dict_only(
required_keys=[
# vertical formatting
("user_id", check_int),
],
optional_keys=[
("avatar_source", check_string),
("avatar_url", check_none_or(check_string)),
("avatar_url_medium", check_none_or(check_string)),
("avatar_version", check_int),
("bot_owner_id", check_int),
("custom_profile_field", _check_custom_profile_field),
("delivery_email", check_string),
("full_name", check_string),
("role", check_int_in(UserProfile.ROLE_TYPES)),
("email", check_string),
("user_id", check_int),
("timezone", check_string),
],
)
I would start the code review by just skimming the changes
to event_schema.py, to get the big picture of the complexity
here. Basically the schema is just the combined superset of
all the individual schemas that we remove from test_events.
Then I would read test_events.py.
The simplest diffs are basically of this form:
- schema_checker = check_events_dict([
- ('type', equals('realm_user')),
- ('op', equals('update')),
- ('person', check_dict_only([
- ('role', check_int_in(UserProfile.ROLE_TYPES)),
- ('user_id', check_int),
- ])),
- ])
# ...
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
Instead of a custom schema checker, we use the "superset"
schema checker, but then we pass in the set of fields that we
expect to be there. Note that 'user_id' is always there.
So most of the heavy lifting happens in this new function
in event_schema.py:
def check_realm_user_update(
var_name: str, event: Dict[str, Any], optional_fields: Set[str],
) -> None:
_check_realm_user_update(var_name, event)
keys = set(event["person"].keys()) - {"user_id"}
assert optional_fields == keys
But we still do some more custom checks in test_events.py.
custom profile fields: check keys of custom_profile_field
def test_custom_profile_field_data_events(self) -> None:
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value", "rendered_value"}
+ )
+ check_realm_user_update('events[0]', events[0], {"custom_profile_field"})
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value"}
+ )
avatar fields: check more specific types, since the superset
schema has check_none_or(check_string)
def test_change_avatar_fields(self) -> None:
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ assert isinstance(events[0]['person']['avatar_url'], str)
+ assert isinstance(events[0]['person']['avatar_url_medium'], str)
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ self.assertEqual(events[0]['person']['avatar_url'], None)
+ self.assertEqual(events[0]['person']['avatar_url_medium'], None)
Also note that avatar_fields is a set of four fields that
are set in event_schema.
full name: no extra work!
def test_change_full_name(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'full_name'})
test_change_user_delivery_email_email_address_visibilty_admins:
no extra work for delivery_email
check avatar fields more directly
roles (several examples) -- actually check the specific role
def test_change_realm_authentication_methods(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
+ self.assertEqual(events[0]['person']['role'], role)
bot_owner_id: no extra work!
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
timezone: no extra work!
- timezone_schema_checker('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"email", "timezone"})
2020-07-23 16:04:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_realm_user_update(
|
2020-08-05 12:31:09 +02:00
|
|
|
# person_flavor tells us which extra fields we need
|
|
|
|
var_name: str,
|
|
|
|
event: Dict[str, object],
|
|
|
|
person_flavor: str,
|
event_schema: Extract check_realm_user_update.
This a pretty big commit, but I really wanted it
to be atomic.
All realm_user/update events look the same from
the top:
_check_realm_user_update = check_events_dict(
required_keys=[
("type", equals("realm_user")),
("op", equals("update")),
("person", _check_realm_user_person),
]
)
And then we have a bunch of fields for person that
are optional, and we usually only send user_id plus
one other field, with the exception of avatar-related
events:
_check_realm_user_person = check_dict_only(
required_keys=[
# vertical formatting
("user_id", check_int),
],
optional_keys=[
("avatar_source", check_string),
("avatar_url", check_none_or(check_string)),
("avatar_url_medium", check_none_or(check_string)),
("avatar_version", check_int),
("bot_owner_id", check_int),
("custom_profile_field", _check_custom_profile_field),
("delivery_email", check_string),
("full_name", check_string),
("role", check_int_in(UserProfile.ROLE_TYPES)),
("email", check_string),
("user_id", check_int),
("timezone", check_string),
],
)
I would start the code review by just skimming the changes
to event_schema.py, to get the big picture of the complexity
here. Basically the schema is just the combined superset of
all the individual schemas that we remove from test_events.
Then I would read test_events.py.
The simplest diffs are basically of this form:
- schema_checker = check_events_dict([
- ('type', equals('realm_user')),
- ('op', equals('update')),
- ('person', check_dict_only([
- ('role', check_int_in(UserProfile.ROLE_TYPES)),
- ('user_id', check_int),
- ])),
- ])
# ...
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
Instead of a custom schema checker, we use the "superset"
schema checker, but then we pass in the set of fields that we
expect to be there. Note that 'user_id' is always there.
So most of the heavy lifting happens in this new function
in event_schema.py:
def check_realm_user_update(
var_name: str, event: Dict[str, Any], optional_fields: Set[str],
) -> None:
_check_realm_user_update(var_name, event)
keys = set(event["person"].keys()) - {"user_id"}
assert optional_fields == keys
But we still do some more custom checks in test_events.py.
custom profile fields: check keys of custom_profile_field
def test_custom_profile_field_data_events(self) -> None:
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value", "rendered_value"}
+ )
+ check_realm_user_update('events[0]', events[0], {"custom_profile_field"})
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value"}
+ )
avatar fields: check more specific types, since the superset
schema has check_none_or(check_string)
def test_change_avatar_fields(self) -> None:
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ assert isinstance(events[0]['person']['avatar_url'], str)
+ assert isinstance(events[0]['person']['avatar_url_medium'], str)
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ self.assertEqual(events[0]['person']['avatar_url'], None)
+ self.assertEqual(events[0]['person']['avatar_url_medium'], None)
Also note that avatar_fields is a set of four fields that
are set in event_schema.
full name: no extra work!
def test_change_full_name(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'full_name'})
test_change_user_delivery_email_email_address_visibilty_admins:
no extra work for delivery_email
check avatar fields more directly
roles (several examples) -- actually check the specific role
def test_change_realm_authentication_methods(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
+ self.assertEqual(events[0]['person']['role'], role)
bot_owner_id: no extra work!
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
timezone: no extra work!
- timezone_schema_checker('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"email", "timezone"})
2020-07-23 16:04:06 +02:00
|
|
|
) -> None:
|
|
|
|
_check_realm_user_update(var_name, event)
|
|
|
|
|
2020-08-05 12:31:09 +02:00
|
|
|
check_data(
|
|
|
|
realm_user_person_types[person_flavor],
|
|
|
|
f"{var_name}['person']",
|
|
|
|
event["person"],
|
|
|
|
)
|
event_schema: Extract check_realm_user_update.
This a pretty big commit, but I really wanted it
to be atomic.
All realm_user/update events look the same from
the top:
_check_realm_user_update = check_events_dict(
required_keys=[
("type", equals("realm_user")),
("op", equals("update")),
("person", _check_realm_user_person),
]
)
And then we have a bunch of fields for person that
are optional, and we usually only send user_id plus
one other field, with the exception of avatar-related
events:
_check_realm_user_person = check_dict_only(
required_keys=[
# vertical formatting
("user_id", check_int),
],
optional_keys=[
("avatar_source", check_string),
("avatar_url", check_none_or(check_string)),
("avatar_url_medium", check_none_or(check_string)),
("avatar_version", check_int),
("bot_owner_id", check_int),
("custom_profile_field", _check_custom_profile_field),
("delivery_email", check_string),
("full_name", check_string),
("role", check_int_in(UserProfile.ROLE_TYPES)),
("email", check_string),
("user_id", check_int),
("timezone", check_string),
],
)
I would start the code review by just skimming the changes
to event_schema.py, to get the big picture of the complexity
here. Basically the schema is just the combined superset of
all the individual schemas that we remove from test_events.
Then I would read test_events.py.
The simplest diffs are basically of this form:
- schema_checker = check_events_dict([
- ('type', equals('realm_user')),
- ('op', equals('update')),
- ('person', check_dict_only([
- ('role', check_int_in(UserProfile.ROLE_TYPES)),
- ('user_id', check_int),
- ])),
- ])
# ...
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
Instead of a custom schema checker, we use the "superset"
schema checker, but then we pass in the set of fields that we
expect to be there. Note that 'user_id' is always there.
So most of the heavy lifting happens in this new function
in event_schema.py:
def check_realm_user_update(
var_name: str, event: Dict[str, Any], optional_fields: Set[str],
) -> None:
_check_realm_user_update(var_name, event)
keys = set(event["person"].keys()) - {"user_id"}
assert optional_fields == keys
But we still do some more custom checks in test_events.py.
custom profile fields: check keys of custom_profile_field
def test_custom_profile_field_data_events(self) -> None:
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value", "rendered_value"}
+ )
+ check_realm_user_update('events[0]', events[0], {"custom_profile_field"})
+ self.assertEqual(
+ events[0]['person']['custom_profile_field'].keys(),
+ {"id", "value"}
+ )
avatar fields: check more specific types, since the superset
schema has check_none_or(check_string)
def test_change_avatar_fields(self) -> None:
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ assert isinstance(events[0]['person']['avatar_url'], str)
+ assert isinstance(events[0]['person']['avatar_url_medium'], str)
+ check_realm_user_update('events[0]', events[0], avatar_fields)
+ self.assertEqual(events[0]['person']['avatar_url'], None)
+ self.assertEqual(events[0]['person']['avatar_url_medium'], None)
Also note that avatar_fields is a set of four fields that
are set in event_schema.
full name: no extra work!
def test_change_full_name(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'full_name'})
test_change_user_delivery_email_email_address_visibilty_admins:
no extra work for delivery_email
check avatar fields more directly
roles (several examples) -- actually check the specific role
def test_change_realm_authentication_methods(self) -> None:
- schema_checker('events[0]', events[0])
+ check_realm_user_update('events[0]', events[0], {'role'})
+ self.assertEqual(events[0]['person']['role'], role)
bot_owner_id: no extra work!
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
- change_bot_owner_checker_user('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"bot_owner_id"})
timezone: no extra work!
- timezone_schema_checker('events[1]', events[1])
+ check_realm_user_update('events[1]', events[1], {"email", "timezone"})
2020-07-23 16:04:06 +02:00
|
|
|
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
stream_create_event = event_dict_type(
|
2020-07-08 12:53:52 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("stream")),
|
|
|
|
("op", Equals("create")),
|
|
|
|
("streams", ListType(DictType(basic_stream_fields))),
|
2020-07-08 12:53:52 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_stream_create = make_checker(stream_create_event)
|
2020-07-08 13:35:37 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
stream_delete_event = event_dict_type(
|
2020-08-01 14:42:06 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("stream")),
|
|
|
|
("op", Equals("delete")),
|
|
|
|
("streams", ListType(DictType(basic_stream_fields))),
|
2020-08-01 14:42:06 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_stream_delete = make_checker(stream_delete_event)
|
2020-08-01 14:42:06 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
stream_update_event = event_dict_type(
|
2020-07-08 13:35:37 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("stream")),
|
|
|
|
("op", Equals("update")),
|
|
|
|
("property", str),
|
|
|
|
("value", optional_value_type),
|
|
|
|
("name", str),
|
|
|
|
("stream_id", int),
|
2020-07-08 13:35:37 +02:00
|
|
|
],
|
|
|
|
optional_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("rendered_description", str),
|
|
|
|
("history_public_to_subscribers", bool),
|
2020-07-08 13:35:37 +02:00
|
|
|
],
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_stream_update = make_checker(stream_update_event)
|
2020-07-08 13:35:37 +02:00
|
|
|
|
|
|
|
|
2020-08-08 13:41:21 +02:00
|
|
|
def check_stream_update(var_name: str, event: Dict[str, object],) -> None:
|
2020-07-08 13:35:37 +02:00
|
|
|
_check_stream_update(var_name, event)
|
|
|
|
prop = event["property"]
|
|
|
|
value = event["value"]
|
|
|
|
|
|
|
|
extra_keys = set(event.keys()) - {
|
|
|
|
"id",
|
|
|
|
"type",
|
|
|
|
"op",
|
|
|
|
"property",
|
|
|
|
"value",
|
|
|
|
"name",
|
|
|
|
"stream_id",
|
|
|
|
}
|
|
|
|
|
|
|
|
if prop == "description":
|
|
|
|
assert extra_keys == {"rendered_description"}
|
|
|
|
assert isinstance(value, str)
|
|
|
|
elif prop == "email_address":
|
|
|
|
assert extra_keys == set()
|
|
|
|
assert isinstance(value, str)
|
|
|
|
elif prop == "invite_only":
|
|
|
|
assert extra_keys == {"history_public_to_subscribers"}
|
|
|
|
assert isinstance(value, bool)
|
|
|
|
elif prop == "message_retention_days":
|
|
|
|
assert extra_keys == set()
|
|
|
|
if value is not None:
|
|
|
|
assert isinstance(value, int)
|
|
|
|
elif prop == "name":
|
|
|
|
assert extra_keys == set()
|
|
|
|
assert isinstance(value, str)
|
|
|
|
elif prop == "stream_post_policy":
|
|
|
|
assert extra_keys == set()
|
|
|
|
assert value in Stream.STREAM_POST_POLICY_TYPES
|
|
|
|
else:
|
|
|
|
raise AssertionError(f"Unknown property: {prop}")
|
2020-07-08 14:13:16 +02:00
|
|
|
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
submessage_event = event_dict_type(
|
2020-07-18 16:27:59 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("submessage")),
|
|
|
|
("message_id", int),
|
|
|
|
("submessage_id", int),
|
|
|
|
("sender_id", int),
|
|
|
|
("msg_type", str),
|
|
|
|
("content", str),
|
2020-07-18 16:27:59 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_submessage = make_checker(submessage_event)
|
2020-07-18 16:27:59 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
single_subscription_type = DictType(
|
2020-07-08 14:13:16 +02:00
|
|
|
required_keys=subscription_fields,
|
|
|
|
optional_keys=[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
("subscribers", ListType(int)),
|
2020-07-08 14:13:16 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
subscription_add_event = event_dict_type(
|
2020-07-08 14:13:16 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("subscription")),
|
|
|
|
("op", Equals("add")),
|
|
|
|
("subscriptions", ListType(single_subscription_type)),
|
2020-07-08 14:13:16 +02:00
|
|
|
],
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_subscription_add = make_checker(subscription_add_event)
|
2020-07-08 14:13:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_subscription_add(
|
2020-08-08 13:41:21 +02:00
|
|
|
var_name: str, event: Dict[str, object], include_subscribers: bool,
|
2020-07-08 14:13:16 +02:00
|
|
|
) -> None:
|
|
|
|
_check_subscription_add(var_name, event)
|
|
|
|
|
2020-08-08 13:41:21 +02:00
|
|
|
assert isinstance(event["subscriptions"], list)
|
2020-07-08 14:13:16 +02:00
|
|
|
for sub in event["subscriptions"]:
|
|
|
|
if include_subscribers:
|
|
|
|
assert "subscribers" in sub.keys()
|
|
|
|
else:
|
|
|
|
assert "subscribers" not in sub.keys()
|
2020-07-08 14:20:25 +02:00
|
|
|
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
subscription_peer_add_event = event_dict_type(
|
2020-07-08 15:04:35 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("subscription")),
|
|
|
|
("op", Equals("peer_add")),
|
|
|
|
("user_id", int),
|
|
|
|
("stream_id", int),
|
2020-07-08 15:04:35 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_subscription_peer_add = make_checker(subscription_peer_add_event)
|
2020-07-08 15:04:35 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
subscription_peer_remove_event = event_dict_type(
|
2020-07-08 15:04:35 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("subscription")),
|
|
|
|
("op", Equals("peer_remove")),
|
|
|
|
("user_id", int),
|
|
|
|
("stream_id", int),
|
2020-07-08 15:04:35 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_subscription_peer_remove = make_checker(subscription_peer_remove_event)
|
2020-07-08 15:04:35 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
remove_sub_type = DictType(
|
2020-07-08 14:20:25 +02:00
|
|
|
required_keys=[
|
|
|
|
# We should eventually just return stream_id here.
|
2020-07-30 18:11:19 +02:00
|
|
|
("name", str),
|
|
|
|
("stream_id", int),
|
2020-07-08 14:20:25 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
subscription_remove_event = event_dict_type(
|
2020-07-08 14:20:25 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("subscription")),
|
|
|
|
("op", Equals("remove")),
|
|
|
|
("subscriptions", ListType(remove_sub_type)),
|
2020-07-08 14:20:25 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_subscription_remove = make_checker(subscription_remove_event)
|
2020-07-08 15:29:13 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
typing_person_type = DictType(
|
2020-07-18 16:39:06 +02:00
|
|
|
required_keys=[
|
|
|
|
# we should eventually just send user_id
|
2020-07-30 18:11:19 +02:00
|
|
|
("email", str),
|
|
|
|
("user_id", int),
|
2020-07-18 16:39:06 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
typing_start_event = event_dict_type(
|
2020-07-18 16:39:06 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("typing")),
|
|
|
|
("op", Equals("start")),
|
|
|
|
("sender", typing_person_type),
|
|
|
|
("recipients", ListType(typing_person_type)),
|
2020-07-18 16:39:06 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_typing_start = make_checker(typing_start_event)
|
2020-07-18 16:39:06 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
typing_stop_event = event_dict_type(
|
2020-08-27 22:10:07 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("typing")),
|
|
|
|
("op", Equals("stop")),
|
|
|
|
("sender", typing_person_type),
|
|
|
|
("recipients", ListType(typing_person_type)),
|
2020-08-27 22:10:07 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_typing_stop = make_checker(typing_stop_event)
|
2020-08-27 22:10:07 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
update_display_settings_event = event_dict_type(
|
2020-07-08 15:29:13 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("update_display_settings")),
|
|
|
|
("setting_name", str),
|
|
|
|
("setting", value_type),
|
|
|
|
("user", str),
|
2020-07-08 15:29:13 +02:00
|
|
|
],
|
|
|
|
optional_keys=[
|
|
|
|
# force vertical
|
2020-07-30 18:11:19 +02:00
|
|
|
("language_name", str),
|
2020-07-08 15:29:13 +02:00
|
|
|
],
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_update_display_settings = make_checker(update_display_settings_event)
|
2020-07-08 15:29:13 +02:00
|
|
|
|
|
|
|
|
2020-08-08 13:41:21 +02:00
|
|
|
def check_update_display_settings(var_name: str, event: Dict[str, object],) -> None:
|
2020-07-08 15:29:13 +02:00
|
|
|
"""
|
|
|
|
Display setting events have a "setting" field that
|
|
|
|
is more specifically typed according to the
|
|
|
|
UserProfile.property_types dictionary.
|
|
|
|
"""
|
|
|
|
_check_update_display_settings(var_name, event)
|
|
|
|
setting_name = event["setting_name"]
|
|
|
|
setting = event["setting"]
|
|
|
|
|
2020-08-08 13:41:21 +02:00
|
|
|
assert isinstance(setting_name, str)
|
2020-07-08 15:29:13 +02:00
|
|
|
setting_type = UserProfile.property_types[setting_name]
|
|
|
|
assert isinstance(setting, setting_type)
|
|
|
|
|
|
|
|
if setting_name == "default_language":
|
|
|
|
assert "language_name" in event.keys()
|
|
|
|
else:
|
|
|
|
assert "language_name" not in event.keys()
|
2020-07-08 15:29:13 +02:00
|
|
|
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
update_global_notifications_event = event_dict_type(
|
2020-07-08 15:29:13 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("update_global_notifications")),
|
|
|
|
("notification_name", str),
|
|
|
|
("setting", value_type),
|
|
|
|
("user", str),
|
2020-07-08 15:29:13 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_update_global_notifications = make_checker(update_global_notifications_event)
|
2020-07-08 15:29:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_update_global_notifications(
|
2020-08-08 13:41:21 +02:00
|
|
|
var_name: str, event: Dict[str, object], desired_val: Union[bool, int, str],
|
2020-07-08 15:29:13 +02:00
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
See UserProfile.notification_setting_types for
|
|
|
|
more details.
|
|
|
|
"""
|
|
|
|
_check_update_global_notifications(var_name, event)
|
|
|
|
setting_name = event["notification_name"]
|
|
|
|
setting = event["setting"]
|
|
|
|
assert setting == desired_val
|
|
|
|
|
2020-08-08 13:41:21 +02:00
|
|
|
assert isinstance(setting_name, str)
|
2020-07-08 15:29:13 +02:00
|
|
|
setting_type = UserProfile.notification_setting_types[setting_name]
|
|
|
|
assert isinstance(setting, setting_type)
|
2020-07-10 18:35:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
update_message_required_fields = [
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("update_message")),
|
|
|
|
("user_id", int),
|
|
|
|
("edit_timestamp", int),
|
|
|
|
("message_id", int),
|
2020-07-10 18:35:58 +02:00
|
|
|
]
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
update_message_content_fields: List[Tuple[str, object]] = [
|
|
|
|
("content", str),
|
|
|
|
("is_me_message", bool),
|
|
|
|
("orig_content", str),
|
|
|
|
("orig_rendered_content", str),
|
|
|
|
("prev_rendered_content_version", int),
|
|
|
|
("rendered_content", str),
|
2020-07-10 18:35:58 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
update_message_topic_fields = [
|
2020-07-30 18:11:19 +02:00
|
|
|
("flags", ListType(str)),
|
|
|
|
("message_ids", ListType(int)),
|
|
|
|
("new_stream_id", int),
|
|
|
|
(ORIG_TOPIC, str),
|
2020-08-05 12:53:36 +02:00
|
|
|
(
|
|
|
|
"propagate_mode",
|
|
|
|
EnumType(
|
|
|
|
[
|
|
|
|
# order matches openapi spec
|
|
|
|
"change_one",
|
|
|
|
"change_later",
|
|
|
|
"change_all",
|
|
|
|
]
|
|
|
|
),
|
|
|
|
),
|
2020-07-30 18:11:19 +02:00
|
|
|
("stream_id", int),
|
|
|
|
("stream_name", str),
|
|
|
|
(TOPIC_LINKS, ListType(str)),
|
|
|
|
(TOPIC_NAME, str),
|
2020-07-10 18:35:58 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
update_message_optional_fields = (
|
|
|
|
update_message_content_fields + update_message_topic_fields
|
|
|
|
)
|
|
|
|
|
|
|
|
# The schema here does not include the "embedded"
|
|
|
|
# variant of update_message; it is for message
|
|
|
|
# and topic editing.
|
2020-07-30 18:11:19 +02:00
|
|
|
update_message_event = event_dict_type(
|
2020-07-10 18:35:58 +02:00
|
|
|
required_keys=update_message_required_fields,
|
|
|
|
optional_keys=update_message_optional_fields,
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_update_message = make_checker(update_message_event)
|
2020-07-10 18:35:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_update_message(
|
|
|
|
var_name: str,
|
2020-08-08 13:41:21 +02:00
|
|
|
event: Dict[str, object],
|
2020-07-10 18:35:58 +02:00
|
|
|
has_content: bool,
|
|
|
|
has_topic: bool,
|
|
|
|
has_new_stream_id: bool,
|
|
|
|
) -> None:
|
|
|
|
# Always check the basic schema first.
|
|
|
|
_check_update_message(var_name, event)
|
|
|
|
|
|
|
|
actual_keys = set(event.keys())
|
|
|
|
expected_keys = {"id"}
|
|
|
|
expected_keys.update(tup[0] for tup in update_message_required_fields)
|
|
|
|
|
|
|
|
if has_content:
|
|
|
|
expected_keys.update(tup[0] for tup in update_message_content_fields)
|
|
|
|
|
|
|
|
if has_topic:
|
|
|
|
expected_keys.update(tup[0] for tup in update_message_topic_fields)
|
|
|
|
|
|
|
|
if not has_new_stream_id:
|
|
|
|
expected_keys.discard("new_stream_id")
|
|
|
|
|
|
|
|
assert expected_keys == actual_keys
|
|
|
|
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
update_message_embedded_event = event_dict_type(
|
2020-07-10 18:35:58 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("update_message")),
|
|
|
|
("flags", ListType(str)),
|
|
|
|
("content", str),
|
|
|
|
("message_id", int),
|
|
|
|
("message_ids", ListType(int)),
|
|
|
|
("rendered_content", str),
|
2020-07-10 18:35:58 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_update_message_embedded = make_checker(update_message_embedded_event)
|
2020-07-17 09:13:10 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
update_message_flags_event = event_dict_type(
|
2020-07-17 09:13:10 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("update_message_flags")),
|
|
|
|
("op", equals_add_or_remove),
|
|
|
|
("operation", equals_add_or_remove),
|
|
|
|
("flag", str),
|
|
|
|
("messages", ListType(int)),
|
|
|
|
("all", bool),
|
2020-07-17 09:13:10 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
_check_update_message_flags = make_checker(update_message_flags_event)
|
2020-07-17 09:13:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_update_message_flags(
|
2020-08-08 13:41:21 +02:00
|
|
|
var_name: str, event: Dict[str, object], operation: str
|
2020-07-17 09:13:10 +02:00
|
|
|
) -> None:
|
|
|
|
_check_update_message_flags(var_name, event)
|
2020-08-19 21:51:47 +02:00
|
|
|
assert event["operation"] == operation and event['op'] == operation
|
2020-07-18 17:15:23 +02:00
|
|
|
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
group_type = DictType(
|
2020-07-18 17:19:30 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("id", int),
|
|
|
|
("name", str),
|
|
|
|
("members", ListType(int)),
|
|
|
|
("description", str),
|
2020-07-18 17:19:30 +02:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
user_group_add_event = event_dict_type(
|
2020-07-18 17:19:30 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("user_group")),
|
|
|
|
("op", Equals("add")),
|
|
|
|
("group", group_type),
|
2020-07-18 17:19:30 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_user_group_add = make_checker(user_group_add_event)
|
2020-07-18 17:19:30 +02:00
|
|
|
|
2020-07-30 18:11:19 +02:00
|
|
|
user_status_event = event_dict_type(
|
2020-07-18 17:15:23 +02:00
|
|
|
required_keys=[
|
2020-07-30 18:11:19 +02:00
|
|
|
("type", Equals("user_status")),
|
|
|
|
("user_id", int),
|
|
|
|
("away", bool),
|
|
|
|
("status_text", str),
|
2020-07-18 17:15:23 +02:00
|
|
|
]
|
|
|
|
)
|
2020-07-30 18:11:19 +02:00
|
|
|
check_user_status = make_checker(user_status_event)
|