Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor reversed constraints #200

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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