forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteUserCommandTest.php
More file actions
188 lines (152 loc) · 7.21 KB
/
DeleteUserCommandTest.php
File metadata and controls
188 lines (152 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Tests\Unit\Commands\User;
use Mockery as m;
use Pterodactyl\Models\User;
use Tests\Unit\Commands\CommandTestCase;
use Tests\Assertions\CommandAssertionsTrait;
use Pterodactyl\Services\Users\UserDeletionService;
use Pterodactyl\Console\Commands\User\DeleteUserCommand;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class DeleteUserCommandTest extends CommandTestCase
{
use CommandAssertionsTrait;
/**
* @var \Pterodactyl\Console\Commands\User\DeleteUserCommand
*/
protected $command;
/**
* @var \Pterodactyl\Services\Users\UserDeletionService|\Mockery\Mock
*/
protected $deletionService;
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
$this->deletionService = m::mock(UserDeletionService::class);
$this->repository = m::mock(UserRepositoryInterface::class);
$this->command = new DeleteUserCommand($this->deletionService, $this->repository);
$this->command->setLaravel($this->app);
}
/**
* Test that a user can be deleted using a normal pathway.
*/
public function testCommandWithNoOptions()
{
$users = collect([
$user1 = factory(User::class)->make(),
$user2 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
$this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull();
$display = $this->runCommand($this->command, [], [$user1->username, $user1->id, 'yes']);
$this->assertNotEmpty($display);
$this->assertTableContains($user1->id, $display);
$this->assertTableContains($user1->email, $display);
$this->assertTableContains($user1->name, $display);
$this->assertStringContainsString(trans('command/messages.user.deleted'), $display);
}
/**
* Test a bad first user search followed by a good second search.
*/
public function testCommandWithInvalidInitialSearch()
{
$users = collect([
$user1 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('setSearchTerm')->with('noResults')->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn(collect());
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
$this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull();
$display = $this->runCommand($this->command, [], ['noResults', $user1->username, $user1->id, 'yes']);
$this->assertNotEmpty($display);
$this->assertStringContainsString(trans('command/messages.user.no_users_found'), $display);
$this->assertTableContains($user1->id, $display);
$this->assertTableContains($user1->email, $display);
$this->assertTableContains($user1->name, $display);
$this->assertStringContainsString(trans('command/messages.user.deleted'), $display);
}
/**
* Test the ability to re-do a search for a user account.
*/
public function testReSearchAbility()
{
$users = collect([
$user1 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->twice()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->twice()->andReturn($users);
$this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull();
$display = $this->runCommand($this->command, [], [$user1->username, 0, $user1->username, $user1->id, 'yes']);
$this->assertNotEmpty($display);
$this->assertStringContainsString(trans('command/messages.user.select_search_user'), $display);
$this->assertTableContains($user1->id, $display);
$this->assertTableContains($user1->email, $display);
$this->assertTableContains($user1->name, $display);
$this->assertStringContainsString(trans('command/messages.user.deleted'), $display);
}
/**
* Test that answering no works as expected when confirming deletion of account.
*/
public function testAnsweringNoToDeletionConfirmationWillNotDeleteUser()
{
$users = collect([
$user1 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
$this->deletionService->shouldNotReceive('handle');
$display = $this->runCommand($this->command, [], [$user1->username, $user1->id, 'no']);
$this->assertNotEmpty($display);
$this->assertStringNotContainsString(trans('command/messages.user.deleted'), $display);
}
/**
* Test a single result is deleted if there is no interaction setup.
*/
public function testNoInteractionWithSingleResult()
{
$users = collect([
$user1 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
$this->deletionService->shouldReceive('handle')->with($user1)->once()->andReturnNull();
$display = $this->withoutInteraction()->runCommand($this->command, ['--user' => $user1->username]);
$this->assertNotEmpty($display);
$this->assertStringContainsString(trans('command/messages.user.deleted'), $display);
}
/**
* Test that an error is returned if there is no interaction but multiple results.
*/
public function testNoInteractionWithMultipleResults()
{
$users = collect([
$user1 = factory(User::class)->make(),
$user2 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
$this->deletionService->shouldNotReceive('handle');
$display = $this->withoutInteraction()->runCommand($this->command, ['--user' => $user1->username]);
$this->assertNotEmpty($display);
$this->assertStringContainsString(trans('command/messages.user.multiple_found'), $display);
}
/**
* Test that an error is returned if there is no interaction and no results returned.
*/
public function testNoInteractionWithNoResults()
{
$this->repository->shouldReceive('setSearchTerm')->with(123456)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn(collect());
$display = $this->withoutInteraction()->runCommand($this->command, ['--user' => 123456]);
$this->assertNotEmpty($display);
$this->assertStringContainsString(trans('command/messages.user.no_users_found'), $display);
}
}