diff --git a/tests/SPT/Domain/Auth/Services/LoginUserTest.php b/tests/SPT/Domain/Auth/Services/LoginUserTest.php new file mode 100644 index 00000000..45b8584e --- /dev/null +++ b/tests/SPT/Domain/Auth/Services/LoginUserTest.php @@ -0,0 +1,160 @@ +. + */ + +namespace SPT\Domain\Auth\Services; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\MockObject; +use SP\Core\Context\ContextException; +use SP\Domain\Auth\Services\AuthException; +use SP\Domain\Auth\Services\LoginStatus; +use SP\Domain\Auth\Services\LoginUser; +use SP\Domain\Common\Services\ServiceException; +use SP\Domain\Core\Exceptions\InvalidArgumentException; +use SP\Domain\Core\Exceptions\QueryException; +use SP\Domain\Http\RequestInterface; +use SP\Domain\Security\Dtos\TrackRequest; +use SP\Domain\Security\Ports\TrackService; +use SP\Domain\User\Dtos\UserDataDto; +use SP\Domain\User\Ports\UserPassRecoverService; +use SPT\Generators\UserDataGenerator; +use SPT\UnitaryTestCase; + +/** + * Class LoginUserTest + */ +#[Group('unitary')] +class LoginUserTest extends UnitaryTestCase +{ + private TrackService|MockObject $trackService; + private RequestInterface|MockObject $request; + + private LoginUser $loginUser; + private MockObject|UserPassRecoverService $userPassRecoverService; + + /** + * @throws AuthException + * @throws ServiceException + */ + public function testCheckUser() + { + $user = UserDataGenerator::factory()->buildUserData(); + $userDataDto = new UserDataDto($user->mutate(['isDisabled' => false, 'isChangePass' => false])); + + $out = $this->loginUser->checkUser($userDataDto); + + $this->assertEquals(LoginStatus::PASS, $out->getStatus()); + } + + /** + * @throws AuthException + * @throws ServiceException + */ + public function testCheckUserWithDisabled() + { + $user = UserDataGenerator::factory()->buildUserData(); + $userDataDto = new UserDataDto($user->mutate(['isDisabled' => true, 'isChangePass' => false])); + + $this->expectException(AuthException::class); + $this->expectExceptionMessage('User disabled'); + + $this->loginUser->checkUser($userDataDto); + } + + /** + * @throws AuthException + * @throws ServiceException + */ + public function testCheckUserWithChangePass() + { + $user = UserDataGenerator::factory()->buildUserData(); + $userDataDto = new UserDataDto($user->mutate(['isDisabled' => false, 'isChangePass' => true])); + + $this->userPassRecoverService + ->expects($this->once()) + ->method('add') + ->with($user->getId(), self::callback(static fn(string $s) => strlen($s) === 32)); + + $out = $this->loginUser->checkUser($userDataDto); + + $this->assertEquals(LoginStatus::PASS_RESET_REQUIRED, $out->getStatus()); + $this->assertMatchesRegularExpression('/index\.php\?r=userPassReset%2Freset%2F\w+/', $out->getRedirect()); + } + + /** + * @throws AuthException + * @throws ServiceException + */ + public function testCheckUserWithChangePassWithException() + { + $user = UserDataGenerator::factory()->buildUserData(); + $userDataDto = new UserDataDto($user->mutate(['isDisabled' => false, 'isChangePass' => true])); + + $this->userPassRecoverService + ->expects($this->once()) + ->method('add') + ->with($user->getId(), self::callback(static fn(string $s) => strlen($s) === 32)) + ->willThrowException(QueryException::error('test')); + + $this->expectException(ServiceException::class); + $this->expectExceptionMessage('Internal error'); + + $this->loginUser->checkUser($userDataDto); + } + + /** + * @throws Exception + * @throws ContextException + * @throws InvalidArgumentException + */ + protected function setUp(): void + { + parent::setUp(); + + $this->trackService = $this->createMock(TrackService::class); + $this->trackService + ->expects($this->atLeast(1)) + ->method('buildTrackRequest') + ->with(LoginUser::class) + ->willReturn( + new TrackRequest( + self::$faker->unixTime(), + self::$faker->colorName(), + self::$faker->ipv4(), + self::$faker->randomNumber(2) + ) + ); + + $this->request = $this->createMock(RequestInterface::class); + $this->userPassRecoverService = $this->createMock(UserPassRecoverService::class); + + $this->loginUser = new LoginUser( + $this->application, + $this->trackService, + $this->request, + $this->userPassRecoverService + ); + } +}