2025-12-28 20:18:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
2025-12-29 23:36:15 +01:00
|
|
|
namespace Tests\Browser\Auth;
|
2025-12-28 20:18:27 +01:00
|
|
|
|
|
|
|
|
use Laravel\Dusk\Browser;
|
|
|
|
|
use Tests\DuskTestCase;
|
|
|
|
|
use App\Models\Planner;
|
2025-12-29 23:36:15 +01:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2025-12-28 20:18:27 +01:00
|
|
|
|
|
|
|
|
class LoginTest extends DuskTestCase
|
|
|
|
|
{
|
2025-12-29 23:36:15 +01:00
|
|
|
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
|
|
|
|
|
{
|
2025-12-29 23:36:15 +01:00
|
|
|
$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')
|
2025-12-29 23:36:15 +01:00
|
|
|
->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')
|
2025-12-29 23:36:15 +01:00
|
|
|
->waitForLocation('/dashboard', self::TIMEOUT_MEDIUM)
|
|
|
|
|
->assertPathIs('/dashboard');
|
2025-12-28 20:18:27 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLoginWithWrongCredentials(): void
|
|
|
|
|
{
|
2025-12-29 23:36:15 +01:00
|
|
|
$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')
|
2025-12-29 23:36:15 +01:00
|
|
|
->waitFor('input[id="email"]', self::TIMEOUT_SHORT)
|
|
|
|
|
->clear('input[id="email"]')
|
|
|
|
|
->type('input[id="email"]', self::$testEmail)
|
|
|
|
|
->clear('input[id="password"]')
|
2025-12-29 02:57:25 +01:00
|
|
|
->type('input[id="password"]', 'wrongpassword')
|
2025-12-28 20:18:27 +01:00
|
|
|
->press('Login')
|
2025-12-29 23:36:15 +01:00
|
|
|
->pause(self::PAUSE_MEDIUM)
|
2025-12-28 20:18:27 +01:00
|
|
|
->assertPathIs('/login')
|
2025-12-29 23:36:15 +01:00
|
|
|
->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')
|
2025-12-29 23:36:15 +01:00
|
|
|
->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')
|
2025-12-29 23:36:15 +01:00
|
|
|
->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
|
|
|
}
|