94 lines
2.4 KiB
PHP
94 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Auth;
|
|
|
|
use App\Models\User;
|
|
use Database\Seeders\DatabaseSeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class LoginTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_database_seeder_admin_can_login(): void
|
|
{
|
|
$this->seed(DatabaseSeeder::class);
|
|
|
|
$response = $this->post('/login', [
|
|
'email' => 'admin@glastree.local',
|
|
'password' => 'password',
|
|
]);
|
|
|
|
$response->assertStatus(302);
|
|
$response->assertRedirect('/dashboard');
|
|
$this->assertAuthenticated();
|
|
}
|
|
|
|
public function test_admin_user_can_login(): void
|
|
{
|
|
User::factory()->create([
|
|
'name' => 'Admin',
|
|
'email' => 'admin@example.com',
|
|
'password' => bcrypt('secret123'),
|
|
'is_admin' => true,
|
|
'status' => 'active',
|
|
]);
|
|
|
|
$response = $this->post('/login', [
|
|
'email' => 'admin@example.com',
|
|
'password' => 'secret123',
|
|
]);
|
|
|
|
$response->assertStatus(302);
|
|
$response->assertRedirect('/dashboard');
|
|
$this->assertAuthenticated();
|
|
}
|
|
|
|
public function test_login_with_wrong_password_fails(): void
|
|
{
|
|
User::factory()->create([
|
|
'email' => 'admin@example.com',
|
|
'password' => bcrypt('correct-password'),
|
|
'is_admin' => true,
|
|
'status' => 'active',
|
|
]);
|
|
|
|
$response = $this->from('/login')->post('/login', [
|
|
'email' => 'admin@example.com',
|
|
'password' => 'wrong-password',
|
|
]);
|
|
|
|
$response->assertStatus(302);
|
|
$response->assertRedirect('/login');
|
|
$this->assertGuest();
|
|
}
|
|
|
|
public function test_suspended_user_cannot_login(): void
|
|
{
|
|
User::factory()->create([
|
|
'email' => 'suspended@example.com',
|
|
'password' => bcrypt('password'),
|
|
'status' => 'suspended',
|
|
]);
|
|
|
|
$response = $this->post('/login', [
|
|
'email' => 'suspended@example.com',
|
|
'password' => 'password',
|
|
]);
|
|
|
|
$response->assertStatus(302);
|
|
$response->assertRedirect('/');
|
|
$this->assertGuest();
|
|
}
|
|
|
|
public function test_login_page_loads(): void
|
|
{
|
|
$response = $this->get('/login');
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertSee('Email');
|
|
$response->assertSee('Password');
|
|
}
|
|
}
|