Skip to content

Commit

Permalink
[PLA-1708] Remove validation on GetBeam (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner authored Apr 5, 2024
1 parent af04f45 commit b03d2e0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/GraphQL/Mutations/ClaimBeamMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 0 additions & 2 deletions src/GraphQL/Queries/GetBeamQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()],
];
Expand Down
10 changes: 9 additions & 1 deletion src/Rules/SingleUseCodeExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

class SingleUseCodeExist implements ValidationRule
{
public function __construct(protected bool $isClaiming = false)
{
}

/**
* Determine if the validation rule passes.
*
Expand All @@ -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;
}

Expand Down
42 changes: 27 additions & 15 deletions tests/Feature/GraphQL/Queries/GetBeamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit b03d2e0

Please sign in to comment.