From 03cf82996aecc2b0d8c9abe0860993d3e17bbe42 Mon Sep 17 00:00:00 2001 From: Shayan Shiravani Date: Wed, 29 Nov 2023 16:46:05 +0330 Subject: [PATCH] Refactor reversed constraints --- prizetap/serializers.py | 4 ++-- prizetap/tests.py | 13 +++++++++---- prizetap/validators.py | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/prizetap/serializers.py b/prizetap/serializers.py index 1cf26212..f7d502b8 100644 --- a/prizetap/serializers.py +++ b/prizetap/serializers.py @@ -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()) diff --git a/prizetap/tests.py b/prizetap/tests.py index 291310c5..a410a11e 100644 --- a/prizetap/tests.py +++ b/prizetap/tests.py @@ -243,7 +243,7 @@ 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) @@ -251,10 +251,15 @@ def test_reversed_constraints(self): 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) @@ -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) diff --git a/prizetap/validators.py b/prizetap/validators.py index 7c0c8c5a..2b86ec33 100644 --- a/prizetap/validators.py +++ b/prizetap/validators.py @@ -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: