From 23cda5d64d3542bf60315a9f952dd9f87b04cf8a Mon Sep 17 00:00:00 2001
From: Ethan Nguyen <etnguyen03@hotmail.com>
Date: Thu, 18 Nov 2021 14:16:29 -0600
Subject: [PATCH] feat(profile): limit GPA to reasonable values

GPA must be 0 <= GPA <= 5
---
 tjdests/apps/profile/forms.py | 12 ++++++++++++
 tjdests/apps/profile/tests.py | 20 ++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/tjdests/apps/profile/forms.py b/tjdests/apps/profile/forms.py
index ec6174e..071fff0 100644
--- a/tjdests/apps/profile/forms.py
+++ b/tjdests/apps/profile/forms.py
@@ -22,6 +22,18 @@ class ProfilePublishForm(forms.ModelForm):
             user=self.instance, admission_status__contains="ADMIT"
         )
 
+    def clean(self) -> Dict[str, Any]:
+        cleaned_data = super().clean()
+
+        # Check the GPA: 0.0 <= GPA <= 5.0
+        if cleaned_data.get("GPA"):
+            gpa = cleaned_data.get("GPA")
+            assert type(gpa) is float
+            if not 0.0 <= gpa <= 5.0:
+                self.add_error("GPA", "This is not a valid GPA")
+
+        return cleaned_data
+
     class Meta:
         model = User
         fields = ["publish_data", "GPA", "biography", "attending_decision"]
diff --git a/tjdests/apps/profile/tests.py b/tjdests/apps/profile/tests.py
index 60bc257..fcea528 100644
--- a/tjdests/apps/profile/tests.py
+++ b/tjdests/apps/profile/tests.py
@@ -52,6 +52,26 @@ class ProfileTest(TJDestsTestCase):
             ).count(),
         )
 
+        response = self.client.post(
+            reverse("profile:index"),
+            data={
+                "biography": "sdf",
+                "attending_decision": "",
+                "publish_data": False,
+            },
+        )
+        self.assertEqual(200, response.status_code)
+        self.assertEqual(
+            1,
+            User.objects.filter(
+                GPA=None,
+                id=user.id,
+                biography="sdf",
+                attending_decision=None,
+                publish_data=False,
+            ).count(),
+        )
+
         # Test creating an admitted decision, then setting that as our destination.
         college = College.objects.get_or_create(name="test college")[0]
         decision = Decision.objects.get_or_create(