Files
website-v2/core/tests/test_validators.py
Lacey Williams Henschel 7507fa50b3 Limit file types of user and news images
- Add file type validator
- Apply validator to user image
- Save validator class to variable
- Add image validator to news image model field
- Add file size validator
- Enable file size validator on news and user model image fields
- Fix test
2023-11-03 10:21:36 -07:00

54 lines
1.5 KiB
Python

import pytest
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
from ..validators import FileTypeValidator, MaxFileSizeValidator
def test_file_type_validator():
"""
Eensure validator only allows specific file extensions.
"""
validator = FileTypeValidator(extensions=[".jpg", ".png"])
# Valid file types
valid_file = SimpleUploadedFile(
"test.jpg", b"file_content", content_type="image/jpeg"
)
validator(valid_file)
valid_file = SimpleUploadedFile(
"test.png", b"file_content", content_type="image/jpeg"
)
validator(valid_file)
# Invalid file type
invalid_file = SimpleUploadedFile(
"test.txt", b"file_content", content_type="text/plain"
)
with pytest.raises(ValidationError):
validator(invalid_file)
def test_max_file_size_validator():
"""
Ensure the validator enforces the file size limit.
"""
# 1MB max file size
validator = MaxFileSizeValidator(max_size=1 * 1024 * 1024)
# Valid file size
valid_file = SimpleUploadedFile(
"small.jpg", b"a" * (1 * 1024 * 1024 - 1), content_type="image/jpeg"
)
validator(valid_file) # Should not raise
# Invalid file size
invalid_file = SimpleUploadedFile(
"large.jpg", b"a" * (1 * 1024 * 1024 + 1), content_type="image/jpeg"
)
with pytest.raises(ValidationError) as exc_info:
validator(invalid_file)
assert "File too large" in str(exc_info.value)