Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Confirm 2FA when enabling #992

Merged
merged 7 commits into from
Mar 16, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
user profile tests
  • Loading branch information
taylorotwell committed Mar 15, 2022
commit cc3e8d00796c336c37b07c57fa86abf5dcde66e0
122 changes: 122 additions & 0 deletions tests/UserProfileControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Laravel\Jetstream\Tests;

use Illuminate\Support\Facades\Schema;
use Laravel\Fortify\Actions\DisableTwoFactorAuthentication;
use Laravel\Fortify\Features;
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\Tests\Fixtures\User;
use Mockery as m;

class UserProfileControllerTest extends OrchestraTestCase
{
public function setUp(): void
{
parent::setUp();

Jetstream::useUserModel(User::class);
}

public function test_empty_two_factor_state_is_noted()
{
$this->migrate();

$disable = $this->mock(DisableTwoFactorAuthentication::class);
$disable->shouldReceive('__invoke')->once();

Jetstream::$inertiaManager = $inertia = m::mock();
$inertia->shouldReceive('render')->once();

$user = User::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => 'secret',
]);

$response = $this->actingAs($user)->get('/user/profile');

$response->assertSessionHas('two_factor_empty_at');

$response->assertStatus(200);
}

public function test_two_factor_is_not_disabled_if_was_previously_empty_and_currently_confirming()
{
$this->migrate();

$disable = $this->mock(DisableTwoFactorAuthentication::class);
$disable->shouldReceive('__invoke')->never();

Jetstream::$inertiaManager = $inertia = m::mock();
$inertia->shouldReceive('render')->once();

$user = User::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => 'secret',
'two_factor_secret' => 'test-secret',
]);

$response = $this->actingAs($user)
->withSession(['two_factor_empty_at' => time()])
->get('/user/profile');

$response->assertStatus(200);
}

public function test_two_factor_is_disabled_if_was_previously_confirming_and_page_is_reloaded()
{
$this->migrate();

$disable = $this->mock(DisableTwoFactorAuthentication::class);
$disable->shouldReceive('__invoke')->once();

Jetstream::$inertiaManager = $inertia = m::mock();
$inertia->shouldReceive('render')->once();

$user = User::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => 'secret',
'two_factor_secret' => 'test-secret',
]);

$response = $this->actingAs($user)
->withSession([
'two_factor_empty_at' => time(),
'two_factor_confirming_at' => time() - 10,
])
->get('/user/profile');

$response->assertStatus(200);
}

protected function migrate()
{
$this->artisan('migrate', ['--database' => 'testbench'])->run();

Schema::table('users', function ($table) {
$table->string('two_factor_secret')->nullable();
$table->timestamp('two_factor_confirmed_at')->nullable();
});
}

protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']->set('jetstream.stack', 'inertia');
$app['config']->set('fortify.features', [
Features::registration(),
Features::resetPasswords(),
// Features::emailVerification(),
Features::updateProfileInformation(),
Features::updatePasswords(),
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]),
]);
}
}