From 1f3220246a87ec2d75a2834d173830056ce21771 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Fri, 24 May 2019 08:38:15 +0200 Subject: [PATCH] counter: fix a bug where you can't register UID card with only number inside --- counter/models.py | 2 +- counter/tests.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/counter/models.py b/counter/models.py index 93105908..703f2404 100644 --- a/counter/models.py +++ b/counter/models.py @@ -749,7 +749,7 @@ class StudentCard(models.Model): @staticmethod def is_valid(uid): return ( - uid.isupper() + (uid.isupper() or uid.isnumeric()) and len(uid) == StudentCard.UID_SIZE and not StudentCard.objects.filter(uid=uid).exists() ) diff --git a/counter/tests.py b/counter/tests.py index 1547eea8..2814d7b7 100644 --- a/counter/tests.py +++ b/counter/tests.py @@ -178,6 +178,7 @@ class StudentCardTest(TestCase): ) def test_add_student_card_from_counter(self): + # Test card with mixed letters and numbers response = self.client.post( reverse( "counter:click", @@ -187,6 +188,26 @@ class StudentCardTest(TestCase): ) 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): # UID too short response = self.client.post( @@ -236,6 +257,18 @@ class StudentCardTest(TestCase): 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): self.client.login(username="sli", password="plop") self.client.post( @@ -318,6 +351,30 @@ class StudentCardTest(TestCase): ) 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 self.client.login(username="root", password="plop") self.client.post( @@ -373,6 +430,15 @@ class StudentCardTest(TestCase): ) 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 self.client.login(username="krophil", password="plop") response = self.client.post(