Skip to content

Commit 22459a8

Browse files
committed
Add test for make user command and add admin option
1 parent 542d1f8 commit 22459a8

File tree

4 files changed

+178
-2
lines changed

4 files changed

+178
-2
lines changed

app/Console/Commands/User/DeleteUserCommand.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ class DeleteUserCommand extends Command
5151
*/
5252
protected $signature = 'p:user:delete {--user=}';
5353

54+
/**
55+
* DeleteUserCommand constructor.
56+
*
57+
* @param \Pterodactyl\Services\Users\UserDeletionService $deletionService
58+
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
59+
*/
5460
public function __construct(
5561
UserDeletionService $deletionService,
5662
UserRepositoryInterface $repository

app/Console/Commands/User/MakeUserCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ class MakeUserCommand extends Command
3434
*/
3535
protected $creationService;
3636

37-
protected $signature = 'p:user:make {--email=} {--username=} {--name-first=} {--name-last=} {--password=} {--no-password}';
37+
/**
38+
* @var string
39+
*/
40+
protected $signature = 'p:user:make {--email=} {--username=} {--name-first=} {--name-last=} {--password=} {--admin=} {--no-password}';
3841

3942
/**
4043
* MakeUserCommand constructor.
@@ -56,6 +59,7 @@ public function __construct(UserCreationService $creationService)
5659
*/
5760
public function handle()
5861
{
62+
$root_admin = $this->option('admin') ?? $this->confirm(trans('command/messages.user.ask_admin'));
5963
$email = $this->option('email') ?? $this->ask(trans('command/messages.user.ask_email'));
6064
$username = $this->option('username') ?? $this->ask(trans('command/messages.user.ask_username'));
6165
$name_first = $this->option('name-first') ?? $this->ask(trans('command/messages.user.ask_name_first'));
@@ -67,12 +71,13 @@ public function handle()
6771
$password = $this->secret(trans('command/messages.user.ask_password'));
6872
}
6973

70-
$user = $this->creationService->handle(compact('email', 'username', 'name_first', 'name_last', 'password'));
74+
$user = $this->creationService->handle(compact('email', 'username', 'name_first', 'name_last', 'password', 'root_admin'));
7175
$this->table(['Field', 'Value'], [
7276
['UUID', $user->uuid],
7377
['Email', $user->email],
7478
['Username', $user->username],
7579
['Name', $user->name],
80+
['Admin', $user->root_admin ? 'Yes' : 'No'],
7681
]);
7782
}
7883
}

resources/lang/en/command/messages.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
'confirm_delete' => 'Are you sure you want to delete this user from the Panel?',
3838
'no_users_found' => 'No users were found for the search term provided.',
3939
'multiple_found' => 'Multiple accounts were found for the user provided, unable to delete a user because of the --no-interaction flag.',
40+
'ask_admin' => 'Is this user an administrator?',
4041
'ask_email' => 'Email Address',
4142
'ask_username' => 'Username',
4243
'ask_name_first' => 'First Name',
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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

Comments
 (0)