forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeUpdateServiceTest.php
More file actions
138 lines (112 loc) · 4.98 KB
/
NodeUpdateServiceTest.php
File metadata and controls
138 lines (112 loc) · 4.98 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
<?php
namespace Tests\Unit\Services\Nodes;
use Mockery as m;
use Tests\TestCase;
use phpmock\phpunit\PHPMock;
use Pterodactyl\Models\Node;
use GuzzleHttp\Psr7\Response;
use Tests\Traits\MocksRequestException;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Nodes\NodeUpdateService;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
class NodeUpdateServiceTest extends TestCase
{
use PHPMock, MocksRequestException;
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
private $connection;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface|\Mockery\Mock
*/
private $configRepository;
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->configRepository = m::mock(ConfigurationRepositoryInterface::class);
$this->repository = m::mock(NodeRepositoryInterface::class);
}
/**
* Test that the daemon secret is reset when `reset_secret` is passed in the data.
*/
public function testNodeIsUpdatedAndDaemonSecretIsReset()
{
$model = factory(Node::class)->make();
$this->getFunctionMock('\\Pterodactyl\\Services\\Nodes', 'str_random')
->expects($this->once())->willReturn('random_string');
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('update')->with($model->id, [
'name' => 'NewName',
'daemonSecret' => 'random_string',
])->andReturn($model);
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->handle($model, ['name' => 'NewName', 'reset_secret' => true]);
$this->assertInstanceOf(Node::class, $response);
}
/**
* Test that daemon secret is not modified when no variable is passed in data.
*/
public function testNodeIsUpdatedAndDaemonSecretIsNotChanged()
{
$model = factory(Node::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('update')->with($model->id, [
'name' => 'NewName',
])->andReturn($model);
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->handle($model, ['name' => 'NewName']);
$this->assertInstanceOf(Node::class, $response);
}
/**
* Test that an exception caused by a connection error is handled.
*
* @expectedException \Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException
*/
public function testExceptionRelatedToConnection()
{
$this->configureExceptionMock(ConnectException::class);
$model = factory(Node::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('update')->andReturn($model);
$this->configRepository->shouldReceive('setNode->update')->once()->andThrow($this->getExceptionMock());
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->getService()->handle($model, ['name' => 'NewName']);
}
/**
* Test that an exception not caused by a daemon connection error is handled.
*
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function testExceptionNotRelatedToConnection()
{
$this->configureExceptionMock();
$model = factory(Node::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('update')->andReturn($model);
$this->configRepository->shouldReceive('setNode->update')->once()->andThrow($this->getExceptionMock());
$this->getService()->handle($model, ['name' => 'NewName']);
}
/**
* Return an instance of the service with mocked injections.
*
* @return \Pterodactyl\Services\Nodes\NodeUpdateService
*/
private function getService(): NodeUpdateService
{
return new NodeUpdateService($this->connection, $this->configRepository, $this->repository);
}
}