Skip to content

Commit 133fd17

Browse files
committed
Fix subuser unit tests
1 parent 54228e8 commit 133fd17

File tree

3 files changed

+56
-88
lines changed

3 files changed

+56
-88
lines changed

app/Services/Subusers/SubuserDeletionService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ class SubuserDeletionService
1919
/**
2020
* @var \Illuminate\Database\ConnectionInterface
2121
*/
22-
protected $connection;
22+
private $connection;
2323

2424
/**
2525
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService
2626
*/
27-
protected $keyDeletionService;
27+
private $keyDeletionService;
2828

2929
/**
3030
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface
3131
*/
32-
protected $repository;
32+
private $repository;
3333

3434
/**
3535
* SubuserDeletionService constructor.

tests/Unit/Services/Subusers/SubuserDeletionServiceTest.php

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,17 @@ class SubuserDeletionServiceTest extends TestCase
2222
/**
2323
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
2424
*/
25-
protected $connection;
25+
private $connection;
2626

2727
/**
2828
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService|\Mockery\Mock
2929
*/
30-
protected $keyDeletionService;
30+
private $keyDeletionService;
3131

3232
/**
3333
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
3434
*/
35-
protected $repository;
36-
37-
/**
38-
* @var \Pterodactyl\Services\Subusers\SubuserDeletionService
39-
*/
40-
protected $service;
35+
private $repository;
4136

4237
/**
4338
* Setup tests.
@@ -49,18 +44,12 @@ public function setUp()
4944
$this->connection = m::mock(ConnectionInterface::class);
5045
$this->keyDeletionService = m::mock(DaemonKeyDeletionService::class);
5146
$this->repository = m::mock(SubuserRepositoryInterface::class);
52-
53-
$this->service = new SubuserDeletionService(
54-
$this->connection,
55-
$this->keyDeletionService,
56-
$this->repository
57-
);
5847
}
5948

6049
/**
6150
* Test that a subuser is deleted correctly.
6251
*/
63-
public function testSubuserIsDeletedIfModelIsPassed()
52+
public function testSubuserIsDeleted()
6453
{
6554
$subuser = factory(Subuser::class)->make();
6655

@@ -69,24 +58,17 @@ public function testSubuserIsDeletedIfModelIsPassed()
6958
$this->repository->shouldReceive('delete')->with($subuser->id)->once()->andReturnNull();
7059
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
7160

72-
$this->service->handle($subuser);
61+
$this->getService()->handle($subuser);
7362
$this->assertTrue(true);
7463
}
7564

7665
/**
77-
* Test that a subuser is deleted correctly if only the subuser ID is passed.
66+
* Return an instance of the service with mocked dependencies for testing.
67+
*
68+
* @return \Pterodactyl\Services\Subusers\SubuserDeletionService
7869
*/
79-
public function testSubuserIsDeletedIfIdPassedInPlaceOfModel()
70+
private function getService(): SubuserDeletionService
8071
{
81-
$subuser = factory(Subuser::class)->make();
82-
83-
$this->repository->shouldReceive('find')->with($subuser->id)->once()->andReturn($subuser);
84-
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
85-
$this->keyDeletionService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id)->once()->andReturnNull();
86-
$this->repository->shouldReceive('delete')->with($subuser->id)->once()->andReturnNull();
87-
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
88-
89-
$this->service->handle($subuser->id);
90-
$this->assertTrue(true);
72+
return new SubuserDeletionService($this->connection, $this->keyDeletionService, $this->repository);
9173
}
9274
}

tests/Unit/Services/Subusers/SubuserUpdateServiceTest.php

Lines changed: 43 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,33 @@
1111

1212
use Mockery as m;
1313
use Tests\TestCase;
14-
use Illuminate\Log\Writer;
1514
use Pterodactyl\Models\Server;
1615
use Pterodactyl\Models\Subuser;
16+
use Tests\Traits\MocksRequestException;
1717
use GuzzleHttp\Exception\RequestException;
1818
use Illuminate\Database\ConnectionInterface;
19-
use Pterodactyl\Exceptions\DisplayException;
2019
use Pterodactyl\Exceptions\PterodactylException;
2120
use Pterodactyl\Services\Subusers\SubuserUpdateService;
2221
use Pterodactyl\Services\Subusers\PermissionCreationService;
2322
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
2423
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
2524
use Pterodactyl\Contracts\Repository\PermissionRepositoryInterface;
25+
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
2626
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
2727

