Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

2.0 KB
Raw Blame
<?php

use App\Models\InstanceSettings;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Fortify;
use PragmaRX\Google2FA\Google2FA;

uses(RefreshDatabase::class);

beforeEach(function () {
    config()->set('app.maintenance.store', 'array');
    InstanceSettings::forceCreate([
        'id' => 0,
        'is_sponsorship_popup_enabled' => false,
    ]);
});

it('makes a member configure 2fa before using a team that requires it', function () {
    $user = User::forceCreate([
        'id' => 0,
        'name' => 'Root User',
        'email' => '[email protected]',
        'password' => Hash::make('password'),
    ]);
    $team = Team::query()->findOrFail(0);
    $team->update(['show_boarding' => false, 'is_two_factor_required' => true]);
    Cache::flush();

    $page = visit('/login');
    $page->fill('email', '[email protected]')
        ->fill('password', 'password')
        ->click('Login')
        ->assertPathIs('/two-factor-required')
        ->assertSee('Two-factor authentication required')
        ->screenshot(filename: 'team-2fa-required');

    $page->click('Configure 2FA')
        ->assertPathIs('/user/confirm-password')
        ->fill('input[name="password"]', 'password')
        ->click('form[action="/user/confirm-password"] button[type=submit]')
        ->assertPathIs('/two-factor-required')
        ->click('Configure 2FA')
        ->assertSee('Scan the QR code')
        ->screenshot(filename: 'team-2fa-qr-code');

    $secret = Fortify::currentEncrypter()->decrypt($user->fresh()->two_factor_secret);
    $page->fill('input[name="code"]', (new Google2FA)->getCurrentOtp($secret))
        ->click('Validate 2FA')
        ->assertSee('Save these recovery codes')
        ->screenshot(filename: 'team-2fa-recovery-codes');

    expect($user->fresh()->two_factor_confirmed_at)->not->toBeNull();

    $page->navigate('/profile')
        ->assertPathIs('/profile')
        ->screenshot(filename: 'team-2fa-profile-after-setup');
});