diff --git a/src/app/User/Domain/Service/PasswordResetTokenValidatorInterface.php b/src/app/User/Domain/Service/PasswordResetTokenValidatorInterface.php new file mode 100644 index 0000000..d44c6af --- /dev/null +++ b/src/app/User/Domain/Service/PasswordResetTokenValidatorInterface.php @@ -0,0 +1,11 @@ +refresh(); + $this->service = new PasswordResetTokenValidatorService(); + $this->user = $this->createUser(); + $this->resetRequest = $this->createResetRequest(); + } + + protected function tearDown(): void + { + $this->refresh(); + parent::tearDown(); + } + + private function refresh(): void + { + if (env('APP_ENV') === 'testing') { + DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=0;'); + User::truncate(); + PasswordResetRequest::truncate(); + DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=1;'); + } + } + + private function createUser(): User + { + return User::Create([ + 'first_name' => 'Sergio', + 'last_name' => 'Ramos', + 'email' => "real-madrid".rand(). "@test.com", + 'password' => 'el-capitán-1234', + 'bio' => 'Real Madrid player', + 'location' => 'Madrid', + 'skills' => ['Football', 'Leadership'], + 'profile_image' => 'https://example.com/sergio.jpg' + ]); + } + + private function createResetRequest(): PasswordResetRequest + { + return PasswordResetRequest::create([ + 'user_id' => $this->user->id, + 'token' => bin2hex(random_bytes(32)), + 'requested_at' => now(), + 'expired_at' => now()->addMinutes(30), + ]); + } + + public function test_check_token_validate(): void + { + $this->expectNotToPerformAssertions(); + + $this->service->validate( + $this->user->id, + $this->resetRequest->token + ); + } +} \ No newline at end of file diff --git a/src/app/User/Infrastructure/Service/PasswordResetTokenValidatorService.php b/src/app/User/Infrastructure/Service/PasswordResetTokenValidatorService.php new file mode 100644 index 0000000..b675e40 --- /dev/null +++ b/src/app/User/Infrastructure/Service/PasswordResetTokenValidatorService.php @@ -0,0 +1,30 @@ +isValidToken($userId, $token)) { + throw new InvalidArgumentException('Invalid password reset token.'); + } + } + + private function isValidToken($userId, $token): bool + { + return DB::table('password_reset_requests') + ->where('token', $token) + ->where('user_id', $userId) + ->where('requested_at', '>=', now()->subMinutes(60)) + ->exists(); + } +} \ No newline at end of file