Skip to content

Commit

Permalink
[PLA-2055] Adds requireAccount to CreateFuelTank & validation rule to…
Browse files Browse the repository at this point in the history
… userFuelBudget (#77)
  • Loading branch information
leonardocustodio authored Dec 10, 2024
1 parent c191b5b commit 25e6b18
Show file tree
Hide file tree
Showing 22 changed files with 191 additions and 151 deletions.
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,20 @@
"enjin/platform-core": "*",
"phrity/websocket": "^1.0",
"rebing/graphql-laravel": "^9.0",
"rector/rector": "^1.0",
"spatie/laravel-package-tools": "^1.0",
"spatie/laravel-ray": "^1.0"
},
"require-dev": {
"dms/phpunit-arraysubset-asserts": "dev-master",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^8.0",
"larastan/larastan": "^2.0",
"larastan/larastan": "^3.0",
"orchestra/testbench": "^9.0",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/php-code-coverage": "^10.0",
"phpunit/phpunit": "^10.0",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/php-code-coverage": "^11.0",
"phpunit/phpunit": "^11.0",
"rector/rector": "2.0.0-rc3",
"roave/security-advisories": "dev-latest"
},
"autoload": {
Expand Down
9 changes: 8 additions & 1 deletion src/GraphQL/Mutations/CreateFuelTankMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public function args(): array
'type' => GraphQL::type('[DispatchRuleInputType!]'),
'description' => __('enjin-platform-fuel-tanks::input_type.dispatch_rule.description'),
],
'requireAccount' => [
'type' => GraphQL::type('Boolean'),
'description' => __('enjin-platform-fuel-tanks::mutation.insert_rule_set.args.requireAccount'),
'defaultValue' => false,
],
...$this->getSigningAccountField(),
...$this->getIdempotencyField(),
...$this->getSimulateField(),
Expand Down Expand Up @@ -114,6 +119,7 @@ public function resolve(
name: $args['name'],
userAccountManagement: $blockchainService->getUserAccountManagementParams($args),
dispatchRules: $dispatchRules,
requireAccount: $args['requireAccount'],
coveragePolicy: $args['coveragePolicy'] ?? CoveragePolicy::FEES,
accountRules: $blockchainService->getAccountRulesParams($args)
));
Expand Down Expand Up @@ -146,6 +152,7 @@ public static function getEncodableParams(...$params): array
$ruleSets = collect(Arr::get($params, 'dispatchRules', []));
$coveragePolicy = is_string($coverage = Arr::get($params, 'coveragePolicy')) ? CoveragePolicy::getEnumCase($coverage) : $coverage;
$accountRules = Arr::get($params, 'accountRules', new AccountRulesParams());
$requireAccount = Arr::get($params, 'requireAccount', false);

