2017-08-12 21:30:27 +00:00
|
|
|
<?php
|
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
namespace Tests\Unit\Services\Eggs\Scripts;
|
2017-08-12 21:30:27 +00:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Models\Egg;
|
2017-10-08 04:29:08 +00:00
|
|
|
use Pterodactyl\Services\Eggs\Scripts\InstallScriptService;
|
2017-10-07 04:57:53 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
2017-10-08 04:29:08 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException;
|
2017-08-12 21:30:27 +00:00
|
|
|
|
2017-10-08 04:29:08 +00:00
|
|
|
class InstallScriptServiceTest extends TestCase
|
2017-08-12 21:30:27 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $data = [
|
|
|
|
'script_install' => 'test-script',
|
|
|
|
'script_is_privileged' => true,
|
|
|
|
'script_entry' => '/bin/bash',
|
|
|
|
'script_container' => 'ubuntu',
|
|
|
|
'copy_script_from' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2017-10-08 04:29:08 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
|
2017-08-12 21:30:27 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-08-12 21:30:27 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
$this->repository = m::mock(EggRepositoryInterface::class);
|
2017-08-12 21:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that passing a new copy_script_from attribute works properly.
|
|
|
|
*/
|
|
|
|
public function testUpdateWithValidCopyScriptFromAttribute()
|
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
$model = factory(Egg::class)->make(['id' => 123, 'nest_id' => 456]);
|
2017-08-12 21:30:27 +00:00
|
|
|
$this->data['copy_script_from'] = 1;
|
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->repository->shouldReceive('isCopyableScript')->with(1, $model->nest_id)->once()->andReturn(true);
|
|
|
|
$this->repository->expects('withoutFreshModel->update')->with($model->id, $this->data)->andReturnNull();
|
2017-08-12 21:30:27 +00:00
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->getService()->handle($model, $this->data);
|
2017-08-12 21:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-13 14:50:56 +00:00
|
|
|
* Test that an exception gets raised when the script is not copyable.
|
2017-08-12 21:30:27 +00:00
|
|
|
*/
|
|
|
|
public function testUpdateWithInvalidCopyScriptFromAttribute()
|
|
|
|
{
|
|
|
|
$this->data['copy_script_from'] = 1;
|
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->expectException(InvalidCopyFromException::class);
|
|
|
|
$this->expectExceptionMessage(trans('exceptions.nest.egg.invalid_copy_id'));
|
|
|
|
|
|
|
|
$model = factory(Egg::class)->make(['id' => 123, 'nest_id' => 456]);
|
|
|
|
|
|
|
|
$this->repository->expects('isCopyableScript')->with(1, $model->nest_id)->andReturn(false);
|
|
|
|
$this->getService()->handle($model, $this->data);
|
2017-08-12 21:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test standard functionality.
|
|
|
|
*/
|
|
|
|
public function testUpdateWithoutNewCopyScriptFromAttribute()
|
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
$model = factory(Egg::class)->make(['id' => 123, 'nest_id' => 456]);
|
|
|
|
|
|
|
|
$this->repository->expects('withoutFreshModel->update')->with($model->id, $this->data)->andReturnNull();
|
2017-08-12 21:30:27 +00:00
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
$this->getService()->handle($model, $this->data);
|
2017-08-12 21:30:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 04:29:35 +00:00
|
|
|
private function getService()
|
2017-08-12 21:30:27 +00:00
|
|
|
{
|
2020-10-06 04:29:35 +00:00
|
|
|
return new InstallScriptService($this->repository);
|
2017-08-12 21:30:27 +00:00
|
|
|
}
|
|
|
|
}
|