generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #354 from ita-social-projects/#174addingImages
#174adding images
- Loading branch information
Showing
11 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ | |
"administration", | ||
"search", | ||
"drf_spectacular", | ||
"images", | ||
] | ||
|
||
MIDDLEWARE = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from rest_framework import serializers | ||
from profiles.models import Profile | ||
|
||
|
||
class BannerSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Profile | ||
fields = ("banner_image",) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
from rest_framework import status | ||
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 TestBannerChange(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", (7000, 7000)) | ||
|
||
self.user = UserFactory(email="[email protected]") | ||
self.company_dnipro = ProfileStartupFactory.create( | ||
person=self.user, | ||
name="Dnipro", | ||
banner_image=f"banners/{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_banner_unauthorized(self): | ||
response = self.client.get(path=f"/api/banner/{self.company_kyiv.id}/") | ||
self.assertEqual(200, response.status_code) | ||
self.assertEqual({"banner_image": None}, response.json()) | ||
|
||
def test_get_banner_unauthorized(self): | ||
response = self.client.get( | ||
path=f"/api/banner/{self.company_dnipro.id}/" | ||
) | ||
self.assertEqual(200, response.status_code) | ||
self.assertEqual( | ||
{ | ||
"banner_image": f"http://testserver/media/banners/{self.right_image.name}" | ||
}, | ||
response.json(), | ||
) | ||
|
||
def test_get_banner_authorized(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.get( | ||
path=f"/api/banner/{self.company_dnipro.id}/" | ||
) | ||
self.assertEqual(200, response.status_code) | ||
self.assertEqual( | ||
{ | ||
"banner_image": f"http://testserver/media/banners/{self.right_image.name}" | ||
}, | ||
response.json(), | ||
) | ||
|
||
def test_get_empty_banner_authorized(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.get(path=f"/api/banner/{self.company_kyiv.id}/") | ||
self.assertEqual(200, response.status_code) | ||
self.assertEqual({"banner_image": None}, response.json()) | ||
|
||
def test_put_banner_unauthorized(self): | ||
response = self.client.put( | ||
path=f"/api/banner/{self.company_dnipro.id}/", | ||
data={"banner_image": self.right_image}, | ||
) | ||
self.assertEqual(401, response.status_code) | ||
self.assertEqual( | ||
{"detail": "Authentication credentials were not provided."}, | ||
response.json(), | ||
) | ||
|
||
def test_put_banner_authorized_not_owner(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.put( | ||
path=f"/api/banner/{self.company_kyiv.id}/", | ||
data={"banner_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_banner_authorized_owner_right_image(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.put( | ||
path=f"/api/banner/{self.company_dnipro.id}/", | ||
data={"banner_image": self.right_image}, | ||
) | ||
self.assertEqual(200, response.status_code) | ||
|
||
def test_put_banner_authorized_owner_wrong_image(self): | ||
self.client.force_authenticate(self.user) | ||
response = self.client.put( | ||
path=f"/api/banner/{self.company_dnipro.id}/", | ||
data={"banner_image": self.wrong_image}, | ||
) | ||
self.assertEqual(400, response.status_code) | ||
self.assertEqual( | ||
{ | ||
"banner_image": [ | ||
"Image size exceeds the maximum allowed (50MB)." | ||
] | ||
}, | ||
response.json(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.urls import path | ||
from .views import BannerRetrieveUpdate | ||
|
||
app_name = "images" | ||
|
||
urlpatterns = [ | ||
path("banner/<pk>/", BannerRetrieveUpdate.as_view(), name="banner_change"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from rest_framework.generics import ( | ||
RetrieveUpdateAPIView, | ||
) | ||
from rest_framework.parsers import MultiPartParser, FormParser | ||
|
||
from profiles.permissions import UserIsProfileOwnerOrReadOnly | ||
from profiles.models import Profile | ||
from .serializers import BannerSerializer | ||
|
||
|
||
class BannerRetrieveUpdate(RetrieveUpdateAPIView): | ||
permission_classes = (UserIsProfileOwnerOrReadOnly,) | ||
serializer_class = BannerSerializer | ||
parser_classes = (MultiPartParser, FormParser) | ||
queryset = Profile.objects.all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters