Skip to content

Commit

Permalink
Merge pull request #358 from ita-social-projects/#356AddImagesToLogoBE
Browse files Browse the repository at this point in the history
#356 add images to logo be
  • Loading branch information
BelousSofiya authored Dec 11, 2023
2 parents d5609ec + 05e71e0 commit b193aa6
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 5 deletions.
6 changes: 6 additions & 0 deletions images/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ class BannerSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ("banner_image",)


class LogoSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ("logo_image",)
1 change: 0 additions & 1 deletion images/tests/test_banner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from rest_framework import status
from rest_framework.test import APITestCase

import os
Expand Down
120 changes: 120 additions & 0 deletions images/tests/test_logo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from rest_framework.test import APITestCase

import os
from io import BytesIO
from PIL import Image

from authentication.factories import UserFactory
from profiles.factories import (
ProfileStartupFactory,
ProfileCompanyFactory,
)

from utils.dump_response import dump # noqa


class TestLogoChange(APITestCase):
@staticmethod
def _generate_image(ext, size=(100, 100)):
"""for mocking png and jpeg files"""
file = BytesIO()
image = Image.new("RGB", size=size)
formatext = ext.upper()
image.save(file, formatext)
file.name = f"test.{formatext}"
file.seek(0)
return file

def setUp(self) -> None:
self.right_image = self._generate_image("jpeg", (100, 100))
self.wrong_image = self._generate_image("png", (8000, 10000))

self.user = UserFactory(email="test1@test.com")
self.company_dnipro = ProfileStartupFactory.create(
person=self.user,
name="Dnipro",
logo_image=f"logos/{self.right_image.name}",
)

self.company_kyiv = ProfileCompanyFactory(name="Kyivbud")

def tearDown(self) -> None:
if os.path.exists(self.right_image.name):
os.remove(self.right_image.name)
if os.path.exists(self.wrong_image.name):
os.remove(self.wrong_image.name)

def test_get_empty_logo_unauthorized(self):
response = self.client.get(path=f"/api/logo/{self.company_kyiv.id}/")
self.assertEqual(200, response.status_code)
self.assertEqual({"logo_image": None}, response.json())

def test_get_logo_unauthorized(self):
response = self.client.get(path=f"/api/logo/{self.company_dnipro.id}/")
self.assertEqual(200, response.status_code)
self.assertEqual(
{
"logo_image": f"http://testserver/media/logos/{self.right_image.name}"
},
response.json(),
)

def test_get_logo_authorized(self):
self.client.force_authenticate(self.user)
response = self.client.get(path=f"/api/logo/{self.company_dnipro.id}/")
self.assertEqual(200, response.status_code)
self.assertEqual(
{
"logo_image": f"http://testserver/media/logos/{self.right_image.name}"
},
response.json(),
)

def test_get_empty_logo_authorized(self):
self.client.force_authenticate(self.user)
response = self.client.get(path=f"/api/logo/{self.company_kyiv.id}/")
self.assertEqual(200, response.status_code)
self.assertEqual({"logo_image": None}, response.json())

def test_put_logo_unauthorized(self):
response = self.client.put(
path=f"/api/logo/{self.company_dnipro.id}/",
data={"logo_image": self.right_image},
)
self.assertEqual(401, response.status_code)
self.assertEqual(
{"detail": "Authentication credentials were not provided."},
response.json(),
)

def test_put_logo_authorized_not_owner(self):
self.client.force_authenticate(self.user)
response = self.client.put(
path=f"/api/logo/{self.company_kyiv.id}/",
data={"logo_image": self.right_image},
)
self.assertEqual(403, response.status_code)
self.assertEqual(
{"detail": "You do not have permission to perform this action."},
response.json(),
)

def test_put_logo_authorized_owner_right_image(self):
self.client.force_authenticate(self.user)
response = self.client.put(
path=f"/api/logo/{self.company_dnipro.id}/",
data={"logo_image": self.right_image},
)
self.assertEqual(200, response.status_code)

def test_put_logo_authorized_owner_wrong_image(self):
self.client.force_authenticate(self.user)
response = self.client.put(
path=f"/api/logo/{self.company_dnipro.id}/",
data={"logo_image": self.wrong_image},
)
self.assertEqual(400, response.status_code)
self.assertEqual(
{"logo_image": ["Image size exceeds the maximum allowed (10MB)."]},
response.json(),
)
3 changes: 2 additions & 1 deletion images/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.urls import path
from .views import BannerRetrieveUpdate
from .views import BannerRetrieveUpdate, LogoRetrieveUpdate

app_name = "images"

urlpatterns = [
path("banner/<pk>/", BannerRetrieveUpdate.as_view(), name="banner_change"),
path("logo/<pk>/", LogoRetrieveUpdate.as_view(), name="logo_change"),
]
9 changes: 8 additions & 1 deletion images/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@

from profiles.permissions import UserIsProfileOwnerOrReadOnly
from profiles.models import Profile
from .serializers import BannerSerializer
from .serializers import BannerSerializer, LogoSerializer


class BannerRetrieveUpdate(RetrieveUpdateAPIView):
permission_classes = (UserIsProfileOwnerOrReadOnly,)
serializer_class = BannerSerializer
parser_classes = (MultiPartParser, FormParser)
queryset = Profile.objects.all()


class LogoRetrieveUpdate(RetrieveUpdateAPIView):
permission_classes = (UserIsProfileOwnerOrReadOnly,)
serializer_class = LogoSerializer
parser_classes = (MultiPartParser, FormParser)
queryset = Profile.objects.all()
4 changes: 2 additions & 2 deletions profiles/tests/test_crud_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def test_partial_update_profile_with_wrong_image_banner(self):
profile_id=self.profile.id
),
data={
"banner_image": f"/Forum/{self.wrong_image}",
"banner_image": self.wrong_image,
"founded": 2005,
},
)
Expand All @@ -502,7 +502,7 @@ def test_partial_update_profile_with_wrong_image_logo(self):
profile_id=self.profile.id
),
data={
"logo_image": f"/Forum/{self.wrong_image}",
"logo_image": self.wrong_image,
"founded": 2005,
},
)
Expand Down

0 comments on commit b193aa6

Please sign in to comment.