2020-07-21 18:04:50 +02:00
|
|
|
import time
|
2020-07-23 14:48:44 +02:00
|
|
|
from copy import deepcopy
|
2020-08-06 07:57:58 +02:00
|
|
|
from typing import Any, Dict, List, Optional
|
2020-07-21 18:04:50 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2020-07-21 18:04:50 +02:00
|
|
|
|
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
|
|
|
from zerver.models import Draft
|
|
|
|
|
|
|
|
|
|
|
|
class DraftCreationTests(ZulipTestCase):
|
2021-02-12 08:19:30 +01:00
|
|
|
def create_and_check_drafts_for_success(
|
|
|
|
self,
|
|
|
|
draft_dicts: List[Dict[str, Any]],
|
|
|
|
expected_draft_dicts: Optional[List[Dict[str, Any]]] = None,
|
|
|
|
) -> None:
|
2020-07-21 18:04:50 +02:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
|
|
|
|
# Now send a POST request to the API endpoint.
|
2020-08-07 01:09:47 +02:00
|
|
|
payload = {"drafts": orjson.dumps(draft_dicts).decode()}
|
2020-07-21 18:04:50 +02:00
|
|
|
resp = self.api_post(hamlet, "/api/v1/drafts", payload)
|
|
|
|
self.assert_json_success(resp)
|
|
|
|
|
|
|
|
# Finally check to make sure that the drafts were actually created properly.
|
2020-08-06 07:57:58 +02:00
|
|
|
new_draft_dicts = []
|
2021-07-30 19:11:13 +02:00
|
|
|
for draft in Draft.objects.filter(user_profile=hamlet).order_by("last_edit_time"):
|
2020-08-06 07:57:58 +02:00
|
|
|
draft_dict = draft.to_dict()
|
2020-08-06 08:10:35 +02:00
|
|
|
draft_dict.pop("id")
|
2020-08-06 07:57:58 +02:00
|
|
|
new_draft_dicts.append(draft_dict)
|
|
|
|
if expected_draft_dicts is None:
|
|
|
|
expected_draft_dicts = draft_dicts
|
2020-07-21 18:04:50 +02:00
|
|
|
self.assertEqual(new_draft_dicts, expected_draft_dicts)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def create_and_check_drafts_for_error(
|
|
|
|
self, draft_dicts: List[Dict[str, Any]], expected_message: str
|
|
|
|
) -> None:
|
2020-07-21 18:04:50 +02:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
|
2021-07-30 19:11:13 +02:00
|
|
|
initial_count = Draft.objects.count()
|
2020-07-21 18:04:50 +02:00
|
|
|
|
|
|
|
# Now send a POST request to the API endpoint.
|
2020-08-07 01:09:47 +02:00
|
|
|
payload = {"drafts": orjson.dumps(draft_dicts).decode()}
|
2020-07-21 18:04:50 +02:00
|
|
|
resp = self.api_post(hamlet, "/api/v1/drafts", payload)
|
|
|
|
self.assert_json_error(resp, expected_message)
|
|
|
|
|
|
|
|
# Make sure that there are no drafts in the database at the
|
|
|
|
# end of the test. Drafts should never be created in error
|
|
|
|
# conditions.
|
2021-07-30 19:11:13 +02:00
|
|
|
self.assertEqual(Draft.objects.count(), initial_count)
|
2020-07-21 18:04:50 +02:00
|
|
|
|
2020-08-02 20:35:11 +02:00
|
|
|
def test_require_enable_drafts_synchronization(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
hamlet.enable_drafts_synchronization = False
|
|
|
|
hamlet.save()
|
|
|
|
payload = {"drafts": "[]"}
|
|
|
|
resp = self.api_post(hamlet, "/api/v1/drafts", payload)
|
|
|
|
self.assert_json_error(resp, "User has disabled synchronizing drafts.")
|
|
|
|
|
2020-07-21 18:04:50 +02:00
|
|
|
def test_create_one_stream_draft_properly(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
visible_stream_name = self.get_streams(hamlet)[0]
|
|
|
|
visible_stream_id = self.get_stream_id(visible_stream_name)
|
2021-02-12 08:19:30 +01:00
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": [visible_stream_id],
|
|
|
|
"topic": "sync drafts",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
|
|
|
"timestamp": 1595479019,
|
|
|
|
}
|
|
|
|
]
|
2020-08-06 07:57:58 +02:00
|
|
|
self.create_and_check_drafts_for_success(draft_dicts)
|
2020-07-21 18:04:50 +02:00
|
|
|
|
|
|
|
def test_create_one_personal_message_draft_properly(self) -> None:
|
|
|
|
zoe = self.example_user("ZOE")
|
2021-02-12 08:19:30 +01:00
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"to": [zoe.id],
|
|
|
|
"topic": "This topic should be ignored.",
|
|
|
|
"content": "What if we made it possible to sync drafts in Zulip?",
|
|
|
|
"timestamp": 1595479019,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
expected_draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"to": [zoe.id],
|
|
|
|
"topic": "", # For private messages the topic should be ignored.
|
|
|
|
"content": "What if we made it possible to sync drafts in Zulip?",
|
|
|
|
"timestamp": 1595479019,
|
|
|
|
}
|
|
|
|
]
|
2020-07-21 18:04:50 +02:00
|
|
|
self.create_and_check_drafts_for_success(draft_dicts, expected_draft_dicts)
|
|
|
|
|
|
|
|
def test_create_one_group_personal_message_draft_properly(self) -> None:
|
|
|
|
zoe = self.example_user("ZOE")
|
|
|
|
othello = self.example_user("othello")
|
2021-02-12 08:19:30 +01:00
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"to": [zoe.id, othello.id],
|
|
|
|
"topic": "This topic should be ignored.",
|
|
|
|
"content": "What if we made it possible to sync drafts in Zulip?",
|
|
|
|
"timestamp": 1595479019,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
expected_draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"to": [zoe.id, othello.id],
|
|
|
|
"topic": "", # For private messages the topic should be ignored.
|
|
|
|
"content": "What if we made it possible to sync drafts in Zulip?",
|
|
|
|
"timestamp": 1595479019,
|
|
|
|
}
|
|
|
|
]
|
2020-07-21 18:04:50 +02:00
|
|
|
self.create_and_check_drafts_for_success(draft_dicts, expected_draft_dicts)
|
|
|
|
|
|
|
|
def test_create_batch_of_drafts_properly(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
visible_stream_name = self.get_streams(hamlet)[0]
|
|
|
|
visible_stream_id = self.get_stream_id(visible_stream_name)
|
|
|
|
zoe = self.example_user("ZOE")
|
|
|
|
othello = self.example_user("othello")
|
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": [visible_stream_id],
|
|
|
|
"topic": "sync drafts",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
2020-08-04 05:31:41 +02:00
|
|
|
"timestamp": 1595479019,
|
2020-07-21 18:04:50 +02:00
|
|
|
}, # Stream message draft
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"to": [zoe.id],
|
2020-08-06 07:57:58 +02:00
|
|
|
"topic": "",
|
2020-07-21 18:04:50 +02:00
|
|
|
"content": "What if we made it possible to sync drafts in Zulip?",
|
2020-08-04 05:31:41 +02:00
|
|
|
"timestamp": 1595479020,
|
2020-07-21 18:04:50 +02:00
|
|
|
}, # Private message draft
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"to": [zoe.id, othello.id],
|
|
|
|
"topic": "",
|
|
|
|
"content": "What if we made it possible to sync drafts in Zulip?",
|
2020-08-04 05:31:41 +02:00
|
|
|
"timestamp": 1595479021,
|
2020-07-21 18:04:50 +02:00
|
|
|
}, # Private group message draft
|
|
|
|
]
|
2020-08-06 07:57:58 +02:00
|
|
|
self.create_and_check_drafts_for_success(draft_dicts)
|
2020-07-21 18:04:50 +02:00
|
|
|
|
|
|
|
def test_missing_timestamps(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
"""If a timestamp is not provided for a draft dict then it should be automatically
|
|
|
|
filled in."""
|
2020-07-21 18:04:50 +02:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
visible_stream_name = self.get_streams(hamlet)[0]
|
|
|
|
visible_stream_id = self.get_stream_id(visible_stream_name)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": [visible_stream_id],
|
|
|
|
"topic": "sync drafts",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
|
|
|
}
|
|
|
|
]
|
2020-07-21 18:04:50 +02:00
|
|
|
|
2021-07-30 19:11:13 +02:00
|
|
|
initial_count = Draft.objects.count()
|
2020-07-21 18:04:50 +02:00
|
|
|
|
2020-08-04 05:31:41 +02:00
|
|
|
current_time = int(time.time())
|
2020-08-07 01:09:47 +02:00
|
|
|
payload = {"drafts": orjson.dumps(draft_dicts).decode()}
|
2020-07-21 18:04:50 +02:00
|
|
|
resp = self.api_post(hamlet, "/api/v1/drafts", payload)
|
2021-07-30 19:11:13 +02:00
|
|
|
ids = orjson.loads(resp.content)["ids"]
|
2020-07-21 18:04:50 +02:00
|
|
|
self.assert_json_success(resp)
|
|
|
|
|
2021-07-30 19:11:13 +02:00
|
|
|
new_drafts = Draft.objects.filter(id__gte=ids[0])
|
|
|
|
self.assertEqual(Draft.objects.count() - initial_count, 1)
|
2020-07-21 18:04:50 +02:00
|
|
|
new_draft = new_drafts[0].to_dict()
|
2020-08-04 05:31:41 +02:00
|
|
|
self.assertTrue(isinstance(new_draft["timestamp"], int))
|
2020-07-21 18:04:50 +02:00
|
|
|
# Since it would be too tricky to get the same times, perform
|
|
|
|
# a relative check.
|
2020-08-04 05:31:41 +02:00
|
|
|
self.assertTrue(new_draft["timestamp"] >= current_time)
|
2020-07-21 18:04:50 +02:00
|
|
|
|
|
|
|
def test_invalid_timestamp(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": [],
|
|
|
|
"topic": "sync drafts",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
|
|
|
"timestamp": -10.10,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
self.create_and_check_drafts_for_error(draft_dicts, "Timestamp must not be negative.")
|
2020-07-21 18:04:50 +02:00
|
|
|
|
|
|
|
def test_create_non_stream_draft_with_no_recipient(self) -> None:
|
2021-05-08 02:36:30 +02:00
|
|
|
"""When "to" is an empty list, the type should become "" as well."""
|
2020-07-21 18:04:50 +02:00
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"to": [],
|
|
|
|
"topic": "sync drafts",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
2020-08-04 05:31:41 +02:00
|
|
|
"timestamp": 1595479019,
|
2020-07-21 18:04:50 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "",
|
|
|
|
"to": [],
|
|
|
|
"topic": "sync drafts",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
2020-08-04 05:31:41 +02:00
|
|
|
"timestamp": 1595479019,
|
2020-07-21 18:04:50 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
expected_draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "",
|
|
|
|
"to": [],
|
|
|
|
"topic": "",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
2020-08-04 05:31:41 +02:00
|
|
|
"timestamp": 1595479019,
|
2020-07-21 18:04:50 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "",
|
|
|
|
"to": [],
|
|
|
|
"topic": "",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
2020-08-04 05:31:41 +02:00
|
|
|
"timestamp": 1595479019,
|
2020-07-21 18:04:50 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
self.create_and_check_drafts_for_success(draft_dicts, expected_draft_dicts)
|
|
|
|
|
|
|
|
def test_create_stream_draft_with_no_recipient(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": [],
|
|
|
|
"topic": "sync drafts",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
|
|
|
"timestamp": 15954790199,
|
|
|
|
}
|
|
|
|
]
|
2020-07-21 18:04:50 +02:00
|
|
|
self.create_and_check_drafts_for_error(
|
2021-02-12 08:19:30 +01:00
|
|
|
draft_dicts, "Must specify exactly 1 stream ID for stream messages"
|
2020-07-21 18:04:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_create_stream_draft_for_inaccessible_stream(self) -> None:
|
|
|
|
# When the user does not have permission to access the stream:
|
|
|
|
stream = self.make_stream("Secret Society", invite_only=True)
|
2021-02-12 08:19:30 +01:00
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": [stream.id], # This can't be accessed by hamlet.
|
|
|
|
"topic": "sync drafts",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
|
|
|
"timestamp": 1595479019,
|
|
|
|
}
|
|
|
|
]
|
2020-07-21 18:04:50 +02:00
|
|
|
self.create_and_check_drafts_for_error(draft_dicts, "Invalid stream id")
|
|
|
|
|
|
|
|
# When the stream itself does not exist:
|
2021-02-12 08:19:30 +01:00
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": [99999999999999], # Hopefully, this doesn't exist.
|
|
|
|
"topic": "sync drafts",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
|
|
|
"timestamp": 1595479019,
|
|
|
|
}
|
|
|
|
]
|
2020-07-21 18:04:50 +02:00
|
|
|
self.create_and_check_drafts_for_error(draft_dicts, "Invalid stream id")
|
|
|
|
|
|
|
|
def test_create_personal_message_draft_for_non_existing_user(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"to": [99999999999999], # Hopefully, this doesn't exist either.
|
|
|
|
"topic": "This topic should be ignored.",
|
|
|
|
"content": "What if we made it possible to sync drafts in Zulip?",
|
|
|
|
"timestamp": 1595479019,
|
|
|
|
}
|
|
|
|
]
|
2020-07-21 18:04:50 +02:00
|
|
|
self.create_and_check_drafts_for_error(draft_dicts, "Invalid user ID 99999999999999")
|
|
|
|
|
|
|
|
def test_create_draft_with_null_bytes(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "",
|
|
|
|
"to": [],
|
|
|
|
"topic": "sync drafts.",
|
|
|
|
"content": "Some regular \x00 content here",
|
|
|
|
"timestamp": 15954790199,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
self.create_and_check_drafts_for_error(draft_dicts, "Message must not contain null bytes")
|
|
|
|
|
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": [10],
|
|
|
|
"topic": "thinking about \x00",
|
|
|
|
"content": "Let's add backend support for syncing drafts.",
|
|
|
|
"timestamp": 15954790199,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
self.create_and_check_drafts_for_error(draft_dicts, "Topic must not contain null bytes")
|
2020-07-21 18:04:50 +02:00
|
|
|
|
2020-07-23 14:48:44 +02:00
|
|
|
|
|
|
|
class DraftEditTests(ZulipTestCase):
|
2020-08-02 20:35:11 +02:00
|
|
|
def test_require_enable_drafts_synchronization(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
hamlet.enable_drafts_synchronization = False
|
|
|
|
hamlet.save()
|
|
|
|
resp = self.api_patch(hamlet, "/api/v1/drafts/1", {"draft": {}})
|
|
|
|
self.assert_json_error(resp, "User has disabled synchronizing drafts.")
|
|
|
|
|
2020-07-23 14:48:44 +02:00
|
|
|
def test_edit_draft_successfully(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
visible_streams = self.get_streams(hamlet)
|
|
|
|
stream_a = self.get_stream_id(visible_streams[0])
|
|
|
|
stream_b = self.get_stream_id(visible_streams[1])
|
|
|
|
|
|
|
|
# Create a draft.
|
|
|
|
draft_dict = {
|
|
|
|
"type": "stream",
|
|
|
|
"to": [stream_a],
|
|
|
|
"topic": "drafts",
|
|
|
|
"content": "The API should be good",
|
2021-02-12 08:19:30 +01:00
|
|
|
"timestamp": 1595505700,
|
2020-07-23 14:48:44 +02:00
|
|
|
}
|
2021-02-12 08:19:30 +01:00
|
|
|
resp = self.api_post(
|
|
|
|
hamlet, "/api/v1/drafts", {"drafts": orjson.dumps([draft_dict]).decode()}
|
|
|
|
)
|
2020-07-23 14:48:44 +02:00
|
|
|
self.assert_json_success(resp)
|
2020-08-07 01:09:47 +02:00
|
|
|
new_draft_id = orjson.loads(resp.content)["ids"][0]
|
2020-07-23 14:48:44 +02:00
|
|
|
|
|
|
|
# Change the draft data.
|
|
|
|
draft_dict["content"] = "The API needs to be structured yet simple to use."
|
|
|
|
draft_dict["to"] = [stream_b]
|
|
|
|
draft_dict["topic"] = "designing drafts"
|
2020-08-04 05:31:41 +02:00
|
|
|
draft_dict["timestamp"] = 1595505800
|
2020-07-23 14:48:44 +02:00
|
|
|
|
|
|
|
# Update this change in the backend.
|
2021-02-12 08:19:30 +01:00
|
|
|
resp = self.api_patch(
|
|
|
|
hamlet, f"/api/v1/drafts/{new_draft_id}", {"draft": orjson.dumps(draft_dict).decode()}
|
|
|
|
)
|
2020-07-23 14:48:44 +02:00
|
|
|
self.assert_json_success(resp)
|
|
|
|
|
|
|
|
# Now make sure that the change was made successfully.
|
|
|
|
new_draft = Draft.objects.get(id=new_draft_id, user_profile=hamlet)
|
|
|
|
new_draft_dict = new_draft.to_dict()
|
2020-08-06 08:10:35 +02:00
|
|
|
new_draft_dict.pop("id")
|
2020-07-23 14:48:44 +02:00
|
|
|
self.assertEqual(new_draft_dict, draft_dict)
|
|
|
|
|
2022-02-08 00:13:33 +01:00
|
|
|
def test_edit_non_existent_draft(self) -> None:
|
2020-07-23 14:48:44 +02:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
|
2021-07-30 19:11:13 +02:00
|
|
|
initial_count = Draft.objects.count()
|
2020-07-23 14:48:44 +02:00
|
|
|
|
|
|
|
# Try to update a draft that doesn't exist.
|
|
|
|
draft_dict = {
|
|
|
|
"type": "stream",
|
|
|
|
"to": [10],
|
|
|
|
"topic": "drafts",
|
|
|
|
"content": "The API should be good",
|
2021-02-12 08:19:30 +01:00
|
|
|
"timestamp": 1595505700,
|
2020-07-23 14:48:44 +02:00
|
|
|
}
|
2021-02-12 08:19:30 +01:00
|
|
|
resp = self.api_patch(
|
|
|
|
hamlet, "/api/v1/drafts/999999999", {"draft": orjson.dumps(draft_dict).decode()}
|
|
|
|
)
|
2020-07-23 14:48:44 +02:00
|
|
|
self.assert_json_error(resp, "Draft does not exist", status_code=404)
|
|
|
|
|
|
|
|
# Now make sure that no changes were made.
|
2021-07-30 19:11:13 +02:00
|
|
|
self.assertEqual(Draft.objects.count() - initial_count, 0)
|
2020-07-23 14:48:44 +02:00
|
|
|
|
|
|
|
def test_edit_unowned_draft(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
visible_streams = self.get_streams(hamlet)
|
|
|
|
stream_id = self.get_stream_id(visible_streams[0])
|
|
|
|
|
|
|
|
# Create a draft.
|
|
|
|
draft_dict = {
|
|
|
|
"type": "stream",
|
|
|
|
"to": [stream_id],
|
|
|
|
"topic": "drafts",
|
|
|
|
"content": "The API should be good",
|
2021-02-12 08:19:30 +01:00
|
|
|
"timestamp": 1595505700,
|
2020-07-23 14:48:44 +02:00
|
|
|
}
|
2021-02-12 08:19:30 +01:00
|
|
|
resp = self.api_post(
|
|
|
|
hamlet, "/api/v1/drafts", {"drafts": orjson.dumps([draft_dict]).decode()}
|
|
|
|
)
|
2020-07-23 14:48:44 +02:00
|
|
|
self.assert_json_success(resp)
|
2020-08-07 01:09:47 +02:00
|
|
|
new_draft_id = orjson.loads(resp.content)["ids"][0]
|
2020-07-23 14:48:44 +02:00
|
|
|
|
|
|
|
# Change the draft data.
|
|
|
|
modified_draft_dict = deepcopy(draft_dict)
|
|
|
|
modified_draft_dict["content"] = "???"
|
|
|
|
|
|
|
|
# Update this change in the backend as a different user.
|
|
|
|
zoe = self.example_user("ZOE")
|
2021-02-12 08:19:30 +01:00
|
|
|
resp = self.api_patch(
|
|
|
|
zoe, f"/api/v1/drafts/{new_draft_id}", {"draft": orjson.dumps(draft_dict).decode()}
|
|
|
|
)
|
2020-07-23 14:48:44 +02:00
|
|
|
self.assert_json_error(resp, "Draft does not exist", status_code=404)
|
|
|
|
|
|
|
|
# Now make sure that no changes were made.
|
|
|
|
existing_draft = Draft.objects.get(id=new_draft_id, user_profile=hamlet)
|
|
|
|
existing_draft_dict = existing_draft.to_dict()
|
2020-08-06 08:10:35 +02:00
|
|
|
existing_draft_dict.pop("id")
|
2020-07-23 14:48:44 +02:00
|
|
|
self.assertEqual(existing_draft_dict, draft_dict)
|
2020-07-23 15:09:58 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-07-23 15:09:58 +02:00
|
|
|
class DraftDeleteTests(ZulipTestCase):
|
2020-08-02 20:35:11 +02:00
|
|
|
def test_require_enable_drafts_synchronization(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
hamlet.enable_drafts_synchronization = False
|
|
|
|
hamlet.save()
|
|
|
|
resp = self.api_delete(hamlet, "/api/v1/drafts/1")
|
|
|
|
self.assert_json_error(resp, "User has disabled synchronizing drafts.")
|
|
|
|
|
2020-07-23 15:09:58 +02:00
|
|
|
def test_delete_draft_successfully(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
visible_streams = self.get_streams(hamlet)
|
|
|
|
stream_id = self.get_stream_id(visible_streams[0])
|
|
|
|
|
|
|
|
# Make sure that there are no drafts at the start of this test.
|
2021-07-30 19:11:13 +02:00
|
|
|
initial_count = Draft.objects.count()
|
2020-07-23 15:09:58 +02:00
|
|
|
|
|
|
|
# Create a draft.
|
|
|
|
draft_dict = {
|
|
|
|
"type": "stream",
|
|
|
|
"to": [stream_id],
|
|
|
|
"topic": "drafts",
|
|
|
|
"content": "The API should be good",
|
2021-02-12 08:19:30 +01:00
|
|
|
"timestamp": 1595505700,
|
2020-07-23 15:09:58 +02:00
|
|
|
}
|
2021-02-12 08:19:30 +01:00
|
|
|
resp = self.api_post(
|
|
|
|
hamlet, "/api/v1/drafts", {"drafts": orjson.dumps([draft_dict]).decode()}
|
|
|
|
)
|
2020-07-23 15:09:58 +02:00
|
|
|
self.assert_json_success(resp)
|
2020-08-07 01:09:47 +02:00
|
|
|
new_draft_id = orjson.loads(resp.content)["ids"][0]
|
2020-07-23 15:09:58 +02:00
|
|
|
|
|
|
|
# Make sure that exactly 1 draft exists now.
|
2021-07-30 19:11:13 +02:00
|
|
|
self.assertEqual(Draft.objects.count() - initial_count, 1)
|
2020-07-23 15:09:58 +02:00
|
|
|
|
|
|
|
# Update this change in the backend.
|
|
|
|
resp = self.api_delete(hamlet, f"/api/v1/drafts/{new_draft_id}")
|
|
|
|
self.assert_json_success(resp)
|
|
|
|
|
|
|
|
# Now make sure that the there are no more drafts.
|
2021-07-30 19:11:13 +02:00
|
|
|
self.assertEqual(Draft.objects.count() - initial_count, 0)
|
2020-07-23 15:09:58 +02:00
|
|
|
|
2022-02-08 00:13:33 +01:00
|
|
|
def test_delete_non_existent_draft(self) -> None:
|
2020-07-23 15:09:58 +02:00
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
|
|
|
|
# Make sure that no draft exists in the first place.
|
2021-07-30 19:11:13 +02:00
|
|
|
initial_count = Draft.objects.count()
|
2020-07-23 15:09:58 +02:00
|
|
|
|
|
|
|
# Try to delete a draft that doesn't exist.
|
|
|
|
resp = self.api_delete(hamlet, "/api/v1/drafts/9999999999")
|
|
|
|
self.assert_json_error(resp, "Draft does not exist", status_code=404)
|
|
|
|
|
|
|
|
# Now make sure that no drafts were made for whatever reason.
|
2021-07-30 19:11:13 +02:00
|
|
|
self.assertEqual(Draft.objects.count() - initial_count, 0)
|
2020-07-23 15:09:58 +02:00
|
|
|
|
|
|
|
def test_delete_unowned_draft(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
visible_streams = self.get_streams(hamlet)
|
|
|
|
stream_id = self.get_stream_id(visible_streams[0])
|
|
|
|
|
|
|
|
# Make sure that there are no drafts at the start of this test.
|
2021-07-30 19:11:13 +02:00
|
|
|
initial_count = Draft.objects.count()
|
2020-07-23 15:09:58 +02:00
|
|
|
|
|
|
|
# Create a draft.
|
|
|
|
draft_dict = {
|
|
|
|
"type": "stream",
|
|
|
|
"to": [stream_id],
|
|
|
|
"topic": "drafts",
|
|
|
|
"content": "The API should be good",
|
2021-02-12 08:19:30 +01:00
|
|
|
"timestamp": 1595505700,
|
2020-07-23 15:09:58 +02:00
|
|
|
}
|
2021-02-12 08:19:30 +01:00
|
|
|
resp = self.api_post(
|
|
|
|
hamlet, "/api/v1/drafts", {"drafts": orjson.dumps([draft_dict]).decode()}
|
|
|
|
)
|
2020-07-23 15:09:58 +02:00
|
|
|
self.assert_json_success(resp)
|
2020-08-07 01:09:47 +02:00
|
|
|
new_draft_id = orjson.loads(resp.content)["ids"][0]
|
2020-07-23 15:09:58 +02:00
|
|
|
|
|
|
|
# Delete this draft in the backend as a different user.
|
|
|
|
zoe = self.example_user("ZOE")
|
|
|
|
resp = self.api_delete(zoe, f"/api/v1/drafts/{new_draft_id}")
|
|
|
|
self.assert_json_error(resp, "Draft does not exist", status_code=404)
|
|
|
|
|
|
|
|
# Make sure that the draft was not deleted.
|
2021-07-30 19:11:13 +02:00
|
|
|
self.assertEqual(Draft.objects.count() - initial_count, 1)
|
2020-07-23 15:09:58 +02:00
|
|
|
|
|
|
|
# Now make sure that no changes were made either.
|
|
|
|
existing_draft = Draft.objects.get(id=new_draft_id, user_profile=hamlet)
|
|
|
|
existing_draft_dict = existing_draft.to_dict()
|
2020-08-06 08:10:35 +02:00
|
|
|
existing_draft_dict.pop("id")
|
2020-07-23 15:09:58 +02:00
|
|
|
self.assertEqual(existing_draft_dict, draft_dict)
|
2020-07-23 21:23:17 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-07-23 21:23:17 +02:00
|
|
|
class DraftFetchTest(ZulipTestCase):
|
2020-08-02 20:35:11 +02:00
|
|
|
def test_require_enable_drafts_synchronization(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
hamlet.enable_drafts_synchronization = False
|
|
|
|
hamlet.save()
|
|
|
|
resp = self.api_get(hamlet, "/api/v1/drafts")
|
|
|
|
self.assert_json_error(resp, "User has disabled synchronizing drafts.")
|
|
|
|
|
2020-07-23 21:23:17 +02:00
|
|
|
def test_fetch_drafts(self) -> None:
|
2021-07-30 19:11:13 +02:00
|
|
|
initial_count = Draft.objects.count()
|
2020-07-23 21:23:17 +02:00
|
|
|
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
zoe = self.example_user("ZOE")
|
|
|
|
othello = self.example_user("othello")
|
|
|
|
visible_stream_id = self.get_stream_id(self.get_streams(hamlet)[0])
|
|
|
|
draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": [visible_stream_id],
|
|
|
|
"topic": "thinking out loud",
|
|
|
|
"content": "What if pigs really could fly?",
|
2020-08-06 08:10:35 +02:00
|
|
|
"timestamp": 15954790197,
|
2020-07-23 21:23:17 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "private",
|
2020-08-06 08:10:35 +02:00
|
|
|
"to": [zoe.id, othello.id],
|
2020-07-23 21:23:17 +02:00
|
|
|
"topic": "",
|
|
|
|
"content": "What if made it possible to sync drafts in Zulip?",
|
2020-08-06 08:10:35 +02:00
|
|
|
"timestamp": 15954790198,
|
2020-07-23 21:23:17 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "private",
|
2020-08-06 08:10:35 +02:00
|
|
|
"to": [zoe.id],
|
2020-07-23 21:23:17 +02:00
|
|
|
"topic": "",
|
|
|
|
"content": "What if made it possible to sync drafts in Zulip?",
|
2020-08-06 08:10:35 +02:00
|
|
|
"timestamp": 15954790199,
|
2020-07-23 21:23:17 +02:00
|
|
|
},
|
|
|
|
]
|
2020-08-07 01:09:47 +02:00
|
|
|
payload = {"drafts": orjson.dumps(draft_dicts).decode()}
|
2020-07-23 21:23:17 +02:00
|
|
|
resp = self.api_post(hamlet, "/api/v1/drafts", payload)
|
|
|
|
self.assert_json_success(resp)
|
|
|
|
|
2021-07-30 19:11:13 +02:00
|
|
|
self.assertEqual(Draft.objects.count() - initial_count, 3)
|
2020-07-23 21:23:17 +02:00
|
|
|
|
|
|
|
zoe_draft_dicts = [
|
|
|
|
{
|
|
|
|
"type": "private",
|
|
|
|
"to": [hamlet.id],
|
|
|
|
"topic": "",
|
|
|
|
"content": "Hello there!",
|
2020-08-06 08:10:35 +02:00
|
|
|
"timestamp": 15954790200,
|
2020-07-23 21:23:17 +02:00
|
|
|
},
|
|
|
|
]
|
2020-08-07 01:09:47 +02:00
|
|
|
payload = {"drafts": orjson.dumps(zoe_draft_dicts).decode()}
|
2020-07-23 21:23:17 +02:00
|
|
|
resp = self.api_post(zoe, "/api/v1/drafts", payload)
|
|
|
|
self.assert_json_success(resp)
|
|
|
|
|
2021-07-30 19:11:13 +02:00
|
|
|
self.assertEqual(Draft.objects.count() - initial_count, 4)
|
2020-07-23 21:23:17 +02:00
|
|
|
|
|
|
|
# Now actually fetch the drafts. Make sure that hamlet gets only
|
|
|
|
# his drafts and exactly as he made them.
|
|
|
|
resp = self.api_get(hamlet, "/api/v1/drafts")
|
|
|
|
self.assert_json_success(resp)
|
2020-08-07 01:09:47 +02:00
|
|
|
data = orjson.loads(resp.content)
|
2020-07-23 21:23:17 +02:00
|
|
|
self.assertEqual(data["count"], 3)
|
|
|
|
|
2021-07-30 19:11:13 +02:00
|
|
|
first_draft_id = Draft.objects.filter(user_profile=hamlet).order_by("id")[0].id
|
2020-08-06 08:10:35 +02:00
|
|
|
expected_draft_contents = [
|
2021-02-12 08:19:30 +01:00
|
|
|
{"id": first_draft_id + i, **draft_dicts[i]} for i in range(0, 3)
|
2020-08-06 08:10:35 +02:00
|
|
|
]
|
2020-07-23 21:23:17 +02:00
|
|
|
|
|
|
|
self.assertEqual(data["drafts"], expected_draft_contents)
|