Skip to content

Commit

Permalink
Refactor get raffle constraints depending on reversed constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
ShayanShiravani committed Nov 30, 2023
1 parent ccc3397 commit 40649ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
21 changes: 21 additions & 0 deletions prizetap/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,27 @@ def test_get_raffle_constraints_when_is_verified(self):
self.assertEqual(data["pk"], self.meet_constraint.pk)
self.assertEqual(data["is_verified"], True)

@patch(
"authentication.helpers.BrightIDSoulboundAPIInterface.get_verification_status",
lambda a, b, c: (True, None),
)
def test_get_raffle_constraints_when_constraint_is_reversed(self):
self.client.force_authenticate(user=self.user_profile.user)
self.raffle.reversed_constraints = str(self.meet_constraint.pk)
self.raffle.save()
response = self.client.get(reverse("get-raffle-constraints", kwargs={"raffle_pk": self.raffle.pk}))
data = response.data["constraints"][0]
self.assertEqual(response.status_code, 200)
self.assertEqual(data["pk"], self.meet_constraint.pk)
self.assertEqual(data["is_verified"], False)

BrightIDSoulboundAPIInterface.get_verification_status = MagicMock(return_value=(False, None))
response = self.client.get(reverse("get-raffle-constraints", kwargs={"raffle_pk": self.raffle.pk}))
data = response.data["constraints"][0]
self.assertEqual(response.status_code, 200)
self.assertEqual(data["pk"], self.meet_constraint.pk)
self.assertEqual(data["is_verified"], True)


class RaffleEntryTestCase(RaffleTestCase):
def setUp(self):
Expand Down
9 changes: 7 additions & 2 deletions prizetap/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def get(self, request, raffle_pk):
except Exception:
param_values = {}

reversed_constraints = raffle.reversed_constraints.split(",") if raffle.reversed_constraints else []
response_constraints = []

for c in raffle.constraints.all():
Expand All @@ -155,8 +156,12 @@ def get(self, request, raffle_pk):
except KeyError:
pass
is_verified = False
if constraint.is_observed():
is_verified = True
if str(c.pk) in reversed_constraints:
if not constraint.is_observed():
is_verified = True
else:
if constraint.is_observed():
is_verified = True
response_constraints.append({**ConstraintSerializer(c).data, "is_verified": is_verified})

return Response({"success": True, "constraints": response_constraints}, status=200)
Expand Down

0 comments on commit 40649ac

Please sign in to comment.