From b03d2e056a878aa9e5641e3cc861ebdec7bf3407 Mon Sep 17 00:00:00 2001 From: Abner Tudtud <114082473+enjinabner@users.noreply.github.com> Date: Fri, 5 Apr 2024 09:26:29 +0800 Subject: [PATCH] [PLA-1708] Remove validation on GetBeam (#69) --- src/GraphQL/Mutations/ClaimBeamMutation.php | 2 +- src/GraphQL/Queries/GetBeamQuery.php | 2 - src/Rules/SingleUseCodeExist.php | 10 ++++- tests/Feature/GraphQL/Queries/GetBeamTest.php | 42 ++++++++++++------- 4 files changed, 37 insertions(+), 19 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/GraphQL/Queries/GetBeamQuery.php b/src/GraphQL/Queries/GetBeamQuery.php index 36e1cd7..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; @@ -86,7 +85,6 @@ protected function rules(array $args = []): array 'filled', 'max:1024', $singleUse ? new SingleUseCodeExist() : 'exists:beams,code,deleted_at,NULL', - new CanClaim($singleUse), ], 'account' => ['sometimes', 'bail', new ValidSubstrateAccount(), new ScanLimit()], ]; 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..31a045f 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. */ @@ -84,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. */