93 lines
3.6 KiB
PHP
93 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Exceptions\DatabaseUnreachable;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\Facades\Route;
|
|
use PDOException;
|
|
use Tests\Concerns\InspectsHtml;
|
|
use Tests\TestCase;
|
|
|
|
/** Server DB tidak terjangkau (mis. di luar jaringan UNTAN): pesan ramah, bukan galat 500. */
|
|
class DatabaseUnreachableTest extends TestCase
|
|
{
|
|
use InspectsHtml;
|
|
|
|
/** Seperti galat PDO asli: SQLSTATE di awal pesan, kode berisi kode driver. */
|
|
private static function queryFailure(string $sqlState): QueryException
|
|
{
|
|
return new QueryException('untan', 'select 1', [], new PDOException("SQLSTATE[{$sqlState}] [7] simulasi", 7));
|
|
}
|
|
|
|
/** Koneksi untan diarahkan ke port tertutup di mesin sendiri: gagal sungguhan dengan SQLSTATE 08006. */
|
|
private function breakLegacyConnection(): void
|
|
{
|
|
config([
|
|
'database.connections.untan.host' => '127.0.0.1',
|
|
'database.connections.untan.port' => 1,
|
|
'auth.guards.web.provider' => 'legacy',
|
|
]);
|
|
DB::purge('untan');
|
|
Auth::forgetGuards();
|
|
}
|
|
|
|
public function test_connection_failure_shows_friendly_page(): void
|
|
{
|
|
Route::get('/_uji/db-putus', fn () => throw self::queryFailure('08006'))->middleware('web');
|
|
|
|
$response = $this->get('/_uji/db-putus')->assertStatus(503)->assertSee(DatabaseUnreachable::MESSAGE);
|
|
|
|
$headings = $this->dom($response)->query('//h1');
|
|
$this->assertSame(1, $headings->length);
|
|
$this->assertSame('Tidak dapat terhubung ke server', trim($headings->item(0)->textContent));
|
|
$response->assertDontSee('select 1');
|
|
}
|
|
|
|
public function test_connection_failure_returns_json_message(): void
|
|
{
|
|
Route::get('/_uji/db-putus', fn () => throw self::queryFailure('08001'))->middleware('web');
|
|
|
|
$this->getJson('/_uji/db-putus')->assertStatus(503)->assertExactJson(['message' => DatabaseUnreachable::MESSAGE]);
|
|
}
|
|
|
|
public function test_other_database_errors_are_not_disguised(): void
|
|
{
|
|
Route::get('/_uji/db-salah', fn () => throw self::queryFailure('42P01'))->middleware('web');
|
|
|
|
$this->get('/_uji/db-salah')->assertStatus(500)->assertDontSee(DatabaseUnreachable::MESSAGE);
|
|
}
|
|
|
|
public function test_login_shows_message_under_username_without_counting_a_failed_attempt(): void
|
|
{
|
|
$this->breakLegacyConnection();
|
|
RateLimiter::clear('198001012005011001|127.0.0.1');
|
|
|
|
$this->from(route('login'))
|
|
->post(route('login.store'), ['username' => '198001012005011001', 'password' => 'rahasia-staf'])
|
|
->assertRedirect(route('login'))
|
|
->assertSessionHasErrors(['username' => DatabaseUnreachable::MESSAGE])
|
|
->assertSessionHasInput('username', '198001012005011001')
|
|
->assertSessionMissing('_old_input.password');
|
|
|
|
$this->assertGuest();
|
|
$this->assertSame(0, RateLimiter::attempts('198001012005011001|127.0.0.1'));
|
|
}
|
|
|
|
public function test_real_connection_failure_is_recognised_and_hides_bindings(): void
|
|
{
|
|
$this->breakLegacyConnection();
|
|
|
|
try {
|
|
DB::connection('untan')->select('select ?', ['nilai-rahasia']);
|
|
$this->fail('Koneksi ke port tertutup seharusnya gagal.');
|
|
} catch (QueryException $e) {
|
|
$this->assertTrue(DatabaseUnreachable::matches($e), "SQLSTATE {$e->getCode()} bukan galat koneksi.");
|
|
$this->assertStringNotContainsString('nilai-rahasia', $e->getMessage());
|
|
}
|
|
}
|
|
}
|