Sith/sas/tests/test_model.py

42 lines
1.5 KiB
Python
Raw Normal View History

2024-08-06 11:23:34 +00:00
from django.test import TestCase
2024-08-09 22:43:15 +00:00
from model_bakery import baker
2024-08-06 11:23:34 +00:00
from core.baker_recipes import old_subscriber_user, subscriber_user
from core.models import User
2024-08-09 22:43:15 +00:00
from sas.baker_recipes import picture_recipe
2024-08-06 11:23:34 +00:00
from sas.models import Picture
class TestPictureQuerySet(TestCase):
@classmethod
def setUpTestData(cls):
Picture.objects.all().delete()
2024-08-09 22:43:15 +00:00
cls.pictures = picture_recipe.make(_quantity=10, _bulk_create=True)
2024-08-06 11:23:34 +00:00
Picture.objects.filter(pk=cls.pictures[0].id).update(is_moderated=False)
def test_root(self):
root = baker.make(User, is_superuser=True)
pictures = list(Picture.objects.viewable_by(root))
self.assertCountEqual(pictures, self.pictures)
def test_subscriber(self):
subscriber = subscriber_user.make()
old_subcriber = old_subscriber_user.make()
for user in (subscriber, old_subcriber):
pictures = list(Picture.objects.viewable_by(user))
self.assertCountEqual(pictures, self.pictures[1:])
def test_not_subscribed_identified(self):
user = baker.make(
# This is the guy who asked the feature of making pictures
# available for tagged users, even if not subscribed
User,
first_name="Pierrick",
last_name="Dheilly",
nick_name="Sahmer",
)
user.pictures.create(picture=self.pictures[0])
user.pictures.create(picture=self.pictures[1])
pictures = list(Picture.objects.viewable_by(user))
assert pictures == [self.pictures[1]]