misc_pterodactyl-panel/tests/Unit/Services/Nodes/NodeCreationServiceTest.php

80 lines
2.3 KiB
PHP
Raw Normal View History

<?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;
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;
use Pterodactyl\Services\Nodes\NodeCreationService;
2017-08-05 22:26:30 +00:00
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
class NodeCreationServiceTest extends TestCase
{
use PHPMock;
/**
2020-06-25 04:54:56 +00:00
* @var \Mockery\MockInterface
*/
2020-06-25 04:54:56 +00:00
private $repository;
/**
2020-06-25 04:54:56 +00:00
* @var \Mockery\MockInterface
*/
2020-06-25 04:54:56 +00:00
private $encrypter;
/**
* Setup tests.
*/
public function setUp(): void
{
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'),
])
);
2020-06-25 04:54:56 +00:00
$this->repository = m::mock(NodeRepositoryInterface::class);
$this->encrypter = m::mock(Encrypter::class);
}
/**
* 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');
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);
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);
}
}