counter: fix a bug where you can't register UID card with only number inside

This commit is contained in:
Antoine Bartuccio 2019-05-24 08:38:15 +02:00
parent 0f832a2774
commit 1f3220246a
Signed by: klmp200
GPG Key ID: E7245548C53F904B
2 changed files with 67 additions and 1 deletions

View File

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

View File

@ -178,6 +178,7 @@ class StudentCardTest(TestCase):
) )
def test_add_student_card_from_counter(self): def test_add_student_card_from_counter(self):
# Test card with mixed letters and numbers
response = self.client.post( response = self.client.post(
reverse( reverse(
"counter:click", "counter:click",
@ -187,6 +188,26 @@ class StudentCardTest(TestCase):
) )
self.assertContains(response, text="8B90734A802A8F") self.assertContains(response, text="8B90734A802A8F")
# Test card with only numbers
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
),
{"student_card_uid": "04786547890123", "action": "add_student_card"},
)
self.assertContains(response, text="04786547890123")
# Test card with only letters
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
),
{"student_card_uid": "ABCAAAFAAFAAAB", "action": "add_student_card"},
)
self.assertContains(response, text="ABCAAAFAAFAAAB")
def test_add_student_card_from_counter_fail(self): def test_add_student_card_from_counter_fail(self):
# UID too short # UID too short
response = self.client.post( response = self.client.post(
@ -236,6 +257,18 @@ 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 white spaces
response = self.client.post(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": self.sli.id},
),
{"student_card_uid": " ", "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(
@ -318,6 +351,30 @@ class StudentCardTest(TestCase):
) )
self.assertContains(response, text="8B90734A802A8A") self.assertContains(response, text="8B90734A802A8A")
# Test card with only numbers
self.client.post(
reverse(
"counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
),
{"uid": "04786547890123"},
)
response = self.client.get(
reverse("core:user_prefs", kwargs={"user_id": self.sli.id})
)
self.assertContains(response, text="04786547890123")
# Test card with only letters
self.client.post(
reverse(
"counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
),
{"uid": "ABCAAAFAAFAAAB"},
)
response = self.client.get(
reverse("core:user_prefs", kwargs={"user_id": self.sli.id})
)
self.assertContains(response, text="ABCAAAFAAFAAAB")
# Test with root # Test with root
self.client.login(username="root", password="plop") self.client.login(username="root", password="plop")
self.client.post( self.client.post(
@ -373,6 +430,15 @@ class StudentCardTest(TestCase):
) )
self.assertContains(response, text="Cet UID est invalide") self.assertContains(response, text="Cet UID est invalide")
# Test with white spaces
response = self.client.post(
reverse(
"counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk}
),
{"uid": " "},
)
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(