2024-07-26 12:25:26 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import transaction
|
2024-07-22 17:12:03 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.urls import reverse
|
|
|
|
from model_bakery import baker
|
2024-08-09 22:43:15 +00:00
|
|
|
from pytest_django.asserts import assertNumQueries
|
2024-07-22 17:12:03 +00:00
|
|
|
|
|
|
|
from core.baker_recipes import old_subscriber_user, subscriber_user
|
2024-07-26 12:25:26 +00:00
|
|
|
from core.models import RealGroup, User
|
2024-08-09 22:43:15 +00:00
|
|
|
from sas.baker_recipes import picture_recipe
|
2024-07-22 17:12:03 +00:00
|
|
|
from sas.models import Album, PeoplePictureRelation, Picture
|
|
|
|
|
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestSas(TestCase):
|
2024-07-22 17:12:03 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
Picture.objects.all().delete()
|
|
|
|
owner = User.objects.get(username="root")
|
|
|
|
|
|
|
|
cls.user_a = old_subscriber_user.make()
|
|
|
|
cls.user_b, cls.user_c = subscriber_user.make(_quantity=2)
|
|
|
|
|
2024-08-09 22:43:15 +00:00
|
|
|
picture = picture_recipe.extend(owner=owner)
|
2024-07-22 17:12:03 +00:00
|
|
|
cls.album_a = baker.make(Album, is_in_sas=True)
|
|
|
|
cls.album_b = baker.make(Album, is_in_sas=True)
|
|
|
|
for album in cls.album_a, cls.album_b:
|
2024-08-09 22:43:15 +00:00
|
|
|
pictures = picture.make(parent=album, _quantity=5, _bulk_create=True)
|
2024-07-22 17:12:03 +00:00
|
|
|
baker.make(PeoplePictureRelation, picture=pictures[1], user=cls.user_a)
|
|
|
|
baker.make(PeoplePictureRelation, picture=pictures[2], user=cls.user_a)
|
|
|
|
baker.make(PeoplePictureRelation, picture=pictures[2], user=cls.user_b)
|
|
|
|
baker.make(PeoplePictureRelation, picture=pictures[3], user=cls.user_b)
|
|
|
|
baker.make(PeoplePictureRelation, picture=pictures[4], user=cls.user_a)
|
|
|
|
baker.make(PeoplePictureRelation, picture=pictures[4], user=cls.user_b)
|
|
|
|
baker.make(PeoplePictureRelation, picture=pictures[4], user=cls.user_c)
|
|
|
|
|
2024-07-26 12:25:26 +00:00
|
|
|
|
|
|
|
class TestPictureSearch(TestSas):
|
2024-08-09 22:43:15 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
super().setUpTestData()
|
|
|
|
cls.url = reverse("api:pictures")
|
|
|
|
|
2024-07-22 17:12:03 +00:00
|
|
|
def test_anonymous_user_forbidden(self):
|
2024-08-09 22:43:15 +00:00
|
|
|
res = self.client.get(self.url)
|
2024-07-22 17:12:03 +00:00
|
|
|
assert res.status_code == 403
|
|
|
|
|
|
|
|
def test_filter_by_album(self):
|
|
|
|
self.client.force_login(self.user_b)
|
2024-08-09 22:43:15 +00:00
|
|
|
res = self.client.get(self.url + f"?album_id={self.album_a.id}")
|
2024-07-22 17:12:03 +00:00
|
|
|
assert res.status_code == 200
|
2024-08-05 17:25:30 +00:00
|
|
|
expected = list(self.album_a.children_pictures.values_list("id", flat=True))
|
|
|
|
assert [i["id"] for i in res.json()["results"]] == expected
|
2024-07-22 17:12:03 +00:00
|
|
|
|
|
|
|
def test_filter_by_user(self):
|
|
|
|
self.client.force_login(self.user_b)
|
2024-08-09 22:43:15 +00:00
|
|
|
res = self.client.get(self.url + f"?users_identified={self.user_a.id}")
|
2024-07-22 17:12:03 +00:00
|
|
|
assert res.status_code == 200
|
|
|
|
expected = list(
|
2024-08-05 17:25:30 +00:00
|
|
|
self.user_a.pictures.order_by(
|
|
|
|
"-picture__parent__date", "picture__date"
|
|
|
|
).values_list("picture_id", flat=True)
|
2024-07-22 17:12:03 +00:00
|
|
|
)
|
2024-08-05 17:25:30 +00:00
|
|
|
assert [i["id"] for i in res.json()["results"]] == expected
|
2024-07-22 17:12:03 +00:00
|
|
|
|
|
|
|
def test_filter_by_multiple_user(self):
|
|
|
|
self.client.force_login(self.user_b)
|
|
|
|
res = self.client.get(
|
2024-08-09 22:43:15 +00:00
|
|
|
self.url
|
2024-07-22 17:12:03 +00:00
|
|
|
+ f"?users_identified={self.user_a.id}&users_identified={self.user_b.id}"
|
|
|
|
)
|
|
|
|
assert res.status_code == 200
|
|
|
|
expected = list(
|
|
|
|
self.user_a.pictures.union(self.user_b.pictures.all())
|
2024-08-05 17:25:30 +00:00
|
|
|
.order_by("-picture__parent__date", "picture__date")
|
2024-07-22 17:12:03 +00:00
|
|
|
.values_list("picture_id", flat=True)
|
|
|
|
)
|
2024-08-05 17:25:30 +00:00
|
|
|
assert [i["id"] for i in res.json()["results"]] == expected
|
2024-07-22 17:12:03 +00:00
|
|
|
|
|
|
|
def test_not_subscribed_user(self):
|
2024-08-06 10:37:50 +00:00
|
|
|
"""Test that a user that never subscribed can only its own pictures."""
|
|
|
|
self.user_a.subscriptions.all().delete()
|
2024-07-22 17:12:03 +00:00
|
|
|
self.client.force_login(self.user_a)
|
2024-08-09 22:43:15 +00:00
|
|
|
res = self.client.get(f"{self.url}?users_identified={self.user_a.id}")
|
2024-07-22 17:12:03 +00:00
|
|
|
assert res.status_code == 200
|
|
|
|
expected = list(
|
2024-08-05 17:25:30 +00:00
|
|
|
self.user_a.pictures.order_by(
|
|
|
|
"-picture__parent__date", "picture__date"
|
|
|
|
).values_list("picture_id", flat=True)
|
2024-07-22 17:12:03 +00:00
|
|
|
)
|
2024-08-05 17:25:30 +00:00
|
|
|
assert [i["id"] for i in res.json()["results"]] == expected
|
2024-07-22 17:12:03 +00:00
|
|
|
|
2024-08-08 11:15:19 +00:00
|
|
|
# trying to access the pictures of someone else mixed with owned pictures
|
|
|
|
# should return only owned pictures
|
2024-07-22 17:12:03 +00:00
|
|
|
res = self.client.get(
|
2024-08-09 22:43:15 +00:00
|
|
|
self.url
|
2024-08-08 11:15:19 +00:00
|
|
|
+ f"?users_identified={self.user_a.id}&users_identified={self.user_b.id}"
|
2024-07-22 17:12:03 +00:00
|
|
|
)
|
2024-08-08 11:15:19 +00:00
|
|
|
assert res.status_code == 200
|
|
|
|
assert [i["id"] for i in res.json()["results"]] == expected
|
|
|
|
|
|
|
|
# trying to fetch everything should be the same
|
|
|
|
# as fetching its own pictures for a non-subscriber
|
2024-08-09 22:43:15 +00:00
|
|
|
res = self.client.get(self.url)
|
2024-08-08 11:15:19 +00:00
|
|
|
assert res.status_code == 200
|
|
|
|
assert [i["id"] for i in res.json()["results"]] == expected
|
2024-07-22 17:12:03 +00:00
|
|
|
|
2024-08-08 11:15:19 +00:00
|
|
|
# trying to access the pictures of someone else should return only
|
|
|
|
# the ones where the non-subscribed user is identified too
|
2024-08-09 22:43:15 +00:00
|
|
|
res = self.client.get(f"{self.url}?users_identified={self.user_b.id}")
|
2024-08-08 11:15:19 +00:00
|
|
|
assert res.status_code == 200
|
|
|
|
expected = list(
|
|
|
|
self.user_b.pictures.intersection(self.user_a.pictures.all())
|
|
|
|
.order_by("-picture__parent__date", "picture__date")
|
|
|
|
.values_list("picture_id", flat=True)
|
|
|
|
)
|
|
|
|
assert [i["id"] for i in res.json()["results"]] == expected
|
2024-07-26 12:25:26 +00:00
|
|
|
|
2024-08-09 22:43:15 +00:00
|
|
|
def test_num_queries(self):
|
|
|
|
"""Test that the number of queries is stable."""
|
|
|
|
self.client.force_login(subscriber_user.make())
|
|
|
|
with assertNumQueries(5):
|
|
|
|
# 1 request to fetch the user from the db
|
|
|
|
# 2 requests to check the user permissions
|
|
|
|
# 1 request to fetch the pictures
|
|
|
|
# 1 request to count the total number of items in the pagination
|
|
|
|
self.client.get(self.url)
|
|
|
|
|
2024-07-26 12:25:26 +00:00
|
|
|
|
|
|
|
class TestPictureRelation(TestSas):
|
|
|
|
def test_delete_relation_route_forbidden(self):
|
|
|
|
"""Test that unauthorized users are properly 403ed"""
|
|
|
|
# take a picture where user_a isn't identified
|
|
|
|
relation = PeoplePictureRelation.objects.exclude(user=self.user_a).first()
|
|
|
|
|
|
|
|
res = self.client.delete(f"/api/sas/relation/{relation.id}")
|
|
|
|
assert res.status_code == 403
|
|
|
|
|
|
|
|
for user in baker.make(User), self.user_a:
|
|
|
|
self.client.force_login(user)
|
|
|
|
res = self.client.delete(f"/api/sas/relation/{relation.id}")
|
|
|
|
assert res.status_code == 403
|
|
|
|
|
|
|
|
def test_delete_relation_with_authorized_users(self):
|
|
|
|
"""Test that deletion works as intended when called by an authorized user."""
|
|
|
|
relation: PeoplePictureRelation = self.user_a.pictures.first()
|
|
|
|
sas_admin_group = RealGroup.objects.get(pk=settings.SITH_GROUP_SAS_ADMIN_ID)
|
|
|
|
sas_admin = baker.make(User, groups=[sas_admin_group])
|
|
|
|
root = baker.make(User, is_superuser=True)
|
|
|
|
for user in sas_admin, root, self.user_a:
|
|
|
|
with transaction.atomic():
|
|
|
|
self.client.force_login(user)
|
|
|
|
res = self.client.delete(f"/api/sas/relation/{relation.id}")
|
|
|
|
assert res.status_code == 200
|
|
|
|
assert not PeoplePictureRelation.objects.filter(pk=relation.id).exists()
|
|
|
|
transaction.set_rollback(True)
|
|
|
|
public = baker.make(User)
|
|
|
|
relation = public.pictures.create(picture=relation.picture)
|
|
|
|
self.client.force_login(public)
|
|
|
|
res = self.client.delete(f"/api/sas/relation/{relation.id}")
|
|
|
|
assert res.status_code == 200
|
|
|
|
assert not PeoplePictureRelation.objects.filter(pk=relation.id).exists()
|
|
|
|
|
|
|
|
def test_delete_twice(self):
|
|
|
|
"""Test a duplicate call on the delete route."""
|
|
|
|
self.client.force_login(baker.make(User, is_superuser=True))
|
|
|
|
relation = PeoplePictureRelation.objects.first()
|
|
|
|
res = self.client.delete(f"/api/sas/relation/{relation.id}")
|
|
|
|
assert res.status_code == 200
|
|
|
|
relation_count = PeoplePictureRelation.objects.count()
|
|
|
|
res = self.client.delete(f"/api/sas/relation/{relation.id}")
|
|
|
|
assert res.status_code == 404
|
|
|
|
assert PeoplePictureRelation.objects.count() == relation_count
|