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 #358 from ita-social-projects/#356AddImagesToLogoBE
#356 add images to logo be
- Loading branch information
Showing
6 changed files
with
138 additions
and
5 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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
import os | ||
|
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,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(), | ||
) |
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 |
---|---|---|
@@ -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"), | ||
] |
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