mirror of
https://github.com/ae-utbm/sith.git
synced 2026-04-09 04:05:26 +00:00
Fix bug where you can't select /SAS as a parent album
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
# OR WITHIN THE LOCAL FILE "LICENSE"
|
||||
#
|
||||
#
|
||||
from datetime import date
|
||||
from typing import Callable
|
||||
|
||||
import pytest
|
||||
@@ -20,6 +21,7 @@ from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.test import Client, TestCase
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from model_bakery import baker
|
||||
from pytest_django.asserts import assertHTMLEqual, assertInHTML, assertRedirects
|
||||
|
||||
@@ -133,6 +135,180 @@ class TestAlbumUpload:
|
||||
assert not album.children.exists()
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
class TestAlbumEdit:
|
||||
@pytest.fixture
|
||||
def sas_root(self) -> Album:
|
||||
return Album.objects.get(id=settings.SITH_SAS_ROOT_DIR_ID)
|
||||
|
||||
@pytest.fixture
|
||||
def album(self, sas_root: Album) -> Album:
|
||||
return baker.make(Album, parent=sas_root, is_moderated=True)
|
||||
|
||||
@pytest.fixture
|
||||
def moderator(self) -> User:
|
||||
return baker.make(
|
||||
User, groups=[Group.objects.get(pk=settings.SITH_GROUP_SAS_ADMIN_ID)]
|
||||
)
|
||||
|
||||
@pytest.fixture
|
||||
def subscriber(self) -> User:
|
||||
return subscriber_user.make()
|
||||
|
||||
@pytest.fixture
|
||||
def root(self) -> User:
|
||||
return User.objects.get(username="root")
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"user",
|
||||
[
|
||||
None,
|
||||
"subscriber",
|
||||
],
|
||||
)
|
||||
def test_permission_denied(
|
||||
self,
|
||||
client: Client,
|
||||
album: Album,
|
||||
request: pytest.FixtureRequest,
|
||||
user: str | None,
|
||||
):
|
||||
if user:
|
||||
client.force_login(request.getfixturevalue(user))
|
||||
response = client.get(reverse("sas:album_edit", kwargs={"album_id": album.pk}))
|
||||
assert response.status_code == 403
|
||||
response = client.post(reverse("sas:album_edit", kwargs={"album_id": album.pk}))
|
||||
assert response.status_code == 403
|
||||
|
||||
def test_sas_root_read_only(self, client: Client, sas_root: Album, moderator: User):
|
||||
client.force_login(moderator)
|
||||
response = client.get(
|
||||
reverse("sas:album_edit", kwargs={"album_id": sas_root.pk})
|
||||
)
|
||||
assert response.status_code == 404
|
||||
response = client.post(
|
||||
reverse("sas:album_edit", kwargs={"album_id": sas_root.pk})
|
||||
)
|
||||
assert response.status_code == 404
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"user",
|
||||
[
|
||||
"root",
|
||||
"moderator",
|
||||
],
|
||||
)
|
||||
def test_update(
|
||||
self,
|
||||
client: Client,
|
||||
album: Album,
|
||||
sas_root: Album,
|
||||
request: pytest.FixtureRequest,
|
||||
user: str,
|
||||
):
|
||||
client.force_login(request.getfixturevalue(user))
|
||||
response = client.get(reverse("sas:album_edit", kwargs={"album_id": album.pk}))
|
||||
assert response.status_code == 200
|
||||
|
||||
# Test no changes
|
||||
response = client.post(reverse("sas:album_edit", kwargs={"album_id": album.pk}))
|
||||
assert response.status_code == 200
|
||||
|
||||
updated_album = Album.objects.get(id=album.pk)
|
||||
assert album.name == updated_album.name
|
||||
assert album.date == updated_album.date
|
||||
assert album.parent == updated_album.parent
|
||||
assert album.edit_groups == updated_album.edit_groups
|
||||
|
||||
# Prepare a good payload
|
||||
payload = {
|
||||
"name": album.name[50],
|
||||
"parent": baker.make(Album, parent=sas_root, is_moderated=True).pk,
|
||||
"date": date.today().isoformat(),
|
||||
"recursive": False,
|
||||
}
|
||||
# Test missing parent
|
||||
payload_missing_parent = {**payload}
|
||||
del payload_missing_parent["parent"]
|
||||
response = client.post(
|
||||
reverse(
|
||||
"sas:album_edit",
|
||||
kwargs={"album_id": album.pk},
|
||||
),
|
||||
payload_missing_parent,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
updated_album = Album.objects.get(id=album.pk)
|
||||
assert updated_album.name == album.name
|
||||
assert updated_album.parent == album.parent
|
||||
assert updated_album.date == album.date
|
||||
|
||||
# Test missing date
|
||||
payload_missing_date = {**payload}
|
||||
del payload_missing_date["date"]
|
||||
response = client.post(
|
||||
reverse(
|
||||
"sas:album_edit",
|
||||
kwargs={"album_id": album.pk},
|
||||
),
|
||||
payload_missing_date,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
updated_album = Album.objects.get(id=album.pk)
|
||||
assert updated_album.name == album.name
|
||||
assert updated_album.parent == album.parent
|
||||
assert updated_album.date == album.date
|
||||
|
||||
# Test recursive parent
|
||||
payload_recursive_parent = {**payload}
|
||||
payload_recursive_parent["parent"] = album.pk
|
||||
response = client.post(
|
||||
reverse(
|
||||
"sas:album_edit",
|
||||
kwargs={"album_id": album.pk},
|
||||
),
|
||||
payload_recursive_parent,
|
||||
)
|
||||
assert response.status_code == 200
|
||||
updated_album = Album.objects.get(id=album.pk)
|
||||
assert updated_album.name == album.name
|
||||
assert updated_album.parent == album.parent
|
||||
assert updated_album.date == album.date
|
||||
|
||||
# Test successful update
|
||||
response = client.post(
|
||||
reverse(
|
||||
"sas:album_edit",
|
||||
kwargs={"album_id": album.pk},
|
||||
),
|
||||
payload,
|
||||
)
|
||||
assert response.status_code == 302
|
||||
updated_album = Album.objects.get(id=album.pk)
|
||||
assert updated_album.name == payload["name"]
|
||||
assert updated_album.parent.id == payload["parent"]
|
||||
assert timezone.localdate(updated_album.date) == date.fromisoformat(
|
||||
payload["date"]
|
||||
)
|
||||
|
||||
# Test root album can be used as parent
|
||||
payload["parent"] = sas_root.pk
|
||||
response = client.post(
|
||||
reverse(
|
||||
"sas:album_edit",
|
||||
kwargs={"album_id": album.pk},
|
||||
),
|
||||
payload,
|
||||
)
|
||||
assert response.status_code == 302
|
||||
updated_album = Album.objects.get(id=album.pk)
|
||||
assert updated_album.name == payload["name"]
|
||||
assert updated_album.parent.id == payload["parent"]
|
||||
assert timezone.localdate(updated_album.date) == date.fromisoformat(
|
||||
payload["date"]
|
||||
)
|
||||
|
||||
|
||||
class TestSasModeration(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
|
||||
Reference in New Issue
Block a user