2017-08-05 22:20:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Nodes;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Tests\TestCase;
|
2020-06-25 04:54:56 +00:00
|
|
|
use Ramsey\Uuid\Uuid;
|
2017-08-05 22:20:07 +00:00
|
|
|
use phpmock\phpunit\PHPMock;
|
2020-06-25 04:54:56 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
|
|
|
use Ramsey\Uuid\UuidFactory;
|
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2017-08-27 20:10:51 +00:00
|
|
|
use Pterodactyl\Services\Nodes\NodeCreationService;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class NodeCreationServiceTest extends TestCase
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
|
|
|
use PHPMock;
|
|
|
|
|
|
|
|
/**
|
2020-06-25 04:54:56 +00:00
|
|
|
* @var \Mockery\MockInterface
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2020-06-25 04:54:56 +00:00
|
|
|
private $repository;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-25 04:54:56 +00:00
|
|
|
* @var \Mockery\MockInterface
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2020-06-25 04:54:56 +00:00
|
|
|
private $encrypter;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2020-06-25 04:54:56 +00:00
|
|
|
/* @noinspection PhpParamsInspection */
|
|
|
|
Uuid::setFactory(
|
|
|
|
m::mock(UuidFactory::class . '[uuid4]', [
|
|
|
|
'uuid4' => Uuid::fromString('00000000-0000-0000-0000-000000000000'),
|
|
|
|
])
|
|
|
|
);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2020-06-25 04:54:56 +00:00
|
|
|
$this->repository = m::mock(NodeRepositoryInterface::class);
|
|
|
|
$this->encrypter = m::mock(Encrypter::class);
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a node is created and a daemon secret token is created.
|
|
|
|
*/
|
|
|
|
public function testNodeIsCreatedAndDaemonSecretIsGenerated()
|
|
|
|
{
|
2020-06-25 04:54:56 +00:00
|
|
|
/** @var \Pterodactyl\Models\Node $node */
|
|
|
|
$node = factory(Node::class)->make();
|
|
|
|
|
|
|
|
$this->encrypter->expects('encrypt')->with(m::on(function ($value) {
|
|
|
|
return strlen($value) === Node::DAEMON_TOKEN_LENGTH;
|
|
|
|
}))->andReturns('encrypted_value');
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2020-06-25 04:54:56 +00:00
|
|
|
$this->repository->expects('create')->with(m::on(function ($value) {
|
|
|
|
$this->assertTrue(is_array($value));
|
|
|
|
$this->assertSame('NodeName', $value['name']);
|
|
|
|
$this->assertSame('00000000-0000-0000-0000-000000000000', $value['uuid']);
|
|
|
|
$this->assertSame('encrypted_value', $value['daemon_token']);
|
|
|
|
$this->assertTrue(strlen($value['daemon_token_id']) === Node::DAEMON_TOKEN_ID_LENGTH);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2020-06-25 04:54:56 +00:00
|
|
|
return true;
|
|
|
|
}), true, true)->andReturn($node);
|
|
|
|
|
|
|
|
$this->assertSame($node, $this->getService()->handle(['name' => 'NodeName']));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Pterodactyl\Services\Nodes\NodeCreationService
|
|
|
|
*/
|
|
|
|
private function getService()
|
|
|
|
{
|
|
|
|
return new NodeCreationService($this->encrypter, $this->repository);
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
}
|