|
| 1 | +"""Test the "media_check" endpoint.""" |
| 2 | + |
| 3 | +from io import BytesIO |
| 4 | +from uuid import uuid4 |
| 5 | + |
| 6 | +from django.core.files.storage import default_storage |
| 7 | + |
| 8 | +import pytest |
| 9 | +from rest_framework.test import APIClient |
| 10 | + |
| 11 | +from core import factories |
| 12 | +from core.enums import DocumentAttachmentStatus |
| 13 | +from core.tests.conftest import TEAM, USER, VIA |
| 14 | + |
| 15 | +pytestmark = pytest.mark.django_db |
| 16 | + |
| 17 | + |
| 18 | +def test_api_documents_media_check_unknown_document(): |
| 19 | + """ |
| 20 | + The "media_check" endpoint should return a 404 error if the document does not exist. |
| 21 | + """ |
| 22 | + client = APIClient() |
| 23 | + response = client.get(f"/api/v1.0/documents/{uuid4()!s}media-check/") |
| 24 | + assert response.status_code == 404 |
| 25 | + |
| 26 | + |
| 27 | +def test_api_documents_media_check_missing_key(): |
| 28 | + """ |
| 29 | + The "media_check" endpoint should return a 404 error if the key is missing. |
| 30 | + """ |
| 31 | + user = factories.UserFactory() |
| 32 | + |
| 33 | + client = APIClient() |
| 34 | + client.force_login(user=user) |
| 35 | + |
| 36 | + document = factories.DocumentFactory(users=[user]) |
| 37 | + |
| 38 | + response = client.get(f"/api/v1.0/documents/{document.id!s}/media-check/") |
| 39 | + assert response.status_code == 400 |
| 40 | + assert response.json() == {"detail": "Missing 'key' query parameter"} |
| 41 | + |
| 42 | + |
| 43 | +def test_api_documents_media_check_key_parameter_not_related_to_document(): |
| 44 | + """ |
| 45 | + The "media_check" endpoint should return a 404 error if the key is not related to the document. |
| 46 | + """ |
| 47 | + user = factories.UserFactory() |
| 48 | + |
| 49 | + client = APIClient() |
| 50 | + client.force_login(user=user) |
| 51 | + |
| 52 | + document = factories.DocumentFactory(users=[user]) |
| 53 | + |
| 54 | + response = client.get( |
| 55 | + f"/api/v1.0/documents/{document.id!s}/media-check/", |
| 56 | + {"key": f"{document.id!s}/attachments/unknown.jpg"}, |
| 57 | + ) |
| 58 | + assert response.status_code == 404 |
| 59 | + assert response.json() == {"detail": "Attachment missing"} |
| 60 | + |
| 61 | + |
| 62 | +def test_api_documents_media_check_anonymous_public_document(): |
| 63 | + """ |
| 64 | + The "media_check" endpoint should return a 200 status code if the document is public. |
| 65 | + """ |
| 66 | + document = factories.DocumentFactory(link_reach="public") |
| 67 | + |
| 68 | + filename = f"{uuid4()!s}.jpg" |
| 69 | + key = f"{document.id!s}/attachments/{filename:s}" |
| 70 | + default_storage.connection.meta.client.put_object( |
| 71 | + Bucket=default_storage.bucket_name, |
| 72 | + Key=key, |
| 73 | + Body=BytesIO(b"my prose"), |
| 74 | + ContentType="text/plain", |
| 75 | + Metadata={"status": DocumentAttachmentStatus.PROCESSING}, |
| 76 | + ) |
| 77 | + document.attachments = [key] |
| 78 | + document.save(update_fields=["attachments"]) |
| 79 | + |
| 80 | + client = APIClient() |
| 81 | + |
| 82 | + response = client.get( |
| 83 | + f"/api/v1.0/documents/{document.id!s}/media-check/", {"key": key} |
| 84 | + ) |
| 85 | + assert response.status_code == 200 |
| 86 | + assert response.json() == {"status": DocumentAttachmentStatus.PROCESSING} |
| 87 | + |
| 88 | + |
| 89 | +def test_api_documents_media_check_anonymous_public_document_ready(): |
| 90 | + """ |
| 91 | + The "media_check" endpoint should return a 200 status code if the document is public. |
| 92 | + """ |
| 93 | + document = factories.DocumentFactory(link_reach="public") |
| 94 | + |
| 95 | + filename = f"{uuid4()!s}.jpg" |
| 96 | + key = f"{document.id!s}/attachments/{filename:s}" |
| 97 | + default_storage.connection.meta.client.put_object( |
| 98 | + Bucket=default_storage.bucket_name, |
| 99 | + Key=key, |
| 100 | + Body=BytesIO(b"my prose"), |
| 101 | + ContentType="text/plain", |
| 102 | + Metadata={"status": DocumentAttachmentStatus.READY}, |
| 103 | + ) |
| 104 | + document.attachments = [key] |
| 105 | + document.save(update_fields=["attachments"]) |
| 106 | + |
| 107 | + client = APIClient() |
| 108 | + |
| 109 | + response = client.get( |
| 110 | + f"/api/v1.0/documents/{document.id!s}/media-check/", {"key": key} |
| 111 | + ) |
| 112 | + assert response.status_code == 200 |
| 113 | + assert response.json() == { |
| 114 | + "status": DocumentAttachmentStatus.READY, |
| 115 | + "file": f"/media/{key:s}", |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize("link_reach", ["restricted", "authenticated"]) |
| 120 | +def test_api_documents_media_check_anonymous_non_public_document(link_reach): |
| 121 | + """ |
| 122 | + The "media_check" endpoint should return a 403 error if the document is not public. |
| 123 | + """ |
| 124 | + document = factories.DocumentFactory(link_reach=link_reach) |
| 125 | + |
| 126 | + client = APIClient() |
| 127 | + |
| 128 | + response = client.get(f"/api/v1.0/documents/{document.id!s}/media-check/") |
| 129 | + assert response.status_code == 401 |
| 130 | + |
| 131 | + |
| 132 | +def test_api_documents_media_check_connected_document(): |
| 133 | + """ |
| 134 | + The "media_check" endpoint should return a 200 status code for a user connected |
| 135 | + checking for a document with link_reach authenticated. |
| 136 | + """ |
| 137 | + document = factories.DocumentFactory(link_reach="authenticated") |
| 138 | + |
| 139 | + filename = f"{uuid4()!s}.jpg" |
| 140 | + key = f"{document.id!s}/attachments/{filename:s}" |
| 141 | + default_storage.connection.meta.client.put_object( |
| 142 | + Bucket=default_storage.bucket_name, |
| 143 | + Key=key, |
| 144 | + Body=BytesIO(b"my prose"), |
| 145 | + ContentType="text/plain", |
| 146 | + Metadata={"status": DocumentAttachmentStatus.READY}, |
| 147 | + ) |
| 148 | + document.attachments = [key] |
| 149 | + document.save(update_fields=["attachments"]) |
| 150 | + |
| 151 | + user = factories.UserFactory() |
| 152 | + client = APIClient() |
| 153 | + client.force_login(user=user) |
| 154 | + |
| 155 | + response = client.get( |
| 156 | + f"/api/v1.0/documents/{document.id!s}/media-check/", {"key": key} |
| 157 | + ) |
| 158 | + assert response.status_code == 200 |
| 159 | + assert response.json() == { |
| 160 | + "status": DocumentAttachmentStatus.READY, |
| 161 | + "file": f"/media/{key:s}", |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | +def test_api_documents_media_check_connected_document_media_not_related(): |
| 166 | + """ |
| 167 | + The "media_check" endpoint should return a 404 error if the key is not related to the document. |
| 168 | + """ |
| 169 | + document = factories.DocumentFactory(link_reach="authenticated") |
| 170 | + |
| 171 | + filename = f"{uuid4()!s}.jpg" |
| 172 | + key = f"{document.id!s}/attachments/{filename:s}" |
| 173 | + |
| 174 | + user = factories.UserFactory() |
| 175 | + client = APIClient() |
| 176 | + client.force_login(user=user) |
| 177 | + |
| 178 | + response = client.get( |
| 179 | + f"/api/v1.0/documents/{document.id!s}/media-check/", {"key": key} |
| 180 | + ) |
| 181 | + assert response.status_code == 404 |
| 182 | + assert response.json() == {"detail": "Attachment missing"} |
| 183 | + |
| 184 | + |
| 185 | +def test_api_documents_media_check_media_missing_on_storage(): |
| 186 | + """ |
| 187 | + The "media_check" endpoint should return a 404 error if the media is missing on storage. |
| 188 | + """ |
| 189 | + document = factories.DocumentFactory(link_reach="authenticated") |
| 190 | + |
| 191 | + filename = f"{uuid4()!s}.jpg" |
| 192 | + key = f"{document.id!s}/attachments/{filename:s}" |
| 193 | + |
| 194 | + document.attachments = [key] |
| 195 | + document.save(update_fields=["attachments"]) |
| 196 | + |
| 197 | + user = factories.UserFactory() |
| 198 | + client = APIClient() |
| 199 | + client.force_login(user=user) |
| 200 | + |
| 201 | + response = client.get( |
| 202 | + f"/api/v1.0/documents/{document.id!s}/media-check/", {"key": key} |
| 203 | + ) |
| 204 | + assert response.status_code == 404 |
| 205 | + assert response.json() == {"detail": "Media not found"} |
| 206 | + |
| 207 | + |
| 208 | +@pytest.mark.parametrize("via", VIA) |
| 209 | +def test_api_documents_media_check_restricted_document(via, mock_user_teams): |
| 210 | + """ |
| 211 | + The "media_check" endpoint should return a 200 status code if the document is restricted and |
| 212 | + the user has access to it. |
| 213 | + """ |
| 214 | + document = factories.DocumentFactory(link_reach="restricted") |
| 215 | + filename = f"{uuid4()!s}.jpg" |
| 216 | + key = f"{document.id!s}/attachments/{filename:s}" |
| 217 | + default_storage.connection.meta.client.put_object( |
| 218 | + Bucket=default_storage.bucket_name, |
| 219 | + Key=key, |
| 220 | + Body=BytesIO(b"my prose"), |
| 221 | + ContentType="text/plain", |
| 222 | + Metadata={"status": DocumentAttachmentStatus.READY}, |
| 223 | + ) |
| 224 | + document.attachments = [key] |
| 225 | + document.save(update_fields=["attachments"]) |
| 226 | + |
| 227 | + user = factories.UserFactory() |
| 228 | + client = APIClient() |
| 229 | + client.force_login(user=user) |
| 230 | + |
| 231 | + if via == USER: |
| 232 | + factories.UserDocumentAccessFactory(document=document, user=user) |
| 233 | + elif via == TEAM: |
| 234 | + mock_user_teams.return_value = ["lasuite", "unknown"] |
| 235 | + factories.TeamDocumentAccessFactory(document=document, team="lasuite") |
| 236 | + |
| 237 | + response = client.get( |
| 238 | + f"/api/v1.0/documents/{document.id!s}/media-check/", {"key": key} |
| 239 | + ) |
| 240 | + assert response.status_code == 200 |
| 241 | + assert response.json() == { |
| 242 | + "status": DocumentAttachmentStatus.READY, |
| 243 | + "file": f"/media/{key:s}", |
| 244 | + } |
0 commit comments