Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\User\Domain\Service;

interface PasswordResetTokenValidatorInterface
{
public function validate(
string $userId,
string $token,
): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\User\Infrastructure\InfrastructureTest\Service;

use App\Models\PasswordResetRequest;
use App\Models\User;
use App\User\Infrastructure\Service\PasswordResetTokenValidatorService;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class PasswordResetTokenValidatorServiceTest extends TestCase
{
private $service;
private $user;
private $resetRequest;

protected function setUp(): void
{
parent::setUp();
$this->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
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\User\Infrastructure\Service;

use App\User\Domain\Service\PasswordResetTokenValidatorInterface;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;

class PasswordResetTokenValidatorService implements PasswordResetTokenValidatorInterface
{
public function validate(string $userId, string $token): void
{
if (empty($userId) || empty($token)) {
throw new InvalidArgumentException('User ID and token must not be empty.');
}

if (!$this->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();
}
}