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;
|
|
|
|
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
2017-08-12 21:30:27 +00:00
|
|
|
use Pterodactyl\Services\Services\Options\InstallScriptUpdateService;
|
2017-08-19 03:19:06 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\ServiceOption\InvalidCopyFromException;
|
2017-08-12 21:30:27 +00:00
|
|
|
|
|
|
|
class InstallScriptUpdateServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @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-07 04:57:53 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
|
2017-08-12 21:30:27 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Services\Options\InstallScriptUpdateService
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
$this->service = new InstallScriptUpdateService($this->repository);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that passing a new copy_script_from attribute works properly.
|
|
|
|
*/
|
|
|
|
public function testUpdateWithValidCopyScriptFromAttribute()
|
|
|
|
{
|
|
|
|
$this->data['copy_script_from'] = 1;
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->service_id)->once()->andReturn(true);
|
|
|
|
$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;
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->service_id)->once()->andReturn(false);
|
|
|
|
try {
|
|
|
|
$this->service->handle($this->model, $this->data);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->assertInstanceOf(InvalidCopyFromException::class, $exception);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertEquals(trans('exceptions.service.options.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);
|
|
|
|
}
|
|
|
|
}
|