We can't prevent document editors from copy/pasting content to from one document to another. The problem is that copying content, will copy the urls pointing to attachments but if we don't do anything, the reader of the document to which the content is being pasted, may not be allowed to access the attachment files from the original document. Using the work from the previous commit, we can grant access to the readers of the target document by extracting the attachment keys from the content and adding themto the target document's "attachments" field. Before doing this, we check that the current user can indeed access the attachment files extracted from the content and that they are allowed to edit the current document.
78 lines
3.3 KiB
Python
78 lines
3.3 KiB
Python
"""Test util base64_yjs_to_text."""
|
|
|
|
import base64
|
|
import uuid
|
|
|
|
import y_py
|
|
|
|
from core import utils
|
|
|
|
# This base64 string is an example of what is saved in the database.
|
|
# This base64 is generated from the blocknote editor, it contains
|
|
# the text \n# *Hello* \n- w**or**ld
|
|
TEST_BASE64_STRING = (
|
|
"AR717vLVDgAHAQ5kb2N1bWVudC1zdG9yZQMKYmxvY2tHcm91cAcA9e7y1Q4AAw5ibG9ja0NvbnRh"
|
|
"aW5lcgcA9e7y1Q4BAwdoZWFkaW5nBwD17vLVDgIGBgD17vLVDgMGaXRhbGljAnt9hPXu8tUOBAVI"
|
|
"ZWxsb4b17vLVDgkGaXRhbGljBG51bGwoAPXu8tUOAg10ZXh0QWxpZ25tZW50AXcEbGVmdCgA9e7y"
|
|
"1Q4CBWxldmVsAX0BKAD17vLVDgECaWQBdyQwNGQ2MjM0MS04MzI2LTQyMzYtYTA4My00ODdlMjZm"
|
|
"YWQyMzAoAPXu8tUOAQl0ZXh0Q29sb3IBdwdkZWZhdWx0KAD17vLVDgEPYmFja2dyb3VuZENvbG9y"
|
|
"AXcHZGVmYXVsdIf17vLVDgEDDmJsb2NrQ29udGFpbmVyBwD17vLVDhADDmJ1bGxldExpc3RJdGVt"
|
|
"BwD17vLVDhEGBAD17vLVDhIBd4b17vLVDhMEYm9sZAJ7fYT17vLVDhQCb3KG9e7y1Q4WBGJvbGQE"
|
|
"bnVsbIT17vLVDhcCbGQoAPXu8tUOEQ10ZXh0QWxpZ25tZW50AXcEbGVmdCgA9e7y1Q4QAmlkAXck"
|
|
"ZDM1MWUwNjgtM2U1NS00MjI2LThlYTUtYWJiMjYzMTk4ZTJhKAD17vLVDhAJdGV4dENvbG9yAXcH"
|
|
"ZGVmYXVsdCgA9e7y1Q4QD2JhY2tncm91bmRDb2xvcgF3B2RlZmF1bHSH9e7y1Q4QAw5ibG9ja0Nv"
|
|
"bnRhaW5lcgcA9e7y1Q4eAwlwYXJhZ3JhcGgoAPXu8tUOHw10ZXh0QWxpZ25tZW50AXcEbGVmdCgA"
|
|
"9e7y1Q4eAmlkAXckODk3MDBjMDctZTBlMS00ZmUwLWFjYTItODQ5MzIwOWE3ZTQyKAD17vLVDh4J"
|
|
"dGV4dENvbG9yAXcHZGVmYXVsdCgA9e7y1Q4eD2JhY2tncm91bmRDb2xvcgF3B2RlZmF1bHQA"
|
|
)
|
|
|
|
|
|
def test_utils_base64_yjs_to_text():
|
|
"""Test extract text from saved yjs document"""
|
|
assert utils.base64_yjs_to_text(TEST_BASE64_STRING) == "Hello world"
|
|
|
|
|
|
def test_utils_base64_yjs_to_xml():
|
|
"""Test extract xml from saved yjs document"""
|
|
content = utils.base64_yjs_to_xml(TEST_BASE64_STRING)
|
|
assert (
|
|
'<heading "level"="1" "textAlignment"="left">Hello</heading>' in content
|
|
or '<heading "textAlignment"="left" "level"="1">Hello</heading>' in content
|
|
)
|
|
assert '<bulletListItem "textAlignment"="left">world</bulletListItem>' in content
|
|
|
|
|
|
def test_utils_extract_attachments():
|
|
"""
|
|
All attachment keys in the document content should be extracted.
|
|
"""
|
|
document_id = uuid.uuid4()
|
|
image_key1 = f"{document_id!s}/attachments/{uuid.uuid4()!s}.png"
|
|
image_url1 = f"http://localhost/media/{image_key1:s}"
|
|
|
|
image_key2 = f"{uuid.uuid4()!s}/attachments/{uuid.uuid4()!s}.png"
|
|
image_url2 = f"http://localhost/{image_key2:s}"
|
|
|
|
image_key3 = f"{uuid.uuid4()!s}/attachments/{uuid.uuid4()!s}.png"
|
|
image_url3 = f"http://localhost/media/{image_key3:s}"
|
|
|
|
ydoc = y_py.YDoc() # pylint: disable=no-member
|
|
with ydoc.begin_transaction() as txn:
|
|
xml_fragment = ydoc.get_xml_element("document-store")
|
|
|
|
xml_image = xml_fragment.push_xml_element(txn, "image")
|
|
xml_image.set_attribute(txn, "src", image_url1)
|
|
|
|
xml_image = xml_fragment.push_xml_element(txn, "image")
|
|
xml_image.set_attribute(txn, "src", image_url2)
|
|
|
|
xml_paragraph = xml_fragment.push_xml_element(txn, "paragraph")
|
|
xml_text = xml_paragraph.push_xml_text(txn)
|
|
xml_text.push(txn, image_url3)
|
|
|
|
update = y_py.encode_state_as_update(ydoc) # pylint: disable=no-member
|
|
base64_string = base64.b64encode(update).decode("utf-8")
|
|
|
|
# image_key2 is missing the "/media/" part and shouldn't get extracted
|
|
assert utils.extract_attachments(base64_string) == [image_key1, image_key3]
|