Skip to content

Commit fd83384

Browse files
test(user): добавил проверку ассертов на короткий пароль
1 parent a389517 commit fd83384

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

backend/src-dev/Tests/Functional/User/Site/ChangePasswordTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,13 @@ public static function notValidPasswordDataProvider(): Iterator
224224
'newPasswordConfirmation' => 'currentPassword',
225225
],
226226
];
227+
228+
yield 'Новый пароль короткий' => [
229+
[
230+
'currentPassword' => 'currentPassword',
231+
'newPassword' => '1',
232+
'newPasswordConfirmation' => '1',
233+
],
234+
];
227235
}
228236
}

backend/src-dev/Tests/Functional/User/Site/RecoverPasswordTest.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function testUserNotFound(): void
104104
self::assertNotFound($response);
105105
}
106106

107-
#[DataProvider('notValidPasswordDataProvider')]
107+
#[DataProvider('notValidEmailDataProvider')]
108108
#[TestDox('Неверный запрос')]
109109
public function testBadRequest(?string $email): void
110110
{
@@ -118,12 +118,60 @@ public function testBadRequest(?string $email): void
118118
self::assertBadRequest($response);
119119
}
120120

121-
public static function notValidPasswordDataProvider(): Iterator
121+
public static function notValidEmailDataProvider(): Iterator
122122
{
123123
yield 'null' => [null];
124124

125125
yield 'Пустая строка' => [''];
126126

127127
yield 'Неверный Email' => ['testEmail'];
128128
}
129+
130+
#[DataProvider('notValidPasswordDataProvider')]
131+
#[TestDox('Неверный запрос')]
132+
public function testConfirmBadRequest(?string $password): void
133+
{
134+
$body = [
135+
'email' => $userEmail = 'first@example.com',
136+
'password' => '123QWE',
137+
];
138+
139+
$response = self::request(
140+
method: Request::METHOD_POST,
141+
uri: '/api/sign-up',
142+
body: json_encode($body, JSON_THROW_ON_ERROR),
143+
);
144+
self::assertSuccessResponse($response);
145+
146+
$response = self::request(
147+
method: Request::METHOD_POST,
148+
uri: '/api/request-password-recovery',
149+
body: json_encode(['email' => $userEmail], JSON_THROW_ON_ERROR),
150+
);
151+
self::assertSuccessResponse($response);
152+
153+
/** @var Message $sentEmail */
154+
$sentEmail = self::getMailerMessage();
155+
156+
/** @var string $recoverToken */
157+
$recoverToken = $sentEmail->getHeaders()->get('recoverToken')?->getBody();
158+
159+
$response = self::request(
160+
method: Request::METHOD_POST,
161+
uri: \sprintf('/api/recover-password/%s', $recoverToken),
162+
body: json_encode(['password' => $password], JSON_THROW_ON_ERROR),
163+
validateRequestSchema: false,
164+
);
165+
166+
self::assertBadRequest($response);
167+
}
168+
169+
public static function notValidPasswordDataProvider(): Iterator
170+
{
171+
yield 'null' => [null];
172+
173+
yield 'Пустой пароль' => [''];
174+
175+
yield 'Короткий пароль' => ['1'];
176+
}
129177
}

backend/src-dev/Tests/Functional/User/Site/SignUpTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function testCreationUserWithSameEmail(): void
6868
#[TestWith(['', 'password'], 'пустой емейл')]
6969
#[TestWith(['test', 'password'], 'невалидный емейл')]
7070
#[TestWith(['test@example.test', ''], 'пустой пароль')]
71+
#[TestWith(['test@example.test', '1'], 'короткий пароль')]
7172
#[TestDox('Неправильный запрос')]
7273
public function testBadRequest(string $email, string $password): void
7374
{

backend/src/Infrastructure/Response/SerializeExceptionResponse.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Infrastructure\Response;
66

7+
use App\Infrastructure\ApiException\ApiBadRequestException;
78
use App\Infrastructure\ApiException\ApiErrorResponse;
89
use App\Infrastructure\ApiException\ApiException;
910
use App\Infrastructure\ApiException\ApiHeaders;
@@ -16,6 +17,7 @@
1617
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1819
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
20+
use Webmozart\Assert\InvalidArgumentException;
1921

2022
/**
2123
* Формирование ответа при ошибках
@@ -36,6 +38,16 @@ public function __invoke(ExceptionEvent $event): void
3638
{
3739
$exception = $event->getThrowable();
3840

41+
if ($exception instanceof InvalidArgumentException) {
42+
/** @var non-empty-string $message */
43+
$message = $exception->getMessage();
44+
45+
$exception = new ApiBadRequestException(
46+
errors: [$message],
47+
previous: $exception,
48+
);
49+
}
50+
3951
if ($exception instanceof HttpExceptionInterface) {
4052
$exception = new ApiSystemException(
4153
errors: [\sprintf('Http ошибка: %s', $exception->getMessage())],

0 commit comments

Comments
 (0)