Skip to content

Commit

Permalink
Merge pull request #200 from UnitapApp/feature/reversed-constraint
Browse files Browse the repository at this point in the history
Refactor reversed constraints
  • Loading branch information
ShayanShiravani authored Nov 29, 2023
2 parents 6ba49f9 + 03cf829 commit dc89de5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions prizetap/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ def validate(self, data):
constraint_class.is_valid_param_keys(constraint_params[c.name])
except KeyError as e:
raise serializers.ValidationError({"constraint_params": [{f"{c.name}": str(e)}]})
valid_constraints = [c.name for c in constraints]
valid_constraints = [str(c.pk) for c in constraints]
if len(reversed_constraints) > 0:
for c in reversed_constraints:
if c not in valid_constraints:
raise serializers.ValidationError({"reversed_constraints": [{f"{c}": "Invalid constraint name"}]})
raise serializers.ValidationError({"reversed_constraints": [{f"{c}": "Invalid constraint pk"}]})
if "winners_count" in data and data["winners_count"] > data["max_number_of_entries"]:
raise serializers.ValidationError({"winners_count": "Invalid value"})
valid_chains = list(CONTRACT_ADDRESSES.keys())
Expand Down
13 changes: 9 additions & 4 deletions prizetap/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,23 @@ def test_create_raffle_with_invalid_constraint_params(self):
lambda a, b, c: (True, None),
)
def test_reversed_constraints(self):
self.raffle.reversed_constraints = "core.BrightIDMeetVerification"
self.raffle.reversed_constraints = str(self.meet_constraint.pk)
self.raffle.save()
validator = RaffleEnrollmentValidator(user_profile=self.user_profile, raffle=self.raffle)
self.assertRaises(PermissionDenied, validator.check_user_constraints)

def test_create_raffle_with_invalid_reversed_constraints(self):
self.client.force_authenticate(user=self.user_profile.user)
self.raffle_data["constraints"] = [self.meet_constraint.pk]
self.raffle_data["reversed_constraints"] = "core.BrightIDAuraVerification"
aura_verified_constraint = Constraint.objects.create(
name="core.BrightIDAuraVerification",
title="BrightID aura",
description="You have to be Aura verified.",
)
self.raffle_data["reversed_constraints"] = str(aura_verified_constraint.pk)
response = self.client.post(reverse("create-raffle"), self.raffle_data)
self.assertEqual(
str(response.data["reversed_constraints"][0]["core.BrightIDAuraVerification"]), "Invalid constraint name"
str(response.data["reversed_constraints"][0][str(aura_verified_constraint.pk)]), "Invalid constraint pk"
)
self.assertEqual(response.status_code, 400)
self.assertEqual(Raffle.objects.count(), 1)
Expand All @@ -273,7 +278,7 @@ def test_create_raffle_with_invalid_reversed_constraints(self):
def test_create_raffle_with_reversed_constraints(self):
self.client.force_authenticate(user=self.user_profile.user)
self.raffle_data["constraints"] = [self.meet_constraint.pk]
self.raffle_data["reversed_constraints"] = self.meet_constraint.name
self.raffle_data["reversed_constraints"] = str(self.meet_constraint.pk)
response = self.client.post(reverse("create-raffle"), self.raffle_data)
self.assertEqual(response.status_code, 200)
self.assertEqual(Raffle.objects.count(), 2)
Expand Down
2 changes: 1 addition & 1 deletion prizetap/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def check_user_constraints(self):
constraint.param_values = param_values[c.name]
except KeyError:
pass
if c.name in reversed_constraints:
if str(c.pk) in reversed_constraints:
if constraint.is_observed():
raise PermissionDenied(constraint.response)
else:
Expand Down

0 comments on commit dc89de5

Please sign in to comment.