forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetDefaultAllocationServiceTest.php
More file actions
156 lines (134 loc) · 5.9 KB
/
SetDefaultAllocationServiceTest.php
File metadata and controls
156 lines (134 loc) · 5.9 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
<?php
namespace Tests\Unit\Services\Allocations;
use Mockery as m;
use Tests\TestCase;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Allocation;
use Tests\Traits\MocksRequestException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Services\Allocations\SetDefaultAllocationService;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonRepositoryInterface;
class SetDefaultAllocationServiceTest extends TestCase
{
use MocksRequestException;
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
*/
private $daemonRepository;
/**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/
private $serverRepository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->daemonRepository = m::mock(DaemonRepositoryInterface::class);
$this->repository = m::mock(AllocationRepositoryInterface::class);
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
}
/**
* Test that an allocation can be updated.
*
* @dataProvider useModelDataProvider
*/
public function testAllocationIsUpdated(bool $useModel)
{
$allocations = factory(Allocation::class)->times(2)->make();
$model = factory(Server::class)->make();
if (! $useModel) {
$this->serverRepository->shouldReceive('find')->with(1234)->once()->andReturn($model);
}
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn($allocations);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
$this->serverRepository->shouldReceive('update')->with($model->id, [
'allocation_id' => $allocations->first()->id,
])->once()->andReturn(new Response);
$this->daemonRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('update')->with([
'build' => [
'default' => [
'ip' => $allocations->first()->ip,
'port' => $allocations->first()->port,
],
'ports|overwrite' => $allocations->groupBy('ip')->map(function ($item) {
return $item->pluck('port');
})->toArray(),
],
])->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->handle($useModel ? $model : 1234, $allocations->first()->id);
$this->assertNotEmpty($response);
$this->assertSame($allocations->first(), $response);
}
/**
* Test that an allocation that doesn't belong to a server throws an exception.
*
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\AllocationDoesNotBelongToServerException
*/
public function testAllocationNotBelongingToServerThrowsException()
{
$model = factory(Server::class)->make();
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn(collect());
$this->getService()->handle($model, 1234);
}
/**
* Test that an exception thrown by guzzle is handled properly.
*/
public function testExceptionThrownByGuzzleIsHandled()
{
$this->configureExceptionMock();
$allocation = factory(Allocation::class)->make();
$model = factory(Server::class)->make();
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn(collect([$allocation]));
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
$this->serverRepository->shouldReceive('update')->with($model->id, [
'allocation_id' => $allocation->id,
])->once()->andReturn(new Response);
$this->daemonRepository->shouldReceive('setServer->update')->once()->andThrow($this->getExceptionMock());
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try {
$this->getService()->handle($model, $allocation->id);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
}
}
/**
* Data provider to determine if a model should be passed or an int.
*
* @return array
*/
public function useModelDataProvider(): array
{
return [[false], [true]];
}
/**
* Return an instance of the service with mocked dependencies.
*
* @return \Pterodactyl\Services\Allocations\SetDefaultAllocationService
*/
private function getService(): SetDefaultAllocationService
{
return new SetDefaultAllocationService($this->repository, $this->connection, $this->daemonRepository, $this->serverRepository);
}
}