|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Tests\Browser\Processes\Dashboard; |
| 4 | + |
| 5 | +use PragmaRX\Google2FA\Google2FA; |
| 6 | +use Facebook\WebDriver\WebDriverKeys; |
| 7 | +use Illuminate\Support\Facades\Crypt; |
| 8 | +use Pterodactyl\Tests\Browser\PterodactylBrowser; |
| 9 | +use Pterodactyl\Tests\Browser\Pages\Dashboard\AccountPage; |
| 10 | + |
| 11 | +class TwoFactorAuthenticationProcessTest extends DashboardTestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Test that the modal can be opened and closed. |
| 15 | + */ |
| 16 | + public function testModalOpenAndClose() |
| 17 | + { |
| 18 | + $this->browse(function (PterodactylBrowser $browser) { |
| 19 | + $browser->loginAs($this->user) |
| 20 | + ->visit(new AccountPage) |
| 21 | + ->assertMissing('.modal-mask') |
| 22 | + ->click('@2fa_button') |
| 23 | + ->waitFor('@2fa_modal') |
| 24 | + ->pause(500)// seems to fix fragile test |
| 25 | + ->clickPosition(100, 100) |
| 26 | + ->waitUntilMissing('@2fa_modal') |
| 27 | + ->click('@2fa_button') |
| 28 | + ->waitFor('@2fa_modal') |
| 29 | + ->click('svg[role="button"][aria-label="Close modal"]') |
| 30 | + ->waitUntilMissing('@2fa_modal') |
| 31 | + ->click('@2fa_button') |
| 32 | + ->waitFor('@2fa_modal') |
| 33 | + ->keys('', [WebDriverKeys::ESCAPE]) |
| 34 | + ->waitUntilMissing('@2fa_modal'); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Test that a user that does not have two-factor enabled can enable it on their account. |
| 40 | + */ |
| 41 | + public function testTwoFactorCanBeEnabled() |
| 42 | + { |
| 43 | + $this->browse(function (PterodactylBrowser $browser) { |
| 44 | + $browser->loginAs($this->user) |
| 45 | + ->visit(new AccountPage) |
| 46 | + ->click('@2fa_button') |
| 47 | + ->waitForText(trans('dashboard/account.two_factor.setup.title')) |
| 48 | + ->assertFocused('@2fa_token') |
| 49 | + ->waitFor('#grid-qr-code') |
| 50 | + ->assertSee(trans('dashboard/account.two_factor.setup.help')); |
| 51 | + |
| 52 | + // Grab information from the database so we can ensure the correct things are showing up. |
| 53 | + // Also because we need to generate a code to send through and activate it with. |
| 54 | + $updated = $this->user->fresh(); |
| 55 | + |
| 56 | + $secret = Crypt::decrypt($updated->totp_secret); |
| 57 | + $code = (new Google2FA())->getCurrentOtp($secret); |
| 58 | + |
| 59 | + $browser->assertSeeIn('code', $secret) |
| 60 | + ->assertVisible('@2fa_enable[disabled="disabled"]') |
| 61 | + ->assertMissing('@2fa_token ~ .input-help.error') |
| 62 | + ->type('@2fa_token', '12') |
| 63 | + ->assertSeeIn('@2fa_token ~ .input-help.error', 'The token length must be 6.') |
| 64 | + ->type('@2fa_token', $code) |
| 65 | + ->assertMissing('@2fa_token ~ .input-help.error') |
| 66 | + ->click('@2fa_enable') |
| 67 | + ->waitUntilMissing('@2fa_modal') |
| 68 | + ->assertSeeIn('@@success', trans('dashboard/account.two_factor.enabled')); |
| 69 | + |
| 70 | + $this->assertDatabaseHas('users', ['id' => $this->user->id, 'use_totp' => 1]); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test that a user can disable two-factor authentication on thier account. |
| 76 | + */ |
| 77 | + public function testTwoFactorCanBeDisabled() |
| 78 | + { |
| 79 | + $secret = (new Google2FA)->generateSecretKey(16); |
| 80 | + |
| 81 | + $this->user->update([ |
| 82 | + 'use_totp' => true, |
| 83 | + 'totp_secret' => Crypt::encrypt($secret), |
| 84 | + ]); |
| 85 | + |
| 86 | + $this->browse(function (PterodactylBrowser $browser) use ($secret) { |
| 87 | + $browser->loginAs($this->user) |
| 88 | + ->visit(new AccountPage) |
| 89 | + ->click('@2fa_button') |
| 90 | + ->waitForText(trans('dashboard/account.two_factor.disable.title')) |
| 91 | + ->click('@2fa_cancel') |
| 92 | + ->waitUntilMissing('@2fa_modal') |
| 93 | + ->click('@2fa_button') |
| 94 | + ->waitForText(trans('dashboard/account.two_factor.disable.title')) |
| 95 | + ->assertVisible('@2fa_disable[disabled="disabled"]') |
| 96 | + ->assertVisible('@2fa_cancel') |
| 97 | + ->assertFocused('@2fa_token_disable') |
| 98 | + ->assertMissing('@2fa_token_disable ~ .input-help.error') |
| 99 | + ->type('@2fa_token_disable', '12') |
| 100 | + ->assertSeeIn('@2fa_token_disable ~ .input-help.error', 'The token length must be 6.'); |
| 101 | + |
| 102 | + $token = (new Google2FA())->getCurrentOtp($secret); |
| 103 | + |
| 104 | + $browser->type('@2fa_token_disable', $token) |
| 105 | + ->assertMissing('@2fa_token_disable ~ .input-help.error') |
| 106 | + ->click('@2fa_disable') |
| 107 | + ->waitUntilMissing('@2fa_modal') |
| 108 | + ->assertSeeIn('@@success', trans('dashboard/account.two_factor.disabled')); |
| 109 | + }); |
| 110 | + } |
| 111 | +} |
0 commit comments