forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableUpdateServiceTest.php
More file actions
238 lines (204 loc) · 8.56 KB
/
Copy pathVariableUpdateServiceTest.php
File metadata and controls
238 lines (204 loc) · 8.56 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
namespace Tests\Unit\Services\Eggs\Variables;
use Exception;
use Mockery as m;
use Tests\TestCase;
use BadMethodCallException;
use Pterodactyl\Models\EggVariable;
use Illuminate\Contracts\Validation\Factory;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Eggs\Variables\VariableUpdateService;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
class VariableUpdateServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Models\EggVariable|\Mockery\Mock
*/
private $model;
/**
* @var \Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* @var \Illuminate\Contracts\Validation\Factory|\Mockery\Mock
*/
private $validator;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->model = factory(EggVariable::class)->make();
$this->repository = m::mock(EggVariableRepositoryInterface::class);
$this->validator = m::mock(Factory::class);
}
/**
* Test the function when no env_variable key is passed into the function.
*/
public function testVariableIsUpdatedWhenNoEnvironmentVariableIsPassed()
{
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, m::subset([
'user_viewable' => false,
'user_editable' => false,
]))->once()->andReturn(true);
$this->assertTrue($this->getService()->handle($this->model, []));
}
/**
* Test that a null value passed in for the default is converted to a string.
*
* @see https://github.com/Pterodactyl/Panel/issues/934
*/
public function testNullDefaultValue()
{
$this->repository->shouldReceive('withoutFreshModel->update')->with($this->model->id, m::subset([
'user_viewable' => false,
'user_editable' => false,
'default_value' => '',
]))->once()->andReturn(true);
$this->assertTrue($this->getService()->handle($this->model, ['default_value' => null]));
}
/**
* Test the function when a valid env_variable key is passed into the function.
*/
public function testVariableIsUpdatedWhenValidEnvironmentVariableIsPassed()
{
$this->repository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'],
['egg_id', '=', $this->model->option_id],
['id', '!=', $this->model->id],
])->once()->andReturn(0);
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, m::subset([
'user_viewable' => false,
'user_editable' => false,
'env_variable' => 'TEST_VAR_123',
]))->once()->andReturn(true);
$this->assertTrue($this->getService()->handle($this->model, ['env_variable' => 'TEST_VAR_123']));
}
/**
* Test that an empty (null) value passed in the option key is handled
* properly as an array. Also tests that a null description is handled.
*
* @see https://github.com/Pterodactyl/Panel/issues/841
*/
public function testNullOptionValueIsPassedAsArray()
{
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, m::subset([
'user_viewable' => false,
'user_editable' => false,
'description' => '',
]))->once()->andReturn(true);
$this->assertTrue($this->getService()->handle($this->model, ['options' => null, 'description' => null]));
}
/**
* Test that data passed into the handler is overwritten inside the handler.
*/
public function testDataPassedIntoHandlerTakesLowerPriorityThanDataSet()
{
$this->repository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'],
['egg_id', '=', $this->model->option_id],
['id', '!=', $this->model->id],
])->once()->andReturn(0);
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, m::subset([
'user_viewable' => false,
'user_editable' => false,
'env_variable' => 'TEST_VAR_123',
]))->once()->andReturn(true);
$this->assertTrue($this->getService()->handle($this->model, ['user_viewable' => 123456, 'env_variable' => 'TEST_VAR_123']));
}
/**
* Test that a non-unique environment variable triggers an exception.
*/
public function testExceptionIsThrownIfEnvironmentVariableIsNotUnique()
{
$this->repository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'],
['egg_id', '=', $this->model->option_id],
['id', '!=', $this->model->id],
])->once()->andReturn(1);
try {
$this->getService()->handle($this->model, ['env_variable' => 'TEST_VAR_123']);
} catch (Exception $exception) {
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(trans('exceptions.service.variables.env_not_unique', [
'name' => 'TEST_VAR_123',
]), $exception->getMessage());
}
}
/**
* Test that all of the reserved variables defined in the model trigger an exception.
*
* @dataProvider reservedNamesProvider
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\ReservedVariableNameException
*/
public function testExceptionIsThrownIfEnvironmentVariableIsInListOfReservedNames(string $variable)
{
$this->getService()->handle($this->model, ['env_variable' => $variable]);
}
/**
* Test that validation errors due to invalid rules are caught and handled properly.
*
* @expectedException \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
* @expectedExceptionMessage The validation rule "hodor_door" is not a valid rule for this application.
*/
public function testInvalidValidationRulesResultInException()
{
$data = ['env_variable' => 'TEST_VAR_123', 'rules' => 'string|hodorDoor'];
$this->repository->shouldReceive('setColumns->findCountWhere')->once()->andReturn(0);
$this->validator->shouldReceive('make')->once()
->with(['__TEST' => 'test'], ['__TEST' => 'string|hodorDoor'])
->andReturnSelf();
$this->validator->shouldReceive('fails')->once()
->withNoArgs()
->andThrow(new BadMethodCallException('Method [validateHodorDoor] does not exist.'));
$this->getService()->handle($this->model, $data);
}
/**
* Test that an exception not stemming from a bad rule is not caught.
*
* @expectedException \BadMethodCallException
* @expectedExceptionMessage Received something, but no expectations were specified.
*/
public function testExceptionNotCausedByBadRuleIsNotCaught()
{
$data = ['rules' => 'string'];
$this->validator->shouldReceive('make')->once()
->with(['__TEST' => 'test'], ['__TEST' => 'string'])
->andReturnSelf();
$this->validator->shouldReceive('fails')->once()
->withNoArgs()
->andThrow(new BadMethodCallException('Received something, but no expectations were specified.'));
$this->getService()->handle($this->model, $data);
}
/**
* Provides the data to be used in the tests.
*
* @return array
*/
public function reservedNamesProvider()
{
$data = [];
$exploded = explode(',', EggVariable::RESERVED_ENV_NAMES);
foreach ($exploded as $e) {
$data[] = [$e];
}
return $data;
}
/**
* Return an instance of the service with mocked dependencies for testing.
*
* @return \Pterodactyl\Services\Eggs\Variables\VariableUpdateService
*/
private function getService(): VariableUpdateService
{
return new VariableUpdateService($this->repository, $this->validator);
}
}