oportunidades 4
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-10-10 07:03:59 +02:00
parent e662230df7
commit 6ddafc95ba
179 changed files with 22750 additions and 933 deletions
@@ -0,0 +1,88 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Encryption\Encrypter;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Session\Middleware\StartSession;
use Laravel\Fortify\Actions\EnableTwoFactorAuthentication;
use Laravel\Fortify\Fortify;
use PragmaRX\Google2FA\Google2FA;
use Tests\TestCase;
class QaAccountSelectionTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
require_once base_path('tools/portal-qa-account-selection.php');
config(['portal.qa_synthetic_accounts_only' => true]);
$kernel = app(Kernel::class);
$kernel->appendMiddlewareToGroup('web', \PortalQaAccountSelection::class);
$kernel->addToMiddlewarePriorityAfter(StartSession::class, \PortalQaAccountSelection::class);
}
private function restored(): User
{
$user = User::factory()->create(['email' => 'restored-source@source.invalid']);
$user->forceFill(['two_factor_secret' => (new Encrypter(random_bytes(32), 'AES-256-CBC'))->encrypt('SYNTHETICONLY'),
'two_factor_confirmed_at' => now()])->save();
return $user;
}
public function test_restored_account_is_stopped_before_mfa_without_changing_its_row(): void
{
$user = $this->restored();
$original = hash('sha256', json_encode($user->fresh()->getRawOriginal(), JSON_THROW_ON_ERROR));
$this->post('/login', ['email' => $user->email, 'password' => 'password'])
->assertRedirect('/login')->assertSessionHasErrors('email')->assertSessionMissing('login.id');
$this->assertGuest();
$this->assertSame($original, hash('sha256', json_encode($user->fresh()->getRawOriginal(), JSON_THROW_ON_ERROR)));
}
public function test_json_restored_login_and_stale_challenge_remain_unauthenticated(): void
{
$user = $this->restored();
$this->postJson('/login', ['email' => $user->email, 'password' => 'password'])
->assertUnprocessable()->assertJsonValidationErrors('email')->assertSessionMissing('login.id');
$this->withSession(['login.id' => $user->id])->get('/two-factor-challenge')
->assertRedirect('/login')->assertSessionHasErrors('email')->assertSessionMissing('login.id');
$this->assertGuest();
}
public function test_synthetic_login_requires_valid_mfa_then_recovers_after_logout(): void
{
$user = User::factory()->create(['email' => 'synthetic-investor@example.invalid']);
app(EnableTwoFactorAuthentication::class)($user);
$user->forceFill(['two_factor_confirmed_at' => now()])->save();
$secret = Fortify::currentEncrypter()->decrypt($user->two_factor_secret);
$credentials = ['email' => $user->email, 'password' => 'password'];
$this->post('/login', $credentials)->assertRedirect('/two-factor-challenge');
$this->assertGuest();
$this->post('/two-factor-challenge', ['code' => 'invalid'])->assertRedirect('/two-factor-challenge');
$this->assertGuest();
$engine = new Google2FA;
$this->post('/two-factor-challenge', ['code' => $engine->getCurrentOtp($secret)])->assertRedirect('/dashboard');
$this->assertAuthenticatedAs($user);
$this->get('/dashboard')->assertOk();
$this->post('/logout')->assertRedirect('/');
$this->assertGuest();
$this->post('/login', $credentials)->assertRedirect('/two-factor-challenge');
// A new login uses a fresh code in Fortify's normal accepted window, not the consumed code.
$this->post('/two-factor-challenge', ['code' => $engine->oathTotp($secret, $engine->getTimestamp() + 1)])->assertRedirect('/dashboard');
$this->assertAuthenticatedAs($user);
}
public function test_qa_account_policy_does_not_apply_when_flag_is_absent(): void
{
config(['portal.qa_synthetic_accounts_only' => false]);
$user = User::factory()->create(['email' => 'ordinary-user@source.invalid']);
$this->post('/login', ['email' => $user->email, 'password' => 'password'])->assertRedirect('/dashboard');
$this->assertAuthenticatedAs($user);
}
}
@@ -0,0 +1,84 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Encryption\Encrypter;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Crypt;
use Laravel\Fortify\Actions\EnableTwoFactorAuthentication;
use Laravel\Fortify\Fortify;
use PragmaRX\Google2FA\Google2FA;
use Tests\TestCase;
class TwoFactorDecryptionFailureTest extends TestCase
{
use RefreshDatabase;
private function actor(bool $brokenRecovery = false): User
{
$user = User::factory()->create();
app(EnableTwoFactorAuthentication::class)($user);
$user->forceFill(['two_factor_confirmed_at' => now()])->save();
$foreign = new Encrypter(random_bytes(32), 'AES-256-CBC');
$user->forceFill($brokenRecovery
? ['two_factor_recovery_codes' => $foreign->encrypt('["synthetic-only"]')]
: ['two_factor_secret' => $foreign->encrypt('SYNTHETICQAONLY')])->save();
return $user;
}
public function test_wrong_key_secret_returns_controlled_json_and_cancels_challenge_without_authenticating(): void
{
$user = $this->actor();
$original = $user->getRawOriginal('two_factor_secret');
$this->postJson('/login', ['email' => $user->email, 'password' => 'password'])->assertOk()->assertJson(['two_factor' => true]);
$oldSession = session()->getId();
$this->postJson('/two-factor-challenge', ['code' => '123456'])->assertUnprocessable()->assertJsonValidationErrors('code')
->assertJsonPath('message', 'Não foi possível verificar a autenticação desta conta. Inicie sessão novamente ou contacte o suporte.')
->assertSessionMissing('login.id')->assertSessionMissing('_old_input.code');
$this->assertGuest();
$this->assertNotSame($oldSession, session()->getId());
$this->assertSame($original, $user->fresh()->two_factor_secret);
$this->getJson('/dashboard')->assertUnauthorized();
}
public function test_wrong_key_recovery_codes_are_controlled_and_never_replaced_or_authenticated(): void
{
$user = $this->actor(true);
$original = $user->two_factor_recovery_codes;
$this->postJson('/login', ['email' => $user->email, 'password' => 'password'])->assertOk();
$this->postJson('/two-factor-challenge', ['recovery_code' => 'synthetic-only'])->assertUnprocessable()->assertJsonValidationErrors('recovery_code')
->assertSessionMissing('login.id')->assertSessionMissing('_old_input.recovery_code');
$this->assertGuest();
$this->assertSame($original, $user->fresh()->two_factor_recovery_codes);
}
public function test_browser_failure_redirects_to_login_with_safe_visible_message(): void
{
$user = $this->actor();
$this->post('/login', ['email' => $user->email, 'password' => 'password'])->assertRedirect('/two-factor-challenge');
$this->post('/two-factor-challenge', ['code' => '123456'])->assertRedirect('/login')->assertSessionHasErrors('email')
->assertSessionMissing('login.id')->assertSessionMissing('_old_input.code');
$this->assertGuest();
$this->get('/two-factor-challenge')->assertRedirect('/login');
}
public function test_normal_enrollment_and_valid_mfa_still_authenticate_after_invalid_code_is_rejected(): void
{
$user = User::factory()->create();
app(EnableTwoFactorAuthentication::class)($user);
$user->forceFill(['two_factor_confirmed_at' => now()])->save();
$secret = Fortify::currentEncrypter()->decrypt($user->two_factor_secret);
$this->assertCount(8, $user->recoveryCodes());
$this->assertSame($secret, Crypt::decrypt($user->two_factor_secret));
$this->postJson('/login', ['email' => $user->email, 'password' => 'password'])->assertOk()->assertJson(['two_factor' => true]);
$this->postJson('/two-factor-challenge', ['code' => 'invalid'])->assertUnprocessable();
$this->assertGuest();
$code = (new Google2FA)->getCurrentOtp($secret);
$this->postJson('/two-factor-challenge', ['code' => $code])->assertNoContent();
$this->assertAuthenticatedAs($user);
$this->postJson('/logout')->assertNoContent();
$this->assertGuest();
}
}