Files
muninn/tests/Feature/AuthTest.php
T

53 lines
1.4 KiB
PHP
Raw Normal View History

<?php
use App\Livewire\Auth\Login;
use App\Models\User;
use Livewire\Livewire;
test('guests are redirected to login when visiting a protected page', function () {
$this->get(route('sites.index'))
->assertRedirect(route('login'));
});
test('the api stays open to guests', function () {
$this->getJson('/api/sites')->assertOk();
});
test('users can log in with correct credentials', function () {
$user = User::factory()->create(['password' => bcrypt('correct-password')]);
Livewire::test(Login::class)
->set('email', $user->email)
->set('password', 'correct-password')
->call('login')
->assertRedirect(route('sites.index'));
$this->assertAuthenticatedAs($user);
});
test('users cannot log in with incorrect credentials', function () {
$user = User::factory()->create(['password' => bcrypt('correct-password')]);
Livewire::test(Login::class)
->set('email', $user->email)
->set('password', 'wrong-password')
->call('login')
->assertHasErrors('email');
$this->assertGuest();
});
test('authenticated users are redirected away from the login page', function () {
$this->actingAs(User::factory()->create())
->get(route('login'))
->assertRedirect();
});
test('users can log out', function () {
$this->actingAs(User::factory()->create())
->post(route('logout'))
->assertRedirect('/login');
$this->assertGuest();
});