Skip to content

Commit e2cdb3b

Browse files
committed
Add test cases for services
1 parent 3ecab82 commit e2cdb3b

File tree

5 files changed

+246
-12
lines changed

5 files changed

+246
-12
lines changed

app/Services/Schedules/ScheduleUpdateService.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@ class ScheduleUpdateService
1515
* @var \Illuminate\Database\ConnectionInterface
1616
*/
1717
private $connection;
18+
1819
/**
1920
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
2021
*/
2122
private $repository;
22-
/**
23-
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
24-
*/
25-
private $taskRepository;
23+
2624
/**
2725
* @var \Pterodactyl\Services\Schedules\Tasks\TaskCreationService
2826
*/
2927
private $taskCreationService;
3028

29+
/**
30+
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
31+
*/
32+
private $taskRepository;
33+
3134
/**
3235
* ScheduleUpdateService constructor.
3336
*

app/Services/Servers/StartupCommandViewService.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ public function handle(int $server): Collection
3737
$server = $this->repository->getPrimaryAllocation($response->server);
3838

3939
$find = ['{{SERVER_MEMORY}}', '{{SERVER_IP}}', '{{SERVER_PORT}}'];
40-
$replace = [$server->memory, $server->allocation->ip, $server->allocation->port];
41-
42-
$variables = $server->egg->variables->each(function ($variable) use (&$find, &$replace, $response) {
43-
$find[] = '{{' . $variable->env_variable . '}}';
44-
$replace[] = $variable->user_viewable ? $response->data[$variable->env_variable] : '[hidden]';
45-
})->filter(function ($variable) {
46-
return $variable->user_viewable === 1;
47-
});
40+
$replace = [$server->memory, $server->getRelation('allocation')->ip, $server->getRelation('allocation')->port];
41+
42+
$variables = $server->getRelation('egg')->getRelation('variables')
43+
->each(function ($variable) use (&$find, &$replace, $response) {
44+
$find[] = '{{' . $variable->env_variable . '}}';
45+
$replace[] = $variable->user_viewable ? $response->data[$variable->env_variable] : '[hidden]';
46+
})->filter(function ($variable) {
47+
return $variable->user_viewable === 1;
48+
});
4849

4950
return collect([
5051
'startup' => str_replace($find, $replace, $server->startup),
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Tests\Unit\Services\Acl\Api;
4+
5+
use Tests\TestCase;
6+
use Pterodactyl\Models\ApiKey;
7+
use Pterodactyl\Services\Acl\Api\AdminAcl;
8+
9+
class AdminAclTest extends TestCase
10+
{
11+
/**
12+
* Test that permissions return the expects values.
13+
*
14+
* @dataProvider permissionsDataProvider
15+
*/
16+
public function testPermissions(int $permission, int $check, bool $outcome)
17+
{
18+
$this->assertSame($outcome, AdminAcl::can($permission, $check));
19+
}
20+
21+
/**
22+
* Test that checking aganist a model works as expected.
23+
*/
24+
public function testCheck()
25+
{
26+
$model = factory(ApiKey::class)->make(['r_servers' => AdminAcl::READ | AdminAcl::WRITE]);
27+
28+
$this->assertTrue(AdminAcl::check($model, AdminAcl::RESOURCE_SERVERS, AdminAcl::WRITE));
29+
}
30+
31+
/**
32+
* Provide valid and invalid permissions combos for testing.
33+
*
34+
* @return array
35+
*/
36+
public function permissionsDataProvider(): array
37+
{
38+
return [
39+
[AdminAcl::READ, AdminAcl::READ, true],
40+
[AdminAcl::READ | AdminAcl::WRITE, AdminAcl::READ, true],
41+
[AdminAcl::READ | AdminAcl::WRITE, AdminAcl::WRITE, true],
42+
[AdminAcl::WRITE, AdminAcl::WRITE, true],
43+
[AdminAcl::READ, AdminAcl::WRITE, false],
44+
[AdminAcl::NONE, AdminAcl::READ, false],
45+
[AdminAcl::NONE, AdminAcl::WRITE, false],
46+
];
47+
}
48+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Tests\Unit\Services\Schedules;
4+
5+
use Mockery as m;
6+
use Tests\TestCase;
7+
use Cron\CronExpression;
8+
use Pterodactyl\Models\Schedule;
9+
use Illuminate\Database\ConnectionInterface;
10+
use Pterodactyl\Services\Schedules\ScheduleUpdateService;
11+
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
12+
use Pterodactyl\Services\Schedules\Tasks\TaskCreationService;
13+
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
14+
15+
class ScheduleUpdateServiceTest extends TestCase
16+
{
17+
/**
18+
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
19+
*/
20+
private $connection;
21+
22+
/**
23+
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface|\Mockery\Mock
24+
*/
25+
private $repository;
26+
27+
/**
28+
* @var \Pterodactyl\Services\Schedules\Tasks\TaskCreationService|\Mockery\Mock
29+
*/
30+
private $taskCreationService;
31+
32+
/**
33+
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface|\Mockery\Mock
34+
*/
35+
private $taskRepository;
36+
37+
/**
38+
* Setup tests.
39+
*/
40+
public function setUp()
41+
{
42+
parent::setUp();
43+
44+
$this->connection = m::mock(ConnectionInterface::class);
45+
$this->repository = m::mock(ScheduleRepositoryInterface::class);
46+
$this->taskCreationService = m::mock(TaskCreationService::class);
47+
$this->taskRepository = m::mock(TaskRepositoryInterface::class);
48+
}
49+
50+
/**
51+
* Test that a schedule can be updated.
52+
*/
53+
public function testUpdate()
54+
{
55+
$schedule = factory(Schedule::class)->make();
56+
$tasks = [['action' => 'test-action']];
57+
$data = [
58+
'cron_minute' => 1,
59+
'cron_hour' => 2,
60+
'cron_day_of_month' => 3,
61+
'cron_day_of_week' => 4,
62+
'next_run_at' => '_INVALID_VALUE',
63+
];
64+
65+
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs();
66+
$this->repository->shouldReceive('update')->once()->with($schedule->id, array_merge($data, [
67+
'next_run_at' => CronExpression::factory('1 2 3 * 4 *')->getNextRunDate(),
68+
]))->andReturn($schedule);
69+
70+
$this->taskRepository->shouldReceive('deleteWhere')->once()->with([['schedule_id', '=', $schedule->id]]);
71+
$this->taskCreationService->shouldReceive('handle')->once()->with($schedule, m::subset([
72+
'sequence_id' => 1,
73+
'action' => 'test-action',
74+
]), false);
75+
76+
$this->connection->shouldReceive('commit')->once()->withNoArgs();
77+
78+
$response = $this->getService()->handle($schedule, $data, $tasks);
79+
$this->assertInstanceOf(Schedule::class, $response);
80+
$this->assertSame($schedule, $response);
81+
}
82+
83+
/**
84+
* Return an instance of the service with mocked dependencies.
85+
*
86+
* @return \Pterodactyl\Services\Schedules\ScheduleUpdateService
87+
*/
88+
private function getService(): ScheduleUpdateService
89+
{
90+
return new ScheduleUpdateService(
91+
$this->connection,
92+
$this->repository,
93+
$this->taskCreationService,
94+
$this->taskRepository
95+
);
96+
}
97+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Tests\Unit\Services\Servers;
4+
5+
use Mockery as m;
6+
use Tests\TestCase;
7+
use Pterodactyl\Models\Egg;
8+
use Pterodactyl\Models\Server;
9+
use Illuminate\Support\Collection;
10+
use Pterodactyl\Models\Allocation;
11+
use Pterodactyl\Models\EggVariable;
12+
use Pterodactyl\Services\Servers\StartupCommandViewService;
13+
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
14+
15+
class StartupCommandViewServiceTest extends TestCase
16+
{
17+
/**
18+
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
19+
*/
20+
private $repository;
21+
22+
/**
23+
* Setup tests.
24+
*/
25+
public function setUp()
26+
{
27+
parent::setUp();
28+
29+
$this->repository = m::mock(ServerRepositoryInterface::class);
30+
}
31+
32+
/**
33+
* Test that the correct startup string is returned.
34+
*/
35+
public function testServiceResponse()
36+
{
37+
$allocation = factory(Allocation::class)->make();
38+
$egg = factory(Egg::class)->make();
39+
$server = factory(Server::class)->make([
40+
'startup' => 'example {{SERVER_MEMORY}} {{SERVER_IP}} {{SERVER_PORT}} {{TEST_VARIABLE}} {{TEST_VARIABLE_HIDDEN}} {{UNKNOWN}}',
41+
]);
42+
43+
$variables = collect([
44+
factory(EggVariable::class)->make(['env_variable' => 'TEST_VARIABLE', 'user_viewable' => 1]),
45+
factory(EggVariable::class)->make(['env_variable' => 'TEST_VARIABLE_HIDDEN', 'user_viewable' => 0]),
46+
]);
47+
48+
$egg->setRelation('variables', $variables);
49+
$server->setRelation('allocation', $allocation);
50+
$server->setRelation('egg', $egg);
51+
52+
$this->repository->shouldReceive('getVariablesWithValues')->once()->with($server->id, true)->andReturn((object) [
53+
'data' => [
54+
'TEST_VARIABLE' => 'Test Value',
55+
'TEST_VARIABLE_HIDDEN' => 'Hidden Value',
56+
],
57+
'server' => $server,
58+
]);
59+
60+
$this->repository->shouldReceive('getPrimaryAllocation')->once()->with($server)->andReturn($server);
61+
62+
$response = $this->getService()->handle($server->id);
63+
$this->assertInstanceOf(Collection::class, $response);
64+
65+
$this->assertSame(
66+
sprintf('example %s %s %s %s %s {{UNKNOWN}}', $server->memory, $allocation->ip, $allocation->port, 'Test Value', '[hidden]'),
67+
$response->get('startup')
68+
);
69+
$this->assertEquals($variables->only(0), $response->get('variables'));
70+
$this->assertSame([
71+
'TEST_VARIABLE' => 'Test Value',
72+
'TEST_VARIABLE_HIDDEN' => 'Hidden Value',
73+
], $response->get('server_values'));
74+
}
75+
76+
/**
77+
* Return an instance of the service with mocked dependencies.
78+
*
79+
* @return \Pterodactyl\Services\Servers\StartupCommandViewService
80+
*/
81+
private function getService(): StartupCommandViewService
82+
{
83+
return new StartupCommandViewService($this->repository);
84+
}
85+
}

0 commit comments

Comments
 (0)