Skip to content

Commit

Permalink
Tests/adverts (#119)
Browse files Browse the repository at this point in the history
* Advert test
  • Loading branch information
berk76 authored Dec 2, 2024
1 parent 2b0af59 commit 7dc9dd1
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 4 deletions.
2 changes: 2 additions & 0 deletions svjis/articles/tests/factories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .article import ArticleFactory
from .preferences import PreferencesFactory
from .company import CompanyFactory
from .advert_type import AdvertTypeFactory

__all__ = [
"ContentTypeFactory",
Expand All @@ -16,4 +17,5 @@
"ArticleFactory",
"PreferencesFactory",
"CompanyFactory",
"AdvertTypeFactory",
]
10 changes: 10 additions & 0 deletions svjis/articles/tests/factories/advert_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import factory

from ...models import AdvertType


class AdvertTypeFactory(factory.django.DjangoModelFactory):
description = factory.Faker('sentence', nb_words=6)

class Meta:
model = AdvertType
136 changes: 136 additions & 0 deletions svjis/articles/tests/test_adverts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
from django.test import TestCase
from django.urls import reverse

from .testdata import UserDataMixin


class AdvertsTest(UserDataMixin, TestCase):

def create_advert(self, username, password, advert_form, expected_status):
logged_in = self.client.login(username=username, password=password)
self.assertTrue(logged_in)
response = self.client.post(
reverse('adverts_save'),
advert_form,
follow=False,
)
if expected_status == 302:
self.assertEqual(response.status_code, expected_status)
self.assertEqual(response.url, '/adverts_list/')
response = self.client.get(reverse('adverts_list'), follow=True)
self.assertEqual(response.status_code, 200)

adverts = response.context['object_list']
self.assertEqual(len(adverts), 1)

advert = adverts[0]['advert']
return advert
else:
self.assertEqual(response.status_code, expected_status)
return None

def test_hide_adverts_of_deactivated_user(self):
# create advert
self.create_advert(
"petr",
"petr",
{
'pk': 0,
'type': 1,
'header': 'testing advert',
'body': 'testing advert body',
'phone': '123',
'email': '[email protected]',
'published': True,
},
302,
)

# advert is visible for other users
logged_in = self.client.login(username='jiri', password='jiri')
self.assertTrue(logged_in)

response = self.client.get(reverse('adverts_list'), follow=True)
self.assertEqual(response.status_code, 200)

adverts = response.context['object_list']
self.assertEqual(len(adverts), 1)

advert = adverts[0]['advert']
self.assertEqual(advert.created_by_user, self.u_petr)

# disable advert owner
self.u_petr.is_active = False
self.u_petr.save()

# advert is not visible for other users
response = self.client.get(reverse('adverts_list'), follow=True)
self.assertEqual(response.status_code, 200)

adverts = response.context['object_list']
self.assertEqual(len(adverts), 0)

def test_advert_update(self):
advert = self.create_advert(
"petr",
"petr",
{
'pk': 0,
'type': 1,
'header': 'testing advert',
'body': 'testing advert body',
'phone': '123',
'email': '[email protected]',
'published': True,
},
302,
)
self.assertEqual(advert.header, 'testing advert')

advert = self.create_advert(
"petr",
"petr",
{
'pk': advert.pk,
'type': 1,
'header': 'testing advert 2',
'body': 'testing advert body',
'phone': '123',
'email': '[email protected]',
'published': True,
},
302,
)
self.assertEqual(advert.header, 'testing advert 2')

def test_advert_update_by_another_user(self):
advert = self.create_advert(
"petr",
"petr",
{
'pk': 0,
'type': 1,
'header': 'testing advert',
'body': 'testing advert body',
'phone': '123',
'email': '[email protected]',
'published': True,
},
302,
)
self.assertEqual(advert.header, 'testing advert')

advert = self.create_advert(
"jiri",
"jiri",
{
'pk': advert.pk,
'type': 1,
'header': 'testing advert 2',
'body': 'testing advert body',
'phone': '123',
'email': '[email protected]',
'published': True,
},
404,
)
8 changes: 5 additions & 3 deletions svjis/articles/tests/test_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ def test_admin_user(self):
self.do_menu_test(
'jarda',
'jarda',
['Articles', 'Contact', 'Personal settings', 'Redaction', 'Fault reporting', 'Administration'],
['Articles', 'Contact', 'Personal settings', 'Redaction', 'Fault reporting', 'Adverts', 'Administration'],
)

def test_board_user(self):
self.do_menu_test('jiri', 'jiri', ['Articles', 'Contact', 'Personal settings', 'Redaction', 'Fault reporting'])
self.do_menu_test(
'jiri', 'jiri', ['Articles', 'Contact', 'Personal settings', 'Redaction', 'Fault reporting', 'Adverts']
)

def test_owner_user(self):
self.do_menu_test('petr', 'petr', ['Articles', 'Contact', 'Personal settings', 'Fault reporting'])
self.do_menu_test('petr', 'petr', ['Articles', 'Contact', 'Personal settings', 'Fault reporting', 'Adverts'])

def test_vendor_user(self):
self.do_menu_test('karel', 'karel', ['Articles', 'Contact', 'Personal settings', 'Fault reporting'])
Expand Down
10 changes: 9 additions & 1 deletion svjis/articles/tests/testdata/preferences_data.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
from ..factories import PreferencesFactory, CompanyFactory
from ..factories import PreferencesFactory, CompanyFactory, AdvertTypeFactory


class PreferencesDataMixin:

@classmethod
def setUpTestData(cls):

cls.company = CompanyFactory(name="Testing company", internet_domain="www.test.cz")

cls.advert_types = [
AdvertTypeFactory(description="Koupím"),
AdvertTypeFactory(description="Prodám"),
AdvertTypeFactory(description="Ostatní"),
]

cls.p_fault_notif = PreferencesFactory(
key="mail.template.fault.notification", value="Uživatel {} vložil novou závadu {}: <br><br><br>{}"
)
Expand Down
2 changes: 2 additions & 0 deletions svjis/articles/tests/testdata/user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def setUpTestData(cls):
PermissionFactory(codename="svjis_view_fault_menu"),
PermissionFactory(codename="svjis_fault_reporter"),
PermissionFactory(codename="svjis_add_fault_comment"),
PermissionFactory(codename="svjis_view_adverts_menu"),
PermissionFactory(codename="svjis_add_advert"),
],
)
cls.g_board_member = GroupFactory(
Expand Down

0 comments on commit 7dc9dd1

Please sign in to comment.