Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions accounts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def resize_image(picture_profile):

def convert_type_image(image, filename):
buffer = BytesIO()
image = image.convert("RGB")
image.save(buffer, format="JPEG")
django_file = ContentFile(buffer.getvalue(), name=f"{filename}.jpg")
return django_file
15 changes: 15 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from PIL import Image
from io import BytesIO
from django.core.files.uploadedfile import SimpleUploadedFile


def create_test_image(width, height, fmt="PNG"):
image = Image.new(mode="RGB", size=(width, height))
buffer = BytesIO()
image.save(buffer, format=fmt)

return SimpleUploadedFile(
name=f"test.{fmt.lower()}",
content=buffer.getvalue(),
content_type=f"image/{fmt.lower()}"
)
38 changes: 38 additions & 0 deletions tests/test_user_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pytest
from PIL import Image
from .helpers import create_test_image
from .factories import UserFactory
from accounts.models import UserProfile
from accounts.utils import resize_image, convert_type_image
from accounts.serializers import UserProfileSerializer


@pytest.mark.django_db
class TestUserProfile:
def test_large_image_upload_is_resized_to_400px_width(self):
image = create_test_image(1000, 1000)
resized_image = resize_image(image)
assert resized_image.size[0] == 400

def test_uploaded_image_is_converted_to_JPEG_with_correct_filename(self):
image = create_test_image(400, 400)
opened_image = Image.open(image)
converted_image = convert_type_image(opened_image, filename="test")
assert Image.open(converted_image).format == "JPEG"
assert converted_image.name == "test.jpg"

def test_serializer_returns_placeholder_when_profile_has_no_picture(self):
user = UserFactory()
serializer = UserProfileSerializer(user.profile)
assert serializer.data['picture_profile'] == "/static/images/placeholder.jpg"

def test_user_profile_is_auto_created_via_signals_on_user_creation(self):
user = UserFactory()
assert user.profile is not None
assert UserProfile.objects.filter(user=user).exists()
assert UserProfile.objects.count() == 1

def test_PNG_image_with_transparency_RGBA_generates_a_valid_JPEG_file(self):
image = Image.new("RGBA", (400, 400))
converted_image = convert_type_image(image, filename="test")
assert Image.open(converted_image).format == "JPEG"
Loading