|
| 1 | +""" |
| 2 | +Test extract-attachments on document update in docs core app. |
| 3 | +""" |
| 4 | + |
| 5 | +import base64 |
| 6 | +from uuid import uuid4 |
| 7 | + |
| 8 | +import pytest |
| 9 | +import y_py |
| 10 | +from rest_framework.test import APIClient |
| 11 | + |
| 12 | +from core import factories |
| 13 | + |
| 14 | +pytestmark = pytest.mark.django_db |
| 15 | + |
| 16 | + |
| 17 | +def get_ydoc_with_mages(image_keys): |
| 18 | + """Return a ydoc from text for testing purposes.""" |
| 19 | + ydoc = y_py.YDoc() # pylint: disable=no-member |
| 20 | + with ydoc.begin_transaction() as txn: |
| 21 | + xml_fragment = ydoc.get_xml_element("document-store") |
| 22 | + for key in image_keys: |
| 23 | + xml_image = xml_fragment.push_xml_element(txn, "image") |
| 24 | + xml_image.set_attribute(txn, "src", f"http://localhost/media/{key:s}") |
| 25 | + |
| 26 | + update = y_py.encode_state_as_update(ydoc) # pylint: disable=no-member |
| 27 | + return base64.b64encode(update).decode("utf-8") |
| 28 | + |
| 29 | + |
| 30 | +def test_api_documents_update_new_attachment_keys_anonymous(django_assert_num_queries): |
| 31 | + """ |
| 32 | + When an anonymous user updates a document, the attachment keys extracted from the |
| 33 | + updated content should be added to the list of "attachments" ot the document if these |
| 34 | + attachments are already readable by anonymous users. |
| 35 | + """ |
| 36 | + image_keys = [f"{uuid4()!s}/attachments/{uuid4()!s}.png" for _ in range(4)] |
| 37 | + document = factories.DocumentFactory( |
| 38 | + content=get_ydoc_with_mages(image_keys[:1]), |
| 39 | + attachments=[image_keys[0]], |
| 40 | + link_reach="public", |
| 41 | + link_role="editor", |
| 42 | + ) |
| 43 | + |
| 44 | + factories.DocumentFactory(attachments=[image_keys[1]], link_reach="public") |
| 45 | + factories.DocumentFactory(attachments=[image_keys[2]], link_reach="authenticated") |
| 46 | + factories.DocumentFactory(attachments=[image_keys[3]], link_reach="restricted") |
| 47 | + expected_keys = {image_keys[i] for i in [0, 1]} |
| 48 | + |
| 49 | + with django_assert_num_queries(4): |
| 50 | + response = APIClient().put( |
| 51 | + f"/api/v1.0/documents/{document.id!s}/", |
| 52 | + {"content": get_ydoc_with_mages(image_keys)}, |
| 53 | + format="json", |
| 54 | + ) |
| 55 | + assert response.status_code == 200 |
| 56 | + |
| 57 | + document.refresh_from_db() |
| 58 | + assert set(document.attachments) == expected_keys |
| 59 | + |
| 60 | + # Check that the db query to check attachments readability for extracted |
| 61 | + # keys is not done if the content changes but no new keys are found |
| 62 | + with django_assert_num_queries(3): |
| 63 | + response = APIClient().put( |
| 64 | + f"/api/v1.0/documents/{document.id!s}/", |
| 65 | + {"content": get_ydoc_with_mages(image_keys[:2])}, |
| 66 | + format="json", |
| 67 | + ) |
| 68 | + assert response.status_code == 200 |
| 69 | + |
| 70 | + document.refresh_from_db() |
| 71 | + assert len(document.attachments) == 2 |
| 72 | + assert set(document.attachments) == expected_keys |
| 73 | + |
| 74 | + |
| 75 | +def test_api_documents_update_new_attachment_keys_authenticated( |
| 76 | + django_assert_num_queries, |
| 77 | +): |
| 78 | + """ |
| 79 | + When an authenticated user updates a document, the attachment keys extracted from the |
| 80 | + updated content should be added to the list of "attachments" ot the document if these |
| 81 | + attachments are already readable by the editing user. |
| 82 | + """ |
| 83 | + user = factories.UserFactory() |
| 84 | + client = APIClient() |
| 85 | + client.force_login(user) |
| 86 | + |
| 87 | + image_keys = [f"{uuid4()!s}/attachments/{uuid4()!s}.png" for _ in range(5)] |
| 88 | + document = factories.DocumentFactory( |
| 89 | + content=get_ydoc_with_mages(image_keys[:1]), |
| 90 | + attachments=[image_keys[0]], |
| 91 | + users=[(user, "editor")], |
| 92 | + ) |
| 93 | + |
| 94 | + factories.DocumentFactory(attachments=[image_keys[1]], link_reach="public") |
| 95 | + factories.DocumentFactory(attachments=[image_keys[2]], link_reach="authenticated") |
| 96 | + factories.DocumentFactory(attachments=[image_keys[3]], link_reach="restricted") |
| 97 | + factories.DocumentFactory(attachments=[image_keys[4]], users=[user]) |
| 98 | + expected_keys = {image_keys[i] for i in [0, 1, 2, 4]} |
| 99 | + |
| 100 | + with django_assert_num_queries(5): |
| 101 | + response = client.put( |
| 102 | + f"/api/v1.0/documents/{document.id!s}/", |
| 103 | + {"content": get_ydoc_with_mages(image_keys)}, |
| 104 | + format="json", |
| 105 | + ) |
| 106 | + assert response.status_code == 200 |
| 107 | + |
| 108 | + document.refresh_from_db() |
| 109 | + assert set(document.attachments) == expected_keys |
| 110 | + |
| 111 | + # Check that the db query to check attachments readability for extracted |
| 112 | + # keys is not done if the content changes but no new keys are found |
| 113 | + with django_assert_num_queries(4): |
| 114 | + response = client.put( |
| 115 | + f"/api/v1.0/documents/{document.id!s}/", |
| 116 | + {"content": get_ydoc_with_mages(image_keys[:2])}, |
| 117 | + format="json", |
| 118 | + ) |
| 119 | + assert response.status_code == 200 |
| 120 | + |
| 121 | + document.refresh_from_db() |
| 122 | + assert len(document.attachments) == 4 |
| 123 | + assert set(document.attachments) == expected_keys |
| 124 | + |
| 125 | + |
| 126 | +def test_api_documents_update_new_attachment_keys_duplicate(): |
| 127 | + """ |
| 128 | + Duplicate keys in the content should not result in duplicates in the document's attachments. |
| 129 | + """ |
| 130 | + user = factories.UserFactory() |
| 131 | + client = APIClient() |
| 132 | + client.force_login(user) |
| 133 | + |
| 134 | + image_key1 = f"{uuid4()!s}/attachments/{uuid4()!s}.png" |
| 135 | + image_key2 = f"{uuid4()!s}/attachments/{uuid4()!s}.png" |
| 136 | + document = factories.DocumentFactory( |
| 137 | + content=get_ydoc_with_mages([image_key1]), |
| 138 | + attachments=[image_key1], |
| 139 | + users=[(user, "editor")], |
| 140 | + ) |
| 141 | + |
| 142 | + factories.DocumentFactory(attachments=[image_key2], users=[user]) |
| 143 | + |
| 144 | + response = client.put( |
| 145 | + f"/api/v1.0/documents/{document.id!s}/", |
| 146 | + {"content": get_ydoc_with_mages([image_key1, image_key2, image_key2])}, |
| 147 | + format="json", |
| 148 | + ) |
| 149 | + assert response.status_code == 200 |
| 150 | + |
| 151 | + document.refresh_from_db() |
| 152 | + assert len(document.attachments) == 2 |
| 153 | + assert set(document.attachments) == {image_key1, image_key2} |
0 commit comments