-
-
Notifications
You must be signed in to change notification settings - Fork 90
chore: scenary validate test #5198
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
Open
Any97Cris
wants to merge
24
commits into
main
Choose a base branch
from
chore/correct-validate-field
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
b4dffc7
chore: scenery validate test
Any97Cris e2b86cf
chore: more steps about tests
Any97Cris b61ee0c
fix: name of scenario
vitormattos f793383
fix: adjust table row size
vitormattos 7755fbb
chore: replace Name by Common Name
vitormattos 0b963db
chore: restrict the return type to nullable and string only
vitormattos 75aabdf
chore: cover with more tests
vitormattos 4ccda64
chore: test get helper text with invalid field name
vitormattos 8f5d520
chore: test get rule to valid field
vitormattos d248628
chore: test get rule to invalid field
vitormattos ada6d5c
chore: add type hint to return
vitormattos d000640
fix: consider to return empty array when is invalid field name
vitormattos ce19a93
fix: run cs:fix
vitormattos d37fad1
fix: run cs:fix
vitormattos a8a6074
chore: simplify test and compare actual and expected directly
vitormattos 2b87869
chore: create ValidateServiceTest file
Any97Cris 2c44b61
chore:more test scenarios
Any97Cris e810867
chore: changes noted in the code review
Any97Cris c7ed69d
chore: create methods with fields as certification.js
Any97Cris 92e0338
chore: add initial state
Any97Cris a68e74e
chore: remove some configurations come at certification.js file
Any97Cris 57d678f
chore: delete certification.js file and adjustment some files
Any97Cris 7e38733
chore: add null option
Any97Cris cb4a7ad
chore: correction of some errors in the AdminTest file
Any97Cris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Libresign\Service\Certificate; | ||
|
||
use OCP\IL10N; | ||
|
||
class RulesService { | ||
|
||
private array $rules = [ | ||
'CN' => [ | ||
'required' => true, | ||
'min' => 1, | ||
'max' => 64, | ||
], | ||
'C' => [ | ||
'min' => 2, | ||
'max' => 2, | ||
], | ||
'ST' => [ | ||
'min' => 1, | ||
'max' => 128, | ||
], | ||
'L' => [ | ||
'min' => 1, | ||
'max' => 128, | ||
], | ||
'O' => [ | ||
'min' => 1, | ||
'max' => 64, | ||
], | ||
'OU' => [ | ||
'min' => 1, | ||
'max' => 64, | ||
], | ||
]; | ||
|
||
public function __construct( | ||
protected IL10N $l10n, | ||
) { | ||
} | ||
|
||
public function getRule(string $fieldName): array { | ||
if (!array_key_exists($fieldName, $this->rules)) { | ||
return []; | ||
} | ||
if (!isset($this->rules[$fieldName]['helperText'])) { | ||
$this->rules[$fieldName]['helperText'] = $this->getHelperText($fieldName); | ||
if (empty($this->rules[$fieldName]['helperText'])) { | ||
unset($this->rules[$fieldName]['helperText']); | ||
} | ||
} | ||
return $this->rules[$fieldName]; | ||
} | ||
|
||
public function getHelperText(string $fieldName): ?string { | ||
return match ($fieldName) { | ||
'CN' => $this->l10n->t('Common Name (CN)'), | ||
'C' => $this->l10n->t('Two-letter ISO 3166 country code'), | ||
'ST' => $this->l10n->t('Full name of states or provinces'), | ||
'L' => $this->l10n->t('Name of a locality or place, such as a city, county, or other geographic region'), | ||
'O' => $this->l10n->t('Name of an organization'), | ||
'OU' => $this->l10n->t('Name of an organizational unit'), | ||
default => null, | ||
}; | ||
} | ||
|
||
public function toArray(): array { | ||
$result = []; | ||
foreach ($this->rules as $field => $rule) { | ||
$result[] = [ | ||
'id' => $field, | ||
'label' => $this->getLabel($field), | ||
'min' => $rule['min'] ?? null, | ||
'max' => $rule['max'] ?? null, | ||
'required' => $rule['required'] ?? false, | ||
'helperText' => $this->getHelperText($field), | ||
]; | ||
} | ||
return $result; | ||
} | ||
|
||
private function getLabel(string $fieldName): string { | ||
return match ($fieldName) { | ||
'CN' => $this->l10n->t('Common Name (CN)'), | ||
'C' => $this->l10n->t('Country'), | ||
'ST' => $this->l10n->t('State'), | ||
'L' => $this->l10n->t('Locality'), | ||
'O' => $this->l10n->t('Organization'), | ||
'OU' => $this->l10n->t('Organizational Unit'), | ||
default => $fieldName, | ||
}; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Libresign\Service\Certificate; | ||
|
||
use InvalidArgumentException; | ||
use OCP\IL10N; | ||
|
||
class ValidateService { | ||
|
||
public function __construct( | ||
protected RulesService $rulesService, | ||
protected IL10N $l10n, | ||
) { | ||
} | ||
|
||
public function validate(string $fieldName, string $value): void { | ||
$rule = $this->rulesService->getRule($fieldName); | ||
vitormattos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$value = trim($value); | ||
$length = strlen($value); | ||
|
||
if ($fieldName === 'id' && $length === 0) { | ||
throw new InvalidArgumentException('Parameter id is invalid'); | ||
} | ||
|
||
if (!$length && isset($rule['required']) && $rule['required']) { | ||
throw new InvalidArgumentException( | ||
$this->l10n->t("Parameter '%s' is required!", [$fieldName]) | ||
); | ||
} | ||
|
||
if ($length > $rule['max'] || $length < $rule['min']) { | ||
throw new InvalidArgumentException( | ||
$this->l10n->t( | ||
"Parameter '%s' should be between %s and %s.", | ||
[$fieldName, $rule['min'], $rule['max']] | ||
) | ||
); | ||
} | ||
} | ||
|
||
|
||
public function validateNames(array $names) { | ||
foreach ($names as $item) { | ||
if (empty($item['id'])) { | ||
throw new InvalidArgumentException('Parameter id is required!'); | ||
} | ||
$this->validate($item['id'], $item['value']); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.