From ad72f1ca92ad1b76eb180727ae0a3840e6c3043c Mon Sep 17 00:00:00 2001 From: Abner Tudtud Date: Thu, 4 Apr 2024 16:33:57 +0800 Subject: [PATCH 1/3] Remove validation on GetBeam --- src/GraphQL/Mutations/ClaimBeamMutation.php | 2 +- src/Rules/SingleUseCodeExist.php | 10 ++++++- tests/Feature/GraphQL/Queries/GetBeamTest.php | 27 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/GraphQL/Mutations/ClaimBeamMutation.php b/src/GraphQL/Mutations/ClaimBeamMutation.php index 91bfb5e..b9274d2 100644 --- a/src/GraphQL/Mutations/ClaimBeamMutation.php +++ b/src/GraphQL/Mutations/ClaimBeamMutation.php @@ -113,7 +113,7 @@ protected function rules(array $args = []): array 'filled', 'max:1024', new NotExpired($beamCode), - $singleUse ? new SingleUseCodeExist() : '', + $singleUse ? new SingleUseCodeExist(true) : '', new CanClaim($singleUse), new NotPaused($beamCode), ...$this->getClaimConditionRules($singleUse), diff --git a/src/Rules/SingleUseCodeExist.php b/src/Rules/SingleUseCodeExist.php index 14a6503..fbd3597 100644 --- a/src/Rules/SingleUseCodeExist.php +++ b/src/Rules/SingleUseCodeExist.php @@ -9,6 +9,10 @@ class SingleUseCodeExist implements ValidationRule { + public function __construct(protected bool $isClaiming = false) + { + } + /** * Determine if the validation rule passes. * @@ -20,7 +24,11 @@ class SingleUseCodeExist implements ValidationRule */ public function validate(string $attribute, mixed $value, Closure $fail): void { - if (BeamService::isSingleUse($value) && BeamClaim::withSingleUseCode($value)->claimable()->exists()) { + if (BeamService::isSingleUse($value) && + BeamClaim::withSingleUseCode($value) + ->when($this->isClaiming, fn ($query) => $query->claimable()) + ->exists() + ) { return; } diff --git a/tests/Feature/GraphQL/Queries/GetBeamTest.php b/tests/Feature/GraphQL/Queries/GetBeamTest.php index 25f3516..3391e86 100644 --- a/tests/Feature/GraphQL/Queries/GetBeamTest.php +++ b/tests/Feature/GraphQL/Queries/GetBeamTest.php @@ -2,7 +2,9 @@ namespace Enjin\Platform\Beam\Tests\Feature\GraphQL\Queries; +use Enjin\Platform\Beam\Enums\BeamType; use Enjin\Platform\Beam\Models\BeamScan; +use Enjin\Platform\Beam\Services\BeamService; use Enjin\Platform\Beam\Tests\Feature\GraphQL\TestCaseGraphQL; use Enjin\Platform\Beam\Tests\Feature\Traits\SeedBeamData; use Enjin\Platform\Providers\Faker\SubstrateProvider; @@ -35,6 +37,31 @@ public function test_it_can_get_beam_without_wallet(): void $this->assertNotEmpty($response); } + public function test_it_can_get_beam_with_single_use_codes(): void + { + $this->seedBeam( + 1, + false, + BeamType::MINT_ON_DEMAND, + ['flags_mask' => BeamService::getFlagsValue([['flag'=> 'SINGLE_USE']])] + ); + $claim = $this->claims->first(); + $singleUseCode = encrypt(implode(':', [$claim->code, $this->beam->code, $claim->nonce])); + $response = $this->graphql($this->method, ['code' => $singleUseCode]); + $this->assertNotEmpty($response); + + $this->seedBeam( + 1, + true, + BeamType::MINT_ON_DEMAND, + ['flags_mask' => BeamService::getFlagsValue([['flag'=> 'SINGLE_USE']])] + ); + $claim = $this->claims->first(); + $singleUseCode = encrypt(implode(':', [$claim->code, $this->beam->code, $claim->nonce])); + $response = $this->graphql($this->method, ['code' => $singleUseCode]); + $this->assertNotEmpty($response); + } + /** * Test get beam with wallet. */ From 62cf13022d39f0c34fd4b476197fd6d1c88a87b2 Mon Sep 17 00:00:00 2001 From: Abner Tudtud Date: Thu, 4 Apr 2024 17:07:37 +0800 Subject: [PATCH 2/3] Remove CanClaim validation --- src/GraphQL/Queries/GetBeamQuery.php | 3 +-- tests/Feature/GraphQL/Queries/GetBeamTest.php | 15 --------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/src/GraphQL/Queries/GetBeamQuery.php b/src/GraphQL/Queries/GetBeamQuery.php index 36e1cd7..ea3bb37 100644 --- a/src/GraphQL/Queries/GetBeamQuery.php +++ b/src/GraphQL/Queries/GetBeamQuery.php @@ -85,8 +85,7 @@ protected function rules(array $args = []): array 'bail', 'filled', 'max:1024', - $singleUse ? new SingleUseCodeExist() : 'exists:beams,code,deleted_at,NULL', - new CanClaim($singleUse), + $singleUse ? new SingleUseCodeExist() : 'exists:beams,code,deleted_at,NULL' ], 'account' => ['sometimes', 'bail', new ValidSubstrateAccount(), new ScanLimit()], ]; diff --git a/tests/Feature/GraphQL/Queries/GetBeamTest.php b/tests/Feature/GraphQL/Queries/GetBeamTest.php index 3391e86..31a045f 100644 --- a/tests/Feature/GraphQL/Queries/GetBeamTest.php +++ b/tests/Feature/GraphQL/Queries/GetBeamTest.php @@ -111,21 +111,6 @@ public function test_it_will_fail_with_scan_limit(): void $this->assertArraySubset(['account' => ['You have reached the maximum limit to retry.']], $response['error']); } - /** - * Test get beam with no more claims. - */ - public function test_it_will_fail_with_no_more_claims(): void - { - $this->claimAllBeams(resolve(SubstrateProvider::class)->public_key()); - - $response = $this->graphql($this->method, [ - 'code' => $this->beam->code, - 'account' => resolve(SubstrateProvider::class)->public_key(), - ], true); - - $this->assertArraySubset(['code' => ['There are no more claims available.']], $response['error']); - } - /** * Test isclaimable flag with no more claims. */ From 7df15c53307ba43c3558692a09b0f5bf9ad97c61 Mon Sep 17 00:00:00 2001 From: Abner Tudtud Date: Thu, 4 Apr 2024 17:09:08 +0800 Subject: [PATCH 3/3] lint --- src/GraphQL/Queries/GetBeamQuery.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/GraphQL/Queries/GetBeamQuery.php b/src/GraphQL/Queries/GetBeamQuery.php index ea3bb37..48b8e92 100644 --- a/src/GraphQL/Queries/GetBeamQuery.php +++ b/src/GraphQL/Queries/GetBeamQuery.php @@ -5,7 +5,6 @@ use Closure; use Enjin\Platform\Beam\GraphQL\Traits\HasBeamCommonFields; use Enjin\Platform\Beam\Models\Beam; -use Enjin\Platform\Beam\Rules\CanClaim; use Enjin\Platform\Beam\Rules\ScanLimit; use Enjin\Platform\Beam\Rules\SingleUseCodeExist; use Enjin\Platform\Beam\Services\BeamService; @@ -85,7 +84,7 @@ protected function rules(array $args = []): array 'bail', 'filled', 'max:1024', - $singleUse ? new SingleUseCodeExist() : 'exists:beams,code,deleted_at,NULL' + $singleUse ? new SingleUseCodeExist() : 'exists:beams,code,deleted_at,NULL', ], 'account' => ['sometimes', 'bail', new ValidSubstrateAccount(), new ScanLimit()], ];