return [
'descriptor' => [
Expand All @@ -155,7 +162,7 @@ public static function getEncodableParams(...$params): array
'ruleSets' => [
[
'rules' => $ruleSets->flatMap(fn ($ruleSet) => $ruleSet->toEncodable())->all(),
'requireAccount' => false,
'requireAccount' => $requireAccount,
],
],
'accountRules' => $accountRules?->toEncodable() ?? [],
Expand Down
2 changes: 2 additions & 0 deletions src/GraphQL/Traits/HasFuelTankValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ protected function dispatchRulesExist(array $args = [], string $attributePrefix
...$this->commonRulesExist("{$attributePrefix}dispatchRules{$array}.tankFuelBudget"),
"{$attributePrefix}dispatchRules{$array}.whitelistedPallets.*" => ['bail', 'distinct', 'max:255', 'filled', new ValidHex()],
"{$attributePrefix}dispatchRules{$array}.whitelistedPallets" => ['nullable', 'array', 'min:1'],
"{$attributePrefix}dispatchRules{$array}.userFuelBudget" => ['prohibited_unless:requireAccount,true'],
];
}

Expand Down Expand Up @@ -194,6 +195,7 @@ protected function dispatchRules(array $args = [], string $attributePrefix = '',
...$this->commonRules("{$attributePrefix}dispatchRules{$array}.tankFuelBudget"),
"{$attributePrefix}dispatchRules{$array}.whitelistedPallets.*" => ['bail', 'distinct', 'max:255', 'filled', new ValidHex()],
"{$attributePrefix}dispatchRules{$array}.whitelistedPallets" => ['nullable', 'array', 'min:1'],
"{$attributePrefix}dispatchRules{$array}.userFuelBudget" => ["prohibited_unless:{$attributePrefix}requireAccount,true"],
];
}
}
14 changes: 7 additions & 7 deletions tests/Feature/GraphQL/Mutations/AddAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => $publicKey, 'userId' => $publicKey],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The selected tankId is invalid.']],
$response['error']
);
Expand All @@ -74,7 +74,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => Str::random(300), 'userId' => $publicKey],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The tank id field must not be greater than 255 characters.']],
$response['error']
);
Expand All @@ -84,7 +84,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => 'Invalid', 'userId' => $publicKey],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The tank id is not a valid substrate address.']],
$response['error']
);
Expand All @@ -105,7 +105,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => $tank->public_key, 'userId' => $publicKey],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The tank id provided is not owned by you.']],
$response['error']
);
Expand All @@ -118,7 +118,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userId' => 'Invalid'],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['userId' => ['The user id is not a valid substrate address.']],
$response['error']
);
Expand All @@ -138,7 +138,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userId' => Str::random(300)],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['userId' => ['The user id field must not be greater than 255 characters.']],
$response['error']
);
Expand All @@ -153,7 +153,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userId' => $publicKey],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['userId' => ['The user id contains an account that already exists in the fuel tank.']],
$response['error']
);
Expand Down
20 changes: 10 additions & 10 deletions tests/Feature/GraphQL/Mutations/BatchAddAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => $pubicKey, 'userIds' => [$pubicKey]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The selected tankId is invalid.']],
$response['error']
);
Expand All @@ -75,7 +75,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => Str::random(300), 'userIds' => [$pubicKey]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The tank id field must not be greater than 255 characters.']],
$response['error']
);
Expand All @@ -85,7 +85,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => 'Invalid', 'userIds' => [$pubicKey]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The tank id is not a valid substrate address.']],
$response['error']
);
Expand All @@ -106,7 +106,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => $tank->public_key, 'userIds' => [$pubicKey]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The tank id provided is not owned by you.']],
$response['error']
);
Expand All @@ -119,7 +119,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userIds' => 'Invalid'],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['userIds.0' => ['The userIds.0 is not a valid substrate address.']],
$response['error']
);
Expand All @@ -139,7 +139,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userIds' => ''],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['userIds.0' => ['The userIds.0 field must have a value.']],
$response['error']
);
Expand All @@ -149,7 +149,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userIds' => [Str::random(300)]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['userIds.0' => ['The userIds.0 field must not be greater than 255 characters.']],
$response['error']
);
Expand All @@ -160,7 +160,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userIds' => [$publicKey, $publicKey]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
[
'userIds.0' => ['The userIds.0 field has a duplicate value.'],
'userIds.1' => ['The userIds.1 field has a duplicate value.'],
Expand All @@ -178,7 +178,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userIds' => [$publicKey]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['userIds' => ['The user ids contains an account that already exists in the fuel tank.']],
$response['error']
);
Expand All @@ -191,7 +191,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
'userIds' => Collection::range(1, 101)->map(fn ($row) => $provider->public_key())->toArray()],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['userIds' => ['The user ids field must not have more than 100 items.']],
$response['error']
);
Expand Down
19 changes: 10 additions & 9 deletions tests/Feature/GraphQL/Mutations/BatchRemoveAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => $pubicKey, 'userIds' => [$this->account]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The selected tankId is invalid.']],
$response['error']
);
Expand All @@ -85,7 +85,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => Str::random(300), 'userIds' => [$this->account]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The tank id field must not be greater than 255 characters.']],
$response['error']
);
Expand All @@ -95,7 +95,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => 'Invalid', 'userIds' => [$this->account]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The tank id is not a valid substrate address.']],
$response['error']
);
Expand All @@ -116,7 +116,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
['tankId' => $tank->public_key, 'userIds' => [$this->account]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['tankId' => ['The tank id provided is not owned by you.']],
$response['error']
);
Expand All @@ -129,7 +129,8 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userIds' => 'Invalid'],
true
);
$this->assertArraySubset(

$this->assertArrayContainsArray(
['userIds.0' => ['The userIds.0 is not a valid substrate address.']],
$response['error']
);
Expand All @@ -149,7 +150,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userIds' => [Str::random(300)]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['userIds.0' => ['The userIds.0 field must not be greater than 255 characters.']],
$response['error']
);
Expand All @@ -160,7 +161,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userIds' => [$pubicKey, $pubicKey]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
[
'userIds.0' => ['The userIds.0 field has a duplicate value.'],
'userIds.1' => ['The userIds.1 field has a duplicate value.'],
Expand All @@ -175,7 +176,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
['tankId' => $this->tank->public_key, 'userIds' => [$this->account]],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['userIds' => ["The user ids contains an account that doesn't exist in the fuel tank."]],
$response['error']
);
Expand All @@ -188,7 +189,7 @@ public function test_it_will_fail_with_invalid_parameter_user_id(): void
'userIds' => Collection::range(1, 101)->map(fn ($row) => $provider->public_key())->toArray()],
true
);
$this->assertArraySubset(
$this->assertArrayContainsArray(
['userIds' => ['The user ids field must not have more than 100 items.']],
$response['error']
);
Expand Down
Loading

0 comments on commit 25e6b18

Please sign in to comment.