Skip to content

Commit

Permalink
model, serializers, tests for CRUD and search
Browse files Browse the repository at this point in the history
  • Loading branch information
BelousSofiya committed Dec 9, 2023
1 parent a3c7a8d commit 4986949
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.4 on 2023-12-09 13:17

from django.db import migrations, models
import validation.validate_image


class Migration(migrations.Migration):

dependencies = [
('profiles', '0004_alter_viewedcompany_unique_together_and_more'),
]

operations = [
migrations.AddField(
model_name='profile',
name='logo_image',
field=models.ImageField(null=True, upload_to='logos', validators=[validation.validate_image.validate_image_format, validation.validate_image.validate_image_size]),
),
migrations.AlterField(
model_name='profile',
name='banner_image',
field=models.ImageField(null=True, upload_to='banners', validators=[validation.validate_image.validate_image_format, validation.validate_image.validate_image_size]),
),
]
6 changes: 6 additions & 0 deletions profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ class Profile(models.Model):
null=True,
)

logo_image = models.ImageField(
upload_to="logos",
validators=[validate_image_format, validate_image_size],
null=True,
)

is_deleted = models.BooleanField(default=False)


Expand Down
6 changes: 6 additions & 0 deletions profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Meta:
"categories",
"activities",
"banner_image",
"logo_image",
"is_saved",
)

Expand Down Expand Up @@ -83,6 +84,7 @@ class Meta:
"service_info",
"product_info",
"banner_image",
"logo_image",
"is_saved",
)
read_only_fields = (
Expand All @@ -103,6 +105,7 @@ class Meta:
"service_info",
"product_info",
"banner_image",
"logo_image",
)

def get_is_saved(self, obj):
Expand Down Expand Up @@ -145,6 +148,7 @@ class Meta:
"address",
"startup_idea",
"banner_image",
"logo_image",
"is_deleted",
)
read_only_fields = (
Expand All @@ -169,6 +173,7 @@ class Meta:
"address",
"startup_idea",
"banner_image",
"logo_image",
"is_deleted",
)

Expand Down Expand Up @@ -202,6 +207,7 @@ class Meta:
"address",
"startup_idea",
"banner_image",
"logo_image",
"is_deleted",
)
read_only_fields = ("person",)
Expand Down
41 changes: 40 additions & 1 deletion profiles/tests/test_crud_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def _generate_image(ext, size=(100, 100)):

def setUp(self) -> None:
self.right_image = self._generate_image("jpeg", (100, 100))
self.right_image_logo = self._generate_image("jpeg", (80, 80))
self.wrong_image = self._generate_image("png", (7000, 7000))
self.user = UserFactory(email="[email protected]")
self.profile = ProfileStartupFactory.create(
Expand All @@ -44,6 +45,8 @@ def tearDown(self) -> None:
os.remove(self.right_image.name)
if os.path.exists(self.wrong_image.name):
os.remove(self.wrong_image.name)
if os.path.exists(self.right_image_logo.name):
os.remove(self.right_image_logo.name)

# GET requests section
def test_get_profile_nonexistent(self):
Expand Down Expand Up @@ -128,6 +131,11 @@ def test_get_profile_unauthorized(self):
response.data.get("banner_image"),
msg="Banner images do not match.",
)
self.assertEqual(
self.profile.logo_image,
response.data.get("logo_image"),
msg="Logo images do not match.",
)
self.assertFalse(
response.data.get("categories"), msg="Categories do not match."
)
Expand Down Expand Up @@ -214,6 +222,11 @@ def test_get_profile_authorized_not_owner(self):
response.data.get("banner_image"),
msg="Banner images do not match.",
)
self.assertEqual(
self.profile.logo_image,
response.data.get("logo_image"),
msg="Logo images do not match.",
)
self.assertFalse(
response.data.get("categories"), msg="Categories do not match."
)
Expand Down Expand Up @@ -311,6 +324,11 @@ def test_get_profile_authorized_owner(self):
response.data.get("banner_image"),
msg="Banner images do not match.",
)
self.assertEqual(
self.profile.logo_image,
response.data.get("logo_image"),
msg="Logo images do not match.",
)
self.assertEqual(
list(self.profile.categories.all()),
response.data.get("categories"),
Expand Down Expand Up @@ -462,7 +480,7 @@ def test_partial_update_authorized_not_owner(self):
)
self.assertEqual(status.HTTP_403_FORBIDDEN, response.status_code)

def test_partial_update_profile_with_wrong_image(self):
def test_partial_update_profile_with_wrong_image_banner(self):
self.client.force_authenticate(self.user)

response = self.client.patch(
Expand All @@ -476,6 +494,20 @@ def test_partial_update_profile_with_wrong_image(self):
)
self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)

def test_partial_update_profile_with_wrong_image_logo(self):
self.client.force_authenticate(self.user)

response = self.client.patch(
path="/api/profiles/{profile_id}".format(
profile_id=self.profile.id
),
data={
"logo_image": f"/Forum/{self.wrong_image}",
"founded": 2005,
},
)
self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)

def test_partial_update_profile_unauthorized(self):
response = self.client.patch(
path="/api/profiles/{profile_id}".format(
Expand Down Expand Up @@ -563,6 +595,7 @@ def test_full_update_profile_authorized_with_full_data(self):
"product_info": "Product info from test case",
"address": "Kyiv",
"banner_image": self.right_image,
"logo_image": self.right_image_logo,
"person_position": "director",
"startup_idea": "StartUp idea from test case",
"is_startup": True,
Expand Down Expand Up @@ -596,6 +629,7 @@ def test_full_update_profile_unauthorized(self):
"product_info": "Product info from test case",
"address": "Kyiv",
"banner_image": self.right_image,
"logo_image": self.right_image_logo,
"person_position": "director",
"startup_idea": "StartUp idea from test case",
"is_startup": True,
Expand Down Expand Up @@ -624,6 +658,7 @@ def test_full_update_profile_not_exist(self):
"product_info": "Product info from test case",
"address": "Kyiv",
"banner_image": self.right_image,
"logo_image": self.right_image_logo,
"person_position": "director",
"startup_idea": "StartUp idea from test case",
"is_startup": True,
Expand Down Expand Up @@ -652,6 +687,7 @@ def test_full_update_profile_wrong_data(self):
"product_info": "Product info from test case",
"address": "Kyiv",
"banner_image": self.wrong_image,
"logo_image": self.right_image_logo,
"person_position": "director",
"startup_idea": "StartUp idea from test case",
"is_startup": True,
Expand Down Expand Up @@ -682,6 +718,7 @@ def test_full_update_authorized_not_owner(self):
"product_info": "Product info from test case",
"address": "Kyiv",
"banner_image": self.right_image,
"logo_image": self.right_image_logo,
"person_position": "director",
"startup_idea": "StartUp idea from test case",
"is_startup": True,
Expand Down Expand Up @@ -754,6 +791,8 @@ def test_create_profile_authorized_full_data(self):
"startup_idea": "StartUp idea from test case",
"is_startup": True,
"is_registered": False,
"banner_image": self.right_image,
"logo_image": self.right_image_logo,
"name": "Comp name from test case",
"categories": [category.id],
"activities": [activity.id],
Expand Down
1 change: 1 addition & 0 deletions search/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ class Meta:
"service_info",
"address",
"banner_image",
"logo_image",
)
Loading

0 comments on commit 4986949

Please sign in to comment.