diff --git a/counter/models.py b/counter/models.py index 3306a550..93105908 100644 --- a/counter/models.py +++ b/counter/models.py @@ -749,7 +749,8 @@ class StudentCard(models.Model): @staticmethod def is_valid(uid): return ( - len(uid) == StudentCard.UID_SIZE + uid.isupper() + 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 e001b13f..1547eea8 100644 --- a/counter/tests.py +++ b/counter/tests.py @@ -188,6 +188,7 @@ class StudentCardTest(TestCase): self.assertContains(response, text="8B90734A802A8F") def test_add_student_card_from_counter_fail(self): + # UID too short response = self.client.post( reverse( "counter:click", @@ -199,6 +200,7 @@ class StudentCardTest(TestCase): response, text="Ce n'est pas un UID de carte étudiante valide" ) + # UID too long response = self.client.post( reverse( "counter:click", @@ -210,6 +212,30 @@ class StudentCardTest(TestCase): 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): self.client.login(username="sli", password="plop") self.client.post( @@ -308,6 +334,7 @@ class StudentCardTest(TestCase): def test_add_student_card_from_user_preferences_fail(self): self.client.login(username="sli", password="plop") + # UID too short response = self.client.post( reverse( "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") + # UID too long response = self.client.post( reverse( "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") + # 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 self.client.login(username="krophil", password="plop") response = self.client.post(