Repository: e-valuation/EvaP

Issue:
Separate error messages and error detection in reward points redemption
A reward point redemption can fail for several reasons, each of which has its own `Exception` subclass in `evap/rewards/models.py`. These exceptions are created deep in the reward point redemption logic. Currently, error messages that ultimately end up on the user's screen are set at the creation of the exceptions. In order to separate the "error display" and "error detection" logic, the user strings should be moved to where the exceptions are caught.

_Originally came up in https://github.com/e-valuation/EvaP/pull/1790#pullrequestreview-1105770573_


Discussion:

Resolving patch:
diff --git a/evap/rewards/tools.py b/evap/rewards/tools.py
--- a/evap/rewards/tools.py
+++ b/evap/rewards/tools.py
@@ -32,26 +32,22 @@ def save_redemptions(request, redemptions: Dict[int, int], previous_redeemed_poi
     # check consistent previous redeemed points
     # do not validate reward points, to allow receiving points after page load
     if previous_redeemed_points != redeemed_points_of_user(request.user):
-        raise OutdatedRedemptionData(
-            _(
-                "It appears that your browser sent multiple redemption requests. You can see all successful redemptions below."
-            )
-        )
+        raise OutdatedRedemptionData()
 
     total_points_available = reward_points_of_user(request.user)
     total_points_redeemed = sum(redemptions.values())
 
     if total_points_redeemed <= 0:
-        raise NoPointsSelected(_("You cannot redeem 0 points."))
+        raise NoPointsSelected()
 
     if total_points_redeemed > total_points_available:
-        raise NotEnoughPoints(_("You don't have enough reward points."))
+        raise NotEnoughPoints()
 
     for event_id in redemptions:
         if redemptions[event_id] > 0:
             event = get_object_or_404(RewardPointRedemptionEvent, pk=event_id)
             if event.redeem_end_date < date.today():
-                raise RedemptionEventExpired(_("Sorry, the deadline for this event expired already."))
+                raise RedemptionEventExpired()
 
             RewardPointRedemption.objects.create(user_profile=request.user, value=redemptions[event_id], event=event)
 
diff --git a/evap/rewards/views.py b/evap/rewards/views.py
--- a/evap/rewards/views.py
+++ b/evap/rewards/views.py
@@ -41,12 +41,21 @@ def redeem_reward_points(request):
     try:
         save_redemptions(request, redemptions, previous_redeemed_points)
         messages.success(request, _("You successfully redeemed your points."))
-    except (NoPointsSelected, NotEnoughPoints, RedemptionEventExpired) as error:
-        messages.warning(request, error)
-        return 400
-    except OutdatedRedemptionData as error:
-        messages.error(request, error)
-        return 409
+    except (NoPointsSelected, NotEnoughPoints, RedemptionEventExpired, OutdatedRedemptionData) as error:
+        status_code = 400
+        if isinstance(error, NoPointsSelected):
+            error_string = _("You cannot redeem 0 points.")
+        elif isinstance(error, NotEnoughPoints):
+            error_string = _("You don't have enough reward points.")
+        elif isinstance(error, RedemptionEventExpired):
+            error_string = _("Sorry, the deadline for this event expired already.")
+        elif isinstance(error, OutdatedRedemptionData):
+            status_code = 409
+            error_string = _(
+                "It appears that your browser sent multiple redemption requests. You can see all successful redemptions below."
+            )
+        messages.error(request, error_string)
+        return status_code
     return 200
 
 


Test patch:
diff --git a/evap/rewards/tests/test_views.py b/evap/rewards/tests/test_views.py
--- a/evap/rewards/tests/test_views.py
+++ b/evap/rewards/tests/test_views.py
@@ -69,6 +69,14 @@ def test_redeem_too_many_points(self):
         self.assertContains(response, "have enough reward points.", status_code=400)
         self.assertEqual(5, reward_points_of_user(self.student))
 
+    def test_redeem_zero_points(self):
+        response = self.app.get(self.url, user=self.student)
+        form = response.forms["reward-redemption-form"]
+        form.set(f"points-{self.event1.pk}", 0)
+        response = form.submit(status=400)
+        self.assertContains(response, "cannot redeem 0 points.", status_code=400)
+        self.assertEqual(5, reward_points_of_user(self.student))
+
     def test_redeem_points_for_expired_event(self):
         """Regression test for #846"""
         response = self.app.get(self.url, user=self.student)