Skip to content

Commit 2a5f9de

Browse files
committed
Tests
1 parent 882ffca commit 2a5f9de

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Tests\Integration\Identity\Controller;
6+
7+
use PhpList\Core\Domain\Identity\Service\PasswordManager;
8+
use PhpList\RestBundle\Identity\Controller\PasswordResetController;
9+
use PhpList\RestBundle\Tests\Integration\Common\AbstractTestController;
10+
use PhpList\RestBundle\Tests\Integration\Identity\Fixtures\AdministratorFixture;
11+
12+
class PasswordResetControllerTest extends AbstractTestController
13+
{
14+
public function testControllerIsAvailableViaContainer(): void
15+
{
16+
self::assertInstanceOf(
17+
PasswordResetController::class,
18+
self::getContainer()->get(PasswordResetController::class)
19+
);
20+
}
21+
22+
public function testRequestPasswordResetWithNoJsonReturnsError400(): void
23+
{
24+
$this->jsonRequest('post', '/api/v2/password-reset/request');
25+
26+
$this->assertHttpBadRequest();
27+
$data = $this->getDecodedJsonResponseContent();
28+
$this->assertStringContainsString('Invalid JSON:', $data['message']);
29+
}
30+
31+
public function testRequestPasswordResetWithInvalidEmailReturnsError422(): void
32+
{
33+
$jsonData = json_encode(['email' => 'not-an-email']);
34+
$this->jsonRequest('post', '/api/v2/password-reset/request', [], [], [], $jsonData);
35+
36+
$this->assertHttpUnprocessableEntity();
37+
$data = $this->getDecodedJsonResponseContent();
38+
$this->assertStringContainsString('This value is not a valid email address', $data['message']);
39+
}
40+
41+
public function testRequestPasswordResetWithNonExistentEmailReturnsError404(): void
42+
{
43+
$this->loadFixtures([AdministratorFixture::class]);
44+
$jsonData = json_encode(['email' => '[email protected]']);
45+
$this->jsonRequest('post', '/api/v2/password-reset/request', [], [], [], $jsonData);
46+
47+
$this->assertHttpNotFound();
48+
}
49+
50+
public function testRequestPasswordResetWithValidEmailReturnsSuccess(): void
51+
{
52+
$this->loadFixtures([AdministratorFixture::class]);
53+
$jsonData = json_encode(['email' => '[email protected]']);
54+
$this->jsonRequest('post', '/api/v2/password-reset/request', [], [], [], $jsonData);
55+
56+
$this->assertHttpNoContent();
57+
}
58+
59+
public function testValidateTokenWithNoJsonReturnsError400(): void
60+
{
61+
$this->jsonRequest('post', '/api/v2/password-reset/validate');
62+
63+
$this->assertHttpBadRequest();
64+
$data = $this->getDecodedJsonResponseContent();
65+
$this->assertStringContainsString('Invalid JSON:', $data['message']);
66+
}
67+
68+
public function testValidateTokenWithInvalidTokenReturnsInvalidResult(): void
69+
{
70+
$this->loadFixtures([AdministratorFixture::class]);
71+
$jsonData = json_encode(['token' => 'invalid-token']);
72+
$this->jsonRequest('post', '/api/v2/password-reset/validate', [], [], [], $jsonData);
73+
74+
$this->assertHttpOkay();
75+
$data = $this->getDecodedJsonResponseContent();
76+
$this->assertFalse($data['valid']);
77+
}
78+
79+
public function testResetPasswordWithNoJsonReturnsError400(): void
80+
{
81+
$this->jsonRequest('post', '/api/v2/password-reset/reset');
82+
83+
$this->assertHttpBadRequest();
84+
$data = $this->getDecodedJsonResponseContent();
85+
$this->assertStringContainsString('Invalid JSON:', $data['message']);
86+
}
87+
88+
public function testResetPasswordWithInvalidTokenReturnsBadRequest(): void
89+
{
90+
$this->loadFixtures([AdministratorFixture::class]);
91+
$jsonData = json_encode(['token' => 'invalid-token', 'newPassword' => 'newPassword123']);
92+
$this->jsonRequest('post', '/api/v2/password-reset/reset', [], [], [], $jsonData);
93+
94+
$this->assertHttpBadRequest();
95+
$data = $this->getDecodedJsonResponseContent();
96+
$this->assertEquals('Invalid or expired token', $data['message']);
97+
}
98+
99+
public function testResetPasswordWithShortPasswordReturnsError422(): void
100+
{
101+
$this->loadFixtures([AdministratorFixture::class]);
102+
$jsonData = json_encode(['token' => 'valid-token', 'newPassword' => 'short']);
103+
$this->jsonRequest('post', '/api/v2/password-reset/reset', [], [], [], $jsonData);
104+
105+
$this->assertHttpUnprocessableEntity();
106+
$data = $this->getDecodedJsonResponseContent();
107+
$this->assertStringContainsString('This value is too short', $data['message']);
108+
}
109+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Tests\Unit\Identity\Request;
6+
7+
use PhpList\RestBundle\Identity\Request\RequestPasswordResetRequest;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class RequestPasswordResetRequestTest extends TestCase
11+
{
12+
public function testGetDtoReturnsSelf(): void
13+
{
14+
$request = new RequestPasswordResetRequest();
15+
$request->email = '[email protected]';
16+
17+
$dto = $request->getDto();
18+
19+
$this->assertSame($request, $dto);
20+
$this->assertEquals('[email protected]', $dto->email);
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Tests\Unit\Identity\Request;
6+
7+
use PhpList\RestBundle\Identity\Request\ResetPasswordRequest;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ResetPasswordRequestTest extends TestCase
11+
{
12+
public function testGetDtoReturnsSelf(): void
13+
{
14+
$request = new ResetPasswordRequest();
15+
$request->token = 'test-token-123';
16+
$request->newPassword = 'newSecurePassword123';
17+
18+
$dto = $request->getDto();
19+
20+
$this->assertSame($request, $dto);
21+
$this->assertEquals('test-token-123', $dto->token);
22+
$this->assertEquals('newSecurePassword123', $dto->newPassword);
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Tests\Unit\Identity\Request;
6+
7+
use PhpList\RestBundle\Identity\Request\ValidateTokenRequest;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ValidateTokenRequestTest extends TestCase
11+
{
12+
public function testGetDtoReturnsSelf(): void
13+
{
14+
$request = new ValidateTokenRequest();
15+
$request->token = 'test-token-123';
16+
17+
$dto = $request->getDto();
18+
19+
$this->assertSame($request, $dto);
20+
$this->assertEquals('test-token-123', $dto->token);
21+
}
22+
}

0 commit comments

Comments
 (0)