[API][Behat] Test currency code validation

This commit is contained in:
Jan Goralski 2024-03-20 17:43:38 +01:00
parent ae04f103e5
commit c0a77acc2a
No known key found for this signature in database
GPG key ID: 95D91BA380F31EDD
4 changed files with 95 additions and 1 deletions

View file

@ -0,0 +1,29 @@
@managing_currencies
Feature: Currency validation
In order to avoid making mistakes when managing currencies
As an Administrator
I want to be prevented from adding an invalid currency
Background:
Given I am logged in as an administrator
@api @no-ui
Scenario: Trying to add currency without code
When I want to add a new currency
And I do not choose a code
And I try to add it
Then I should be notified that a code is required
@api @no-ui
Scenario: Trying to add a currency with an invalid code
When I want to add a new currency
And I set code to "invalid"
And I try to add it
Then I should be notified that the code is invalid
@api @no-ui
Scenario: Trying to add a currency with an non-existent code
When I want to add a new currency
And I set code to "B0B"
And I try to add it
Then I should be notified that the code is invalid

View file

@ -45,8 +45,10 @@ final class ManagingCurrenciesContext implements Context
/**
* @When I choose :currencyCode
* @When I set code to :code
* @When I do not choose a code
*/
public function iChoose(string $currencyCode): void
public function iChoose(string $currencyCode = ''): void
{
$this->client->addRequestData('code', $currencyCode);
}
@ -107,6 +109,32 @@ final class ManagingCurrenciesContext implements Context
Assert::same($this->responseChecker->getError($response), 'code: Currency code must be unique.');
}
/**
* @Then I should be notified that a code is required
*/
public function iShouldBeNotifiedThatACodeIsRequired(): void
{
$response = $this->client->getLastResponse();
Assert::false(
$this->responseChecker->isCreationSuccessful($response),
'Currency has been created successfully, but it should not',
);
Assert::same($this->responseChecker->getError($response), 'code: Please choose currency code.');
}
/**
* @Then I should be notified that the code is invalid
*/
public function iShouldBeNotifiedThatTheCodeIsInvalid(): void
{
$response = $this->client->getLastResponse();
Assert::false(
$this->responseChecker->isCreationSuccessful($response),
'Currency has been created successfully, but it should not',
);
Assert::same($this->responseChecker->getError($response), 'code: This value is not a valid currency code.');
}
/**
* @Then I should be notified that it has been successfully created
*/

View file

@ -59,6 +59,30 @@ final class CurrenciesTest extends JsonApiTestCase
);
}
/** @test */
public function it_does_not_allow_creating_a_currency_with_invalid_code(): void
{
$this->loadFixturesFromFiles(['channel.yaml', 'authentication/api_administrator.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);
$this->client->request(
'POST',
'/api/v2/admin/currencies',
[],
[],
$header,
json_encode([
'code' => 'lol',
], \JSON_THROW_ON_ERROR),
);
$this->assertResponse(
$this->client->getResponse(),
'admin/currency/post_currency_with_invalid_code_response',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
}
/** @test */
public function it_creates_a_currency(): void
{

View file

@ -0,0 +1,13 @@
{
"@context": "\/api\/v2\/contexts\/ConstraintViolationList",
"@type": "ConstraintViolationList",
"hydra:title": "An error occurred",
"hydra:description": "code: This value is not a valid currency code.",
"violations": [
{
"propertyPath": "code",
"message": "This value is not a valid currency code.",
"code": @string@
}
]
}