5683cd3b0e
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
132 lines
4.0 KiB
PHP
132 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Spatie\Permission\Models\Role;
|
|
use Tests\TestCase;
|
|
|
|
class AuthTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
|
|
}
|
|
|
|
public function test_users_table_exists_and_user_can_be_created(): void
|
|
{
|
|
$user = User::create([
|
|
'name' => 'Test Admin',
|
|
'email' => 'admin@test.com',
|
|
'password' => bcrypt('password'),
|
|
]);
|
|
|
|
$this->assertDatabaseHas('users', ['email' => 'admin@test.com']);
|
|
}
|
|
|
|
public function test_roles_seeder_creates_four_roles(): void
|
|
{
|
|
$this->artisan('db:seed', ['--class' => 'RolesAndAdminSeeder']);
|
|
|
|
$this->assertDatabaseHas('roles', ['name' => 'administrator']);
|
|
$this->assertDatabaseHas('roles', ['name' => 'pengurus_ibadah']);
|
|
$this->assertDatabaseHas('roles', ['name' => 'petugas_pendataan']);
|
|
$this->assertDatabaseHas('roles', ['name' => 'pemangku_kebijakan']);
|
|
}
|
|
|
|
public function test_roles_seeder_creates_default_admin(): void
|
|
{
|
|
$this->artisan('db:seed', ['--class' => 'RolesAndAdminSeeder']);
|
|
|
|
$user = User::where('email', 'admin@webgis.local')->first();
|
|
$this->assertNotNull($user);
|
|
$this->assertTrue($user->hasRole('administrator'));
|
|
}
|
|
|
|
public function test_login_page_is_accessible(): void
|
|
{
|
|
$response = $this->get('/login');
|
|
$response->assertStatus(200);
|
|
$response->assertViewIs('auth.login');
|
|
}
|
|
|
|
public function test_login_with_valid_credentials_redirects_to_map(): void
|
|
{
|
|
$this->artisan('db:seed', ['--class' => 'RolesAndAdminSeeder']);
|
|
|
|
$response = $this->post('/login', [
|
|
'email' => 'admin@webgis.local',
|
|
'password' => 'admin123',
|
|
]);
|
|
|
|
$response->assertRedirect('/map');
|
|
}
|
|
|
|
public function test_login_with_invalid_credentials_returns_error(): void
|
|
{
|
|
$response = $this->post('/login', [
|
|
'email' => 'wrong@email.com',
|
|
'password' => 'wrongpass',
|
|
]);
|
|
|
|
$response->assertSessionHasErrors('email');
|
|
}
|
|
|
|
public function test_logout_redirects_to_login(): void
|
|
{
|
|
$this->artisan('db:seed', ['--class' => 'RolesAndAdminSeeder']);
|
|
$user = User::where('email', 'admin@webgis.local')->first();
|
|
|
|
$response = $this->actingAs($user)->post('/logout');
|
|
|
|
$response->assertRedirect('/login');
|
|
}
|
|
|
|
public function test_unauthenticated_user_cannot_access_map(): void
|
|
{
|
|
$response = $this->get('/map');
|
|
$response->assertRedirect('/login');
|
|
}
|
|
|
|
public function test_pemangku_kebijakan_cannot_delete_ibadah(): void
|
|
{
|
|
$this->artisan('db:seed', ['--class' => 'RolesAndAdminSeeder']);
|
|
|
|
$viewer = User::firstOrCreate(
|
|
['email' => 'viewer@test.com'],
|
|
['name' => 'Viewer', 'password' => bcrypt('password')]
|
|
);
|
|
$viewer->syncRoles(['pemangku_kebijakan']);
|
|
|
|
$response = $this->actingAs($viewer)->delete('/api/ibadah/999');
|
|
$response->assertStatus(403);
|
|
}
|
|
|
|
public function test_data_page_shows_user_name_and_logout(): void
|
|
{
|
|
$this->artisan('db:seed', ['--class' => 'RolesAndAdminSeeder']);
|
|
$user = User::where('email', 'admin@webgis.local')->first();
|
|
|
|
$response = $this->actingAs($user)->get('/data');
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertSee('Keluar');
|
|
$response->assertSee($user->name);
|
|
}
|
|
|
|
public function test_map_page_passes_user_role_to_js(): void
|
|
{
|
|
$this->artisan('db:seed', ['--class' => 'RolesAndAdminSeeder']);
|
|
$user = User::where('email', 'admin@webgis.local')->first();
|
|
|
|
$response = $this->actingAs($user)->get('/map');
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertSee("authUserRole = 'administrator'", false);
|
|
}
|
|
}
|