|
| 1 | +import os |
| 2 | +import uuid |
| 3 | +from io import BytesIO |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +import sqlalchemy.exc as sa_exc |
| 8 | +from ellar.common.datastructures import ContentFile, UploadFile |
| 9 | +from ellar.core.files import storages |
| 10 | +from starlette.datastructures import Headers |
| 11 | + |
| 12 | +from ellar_sqlalchemy import model |
| 13 | +from ellar_sqlalchemy.model.utils import MB |
| 14 | + |
| 15 | + |
| 16 | +def serialize_file_data(file): |
| 17 | + keys = { |
| 18 | + "original_filename", |
| 19 | + "content_type", |
| 20 | + "extension", |
| 21 | + "file_size", |
| 22 | + "service_name", |
| 23 | + } |
| 24 | + return {k: v for k, v in file.to_dict().items() if k in keys} |
| 25 | + |
| 26 | + |
| 27 | +def test_file_column_type(db_service, ignore_base, tmp_path): |
| 28 | + path = str(tmp_path / "files") |
| 29 | + fs = storages.FileSystemStorage(path) |
| 30 | + |
| 31 | + class File(model.Model): |
| 32 | + id: model.Mapped[uuid.uuid4] = model.mapped_column( |
| 33 | + "id", model.Integer(), nullable=False, unique=True, primary_key=True |
| 34 | + ) |
| 35 | + file: model.Mapped[model.FileObject] = model.mapped_column( |
| 36 | + "file", model.FileField(storage=fs), nullable=False |
| 37 | + ) |
| 38 | + |
| 39 | + db_service.create_all() |
| 40 | + session = db_service.session_factory() |
| 41 | + session.add(File(file=ContentFile(b"Testing file column type", name="text.txt"))) |
| 42 | + session.commit() |
| 43 | + |
| 44 | + file: File = session.execute(model.select(File)).scalar() |
| 45 | + assert "content_type=text/plain" in repr(file.file) |
| 46 | + |
| 47 | + data = serialize_file_data(file.file) |
| 48 | + assert data == { |
| 49 | + "content_type": "text/plain", |
| 50 | + "extension": ".txt", |
| 51 | + "file_size": 24, |
| 52 | + "original_filename": "text.txt", |
| 53 | + "service_name": "local", |
| 54 | + } |
| 55 | + |
| 56 | + assert os.listdir(path)[0].split(".")[1] == "txt" |
| 57 | + |
| 58 | + |
| 59 | +def test_file_column_invalid_file_extension(db_service, ignore_base, tmp_path): |
| 60 | + fs = storages.FileSystemStorage(str(tmp_path / "files")) |
| 61 | + |
| 62 | + class File(model.Model): |
| 63 | + id: model.Mapped[uuid.uuid4] = model.mapped_column( |
| 64 | + "id", model.Integer(), nullable=False, unique=True, primary_key=True |
| 65 | + ) |
| 66 | + file: model.Mapped[model.FileObject] = model.mapped_column( |
| 67 | + "file", |
| 68 | + model.FileField(storage=fs, allowed_content_types=["application/pdf"]), |
| 69 | + nullable=False, |
| 70 | + ) |
| 71 | + |
| 72 | + with pytest.raises(sa_exc.StatementError) as stmt_exc: |
| 73 | + db_service.create_all() |
| 74 | + session = db_service.session_factory() |
| 75 | + session.add( |
| 76 | + File(file=ContentFile(b"Testing file column type", name="text.txt")) |
| 77 | + ) |
| 78 | + session.commit() |
| 79 | + assert ( |
| 80 | + str(stmt_exc.value.orig) |
| 81 | + == "Content type is not supported text/plain. Valid options are: application/pdf" |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +@patch( |
| 86 | + "ellar_sqlalchemy.model.typeDecorator.file.base.magic_mime_from_buffer", |
| 87 | + return_value=None, |
| 88 | +) |
| 89 | +def test_file_column_invalid_file_extension_case_2( |
| 90 | + mock_buffer, db_service, ignore_base, tmp_path |
| 91 | +): |
| 92 | + fs = storages.FileSystemStorage(str(tmp_path / "files")) |
| 93 | + |
| 94 | + class File(model.Model): |
| 95 | + id: model.Mapped[uuid.uuid4] = model.mapped_column( |
| 96 | + "id", model.Integer(), nullable=False, unique=True, primary_key=True |
| 97 | + ) |
| 98 | + file: model.Mapped[model.FileObject] = model.mapped_column( |
| 99 | + "file", |
| 100 | + model.FileField(storage=fs, allowed_content_types=["application/pdf"]), |
| 101 | + nullable=False, |
| 102 | + ) |
| 103 | + |
| 104 | + with pytest.raises(sa_exc.StatementError) as stmt_exc: |
| 105 | + db_service.create_all() |
| 106 | + session = db_service.session_factory() |
| 107 | + session.add( |
| 108 | + File( |
| 109 | + file=UploadFile( |
| 110 | + BytesIO(b"Testing file column type"), |
| 111 | + size=24, |
| 112 | + filename="test.txt", |
| 113 | + headers=Headers({"content-type": ""}), |
| 114 | + ) |
| 115 | + ) |
| 116 | + ) |
| 117 | + session.commit() |
| 118 | + assert mock_buffer.called |
| 119 | + assert ( |
| 120 | + str(stmt_exc.value.orig) |
| 121 | + == "Content type is not supported . Valid options are: application/pdf" |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +@patch("ellar_sqlalchemy.model.typeDecorator.file.base.get_length", return_value=MB * 7) |
| 126 | +def test_file_column_invalid_file_size_case_2( |
| 127 | + mock_buffer, db_service, ignore_base, tmp_path |
| 128 | +): |
| 129 | + fs = storages.FileSystemStorage(str(tmp_path / "files")) |
| 130 | + |
| 131 | + class File(model.Model): |
| 132 | + id: model.Mapped[uuid.uuid4] = model.mapped_column( |
| 133 | + "id", model.Integer(), nullable=False, unique=True, primary_key=True |
| 134 | + ) |
| 135 | + file: model.Mapped[model.FileObject] = model.mapped_column( |
| 136 | + "file", model.FileField(storage=fs, max_size=MB * 6), nullable=False |
| 137 | + ) |
| 138 | + |
| 139 | + with pytest.raises(sa_exc.StatementError) as stmt_exc: |
| 140 | + db_service.create_all() |
| 141 | + session = db_service.session_factory() |
| 142 | + session.add(File(file=ContentFile(b"Testing File Size Validation"))) |
| 143 | + session.commit() |
| 144 | + assert mock_buffer.called |
| 145 | + assert str(stmt_exc.value.orig) == "Cannot store files larger than: 6291456 bytes" |
| 146 | + |
| 147 | + |
| 148 | +def test_file_column_invalid_set(db_service, ignore_base, tmp_path): |
| 149 | + fs = storages.FileSystemStorage(str(tmp_path / "files")) |
| 150 | + |
| 151 | + class File(model.Model): |
| 152 | + id: model.Mapped[uuid.uuid4] = model.mapped_column( |
| 153 | + "id", model.Integer(), nullable=False, unique=True, primary_key=True |
| 154 | + ) |
| 155 | + file: model.Mapped[model.FileObject] = model.mapped_column( |
| 156 | + "file", model.FileField(storage=fs, max_size=MB * 6), nullable=False |
| 157 | + ) |
| 158 | + |
| 159 | + db_service.create_all() |
| 160 | + session = db_service.session_factory() |
| 161 | + with pytest.raises(sa_exc.StatementError) as stmt_exc: |
| 162 | + session.add(File(file={})) |
| 163 | + session.commit() |
| 164 | + |
| 165 | + assert str(stmt_exc.value.orig) == "{} is not supported" |
0 commit comments