2021-06-14 19:45:36 +00:00
|
|
|
# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
|
2020-05-30 13:46:09 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-08-15 22:35:13 +00:00
|
|
|
from datetime import date
|
2020-09-04 14:43:57 +00:00
|
|
|
from json.decoder import JSONDecodeError
|
2020-08-15 22:35:13 +00:00
|
|
|
|
2020-05-30 13:46:09 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.core.exceptions import FieldError
|
|
|
|
from django.db.models import F, Q
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.utils import timezone
|
|
|
|
from member.models import Club, Membership
|
|
|
|
from note.models import NoteUser, Note, NoteClub, NoteSpecial
|
|
|
|
from wei.models import WEIMembership, WEIRegistration, WEIClub, Bus, BusTeam
|
|
|
|
|
2020-08-13 13:20:15 +00:00
|
|
|
from ..models import Permission
|
2020-05-30 13:46:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PermissionQueryTestCase(TestCase):
|
|
|
|
fixtures = ('initial', )
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
user = User.objects.create(username="user")
|
|
|
|
NoteUser.objects.create(user=user)
|
|
|
|
wei = WEIClub.objects.create(
|
|
|
|
name="wei",
|
2020-08-15 22:35:13 +00:00
|
|
|
date_start=date.today(),
|
|
|
|
date_end=date.today(),
|
2020-05-30 13:46:09 +00:00
|
|
|
)
|
|
|
|
NoteClub.objects.create(club=wei)
|
|
|
|
weiregistration = WEIRegistration.objects.create(
|
|
|
|
user=user,
|
|
|
|
wei=wei,
|
2020-08-15 22:35:13 +00:00
|
|
|
birth_date=date.today(),
|
2020-05-30 13:46:09 +00:00
|
|
|
)
|
|
|
|
bus = Bus.objects.create(
|
|
|
|
name="bus",
|
|
|
|
wei=wei,
|
|
|
|
)
|
|
|
|
team = BusTeam.objects.create(
|
|
|
|
name="team",
|
|
|
|
bus=bus,
|
|
|
|
color=0xFFFFFF,
|
|
|
|
)
|
|
|
|
WEIMembership.objects.create(
|
|
|
|
user=user,
|
|
|
|
club=wei,
|
|
|
|
registration=weiregistration,
|
|
|
|
bus=bus,
|
|
|
|
team=team,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_permission_queries(self):
|
|
|
|
"""
|
|
|
|
Check for all permissions that the query is compilable and that the database can parse the query.
|
|
|
|
We use a random user with a random WEIClub (to use permissions for the WEI) in a random team in a random bus.
|
|
|
|
"""
|
|
|
|
for perm in Permission.objects.all():
|
|
|
|
try:
|
2020-09-04 14:43:57 +00:00
|
|
|
instanced = perm.about(
|
|
|
|
user=User.objects.get(),
|
|
|
|
club=WEIClub.objects.get(),
|
|
|
|
membership=Membership.objects.get(),
|
|
|
|
User=User,
|
|
|
|
Club=Club,
|
|
|
|
Membership=Membership,
|
|
|
|
Note=Note,
|
|
|
|
NoteUser=NoteUser,
|
|
|
|
NoteClub=NoteClub,
|
|
|
|
NoteSpecial=NoteSpecial,
|
|
|
|
F=F,
|
|
|
|
Q=Q,
|
|
|
|
now=timezone.now(),
|
|
|
|
today=date.today(),
|
|
|
|
)
|
2020-05-30 13:46:09 +00:00
|
|
|
instanced.update_query()
|
|
|
|
query = instanced.query
|
|
|
|
model = perm.model.model_class()
|
|
|
|
model.objects.filter(query).all()
|
2020-12-23 17:45:05 +00:00
|
|
|
except (FieldError, AttributeError, ValueError, TypeError, JSONDecodeError): # pragma: no cover
|
2020-05-30 13:46:09 +00:00
|
|
|
print("Query error for permission", perm)
|
|
|
|
print("Query:", perm.query)
|
|
|
|
if instanced.query:
|
|
|
|
print("Compiled query:", instanced.query)
|
|
|
|
raise
|