2017-08-12 21:30:27 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-08-12 21:30:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Services\Options;
|
|
|
|
|
|
|
|
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-07 04:57:53 +00:00
|
|
|
* @var \Pterodactyl\Models\Egg
|
2017-08-12 21:30:27 +00:00
|
|
|
*/
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
/**
|
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;
|
|
|
|
|
|
|
|
/**
|
2017-10-08 04:29:08 +00:00
|
|
|
* @var \Pterodactyl\Services\Eggs\Scripts\InstallScriptService
|
2017-08-12 21:30:27 +00:00
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-10-07 04:57:53 +00:00
|
|
|
$this->model = factory(Egg::class)->make();
|
|
|
|
$this->repository = m::mock(EggRepositoryInterface::class);
|
2017-08-12 21:30:27 +00:00
|
|
|
|
2017-10-07 21:16:51 +00:00
|
|
|
$this->service = new InstallScriptService($this->repository);
|
2017-08-12 21:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that passing a new copy_script_from attribute works properly.
|
|
|
|
*/
|
|
|
|
public function testUpdateWithValidCopyScriptFromAttribute()
|
|
|
|
{
|
|
|
|
$this->data['copy_script_from'] = 1;
|
|
|
|
|
2017-10-08 04:29:08 +00:00
|
|
|
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(true);
|
2017-08-12 21:30:27 +00:00
|
|
|
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
|
|
|
|
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
|
|
|
|
|
|
|
|
$this->service->handle($this->model, $this->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception gets raised when the script is not copiable.
|
|
|
|
*/
|
|
|
|
public function testUpdateWithInvalidCopyScriptFromAttribute()
|
|
|
|
{
|
|
|
|
$this->data['copy_script_from'] = 1;
|
|
|
|
|
2017-10-08 04:29:08 +00:00
|
|
|
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(false);
|
2017-08-12 21:30:27 +00:00
|
|
|
try {
|
|
|
|
$this->service->handle($this->model, $this->data);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->assertInstanceOf(InvalidCopyFromException::class, $exception);
|
2017-10-08 04:29:08 +00:00
|
|
|
$this->assertEquals(trans('exceptions.nest.egg.invalid_copy_id'), $exception->getMessage());
|
2017-08-12 21:30:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test standard functionality.
|
|
|
|
*/
|
|
|
|
public function testUpdateWithoutNewCopyScriptFromAttribute()
|
|
|
|
{
|
|
|
|
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
|
|
|
|
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
|
|
|
|
|
|
|
|
$this->service->handle($this->model, $this->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an integer can be passed in place of a model.
|
|
|
|
*/
|
|
|
|
public function testFunctionAcceptsIntegerInPlaceOfModel()
|
|
|
|
{
|
|
|
|
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
|
|
|
|
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
|
|
|
|
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
|
|
|
|
|
|
|
|
$this->service->handle($this->model->id, $this->data);
|
|
|
|
}
|
|
|
|
}
|