Enforce uid with uppercase for Studentcard and test more edge cases

This commit is contained in:
Antoine Bartuccio 2019-05-14 15:51:14 +02:00
parent 5ae7d10e84
commit 19e353970d
Signed by: klmp200
GPG Key ID: E7245548C53F904B
2 changed files with 50 additions and 1 deletions

View File

@ -749,7 +749,8 @@ class StudentCard(models.Model):
@staticmethod @staticmethod
def is_valid(uid): def is_valid(uid):
return ( return (
len(uid) == StudentCard.UID_SIZE uid.isupper()
and len(uid) == StudentCard.UID_SIZE
and not StudentCard.objects.filter(uid=uid).exists() and not StudentCard.objects.filter(uid=uid).exists()
) )

View File

@ -188,6 +188,7 @@ class StudentCardTest(TestCase):
self.assertContains(response, text="8B90734A802A8F") self.assertContains(response, text="8B90734A802A8F")
def test_add_student_card_from_counter_fail(self): def test_add_student_card_from_counter_fail(self):
# UID too short
response = self.client.post( response = self.client.post(
reverse( reverse(
"counter:click", "counter:click",
@ -199,6 +200,7 @@ class StudentCardTest(TestCase):
response, text="Ce n'est pas un UID de carte étudiante valide" response, text="Ce n'est pas un UID de carte étudiante valide"
) )
# UID too long
response = self.client.post( response = self.client.post(
reverse( reverse(
"counter:click", "counter:click",
@ -210,6 +212,30 @@ class StudentCardTest(TestCase):
response, text="Ce n'est pas un UID de carte étudiante valide" response, text="Ce n'est pas un UID de carte étudiante valide"
) )
# Test with already existing card
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
),
{"student_card_uid": "9A89B82018B0A0", "action": "add_student_card"},
)
self.assertContains(
response, text="Ce n'est pas un UID de carte étudiante valide"
)
# Test with lowercase
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
),
{"student_card_uid": "8b90734a802a9f", "action": "add_student_card"},
)
self.assertContains(
response, text="Ce n'est pas un UID de carte étudiante valide"
)
def test_delete_student_card_with_owner(self): def test_delete_student_card_with_owner(self):
self.client.login(username="sli", password="plop") self.client.login(username="sli", password="plop")
self.client.post( self.client.post(
@ -308,6 +334,7 @@ class StudentCardTest(TestCase):
def test_add_student_card_from_user_preferences_fail(self): def test_add_student_card_from_user_preferences_fail(self):
self.client.login(username="sli", password="plop") self.client.login(username="sli", password="plop")
# UID too short
response = self.client.post( response = self.client.post(
reverse( reverse(
"counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk} "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
@ -317,6 +344,7 @@ class StudentCardTest(TestCase):
self.assertContains(response, text="Cet UID est invalide") self.assertContains(response, text="Cet UID est invalide")
# UID too long
response = self.client.post( response = self.client.post(
reverse( reverse(
"counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk} "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
@ -325,6 +353,26 @@ class StudentCardTest(TestCase):
) )
self.assertContains(response, text="Cet UID est invalide") self.assertContains(response, text="Cet UID est invalide")
# Test with already existing card
response = self.client.post(
reverse(
"counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
),
{"uid": "9A89B82018B0A0"},
)
self.assertContains(
response, text="Un objet Student card avec ce champ Uid existe déjà."
)
# Test with lowercase
response = self.client.post(
reverse(
"counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
),
{"uid": "8b90734a802a9f"},
)
self.assertContains(response, text="Cet UID est invalide")
# Test with unauthorized user # Test with unauthorized user
self.client.login(username="krophil", password="plop") self.client.login(username="krophil", password="plop")
response = self.client.post( response = self.client.post(