Skip to content

Commit dd456a4

Browse files
committed
Fix all currently failing tests
1 parent 3a8bea9 commit dd456a4

File tree

11 files changed

+175
-315
lines changed

11 files changed

+175
-315
lines changed

app/Services/Servers/DetailsModificationService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class DetailsModificationService
5454
/**
5555
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService
5656
*/
57-
private $keyDeletionService;
57+
protected $keyDeletionService;
5858

5959
/**
6060
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository

app/Services/Subusers/SubuserCreationService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,27 @@ class SubuserCreationService
7676
* SubuserCreationService constructor.
7777
*
7878
* @param \Illuminate\Database\ConnectionInterface $connection
79-
* @param \Pterodactyl\Services\Users\UserCreationService $userCreationService
8079
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService $keyCreationService
8180
* @param \Pterodactyl\Services\Subusers\PermissionCreationService $permissionService
8281
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $serverRepository
8382
* @param \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface $subuserRepository
83+
* @param \Pterodactyl\Services\Users\UserCreationService $userCreationService
8484
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
8585
*/
8686
public function __construct(
8787
ConnectionInterface $connection,
88-
UserCreationService $userCreationService,
8988
DaemonKeyCreationService $keyCreationService,
9089
PermissionCreationService $permissionService,
9190
ServerRepositoryInterface $serverRepository,
9291
SubuserRepositoryInterface $subuserRepository,
92+
UserCreationService $userCreationService,
9393
UserRepositoryInterface $userRepository
9494
) {
9595
$this->connection = $connection;
9696
$this->keyCreationService = $keyCreationService;
9797
$this->permissionService = $permissionService;
98-
$this->subuserRepository = $subuserRepository;
9998
$this->serverRepository = $serverRepository;
99+
$this->subuserRepository = $subuserRepository;
100100
$this->userRepository = $userRepository;
101101
$this->userCreationService = $userCreationService;
102102
}

app/Services/Subusers/SubuserDeletionService.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
namespace Pterodactyl\Services\Subusers;
2626

27+
use Pterodactyl\Models\Subuser;
2728
use Illuminate\Database\ConnectionInterface;
2829
use Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService;
2930
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
@@ -65,14 +66,16 @@ public function __construct(
6566
/**
6667
* Delete a subuser and their associated permissions from the Panel and Daemon.
6768
*
68-
* @param int $subuser
69+
* @param int|\Pterodactyl\Models\Subuser $subuser
6970
*
7071
* @throws \Pterodactyl\Exceptions\DisplayException
7172
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
7273
*/
7374
public function handle($subuser)
7475
{
75-
$subuser = $this->repository->getWithServer($subuser);
76+
if (! $subuser instanceof Subuser) {
77+
$subuser = $this->repository->find($subuser);
78+
}
7679

7780
$this->connection->beginTransaction();
7881
$this->keyDeletionService->handle($subuser->server_id, $subuser->user_id);

database/factories/ModelFactory.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
'cpu' => 0,
3131
'oom_disabled' => 0,
3232
'pack_id' => null,
33-
'daemonSecret' => $faker->uuid,
3433
'username' => $faker->userName,
3534
'sftp_password' => null,
3635
'installed' => 1,
@@ -153,7 +152,6 @@
153152
'id' => $faker->unique()->randomNumber(),
154153
'user_id' => $faker->randomNumber(),
155154
'server_id' => $faker->randomNumber(),
156-
'daemonSecret' => $faker->unique()->uuid,
157155
];
158156
});
159157

@@ -197,3 +195,13 @@
197195
'is_queued' => false,
198196
];
199197
});
198+
199+
$factory->define(Pterodactyl\Models\DaemonKey::class, function (Faker\Generator $faker) {
200+
return [
201+
'id' => $faker->unique()->randomNumber(),
202+
'server_id' => $faker->randomNumber(),
203+
'user_id' => $faker->randomNumber(),
204+
'secret' => 'i_' . str_random(40),
205+
'expires_at' => \Carbon\Carbon::now()->addMinutes(10)->toDateTimeString(),
206+
];
207+
});

tests/Unit/Http/Controllers/Base/IndexControllerTest.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,36 @@
3131
use Pterodactyl\Models\Server;
3232
use Tests\Assertions\ControllerAssertionsTrait;
3333
use Pterodactyl\Http\Controllers\Base\IndexController;
34-
use Pterodactyl\Services\Servers\ServerAccessHelperService;
34+
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
3535
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
3636
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
3737