2828
class SubuserUpdateServiceTest extends TestCase
2929
{
30+
use MocksRequestException;
31+
3032
/**
3133
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
3234
*/
33-
protected $connection;
35+
private $connection;
3436

3537
/**
3638
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
3739
*/
38-
protected $daemonRepository;
39-
40-
/**
41-
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
42-
*/
43-
protected $exception;
40+
private $daemonRepository;
4441

4542
/**
4643
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock
@@ -50,27 +47,17 @@ class SubuserUpdateServiceTest extends TestCase
5047
/**
5148
* @var \Pterodactyl\Contracts\Repository\PermissionRepositoryInterface|\Mockery\Mock
5249
*/
53-
protected $permissionRepository;
50+
private $permissionRepository;
5451

5552
/**
5653
* @var \Pterodactyl\Services\Subusers\PermissionCreationService|\Mockery\Mock
5754
*/
58-
protected $permissionService;
55+
private $permissionService;
5956

6057
/**
6158
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
6259
*/
63-
protected $repository;
64-
65-
/**
66-
* @var \Pterodactyl\Services\Subusers\SubuserUpdateService
67-
*/
68-
protected $service;
69-
70-
/**
71-
* @var \Illuminate\Log\Writer|\Mockery\Mock
72-
*/
73-
protected $writer;
60+
private $repository;
7461

7562
/**
7663
* Setup tests.
@@ -81,22 +68,10 @@ public function setUp()
8168

8269
$this->connection = m::mock(ConnectionInterface::class);
8370
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
84-
$this->exception = m::mock(RequestException::class);
8571
$this->keyProviderService = m::mock(DaemonKeyProviderService::class);
8672
$this->permissionRepository = m::mock(PermissionRepositoryInterface::class);
8773
$this->permissionService = m::mock(PermissionCreationService::class);
8874
$this->repository = m::mock(SubuserRepositoryInterface::class);
89-
$this->writer = m::mock(Writer::class);
90-
91-
$this->service = new SubuserUpdateService(
92-
$this->connection,
93-
$this->keyProviderService,
94-
$this->daemonRepository,
95-
$this->permissionService,
96-
$this->permissionRepository,
97-
$this->repository,
98-
$this->writer
99-
);
10075
}
10176

10277
/**
@@ -105,22 +80,20 @@ public function setUp()
10580
public function testPermissionsAreUpdated()
10681
{
10782
$subuser = factory(Subuser::class)->make();
108-
$subuser->server = factory(Server::class)->make();
83+
$subuser->setRelation('server', factory(Server::class)->make());
10984

110-
$this->repository->shouldReceive('getWithServer')->with($subuser->id)->once()->andReturn($subuser);
85+
$this->repository->shouldReceive('getWithServer')->with($subuser)->once()->andReturn($subuser);
11186
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
112-
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])
113-
->once()->andReturnNull();
87+
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturnNull();
11488
$this->permissionService->shouldReceive('handle')->with($subuser->id, ['some-permission'])->once()->andReturnNull();
11589

116-
$this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false)
117-
->once()->andReturn('test123');
118-
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andReturnSelf()
119-
->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturnNull();
90+
$this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false)->once()->andReturn('test123');
91+
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andReturnSelf();
92+
$this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturnNull();
12093

12194
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
12295

123-
$this->service->handle($subuser->id, ['some-permission']);
96+
$this->getService()->handle($subuser, ['some-permission']);
12497
$this->assertTrue(true);
12598
}
12699

@@ -129,29 +102,42 @@ public function testPermissionsAreUpdated()
129102
*/
130103
public function testExceptionIsThrownIfDaemonConnectionFails()
131104
{
105+
$this->configureExceptionMock();
106+
132107
$subuser = factory(Subuser::class)->make();
133-
$subuser->server = factory(Server::class)->make();
108+
$subuser->setRelation('server', factory(Server::class)->make());
134109

135-
$this->repository->shouldReceive('getWithServer')->with($subuser->id)->once()->andReturn($subuser);
110+
$this->repository->shouldReceive('getWithServer')->with($subuser)->once()->andReturn($subuser);
136111
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
137-
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])
138-
->once()->andReturnNull();
112+
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturnNull();
139113
$this->permissionService->shouldReceive('handle')->with($subuser->id, [])->once()->andReturnNull();
140114

141-
$this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false)
142-
->once()->andReturn('test123');
143-
$this->daemonRepository->shouldReceive('setNode')->once()->andThrow($this->exception);
115+
$this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false)->once()->andReturn('test123');
116+
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andThrow($this->getExceptionMock());
144117
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
145-
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
146-
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
147118

148119
try {
149-
$this->service->handle($subuser->id, []);
120+
$this->getService()->handle($subuser, []);
150121
} catch (PterodactylException $exception) {
151-
$this->assertInstanceOf(DisplayException::class, $exception);
152-
$this->assertEquals(trans('exceptions.daemon_connection_failed', [
153-
'code' => 'E_CONN_REFUSED',
154-
]), $exception->getMessage());
122+
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
123+
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
155124
}
156125
}
126+
127+
/**
128+
* Return an instance of the service with mocked dependencies for testing.
129+
*
130+
* @return \Pterodactyl\Services\Subusers\SubuserUpdateService
131+
*/
132+
private function getService(): SubuserUpdateService
133+
{
134+
return new SubuserUpdateService(
135+
$this->connection,
136+
$this->keyProviderService,
137+
$this->daemonRepository,
138+
$this->permissionService,
139+
$this->permissionRepository,
140+
$this->repository
141+
);
142+
}
157143
}

0 commit comments

Comments
 (0)