feat: add OtpCodeFactory and comprehensive tests for AuthService and RegistrationService
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Services\Auth;
|
||||
|
||||
use App\Enums\UserEnum;
|
||||
use App\Models\User;
|
||||
use App\Services\Auth\RegistrationService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RegistrationServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected RegistrationService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->service = app(RegistrationService::class);
|
||||
}
|
||||
|
||||
protected function payload(array $overrides = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'phone' => '09120000000',
|
||||
'national_code' => '0012345678',
|
||||
'level' => UserEnum::Vet->value,
|
||||
], $overrides);
|
||||
}
|
||||
|
||||
public function test_create_user_stores_all_profile_fields(): void
|
||||
{
|
||||
$user = $this->service->createUser($this->payload([
|
||||
'email' => 'vet@example.com',
|
||||
'fullname' => 'دکتر نمونه',
|
||||
'clinic_name' => 'کلینیک نمونه',
|
||||
'clinic_phone' => '02100000000',
|
||||
'province' => 'تهران',
|
||||
'city' => 'تهران',
|
||||
'address' => 'خیابان نمونه',
|
||||
'specialty' => 'داخلی',
|
||||
]));
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'id' => $user->id,
|
||||
'phone' => '09120000000',
|
||||
'national_code' => '0012345678',
|
||||
'level' => UserEnum::Vet->value,
|
||||
'email' => 'vet@example.com',
|
||||
'fullname' => 'دکتر نمونه',
|
||||
'clinic_name' => 'کلینیک نمونه',
|
||||
'clinic_phone' => '02100000000',
|
||||
'province' => 'تهران',
|
||||
'city' => 'تهران',
|
||||
'address' => 'خیابان نمونه',
|
||||
'specialty' => 'داخلی',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_create_user_generates_prefixed_random_username(): void
|
||||
{
|
||||
$user = $this->service->createUser($this->payload());
|
||||
|
||||
$this->assertMatchesRegularExpression('/^rdl_[A-Za-z0-9]{10}$/', $user->username);
|
||||
}
|
||||
|
||||
public function test_create_user_generates_unique_usernames(): void
|
||||
{
|
||||
$first = $this->service->createUser($this->payload());
|
||||
$second = $this->service->createUser($this->payload());
|
||||
|
||||
$this->assertNotSame($first->username, $second->username);
|
||||
}
|
||||
|
||||
public function test_create_user_hashes_given_password(): void
|
||||
{
|
||||
$user = $this->service->createUser($this->payload(['password' => 'secret123']));
|
||||
|
||||
$this->assertNotSame('secret123', $user->password);
|
||||
$this->assertTrue(Hash::check('secret123', $user->password));
|
||||
}
|
||||
|
||||
public function test_create_user_sets_random_password_when_none_given(): void
|
||||
{
|
||||
$user = $this->service->createUser($this->payload());
|
||||
|
||||
$this->assertNotEmpty($user->password);
|
||||
$this->assertFalse(Hash::check('', $user->password));
|
||||
}
|
||||
|
||||
public function test_create_user_leaves_optional_fields_null(): void
|
||||
{
|
||||
$user = $this->service->createUser($this->payload());
|
||||
|
||||
$this->assertNull($user->email);
|
||||
$this->assertNull($user->fullname);
|
||||
$this->assertNull($user->clinic_name);
|
||||
$this->assertNull($user->specialty);
|
||||
}
|
||||
|
||||
public function test_create_user_does_not_log_in(): void
|
||||
{
|
||||
$this->service->createUser($this->payload());
|
||||
|
||||
$this->assertFalse(Auth::check());
|
||||
}
|
||||
|
||||
public function test_register_and_login_creates_user_and_authenticates(): void
|
||||
{
|
||||
$user = $this->service->registerAndLogin($this->payload());
|
||||
|
||||
$this->assertInstanceOf(User::class, $user);
|
||||
$this->assertTrue(Auth::check());
|
||||
$this->assertTrue(Auth::user()->is($user));
|
||||
$this->assertDatabaseCount('users', 1);
|
||||
}
|
||||
|
||||
public function test_register_and_login_clears_registration_flow_session_keys(): void
|
||||
{
|
||||
session(['register_phone' => 'x', 'verified_phone' => 'x', 'otp_verified' => true, 'login_phone' => 'x', 'login_level' => 'x', 'available_levels' => []]);
|
||||
|
||||
$this->service->registerAndLogin($this->payload());
|
||||
|
||||
foreach (['register_phone', 'verified_phone', 'otp_verified', 'login_phone', 'login_level', 'available_levels'] as $key) {
|
||||
$this->assertFalse(session()->has($key), "session key {$key} was not forgotten");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user