]> BookStack Code Mirror - bookstack/blob - tests/Commands/ResetMfaCommandTest.php
Played around with a new app structure
[bookstack] / tests / Commands / ResetMfaCommandTest.php
1 <?php
2
3 namespace Tests\Commands;
4
5 use BookStack\Access\Mfa\MfaValue;
6 use BookStack\Users\Models\User;
7 use Tests\TestCase;
8
9 class ResetMfaCommandTest extends TestCase
10 {
11     public function test_command_requires_email_or_id_option()
12     {
13         $this->artisan('bookstack:reset-mfa')
14             ->expectsOutput('Either a --id=<number> or --email=<email> option must be provided.')
15             ->assertExitCode(1);
16     }
17
18     public function test_command_runs_with_provided_email()
19     {
20         /** @var User $user */
21         $user = User::query()->first();
22         MfaValue::upsertWithValue($user, MfaValue::METHOD_TOTP, 'test');
23
24         $this->assertEquals(1, $user->mfaValues()->count());
25         $this->artisan("bookstack:reset-mfa --email={$user->email}")
26             ->expectsQuestion('Are you sure you want to proceed?', true)
27             ->expectsOutput('User MFA methods have been reset.')
28             ->assertExitCode(0);
29         $this->assertEquals(0, $user->mfaValues()->count());
30     }
31
32     public function test_command_runs_with_provided_id()
33     {
34         /** @var User $user */
35         $user = User::query()->first();
36         MfaValue::upsertWithValue($user, MfaValue::METHOD_TOTP, 'test');
37
38         $this->assertEquals(1, $user->mfaValues()->count());
39         $this->artisan("bookstack:reset-mfa --id={$user->id}")
40             ->expectsQuestion('Are you sure you want to proceed?', true)
41             ->expectsOutput('User MFA methods have been reset.')
42             ->assertExitCode(0);
43         $this->assertEquals(0, $user->mfaValues()->count());
44     }
45
46     public function test_saying_no_to_confirmation_does_not_reset_mfa()
47     {
48         /** @var User $user */
49         $user = User::query()->first();
50         MfaValue::upsertWithValue($user, MfaValue::METHOD_TOTP, 'test');
51
52         $this->assertEquals(1, $user->mfaValues()->count());
53         $this->artisan("bookstack:reset-mfa --id={$user->id}")
54             ->expectsQuestion('Are you sure you want to proceed?', false)
55             ->assertExitCode(1);
56         $this->assertEquals(1, $user->mfaValues()->count());
57     }
58
59     public function test_giving_non_existing_user_shows_error_message()
60     {
61         $this->artisan('bookstack:reset-mfa [email protected]')
62             ->expectsOutput('A user where [email protected] could not be found.')
63             ->assertExitCode(1);
64     }
65 }