Files

122 lines
4.9 KiB
PHP

<?php
namespace Tests\Feature\Services\Auth;
use App\Enums\UserEnum;
use App\Models\User;
use App\Services\Auth\AuthService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;
class AuthServiceTest extends TestCase
{
use RefreshDatabase;
protected AuthService $service;
protected function setUp(): void
{
parent::setUp();
$this->service = app(AuthService::class);
}
public function test_get_levels_for_phone_maps_user_levels_to_slugs(): void
{
User::factory()->create(['phone' => '09120000000', 'level' => UserEnum::Doctor->value]);
User::factory()->create(['phone' => '09120000000', 'level' => UserEnum::Supervisor->value]);
User::factory()->create(['phone' => '09120000000', 'level' => UserEnum::Vet->value]);
$levels = $this->service->getLevelsForPhone('09120000000');
$this->assertEqualsCanonicalizing(['doctor', 'supervisor', 'veterinarian'], $levels);
}
public function test_get_levels_for_phone_excludes_admin_level(): void
{
User::factory()->create(['phone' => '09120000000', 'level' => UserEnum::Admin->value]);
User::factory()->create(['phone' => '09120000000', 'level' => UserEnum::Doctor->value]);
$this->assertSame(['doctor'], $this->service->getLevelsForPhone('09120000000'));
}
public function test_get_levels_for_phone_returns_unique_levels(): void
{
User::factory()->count(2)->create(['phone' => '09120000000', 'level' => UserEnum::Doctor->value]);
$this->assertSame(['doctor'], $this->service->getLevelsForPhone('09120000000'));
}
public function test_get_levels_for_phone_returns_empty_array_for_unknown_phone(): void
{
User::factory()->create(['phone' => '09130000000']);
$this->assertSame([], $this->service->getLevelsForPhone('09120000000'));
}
public function test_get_levels_for_phone_flushes_session(): void
{
session(['login_phone' => '09120000000']);
$this->service->getLevelsForPhone('09120000000');
$this->assertFalse(session()->has('login_phone'));
}
public function test_authenticate_logs_in_user_matching_phone_level_and_password(): void
{
$user = User::factory()->create(['phone' => '09120000000', 'level' => UserEnum::Doctor->value, 'password' => 'secret123']);
$authenticated = $this->service->authenticateByPhoneAndLevel('09120000000', UserEnum::Doctor->value, 'secret123');
$this->assertTrue($authenticated->is($user));
$this->assertTrue(Auth::check());
$this->assertTrue(Auth::user()->is($user));
}
public function test_authenticate_clears_login_flow_session_keys(): void
{
User::factory()->create(['phone' => '09120000000', 'level' => UserEnum::Doctor->value, 'password' => 'secret123']);
session(['register_phone' => 'x', 'verified_phone' => 'x', 'otp_verified' => true, 'login_phone' => 'x', 'login_level' => 'x', 'available_levels' => []]);
$this->service->authenticateByPhoneAndLevel('09120000000', UserEnum::Doctor->value, 'secret123');
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");
}
}
public function test_authenticate_returns_null_for_wrong_password(): void
{
User::factory()->create(['phone' => '09120000000', 'level' => UserEnum::Doctor->value, 'password' => 'secret123']);
$this->assertNull($this->service->authenticateByPhoneAndLevel('09120000000', UserEnum::Doctor->value, 'wrong'));
$this->assertFalse(Auth::check());
}
public function test_authenticate_returns_null_for_wrong_level(): void
{
User::factory()->create(['phone' => '09120000000', 'level' => UserEnum::Doctor->value, 'password' => 'secret123']);
$this->assertNull($this->service->authenticateByPhoneAndLevel('09120000000', UserEnum::Supervisor->value, 'secret123'));
$this->assertFalse(Auth::check());
}
public function test_authenticate_returns_null_for_unknown_phone(): void
{
$this->assertNull($this->service->authenticateByPhoneAndLevel('09120000000', UserEnum::Doctor->value, 'secret123'));
$this->assertFalse(Auth::check());
}
public function test_authenticate_picks_account_with_requested_level_when_phone_has_many(): void
{
User::factory()->create(['phone' => '09120000000', 'level' => UserEnum::Doctor->value, 'password' => 'doctor-pass']);
$supervisor = User::factory()->create(['phone' => '09120000000', 'level' => UserEnum::Supervisor->value, 'password' => 'supervisor-pass']);
$authenticated = $this->service->authenticateByPhoneAndLevel('09120000000', UserEnum::Supervisor->value, 'supervisor-pass');
$this->assertTrue($authenticated->is($supervisor));
}
}