2019-11-18 15:48:49 +01:00
|
|
|
from typing import Dict, Any, Callable, Set, List, Optional, Tuple
|
2019-10-21 12:43:00 +02:00
|
|
|
|
|
|
|
from functools import wraps
|
|
|
|
|
2019-10-23 13:09:06 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2020-03-07 11:20:57 +01:00
|
|
|
from zerver.models import Client, Message, UserPresence, UserGroup, get_realm
|
2019-10-21 12:43:00 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2019-10-23 10:18:10 +02:00
|
|
|
from zerver.lib.events import do_events_register
|
2020-03-07 11:20:57 +01:00
|
|
|
from zerver.lib.actions import update_user_presence, do_add_realm_filter, do_add_reaction
|
2019-10-21 12:43:00 +02:00
|
|
|
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
GENERATOR_FUNCTIONS: Dict[str, Callable[..., Dict[Any, Any]]] = dict()
|
|
|
|
REGISTERED_GENERATOR_FUNCTIONS: Set[str] = set()
|
|
|
|
CALLED_GENERATOR_FUNCTIONS: Set[str] = set()
|
2019-10-21 12:43:00 +02:00
|
|
|
|
|
|
|
helpers = ZulipTestCase()
|
|
|
|
|
|
|
|
def openapi_param_value_generator(endpoints: List[str]) -> Callable[[Callable[..., Any]],
|
|
|
|
Callable[..., Any]]:
|
|
|
|
"""This decorator is used to register openapi param value genarator functions
|
|
|
|
with endpoints. Example usage:
|
|
|
|
|
|
|
|
@openapi_param_value_generator(["/messages/render:post"])
|
|
|
|
def ...
|
|
|
|
"""
|
|
|
|
def wrapper(generator_func: Callable[..., Dict[Any, Any]]) -> Callable[..., Dict[Any, Any]]:
|
|
|
|
@wraps(generator_func)
|
|
|
|
def _record_calls_wrapper(*args: Any, **kwargs: Any) -> Dict[Any, Any]:
|
|
|
|
CALLED_GENERATOR_FUNCTIONS.add(generator_func.__name__)
|
|
|
|
return generator_func(*args, **kwargs)
|
|
|
|
|
|
|
|
REGISTERED_GENERATOR_FUNCTIONS.add(generator_func.__name__)
|
|
|
|
for endpoint in endpoints:
|
|
|
|
GENERATOR_FUNCTIONS[endpoint] = _record_calls_wrapper
|
|
|
|
|
|
|
|
return _record_calls_wrapper
|
|
|
|
return wrapper
|
|
|
|
|
2019-11-18 15:48:49 +01:00
|
|
|
def patch_openapi_example_values(entry: str, params: List[Dict[str, Any]],
|
|
|
|
request_body: Optional[Dict[str, Any]]=None) \
|
|
|
|
-> Tuple[List[Dict[str, Any]], Optional[Dict[str, Any]]]:
|
|
|
|
if entry not in GENERATOR_FUNCTIONS:
|
|
|
|
return params, request_body
|
|
|
|
func = GENERATOR_FUNCTIONS[entry]
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
realm_example_values: Dict[str, Any] = func()
|
2019-11-18 15:48:49 +01:00
|
|
|
|
|
|
|
for param in params:
|
2019-10-21 12:43:00 +02:00
|
|
|
param_name = param["name"]
|
2019-11-18 15:48:49 +01:00
|
|
|
if param_name in realm_example_values:
|
|
|
|
param["example"] = realm_example_values[param_name]
|
|
|
|
|
|
|
|
if request_body is not None:
|
|
|
|
properties = request_body["content"]["multipart/form-data"]["schema"]["properties"]
|
|
|
|
for key, property in properties.items():
|
|
|
|
if key in realm_example_values:
|
|
|
|
property["example"] = realm_example_values[key]
|
|
|
|
return params, request_body
|
2019-10-21 12:43:00 +02:00
|
|
|
|
2019-11-14 06:03:17 +01:00
|
|
|
@openapi_param_value_generator(["/messages/{message_id}:get", "/messages/{message_id}/history:get",
|
2019-11-14 06:19:45 +01:00
|
|
|
"/messages/{message_id}:patch", "/messages/{message_id}:delete"])
|
2019-10-21 12:43:00 +02:00
|
|
|
def iago_message_id() -> Dict[str, int]:
|
|
|
|
return {
|
2020-03-07 11:43:05 +01:00
|
|
|
"message_id": helpers.send_stream_message(helpers.example_user("iago"), "Denmark")
|
2019-10-21 12:43:00 +02:00
|
|
|
}
|
2019-10-21 13:00:25 +02:00
|
|
|
|
2020-03-07 11:20:57 +01:00
|
|
|
@openapi_param_value_generator(["/messages/{message_id}/reactions:delete"])
|
|
|
|
def add_emoji_to_message() -> Dict[str, List[Dict[None, None]]]:
|
|
|
|
user_profile = helpers.example_user('iago')
|
|
|
|
|
|
|
|
# from OpenAPI format data in zulip.yaml
|
|
|
|
message_id = 41
|
|
|
|
emoji_name = 'octopus'
|
|
|
|
emoji_code = '1f419'
|
|
|
|
reaction_type = 'unicode_emoji'
|
|
|
|
|
|
|
|
message = Message.objects.select_related().get(id=message_id)
|
|
|
|
do_add_reaction(user_profile, message, emoji_name, emoji_code, reaction_type)
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
2019-10-21 13:54:46 +02:00
|
|
|
@openapi_param_value_generator(["/messages/flags:post"])
|
|
|
|
def update_flags_message_ids() -> Dict[str, List[int]]:
|
|
|
|
stream_name = "Venice"
|
2019-11-14 06:03:17 +01:00
|
|
|
helpers.subscribe(helpers.example_user("iago"), stream_name)
|
2019-10-21 13:54:46 +02:00
|
|
|
|
|
|
|
messages = []
|
|
|
|
for _ in range(3):
|
2020-03-07 11:43:05 +01:00
|
|
|
messages.append(helpers.send_stream_message(helpers.example_user("iago"), stream_name))
|
2019-10-21 13:54:46 +02:00
|
|
|
return {
|
|
|
|
"messages": messages,
|
|
|
|
}
|
2019-10-23 09:01:17 +02:00
|
|
|
|
2019-10-23 09:33:43 +02:00
|
|
|
@openapi_param_value_generator(["/mark_stream_as_read:post", "/users/me/{stream_id}/topics:get"])
|
2019-10-23 09:01:17 +02:00
|
|
|
def get_venice_stream_id() -> Dict[str, int]:
|
|
|
|
return {
|
|
|
|
"stream_id": helpers.get_stream_id("Venice"),
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:48:21 +01:00
|
|
|
@openapi_param_value_generator(["/streams/{stream_id}:patch"])
|
|
|
|
def update_stream() -> Dict[str, Any]:
|
|
|
|
stream = helpers.subscribe(helpers.example_user("iago"), "temp_stream 1")
|
|
|
|
return {
|
|
|
|
"stream_id": stream.id,
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:09:48 +01:00
|
|
|
@openapi_param_value_generator(["/streams/{stream_id}:delete"])
|
|
|
|
def create_temp_stream_and_get_id() -> Dict[str, int]:
|
2019-11-14 09:48:21 +01:00
|
|
|
stream = helpers.subscribe(helpers.example_user("iago"), "temp_stream 2")
|
2019-11-14 09:09:48 +01:00
|
|
|
return {
|
|
|
|
"stream_id": stream.id,
|
|
|
|
}
|
|
|
|
|
2019-10-23 09:01:17 +02:00
|
|
|
@openapi_param_value_generator(["/mark_topic_as_read:post"])
|
|
|
|
def get_denmark_stream_id_and_topic() -> Dict[str, Any]:
|
|
|
|
stream_name = "Denmark"
|
|
|
|
topic_name = "Tivoli Gardens"
|
|
|
|
|
2019-11-14 06:03:17 +01:00
|
|
|
helpers.subscribe(helpers.example_user("iago"), stream_name)
|
2020-03-07 11:43:05 +01:00
|
|
|
helpers.send_stream_message(helpers.example_user("hamlet"), stream_name, topic_name=topic_name)
|
2019-10-23 09:01:17 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
"stream_id": helpers.get_stream_id(stream_name),
|
|
|
|
"topic_name": topic_name,
|
|
|
|
}
|
2019-10-23 09:16:46 +02:00
|
|
|
|
|
|
|
@openapi_param_value_generator(["/users/me/subscriptions/properties:post"])
|
|
|
|
def update_subscription_data() -> Dict[str, List[Dict[str, Any]]]:
|
2019-11-14 06:03:17 +01:00
|
|
|
profile = helpers.example_user("iago")
|
|
|
|
helpers.subscribe(profile, "Verona")
|
|
|
|
helpers.subscribe(profile, "social")
|
2019-10-23 09:16:46 +02:00
|
|
|
return {
|
|
|
|
"subscription_data": [
|
|
|
|
{"stream_id": helpers.get_stream_id("Verona"), "property": "pin_to_top", "value": True},
|
|
|
|
{"stream_id": helpers.get_stream_id("social"), "property": "color", "value": "#f00f00"}
|
|
|
|
]
|
|
|
|
}
|
2019-10-23 10:18:10 +02:00
|
|
|
|
2019-11-14 06:34:39 +01:00
|
|
|
@openapi_param_value_generator(["/users/me/subscriptions:delete"])
|
|
|
|
def delete_subscription_data() -> Dict[str, List[Dict[None, None]]]:
|
|
|
|
iago = helpers.example_user("iago")
|
|
|
|
zoe = helpers.example_user("ZOE")
|
|
|
|
helpers.subscribe(iago, "Verona")
|
|
|
|
helpers.subscribe(iago, "social")
|
|
|
|
helpers.subscribe(zoe, "Verona")
|
|
|
|
helpers.subscribe(zoe, "social")
|
|
|
|
return {}
|
|
|
|
|
2019-10-23 10:18:10 +02:00
|
|
|
@openapi_param_value_generator(["/events:get"])
|
|
|
|
def get_events() -> Dict[str, Any]:
|
2019-11-14 06:03:17 +01:00
|
|
|
profile = helpers.example_user("iago")
|
|
|
|
helpers.subscribe(profile, "Verona")
|
2019-10-23 10:18:10 +02:00
|
|
|
client = Client.objects.create(name="curl-test-client-1")
|
2019-11-14 06:03:17 +01:00
|
|
|
response = do_events_register(profile, client, event_types=['message', 'realm_emoji'])
|
2020-03-07 11:43:05 +01:00
|
|
|
helpers.send_stream_message(helpers.example_user("hamlet"), "Verona")
|
2019-10-23 10:18:10 +02:00
|
|
|
return {
|
|
|
|
"queue_id": response["queue_id"],
|
|
|
|
"last_event_id": response["last_event_id"],
|
|
|
|
}
|
2019-10-23 10:21:41 +02:00
|
|
|
|
|
|
|
@openapi_param_value_generator(["/events:delete"])
|
|
|
|
def delete_event_queue() -> Dict[str, Any]:
|
2019-11-14 06:03:17 +01:00
|
|
|
profile = helpers.example_user("iago")
|
2019-10-23 10:21:41 +02:00
|
|
|
client = Client.objects.create(name="curl-test-client-2")
|
2019-11-14 06:03:17 +01:00
|
|
|
response = do_events_register(profile, client, event_types=['message'])
|
2019-10-23 10:21:41 +02:00
|
|
|
return {
|
|
|
|
"queue_id": response["queue_id"],
|
|
|
|
"last_event_id": response["last_event_id"],
|
|
|
|
}
|
2019-10-23 13:09:06 +02:00
|
|
|
|
|
|
|
@openapi_param_value_generator(["/users/{email}/presence:get"])
|
|
|
|
def get_user_presence() -> Dict[None, None]:
|
|
|
|
iago = helpers.example_user("iago")
|
|
|
|
client = Client.objects.create(name="curl-test-client-3")
|
|
|
|
update_user_presence(iago, client, timezone_now(), UserPresence.ACTIVE, False)
|
|
|
|
return {}
|
2019-11-14 09:16:11 +01:00
|
|
|
|
2019-11-18 04:57:21 +01:00
|
|
|
@openapi_param_value_generator(["/users:post"])
|
|
|
|
def create_user() -> Dict[str, str]:
|
|
|
|
return {
|
|
|
|
"email": helpers.nonreg_email("test")
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:16:11 +01:00
|
|
|
@openapi_param_value_generator(["/user_groups/create:post"])
|
|
|
|
def create_user_group_data() -> Dict[str, Any]:
|
|
|
|
return {
|
|
|
|
"members": [helpers.example_user("hamlet").id, helpers.example_user("othello").id]
|
|
|
|
}
|
2019-11-14 09:35:28 +01:00
|
|
|
|
2019-11-14 09:41:39 +01:00
|
|
|
@openapi_param_value_generator(["/user_groups/{group_id}:patch", "/user_groups/{group_id}:delete"])
|
2019-11-14 09:35:28 +01:00
|
|
|
def get_temp_user_group_id() -> Dict[str, str]:
|
2019-11-14 09:41:39 +01:00
|
|
|
user_group, _ = UserGroup.objects.get_or_create(name="temp", realm=get_realm("zulip"))
|
2019-11-14 09:35:28 +01:00
|
|
|
return {
|
|
|
|
"group_id": user_group.id,
|
|
|
|
}
|
2019-11-18 05:47:32 +01:00
|
|
|
|
|
|
|
@openapi_param_value_generator(["/realm/filters/{filter_id}:delete"])
|
|
|
|
def remove_realm_filters() -> Dict[str, Any]:
|
|
|
|
filter_id = do_add_realm_filter(get_realm("zulip"), "#(?P<id>[0-9]{2,8})", "https://github.com/zulip/zulip/pull/%(id)s")
|
|
|
|
return {
|
|
|
|
"filter_id": filter_id
|
|
|
|
}
|
2019-11-18 16:46:06 +01:00
|
|
|
|
2019-11-18 16:47:41 +01:00
|
|
|
@openapi_param_value_generator(["/realm/emoji/{emoji_name}:post", "/user_uploads:post"])
|
2019-11-18 16:46:06 +01:00
|
|
|
def upload_custom_emoji() -> Dict[str, Any]:
|
|
|
|
return {
|
|
|
|
"filename": "zerver/tests/images/animated_img.gif",
|
|
|
|
}
|