|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Tests\Unit\Commands\User; |
| 26 | + |
| 27 | +use Mockery as m; |
| 28 | +use Tests\TestCase; |
| 29 | +use Pterodactyl\Models\User; |
| 30 | +use Symfony\Component\Console\Tester\CommandTester; |
| 31 | +use Pterodactyl\Contracts\Repository\UserRepositoryInterface; |
| 32 | +use Pterodactyl\Console\Commands\User\DisableTwoFactorCommand; |
| 33 | + |
| 34 | +class DisableTwoFactorCommandTest extends TestCase |
| 35 | +{ |
| 36 | + /** |
| 37 | + * @var \Pterodactyl\Console\Commands\User\DisableTwoFactorCommand |
| 38 | + */ |
| 39 | + protected $command; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface |
| 43 | + */ |
| 44 | + protected $repository; |
| 45 | + |
| 46 | + public function setUp() |
| 47 | + { |
| 48 | + parent::setUp(); |
| 49 | + |
| 50 | + $this->repository = m::mock(UserRepositoryInterface::class); |
| 51 | + |
| 52 | + $this->command = new DisableTwoFactorCommand($this->repository); |
| 53 | + $this->command->setLaravel($this->app); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test 2-factor auth is disabled when no option is passed. |
| 58 | + */ |
| 59 | + public function testTwoFactorIsDisabledWhenNoOptionIsPassed() |
| 60 | + { |
| 61 | + $user = factory(User::class)->make(); |
| 62 | + |
| 63 | + $this->repository->shouldReceive('withColumns')->with(['id', 'email'])->once()->andReturnSelf() |
| 64 | + ->shouldReceive('findFirstWhere')->with([['email', '=', $user->email]])->once()->andReturn($user); |
| 65 | + $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() |
| 66 | + ->shouldReceive('update')->with($user->id, [ |
| 67 | + 'use_totp' => false, |
| 68 | + 'totp_secret' => null, |
| 69 | + ])->once()->andReturnNull(); |
| 70 | + |
| 71 | + $response = new CommandTester($this->command); |
| 72 | + $response->setInputs([$user->email]); |
| 73 | + $response->execute([]); |
| 74 | + |
| 75 | + $display = $response->getDisplay(); |
| 76 | + $this->assertNotEmpty($display); |
| 77 | + $this->assertContains(trans('command/messages.user.2fa_disabled', ['email' => $user->email]), $display); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Test 2-factor auth is disabled when user is passed in option. |
| 82 | + */ |
| 83 | + public function testTwoFactorIsDisabledWhenOptionIsPassed() |
| 84 | + { |
| 85 | + $user = factory(User::class)->make(); |
| 86 | + |
| 87 | + $this->repository->shouldReceive('withColumns')->with(['id', 'email'])->once()->andReturnSelf() |
| 88 | + ->shouldReceive('findFirstWhere')->with([['email', '=', $user->email]])->once()->andReturn($user); |
| 89 | + $this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf() |
| 90 | + ->shouldReceive('update')->with($user->id, [ |
| 91 | + 'use_totp' => false, |
| 92 | + 'totp_secret' => null, |
| 93 | + ])->once()->andReturnNull(); |
| 94 | + |
| 95 | + $response = new CommandTester($this->command); |
| 96 | + $response->execute([ |
| 97 | + '--email' => $user->email, |
| 98 | + ]); |
| 99 | + |
| 100 | + $display = $response->getDisplay(); |
| 101 | + $this->assertNotEmpty($display); |
| 102 | + $this->assertContains(trans('command/messages.user.2fa_disabled', ['email' => $user->email]), $display); |
| 103 | + } |
| 104 | +} |
0 commit comments