Skip to content

Commit

Permalink
[PLA-1302] Add PassesClaimCondition rule and update ClaimBeam mutatio…
Browse files Browse the repository at this point in the history
…n. (#40)
  • Loading branch information
v16Studios authored Oct 5, 2023
1 parent 3ea13df commit e658b19
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
1 change: 1 addition & 0 deletions lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'end_date_after_start' => 'The :attribute must be a date after start.',
'end_date_greater_than' => 'The :attribute must be a date greater than :value.',
'is_paused' => 'The beam is paused.',
'passes_condition' => 'A condition to claim has not been met.',
'passes_conditions' => 'Not all the conditions to claim have been met.',
'scan_limit' => 'You have reached the maximum limit to retry.',
'start_date_after_end' => 'The :attribute must be a date before end.',
Expand Down
6 changes: 3 additions & 3 deletions src/GraphQL/Mutations/ClaimBeamMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Enjin\Platform\Beam\GraphQL\Mutations;

use Closure;
use Enjin\Platform\Beam\GraphQL\Traits\HasBeamClaimConditions;
use Enjin\Platform\Beam\GraphQL\Traits\HasBeamCommonFields;
use Enjin\Platform\Beam\Rules\CanClaim;
use Enjin\Platform\Beam\Rules\NotExpired;
use Enjin\Platform\Beam\Rules\NotOwner;
use Enjin\Platform\Beam\Rules\NotPaused;
use Enjin\Platform\Beam\Rules\PassesClaimConditions;
use Enjin\Platform\Beam\Rules\SingleUseCodeExist;
use Enjin\Platform\Beam\Rules\VerifySignedMessage;
use Enjin\Platform\Beam\Services\BeamService;
Expand All @@ -23,6 +23,7 @@
class ClaimBeamMutation extends Mutation implements PlatformPublicGraphQlOperation
{
use HasBeamCommonFields;
use HasBeamClaimConditions;

/**
* Get the mutation's attributes.
Expand Down Expand Up @@ -98,14 +99,13 @@ protected function rules(array $args = []): array

return [
'code' => [
'bail',
'filled',
'max:1024',
new NotExpired($beamCode),
$singleUse ? new SingleUseCodeExist() : '',
new CanClaim($singleUse),
new NotPaused($beamCode),
new PassesClaimConditions($singleUse),
...$this->getClaimConditionRules($singleUse),
],
'account' => ['filled', new ValidSubstrateAccount(), new NotOwner($singleUse)],
'signature' => ['sometimes', new VerifySignedMessage()],
Expand Down
18 changes: 18 additions & 0 deletions src/GraphQL/Traits/HasBeamClaimConditions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Enjin\Platform\Beam\GraphQL\Traits;

use Enjin\Platform\Beam\Rules\PassesClaimCondition;
use Enjin\Platform\Beam\Rules\PassesClaimConditions;

trait HasBeamClaimConditions
{
public function getClaimConditionRules(bool $singleUse): array
{
$conditions = collect(PassesClaimConditions::getConditionalFunctions());

return $conditions
->map(fn ($condition) => new PassesClaimCondition($condition, $singleUse))
->toArray();
}
}
40 changes: 40 additions & 0 deletions src/Rules/PassesClaimCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Enjin\Platform\Beam\Rules;

use Closure;
use Enjin\Platform\Rules\Traits\HasDataAwareRule;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ValidationRule;

class PassesClaimCondition implements DataAwareRule, ValidationRule
{
use HasDataAwareRule;

/**
* Create new rule instance.
*/
public function __construct(
protected Closure $function,
protected bool $singleUse
) {
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @return void
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$result = ($this->function)($attribute, $value, $this->singleUse, $this->data);

if (true !== $result) {
$fail(is_string($result) ? $result : __('enjin-platform-beam::validation.passes_condition'));
}
}
}
13 changes: 8 additions & 5 deletions tests/Feature/GraphQL/Mutations/ClaimBeamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function ($attribute, $code, $singleUse, $data) {
public function test_it_cannot_claim_beam_with_single_condition_that_fails(): void
{
PassesClaimConditions::addConditionalFunctions(function ($attribute, $code, $singleUse, $data) {
return CryptoSignatureType::SR25519->name == $data['cryptoSignatureType'];
return CryptoSignatureType::SR25519->name == $data['cryptoSignatureType'] ? true : 'Signature is not SR25519.';
});
$this->assertNotEmpty(PassesClaimConditions::getConditionalFunctions());

Expand All @@ -201,7 +201,7 @@ public function test_it_cannot_claim_beam_with_single_condition_that_fails(): vo

$this->assertNotEmpty($response);

$this->assertArraySubset(['code' => ['Not all the conditions to claim have been met.']], $response['error']);
$this->assertArraySubset(['code' => ['Signature is not SR25519.']], $response['error']);

PassesClaimConditions::clearConditionalFunctions();
$this->assertEmpty(PassesClaimConditions::getConditionalFunctions());
Expand All @@ -214,10 +214,10 @@ public function test_it_cannot_claim_beam_with_multiple_conditions_that_fail():
{
$functions = collect([
function ($attribute, $code, $singleUse, $data) {
return 'code' == $attribute;
return 'code' == $data[$attribute];
},
function ($attribute, $code, $singleUse, $data) {
return CryptoSignatureType::SR25519->name == $data['cryptoSignatureType'];
return CryptoSignatureType::SR25519->name == $data['cryptoSignatureType'] ? true : 'Signature is not SR25519.';
},
]);

Expand Down Expand Up @@ -246,7 +246,10 @@ function ($attribute, $code, $singleUse, $data) {

$this->assertNotEmpty($response);

$this->assertArraySubset(['code' => ['Not all the conditions to claim have been met.']], $response['error']);
$this->assertArraySubset(['code' => [
'A condition to claim has not been met.',
'Signature is not SR25519.',
]], $response['error']);

PassesClaimConditions::clearConditionalFunctions();
$this->assertEmpty(PassesClaimConditions::getConditionalFunctions());
Expand Down

0 comments on commit e658b19

Please sign in to comment.