Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLA-1302] Add PassesClaimCondition rule and update ClaimBeam mutation. #40

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading