initial commit

This commit is contained in:
Debian
2026-06-03 04:18:36 +07:00
commit a5569ce2a0
220 changed files with 30797 additions and 0 deletions
@@ -0,0 +1,111 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Tests\TestCase;
class AuthenticationTest extends TestCase
{
use RefreshDatabase;
public function test_login_screen_can_be_rendered(): void
{
$response = $this->get('/login');
$response->assertStatus(200);
}
public function test_users_can_authenticate_with_valid_credentials(): void
{
$user = User::factory()->create([
'password' => bcrypt($password = 'password'),
]);
$response = $this->post('/login', [
'email' => $user->email,
'password' => $password,
]);
$this->assertAuthenticatedAs($user);
// Assert redirect based on user role
if ($user->user_role === 'admin') {
$response->assertRedirect(route('admin-dashboard', [], false));
} elseif ($user->user_role === 'doctor') {
//$response->assertRedirect(route('doctor-dashboard', [], false));
} else {
$response->assertRedirect(route('user-dashboard', [], false));
}
}
public function test_users_can_not_authenticate_with_invalid_password(): void
{
$user = User::factory()->create();
$response = $this->post('/login', [
'email' => $user->email,
'password' => 'wrong-password',
]);
$response->assertSessionHasErrors();
$this->assertGuest();
}
public function test_users_can_logout(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post('/logout');
$response->assertRedirect('/');
$this->assertGuest();
}
public function test_user_can_redirect_to_user_dashboard(): void
{
$user = User::factory()->create(['user_role' => 'user']);
$userResponse = $this->post('/login', [
'email' => $user->email,
'password' => 'password',
]);
$userResponse->assertRedirect(route('user-dashboard'));
}
public function test_admin_can_redirect_to_admin_dashboard(): void
{
$admin = User::factory()->create(['user_role' => 'admin']);
$adminResponse = $this->post('/login', [
'email' => $admin->email,
'password' => 'password', // Assuming password is 'password' for all users
]);
$adminResponse->assertRedirect(route('admin-dashboard'));
}
// public function test_doctor_can_redirect_to_doctor_dashboard(): void
// {
// $doctor = User::factory()->create(['user_role' => 'doctor']);
// $doctorResponse = $this->post('/login', [
// 'email' => $doctor->email,
// 'password' => 'password',
// ]);
// $doctorResponse->assertRedirect(route('doctor-dashboard'));
// }
public function test_logout_destroys_authenticated_session(): void
{
$user = User::factory()->create();
$this->actingAs($user)->post('/logout');
$this->assertGuest();
$this->assertFalse(Auth::check());
$this->assertNull(Auth::user());
$this->assertTrue(Session::has('_token'));
}
}
@@ -0,0 +1,123 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
class RegistrationTest extends TestCase
{
use RefreshDatabase;
use WithFaker;
public function test_registration_screen_can_be_rendered(): void
{
$response = $this->get('/register');
$response->assertStatus(200);
}
public function test_new_user_can_register()
{
$userData = [
'username' => $this->faker->userName,
'password' => 'password123',
'password_confirmation' => 'password123',
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'telepon' => $this->faker->phoneNumber,
'alamat' => $this->faker->address,
'gender' => $this->faker->randomElement(['male', 'female']),
];
$response = $this->post('/register', $userData);
$response->assertStatus(302); // Check if redirected after successful registration
$response->assertRedirect(route('login'));
$this->assertDatabaseHas('users', [
'username' => $userData['username'],
'email' => strtolower($userData['email']),
'telepon' => $userData['telepon'],
'alamat' => $userData['alamat'],
'gender' => $userData['gender'],
]);
// Assert that the password is hashed
$user = User::where('email', $userData['email'])->first();
$this->assertTrue(Hash::check($userData['password'], $user->password));
}
public function test_registration_validation_fails_if_missing_required_fields()
{
$response = $this->post('/register', []);
$response->assertSessionHasErrors(['username', 'password', 'name', 'email', 'telepon', 'gender']);
}
public function test_registration_validation_fails_if_email_invalid()
{
$userData = [
'username' => $this->faker->userName,
'password' => 'password123',
'password_confirmation' => 'password123',
'name' => $this->faker->name,
'email' => 'invalid_email',
'telepon' => $this->faker->phoneNumber,
'alamat' => $this->faker->address,
'gender' => $this->faker->randomElement(['male', 'female']),
];
$response = $this->post('/register', $userData);
$response->assertSessionHasErrors(['email']);
}
public function test_registration_fails_if_duplicate_username()
{
$existingUser = User::factory()->create();
$userData = [
'username' => $existingUser->username,
'password' => 'password123',
'password_confirmation' => 'password123',
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'telepon' => $this->faker->phoneNumber,
'alamat' => $this->faker->address,
'gender' => $this->faker->randomElement(['male', 'female']),
];
$response = $this->post('/register', $userData);
$response->assertSessionHasErrors(['username']);
}
public function test_registration_fails_if_duplicate_email()
{
$existingUser = User::factory()->create();
$userData = [
'username' => $this->faker->userName,
'password' => 'password123',
'password_confirmation' => 'password123',
'name' => $this->faker->name,
'email' => $existingUser->email,
'telepon' => $this->faker->phoneNumber,
'alamat' => $this->faker->address,
'gender' => $this->faker->randomElement(['male', 'female']),
];
$response = $this->post('/register', $userData);
$response->assertSessionHasErrors(['email']);
}
}