3838
class IndexControllerTest extends TestCase
3939
{
4040
use ControllerAssertionsTrait;
4141

42-
/**
43-
* @var \Pterodactyl\Services\Servers\ServerAccessHelperService
44-
*/
45-
protected $access;
46-
4742
/**
4843
* @var \Pterodactyl\Http\Controllers\Base\IndexController
4944
*/
5045
protected $controller;
5146

5247
/**
53-
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
48+
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
5449
*/
5550
protected $daemonRepository;
5651

5752
/**
58-
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
53+
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock
54+
*/
55+
protected $keyProviderService;
56+
57+
/**
58+
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
5959
*/
6060
protected $repository;
6161

6262
/**
63-
* @var \Illuminate\Http\Request
63+
* @var \Illuminate\Http\Request|\Mockery\Mock
6464
*/
6565
protected $request;
6666

@@ -71,12 +71,12 @@ public function setUp()
7171
{
7272
parent::setUp();
7373

74-
$this->access = m::mock(ServerAccessHelperService::class);
7574
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
75+
$this->keyProviderService = m::mock(DaemonKeyProviderService::class);
7676
$this->repository = m::mock(ServerRepositoryInterface::class);
7777
$this->request = m::mock(Request::class);
7878

79-
$this->controller = new IndexController($this->daemonRepository, $this->access, $this->repository);
79+
$this->controller = new IndexController($this->keyProviderService, $this->daemonRepository, $this->repository);
8080
}
8181

8282
/**
@@ -109,16 +109,17 @@ public function testStatusController()
109109
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
110110

111111
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
112-
$this->access->shouldReceive('handle')->with($server->uuid, $user)->once()->andReturn($server);
112+
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
113+
$this->keyProviderService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn('test123');
113114

114115
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
115116
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
116-
->shouldReceive('setAccessToken')->with($server->daemonSecret)->once()->andReturnSelf()
117+
->shouldReceive('setAccessToken')->with('test123')->once()->andReturnSelf()
117118
->shouldReceive('details')->withNoArgs()->once()->andReturnSelf();
118119

119120
$this->daemonRepository->shouldReceive('getBody')->withNoArgs()->once()->andReturn('["test"]');
120121

121-
$response = $this->controller->status($this->request, $server->uuid);
122+
$response = $this->controller->status($this->request, $server->uuidShort);
122123
$this->assertIsJsonResponse($response);
123124
$this->assertResponseJsonEquals(['test'], $response);
124125
}
@@ -132,9 +133,10 @@ public function testStatusControllerWhenServerNotInstalled()
132133
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 0]);
133134

134135
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
135-
$this->access->shouldReceive('handle')->with($server->uuid, $user)->once()->andReturn($server);
136+
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
137+
$this->keyProviderService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn('test123');
136138

137-
$response = $this->controller->status($this->request, $server->uuid);
139+
$response = $this->controller->status($this->request, $server->uuidShort);
138140
$this->assertIsJsonResponse($response);
139141
$this->assertResponseCodeEquals(200, $response);
140142
$this->assertResponseJsonEquals(['status' => 20], $response);
@@ -149,9 +151,10 @@ public function testStatusControllerWhenServerIsSuspended()
149151
$server = factory(Server::class)->make(['suspended' => 1, 'installed' => 1]);
150152

151153
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
152-
$this->access->shouldReceive('handle')->with($server->uuid, $user)->once()->andReturn($server);
154+
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
155+
$this->keyProviderService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn('test123');
153156

154-
$response = $this->controller->status($this->request, $server->uuid);
157+
$response = $this->controller->status($this->request, $server->uuidShort);
155158
$this->assertIsJsonResponse($response);
156159
$this->assertResponseCodeEquals(200, $response);
157160
$this->assertResponseJsonEquals(['status' => 30], $response);

tests/Unit/Services/Nodes/NodeUpdateServiceTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ class NodeUpdateServiceTest extends TestCase
4141
use PHPMock;
4242

4343
/**
44-
* @var \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface
44+
* @var \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface|\Mockery\Mock
4545
*/
4646
protected $configRepository;
4747

4848
/**
49-
* @var \GuzzleHttp\Exception\RequestException
49+
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
5050
*/
5151
protected $exception;
5252

@@ -56,7 +56,7 @@ class NodeUpdateServiceTest extends TestCase
5656
protected $node;
5757

5858
/**
59-
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
59+
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
6060
*/
6161
protected $repository;
6262

@@ -66,7 +66,7 @@ class NodeUpdateServiceTest extends TestCase
6666
protected $service;
6767

6868
/**
69-
* @var \Illuminate\Log\Writer
69+
* @var \Illuminate\Log\Writer|\Mockery\Mock
7070
*/
7171
protected $writer;
7272

@@ -106,7 +106,6 @@ public function testNodeIsUpdatedAndDaemonSecretIsReset()
106106
])->andReturn(true);
107107

108108
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
109-
->shouldReceive('setAccessToken')->with($this->node->daemonSecret)->once()->andReturnSelf()
110109
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
111110

112111
$this->assertTrue($this->service->handle($this->node, ['name' => 'NewName', 'reset_secret' => true]));
@@ -123,7 +122,6 @@ public function testNodeIsUpdatedAndDaemonSecretIsNotChanged()
123122
])->andReturn(true);
124123

125124
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
126-
->shouldReceive('setAccessToken')->with($this->node->daemonSecret)->once()->andReturnSelf()
127125
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
128126

129127
$this->assertTrue($this->service->handle($this->node, ['name' => 'NewName']));
@@ -167,7 +165,6 @@ public function testFunctionCanAcceptANodeIdInPlaceOfModel()
167165
])->andReturn(true);
168166

169167
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
170-
->shouldReceive('setAccessToken')->with($this->node->daemonSecret)->once()->andReturnSelf()
171168
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
172169

173170
$this->assertTrue($this->service->handle($this->node->id, ['name' => 'NewName']));

0 commit comments

Comments
 (0)