app/tests/Browser/Auth/LoginTest.php

90 lines
3.3 KiB
PHP
Raw Normal View History

2025-12-28 20:18:27 +01:00
<?php
namespace Tests\Browser\Auth;
2025-12-28 20:18:27 +01:00
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
use App\Models\Planner;
use Illuminate\Support\Facades\Hash;
2025-12-28 20:18:27 +01:00
class LoginTest extends DuskTestCase
{
protected static $testPlanner = null;
protected static $testEmail = null;
protected static $testPassword = 'password';
protected function ensureTestPlannerExists(): void
{
if (self::$testPlanner === null) {
// Generate unique email for this test run
self::$testEmail = fake()->unique()->safeEmail();
self::$testPlanner = Planner::factory()->create([
'email' => self::$testEmail,
'password' => Hash::make(self::$testPassword),
]);
}
}
2025-12-28 20:18:27 +01:00
public function testSuccessfulLogin(): void
{
$this->ensureTestPlannerExists();
2025-12-28 20:18:27 +01:00
$this->browse(function (Browser $browser) {
2025-12-29 17:38:37 +01:00
$browser->driver->manage()->deleteAllCookies();
2025-12-28 20:18:27 +01:00
$browser->visit('http://dishplanner_app:8000/login')
->waitFor('input[id="email"]', self::TIMEOUT_SHORT)
->clear('input[id="email"]')
->type('input[id="email"]', self::$testEmail)
->clear('input[id="password"]')
->type('input[id="password"]', self::$testPassword)
2025-12-28 20:18:27 +01:00
->press('Login')
->waitForLocation('/dashboard', self::TIMEOUT_MEDIUM)
->assertPathIs('/dashboard');
2025-12-28 20:18:27 +01:00
});
}
public function testLoginWithWrongCredentials(): void
{
$this->ensureTestPlannerExists();
2025-12-28 20:18:27 +01:00
$this->browse(function (Browser $browser) {
$browser->driver->manage()->deleteAllCookies();
$browser->visit('http://dishplanner_app:8000/login')
->waitFor('input[id="email"]', self::TIMEOUT_SHORT)
->clear('input[id="email"]')
->type('input[id="email"]', self::$testEmail)
->clear('input[id="password"]')
->type('input[id="password"]', 'wrongpassword')
2025-12-28 20:18:27 +01:00
->press('Login')
->pause(self::PAUSE_MEDIUM)
2025-12-28 20:18:27 +01:00
->assertPathIs('/login')
->assertSee('These credentials do not match our records');
2025-12-28 20:18:27 +01:00
});
}
2025-12-29 17:38:37 +01:00
public function testLoginFormRequiredFields(): void
2025-12-28 20:18:27 +01:00
{
$this->browse(function (Browser $browser) {
$browser->driver->manage()->deleteAllCookies();
$browser->visit('http://dishplanner_app:8000/login')
->waitFor('input[id="email"]', self::TIMEOUT_SHORT);
2025-12-29 17:38:37 +01:00
// Check that both fields have the required attribute
$browser->assertAttribute('input[id="email"]', 'required', 'true');
$browser->assertAttribute('input[id="password"]', 'required', 'true');
// Verify email field is type email
$browser->assertAttribute('input[id="email"]', 'type', 'email');
// Verify password field is type password
$browser->assertAttribute('input[id="password"]', 'type', 'password');
// Test that we stay on login page if we try to submit with empty fields
$browser->press('Login')
->pause(self::PAUSE_SHORT)
2025-12-29 17:38:37 +01:00
->assertPathIs('/login');
2025-12-28 20:18:27 +01:00
});
}
2025-12-29 17:38:37 +01:00
}