|
| 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 | +} |
0 commit comments