|
| 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 Pterodactyl\Services\Users\UserCreationService; |
| 31 | +use Symfony\Component\Console\Tester\CommandTester; |
| 32 | + |
| 33 | +class MakeUserCommandTest extends TestCase |
| 34 | +{ |
| 35 | + /** |
| 36 | + * @var \Pterodactyl\Console\Commands\User\MakeUserCommand |
| 37 | + */ |
| 38 | + protected $command; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Pterodactyl\Services\Users\UserCreationService |
| 42 | + */ |
| 43 | + protected $creationService; |
| 44 | + |
| 45 | + /** |
| 46 | + * Setup tests. |
| 47 | + */ |
| 48 | + public function setUp() |
| 49 | + { |
| 50 | + parent::setUp(); |
| 51 | + |
| 52 | + $this->creationService = m::mock(UserCreationService::class); |
| 53 | + |
| 54 | + $this->command = m::mock('\Pterodactyl\Console\Commands\User\MakeUserCommand[confirm, ask, secret]', [$this->creationService]); |
| 55 | + $this->command->setLaravel($this->app); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test that the command executes if no options are passed. |
| 60 | + */ |
| 61 | + public function testCommandWithNoPassedOptions() |
| 62 | + { |
| 63 | + $user = factory(User::class)->make(['root_admin' => true]); |
| 64 | + |
| 65 | + $this->command->shouldReceive('confirm')->with(trans('command/messages.user.ask_admin'))->once()->andReturn($user->root_admin); |
| 66 | + $this->command->shouldReceive('ask')->with(trans('command/messages.user.ask_email'))->once()->andReturn($user->email); |
| 67 | + $this->command->shouldReceive('ask')->with(trans('command/messages.user.ask_username'))->once()->andReturn($user->username); |
| 68 | + $this->command->shouldReceive('ask')->with(trans('command/messages.user.ask_name_first'))->once()->andReturn($user->name_first); |
| 69 | + $this->command->shouldReceive('ask')->with(trans('command/messages.user.ask_name_last'))->once()->andReturn($user->name_last); |
| 70 | + $this->command->shouldReceive('secret')->with(trans('command/messages.user.ask_password'))->once()->andReturn('Password123'); |
| 71 | + |
| 72 | + $this->creationService->shouldReceive('handle')->with([ |
| 73 | + 'email' => $user->email, |
| 74 | + 'username' => $user->username, |
| 75 | + 'name_first' => $user->name_first, |
| 76 | + 'name_last' => $user->name_last, |
| 77 | + 'password' => 'Password123', |
| 78 | + 'root_admin' => $user->root_admin, |
| 79 | + ])->once()->andReturn($user); |
| 80 | + |
| 81 | + $response = new CommandTester($this->command); |
| 82 | + $response->execute([]); |
| 83 | + |
| 84 | + $display = $response->getDisplay(); |
| 85 | + $this->assertNotEmpty($display); |
| 86 | + $this->assertContains(trans('command/messages.user.ask_password_help'), $display); |
| 87 | + $this->assertContains($user->uuid, $display); |
| 88 | + $this->assertContains($user->email, $display); |
| 89 | + $this->assertContains($user->username, $display); |
| 90 | + $this->assertContains($user->name, $display); |
| 91 | + $this->assertContains('Yes', $display); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Test that the --no-password flag works as intended. |
| 96 | + */ |
| 97 | + public function testCommandWithNoPasswordOption() |
| 98 | + { |
| 99 | + $user = factory(User::class)->make(['root_admin' => true]); |
| 100 | + |
| 101 | + $this->command->shouldReceive('confirm')->with(trans('command/messages.user.ask_admin'))->once()->andReturn($user->root_admin); |
| 102 | + $this->command->shouldReceive('ask')->with(trans('command/messages.user.ask_email'))->once()->andReturn($user->email); |
| 103 | + $this->command->shouldReceive('ask')->with(trans('command/messages.user.ask_username'))->once()->andReturn($user->username); |
| 104 | + $this->command->shouldReceive('ask')->with(trans('command/messages.user.ask_name_first'))->once()->andReturn($user->name_first); |
| 105 | + $this->command->shouldReceive('ask')->with(trans('command/messages.user.ask_name_last'))->once()->andReturn($user->name_last); |
| 106 | + $this->command->shouldNotReceive('secret'); |
| 107 | + |
| 108 | + $this->creationService->shouldReceive('handle')->with([ |
| 109 | + 'email' => $user->email, |
| 110 | + 'username' => $user->username, |
| 111 | + 'name_first' => $user->name_first, |
| 112 | + 'name_last' => $user->name_last, |
| 113 | + 'password' => null, |
| 114 | + 'root_admin' => $user->root_admin, |
| 115 | + ])->once()->andReturn($user); |
| 116 | + |
| 117 | + $response = new CommandTester($this->command); |
| 118 | + $response->execute(['--no-password' => true]); |
| 119 | + |
| 120 | + $display = $response->getDisplay(); |
| 121 | + $this->assertNotEmpty($display); |
| 122 | + $this->assertNotContains(trans('command/messages.user.ask_password_help'), $display); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test command when arguments are passed as flags. |
| 127 | + */ |
| 128 | + public function testCommandWithOptionsPassed() |
| 129 | + { |
| 130 | + $user = factory(User::class)->make(['root_admin' => false]); |
| 131 | + |
| 132 | + $this->command->shouldNotReceive('confirm'); |
| 133 | + $this->command->shouldNotReceive('ask'); |
| 134 | + $this->command->shouldNotReceive('secret'); |
| 135 | + |
| 136 | + $this->creationService->shouldReceive('handle')->with([ |
| 137 | + 'email' => $user->email, |
| 138 | + 'username' => $user->username, |
| 139 | + 'name_first' => $user->name_first, |
| 140 | + 'name_last' => $user->name_last, |
| 141 | + 'password' => 'Password123', |
| 142 | + 'root_admin' => $user->root_admin, |
| 143 | + ])->once()->andReturn($user); |
| 144 | + |
| 145 | + $response = new CommandTester($this->command); |
| 146 | + $response->execute([ |
| 147 | + '--email' => $user->email, |
| 148 | + '--username' => $user->username, |
| 149 | + '--name-first' => $user->name_first, |
| 150 | + '--name-last' => $user->name_last, |
| 151 | + '--password' => 'Password123', |
| 152 | + '--admin' => 0, |
| 153 | + ]); |
| 154 | + |
| 155 | + $display = $response->getDisplay(); |
| 156 | + $this->assertNotEmpty($display); |
| 157 | + $this->assertNotContains(trans('command/messages.user.ask_password_help'), $display); |
| 158 | + $this->assertContains($user->uuid, $display); |
| 159 | + $this->assertContains($user->email, $display); |
| 160 | + $this->assertContains($user->username, $display); |
| 161 | + $this->assertContains($user->name, $display); |
| 162 | + $this->assertContains('No', $display); |
| 163 | + } |
| 164 | +} |
0 commit comments