Files
investbrain/tests/AuthenticationTest.php
T

80 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2024-08-01 13:53:10 -05:00
<?php
2025-01-28 17:33:54 -06:00
declare(strict_types=1);
2024-09-06 19:39:04 -05:00
namespace Tests;
2024-08-01 13:53:10 -05:00
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
class AuthenticationTest extends TestCase
{
use RefreshDatabase;
2025-04-09 19:25:15 -05:00
public function test_first_user_is_admin(): void
{
$response = $this->post('/register', [
2025-04-09 19:25:15 -05:00
'name' => 'should_be_admin',
'email' => 'should_be_admin@example.net',
'password' => 'password',
'password_confirmation' => 'password',
'terms' => 1,
2025-04-09 19:25:15 -05:00
]);
$should_be_admin = User::where(['email' => 'should_be_admin@example.net'])->first();
$this->assertModelExists($should_be_admin);
2025-04-09 19:25:15 -05:00
$this->assertTrue($should_be_admin->admin);
}
public function test_other_users_are_not_admin(): void
{
User::factory()->create();
$this->post('/register', [
'name' => 'not_admin',
'email' => 'not_admin@example.net',
'password' => 'password',
'password_confirmation' => 'password',
'terms' => 1,
2025-04-09 19:25:15 -05:00
]);
$not_admin = User::where(['email' => 'not_admin@example.net'])->first();
$this->assertModelExists($not_admin);
2025-04-09 19:25:15 -05:00
$this->assertNotTrue($not_admin->admin);
}
2024-08-01 13:53:10 -05:00
public function test_login_screen_can_be_rendered(): void
{
$response = $this->get('/login');
$response->assertStatus(200);
}
public function test_users_can_authenticate_using_the_login_screen(): void
{
$user = User::factory()->create();
$response = $this->post('/login', [
'email' => $user->email,
'password' => 'password',
]);
$this->assertAuthenticated();
$response->assertRedirect(route('dashboard', absolute: false));
}
public function test_users_can_not_authenticate_with_invalid_password(): void
{
$user = User::factory()->create();
$this->post('/login', [
'email' => $user->email,
'password' => 'wrong-password',
]);
$this->assertGuest();
}
}