71 lines
2.5 KiB
PHP
71 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Http\Admin;
|
|
|
|
use App\Enums\UserEnum;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Inertia\Testing\AssertableInertia as Assert;
|
|
use Tests\TestCase;
|
|
|
|
class AuthControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_login_page_renders_for_guests(): void
|
|
{
|
|
$this->get(route('admin.auth.login'))->assertOk()->assertInertia(fn (Assert $page) => $page->component('Auth/login'));
|
|
}
|
|
|
|
public function test_login_page_redirects_authenticated_users_to_dashboard(): void
|
|
{
|
|
$this->actingAs(User::factory()->create())->get(route('admin.auth.login'))->assertRedirect('/admin/dashboard');
|
|
}
|
|
|
|
public function test_login_action_authenticates_with_email_and_password(): void
|
|
{
|
|
$user = User::factory()->create(['email' => 'admin@example.com', 'password' => 'secret123', 'level' => UserEnum::Admin->value]);
|
|
|
|
$this->postJson('/admin/auth/login', ['email' => 'admin@example.com', 'password' => 'secret123'])->assertOk();
|
|
|
|
$this->assertAuthenticatedAs($user);
|
|
}
|
|
|
|
public function test_login_action_rejects_wrong_password(): void
|
|
{
|
|
User::factory()->create(['email' => 'admin@example.com', 'password' => 'secret123']);
|
|
|
|
$this->postJson('/admin/auth/login', ['email' => 'admin@example.com', 'password' => 'wrongpass'])
|
|
->assertUnauthorized()
|
|
->assertJsonPath('errors.email.0', 'ایمیل و کلمه عبور شما با هم مطابقت ندارن');
|
|
|
|
$this->assertGuest();
|
|
}
|
|
|
|
public function test_login_action_validates_input(): void
|
|
{
|
|
$this->postJson('/admin/auth/login', ['email' => 'unknown@example.com', 'password' => 'short'])
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors(['email', 'password']);
|
|
}
|
|
|
|
public function test_register_page_renders_for_guests(): void
|
|
{
|
|
$this->get(route('admin.auth.register'))->assertOk()->assertInertia(fn (Assert $page) => $page->component('Auth/register'));
|
|
}
|
|
|
|
public function test_register_action_validates_input(): void
|
|
{
|
|
$this->postJson('/admin/auth/register', ['name' => 'a', 'email' => 'x', 'password' => 'short'])
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors(['name', 'email', 'password']);
|
|
}
|
|
|
|
public function test_logout_ends_session_and_redirects_to_login(): void
|
|
{
|
|
$this->actingAs(User::factory()->create())->get('/admin/auth/logout')->assertRedirect('/admin/auth/login');
|
|
|
|
$this->assertGuest();
|
|
}
|
|
}
|