mirror of
				https://github.com/ae-utbm/sith.git
				synced 2025-10-31 09:03:06 +00:00 
			
		
		
		
	Add unit tests for student cards and fix edge cases
This commit is contained in:
		
							
								
								
									
										192
									
								
								counter/tests.py
									
									
									
									
									
								
							
							
						
						
									
										192
									
								
								counter/tests.py
									
									
									
									
									
								
							| @@ -142,3 +142,195 @@ class BarmanConnectionTest(TestCase): | ||||
|         self.assertFalse( | ||||
|             '<li><a href="/user/1/">S' Kia</a></li>' in str(response_get.content) | ||||
|         ) | ||||
|  | ||||
|  | ||||
| class StudentCardTest(TestCase): | ||||
|     """ | ||||
|     Tests for adding and deleting Stundent Cards | ||||
|     Test that an user can be found with it's student card | ||||
|     """ | ||||
|  | ||||
|     def setUp(self): | ||||
|         call_command("populate") | ||||
|         self.krophil = User.objects.get(username="krophil") | ||||
|         self.sli = User.objects.get(username="sli") | ||||
|  | ||||
|         self.counter = Counter.objects.filter(id=2).first() | ||||
|  | ||||
|         # Auto login on counter | ||||
|         self.client.post( | ||||
|             reverse("counter:login", args=[self.counter.id]), | ||||
|             {"username": "krophil", "password": "plop"}, | ||||
|         ) | ||||
|  | ||||
|     def test_search_user_with_student_card(self): | ||||
|         response = self.client.post( | ||||
|             reverse("counter:details", args=[self.counter.id]), | ||||
|             {"code": "9A89B82018B0A0"}, | ||||
|         ) | ||||
|  | ||||
|         self.assertEqual( | ||||
|             response.url, | ||||
|             reverse( | ||||
|                 "counter:click", | ||||
|                 kwargs={"counter_id": self.counter.id, "user_id": self.sli.id}, | ||||
|             ), | ||||
|         ) | ||||
|  | ||||
|     def test_add_student_card_from_counter(self): | ||||
|         response = self.client.post( | ||||
|             reverse( | ||||
|                 "counter:click", | ||||
|                 kwargs={"counter_id": self.counter.id, "user_id": self.sli.id}, | ||||
|             ), | ||||
|             {"student_card_uid": "8B90734A802A8F", "action": "add_student_card"}, | ||||
|         ) | ||||
|         self.assertContains(response, text="8B90734A802A8F") | ||||
|  | ||||
|     def test_add_student_card_from_counter_fail(self): | ||||
|         response = self.client.post( | ||||
|             reverse( | ||||
|                 "counter:click", | ||||
|                 kwargs={"counter_id": self.counter.id, "user_id": self.sli.id}, | ||||
|             ), | ||||
|             {"student_card_uid": "8B90734A802A8", "action": "add_student_card"}, | ||||
|         ) | ||||
|         self.assertContains( | ||||
|             response, text="Ce n'est pas un UID de carte étudiante valide" | ||||
|         ) | ||||
|  | ||||
|         response = self.client.post( | ||||
|             reverse( | ||||
|                 "counter:click", | ||||
|                 kwargs={"counter_id": self.counter.id, "user_id": self.sli.id}, | ||||
|             ), | ||||
|             {"student_card_uid": "8B90734A802A8FA", "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( | ||||
|             reverse( | ||||
|                 "counter:delete_student_card", | ||||
|                 kwargs={ | ||||
|                     "customer_id": self.sli.customer.pk, | ||||
|                     "card_id": self.sli.customer.student_cards.first().id, | ||||
|                 }, | ||||
|             ) | ||||
|         ) | ||||
|         self.assertFalse(self.sli.customer.student_cards.exists()) | ||||
|  | ||||
|     def test_delete_student_card_with_board_member(self): | ||||
|         self.client.login(username="skia", password="plop") | ||||
|         self.client.post( | ||||
|             reverse( | ||||
|                 "counter:delete_student_card", | ||||
|                 kwargs={ | ||||
|                     "customer_id": self.sli.customer.pk, | ||||
|                     "card_id": self.sli.customer.student_cards.first().id, | ||||
|                 }, | ||||
|             ) | ||||
|         ) | ||||
|         self.assertFalse(self.sli.customer.student_cards.exists()) | ||||
|  | ||||
|     def test_delete_student_card_with_root(self): | ||||
|         self.client.login(username="root", password="plop") | ||||
|         self.client.post( | ||||
|             reverse( | ||||
|                 "counter:delete_student_card", | ||||
|                 kwargs={ | ||||
|                     "customer_id": self.sli.customer.pk, | ||||
|                     "card_id": self.sli.customer.student_cards.first().id, | ||||
|                 }, | ||||
|             ) | ||||
|         ) | ||||
|         self.assertFalse(self.sli.customer.student_cards.exists()) | ||||
|  | ||||
|     def test_delete_student_card_fail(self): | ||||
|         self.client.login(username="krophil", password="plop") | ||||
|         response = self.client.post( | ||||
|             reverse( | ||||
|                 "counter:delete_student_card", | ||||
|                 kwargs={ | ||||
|                     "customer_id": self.sli.customer.pk, | ||||
|                     "card_id": self.sli.customer.student_cards.first().id, | ||||
|                 }, | ||||
|             ) | ||||
|         ) | ||||
|         self.assertEqual(response.status_code, 403) | ||||
|         self.assertTrue(self.sli.customer.student_cards.exists()) | ||||
|  | ||||
|     def test_add_student_card_from_user_preferences(self): | ||||
|         # Test with owner of the card | ||||
|         self.client.login(username="sli", password="plop") | ||||
|         self.client.post( | ||||
|             reverse( | ||||
|                 "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk} | ||||
|             ), | ||||
|             {"uid": "8B90734A802A8F"}, | ||||
|         ) | ||||
|  | ||||
|         response = self.client.get( | ||||
|             reverse("core:user_prefs", kwargs={"user_id": self.sli.id}) | ||||
|         ) | ||||
|         self.assertContains(response, text="8B90734A802A8F") | ||||
|  | ||||
|         # Test with board member | ||||
|         self.client.login(username="skia", password="plop") | ||||
|         self.client.post( | ||||
|             reverse( | ||||
|                 "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk} | ||||
|             ), | ||||
|             {"uid": "8B90734A802A8A"}, | ||||
|         ) | ||||
|  | ||||
|         response = self.client.get( | ||||
|             reverse("core:user_prefs", kwargs={"user_id": self.sli.id}) | ||||
|         ) | ||||
|         self.assertContains(response, text="8B90734A802A8A") | ||||
|  | ||||
|         # Test with root | ||||
|         self.client.login(username="root", password="plop") | ||||
|         self.client.post( | ||||
|             reverse( | ||||
|                 "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk} | ||||
|             ), | ||||
|             {"uid": "8B90734A802A8B"}, | ||||
|         ) | ||||
|  | ||||
|         response = self.client.get( | ||||
|             reverse("core:user_prefs", kwargs={"user_id": self.sli.id}) | ||||
|         ) | ||||
|         self.assertContains(response, text="8B90734A802A8B") | ||||
|  | ||||
|     def test_add_student_card_from_user_preferences_fail(self): | ||||
|         self.client.login(username="sli", password="plop") | ||||
|         response = self.client.post( | ||||
|             reverse( | ||||
|                 "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk} | ||||
|             ), | ||||
|             {"uid": "8B90734A802A8"}, | ||||
|         ) | ||||
|  | ||||
|         self.assertContains(response, text="Cet UID est invalide") | ||||
|  | ||||
|         response = self.client.post( | ||||
|             reverse( | ||||
|                 "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk} | ||||
|             ), | ||||
|             {"uid": "8B90734A802A8FA"}, | ||||
|         ) | ||||
|         self.assertContains(response, text="Cet UID est invalide") | ||||
|  | ||||
|         # Test with unauthorized user | ||||
|         self.client.login(username="krophil", password="plop") | ||||
|         response = self.client.post( | ||||
|             reverse( | ||||
|                 "counter:add_student_card", kwargs={"customer_id": self.sli.customer.pk} | ||||
|             ), | ||||
|             {"uid": "8B90734A802A8F"}, | ||||
|         ) | ||||
|         self.assertEqual(response.status_code, 403) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user