-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Recovery: Skip the recovery code challenge when no codes exist (#30)
* Use security indicator in example views * Recovery: Skip the recovery code challenge when no codes exist
- Loading branch information
1 parent
873eee6
commit 72ab21f
Showing
6 changed files
with
99 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,12 @@ | |
namespace ClaudioDekker\LaravelAuth\Testing\Partials\Challenges\Recovery; | ||
|
||
use App\Providers\RouteServiceProvider; | ||
use Carbon\Carbon; | ||
use ClaudioDekker\LaravelAuth\Events\AccountRecovered; | ||
use ClaudioDekker\LaravelAuth\Events\AccountRecoveryFailed; | ||
use ClaudioDekker\LaravelAuth\Events\SudoModeEnabled; | ||
use ClaudioDekker\LaravelAuth\Http\Middleware\EnsureSudoMode; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Facades\Password; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
|
@@ -12,7 +17,7 @@ trait ViewAccountRecoveryChallengePageTests | |
/** @test */ | ||
public function the_account_recovery_challenge_page_can_be_viewed(): void | ||
{ | ||
$user = $this->generateUser(); | ||
$user = $this->generateUser(['recovery_codes' => ['H4PFK-ENVZV', 'PIPIM-7LTUT', 'GPP13-AEXMR', 'WGAHD-95VNQ', 'BSFYG-VFG2N', 'AWOPQ-NWYJX', '2PVJM-QHPBM', 'STR7J-5ND0P']]); | ||
$token = Password::getRepository()->create($user); | ||
|
||
$response = $this->get(route('recover-account.challenge', [ | ||
|
@@ -23,10 +28,36 @@ public function the_account_recovery_challenge_page_can_be_viewed(): void | |
$response->assertOk(); | ||
} | ||
|
||
/** @test */ | ||
public function the_account_recovery_challenge_page_is_skipped_when_the_user_does_not_have_any_recovery_codes(): void | ||
{ | ||
Carbon::setTestNow(now()); | ||
Event::fake([AccountRecovered::class, AccountRecoveryFailed::class, SudoModeEnabled::class]); | ||
$user = $this->generateUser(['recovery_codes' => null]); | ||
$repository = Password::getRepository(); | ||
$token = $repository->create($user); | ||
$this->assertTrue($repository->exists($user, $token)); | ||
|
||
$response = $this->get(route('recover-account.challenge', [ | ||
'token' => $token, | ||
'email' => $user->getEmailForPasswordReset(), | ||
])); | ||
|
||
$response->assertRedirect(route('auth.settings')); | ||
$response->assertSessionMissing(EnsureSudoMode::REQUIRED_AT_KEY); | ||
$response->assertSessionHas(EnsureSudoMode::CONFIRMED_AT_KEY, now()->unix()); | ||
$this->assertFullyAuthenticatedAs($response, $user); | ||
$this->assertFalse($repository->exists($user, $token)); | ||
Event::assertDispatched(AccountRecovered::class, fn ($event) => $event->user->is($user) && $event->request === request()); | ||
Event::assertNotDispatched(AccountRecoveryFailed::class); | ||
Event::assertNotDispatched(SudoModeEnabled::class); | ||
Carbon::setTestNow(); | ||
} | ||
|
||
/** @test */ | ||
public function the_account_recovery_challenge_page_cannot_be_viewed_when_authenticated(): void | ||
{ | ||
$user = $this->generateUser(); | ||
$user = $this->generateUser(['recovery_codes' => ['H4PFK-ENVZV', 'PIPIM-7LTUT', 'GPP13-AEXMR', 'WGAHD-95VNQ', 'BSFYG-VFG2N', 'AWOPQ-NWYJX', '2PVJM-QHPBM', 'STR7J-5ND0P']]); | ||
|
||
$response = $this->actingAs($user) | ||
->get(route('recover-account.challenge', ['token' => 'foo'])); | ||
|
@@ -37,7 +68,7 @@ public function the_account_recovery_challenge_page_cannot_be_viewed_when_authen | |
/** @test */ | ||
public function the_account_recovery_challenge_page_cannot_be_viewed_when_the_provided_email_does_not_resolve_to_an_existing_user(): void | ||
{ | ||
$user = $this->generateUser(); | ||
$user = $this->generateUser(['recovery_codes' => ['H4PFK-ENVZV', 'PIPIM-7LTUT', 'GPP13-AEXMR', 'WGAHD-95VNQ', 'BSFYG-VFG2N', 'AWOPQ-NWYJX', '2PVJM-QHPBM', 'STR7J-5ND0P']]); | ||
$token = Password::getRepository()->create($user); | ||
|
||
$response = $this->get(route('recover-account.challenge', [ | ||
|
@@ -53,8 +84,8 @@ public function the_account_recovery_challenge_page_cannot_be_viewed_when_the_pr | |
/** @test */ | ||
public function the_account_recovery_challenge_page_cannot_be_viewed_when_the_recovery_token_does_not_belong_to_the_user_that_is_being_recovered(): void | ||
{ | ||
$userA = $this->generateUser(['id' => 1, 'email' => '[email protected]']); | ||
$userB = $this->generateUser(['id' => 2, 'email' => '[email protected]', $this->usernameField() => $this->anotherUsername()]); | ||
$userA = $this->generateUser(['id' => 1, 'email' => '[email protected]', 'recovery_codes' => ['H4PFK-ENVZV', 'PIPIM-7LTUT', 'GPP13-AEXMR', 'WGAHD-95VNQ', 'BSFYG-VFG2N', 'AWOPQ-NWYJX', '2PVJM-QHPBM', 'STR7J-5ND0P']]); | ||
$userB = $this->generateUser(['id' => 2, 'email' => '[email protected]', $this->usernameField() => $this->anotherUsername(), 'recovery_codes' => ['H4PFK-ENVZV', 'PIPIM-7LTUT', 'GPP13-AEXMR', 'WGAHD-95VNQ', 'BSFYG-VFG2N', 'AWOPQ-NWYJX', '2PVJM-QHPBM', 'STR7J-5ND0P']]); | ||
$token = Password::getRepository()->create($userA); | ||
|
||
$response = $this->get(route('recover-account.challenge', [ | ||
|
@@ -70,7 +101,7 @@ public function the_account_recovery_challenge_page_cannot_be_viewed_when_the_re | |
/** @test */ | ||
public function the_account_recovery_challenge_page_cannot_be_viewed_when_the_recovery_token_does_not_exist(): void | ||
{ | ||
$user = $this->generateUser(); | ||
$user = $this->generateUser(['recovery_codes' => ['H4PFK-ENVZV', 'PIPIM-7LTUT', 'GPP13-AEXMR', 'WGAHD-95VNQ', 'BSFYG-VFG2N', 'AWOPQ-NWYJX', '2PVJM-QHPBM', 'STR7J-5ND0P']]); | ||
|
||
$response = $this->get(route('recover-account.challenge', [ | ||
'token' => 'invalid-token', | ||
|
@@ -86,7 +117,7 @@ public function the_account_recovery_challenge_page_cannot_be_viewed_when_the_re | |
public function the_account_recovery_challenge_page_cannot_be_viewed_when_the_recovery_token_has_expired(): void | ||
{ | ||
Carbon::setTestNow('2022-01-01 00:00:00'); | ||
$user = $this->generateUser(); | ||
$user = $this->generateUser(['recovery_codes' => ['H4PFK-ENVZV', 'PIPIM-7LTUT', 'GPP13-AEXMR', 'WGAHD-95VNQ', 'BSFYG-VFG2N', 'AWOPQ-NWYJX', '2PVJM-QHPBM', 'STR7J-5ND0P']]); | ||
$token = Password::getRepository()->create($user); | ||
Carbon::setTestNow(now()->addHour()->addSecond()); | ||
|
||
|