2017-12-03 20:00:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\DaemonKeys;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
2018-01-06 00:27:47 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
2017-12-03 20:00:47 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2018-01-06 00:27:47 +00:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
2017-12-03 20:00:47 +00:00
|
|
|
use Pterodactyl\Models\DaemonKey;
|
|
|
|
use Tests\Traits\MocksRequestException;
|
|
|
|
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
|
|
|
use Pterodactyl\Services\DaemonKeys\RevokeMultipleDaemonKeysService;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
|
|
|
|
|
|
|
|
class RevokeMultipleDaemonKeysServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
use MocksRequestException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $daemonRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->daemonRepository = m::mock(ServerRepositoryInterface::class);
|
|
|
|
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that keys can be successfully revoked.
|
|
|
|
*/
|
|
|
|
public function testSuccessfulKeyRevocation()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->make();
|
2018-01-06 00:27:47 +00:00
|
|
|
$node = factory(Node::class)->make();
|
2017-12-03 20:00:47 +00:00
|
|
|
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
|
2018-01-06 00:27:47 +00:00
|
|
|
$key->setRelation('node', $node);
|
2017-12-03 20:00:47 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key]));
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonRepository->shouldReceive('setNode')->with($node)->once()->andReturnSelf();
|
|
|
|
$this->daemonRepository->shouldReceive('revokeAccessKey')->with([$key->secret])->once()->andReturn(new Response);
|
2017-12-03 20:00:47 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('deleteKeys')->with([$key->id])->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->getService()->handle($user);
|
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception thrown by a call to the daemon is handled.
|
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
|
|
*/
|
|
|
|
public function testExceptionThrownFromDaemonCallIsHandled()
|
|
|
|
{
|
|
|
|
$this->configureExceptionMock();
|
|
|
|
|
|
|
|
$user = factory(User::class)->make();
|
2018-01-06 00:27:47 +00:00
|
|
|
$node = factory(Node::class)->make();
|
2017-12-03 20:00:47 +00:00
|
|
|
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
|
2018-01-06 00:27:47 +00:00
|
|
|
$key->setRelation('node', $node);
|
2017-12-03 20:00:47 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key]));
|
|
|
|
$this->daemonRepository->shouldReceive('setNode->revokeAccessKey')->with([$key->secret])->once()->andThrow($this->getExceptionMock());
|
|
|
|
|
|
|
|
$this->getService()->handle($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the behavior for handling exceptions that should not be thrown
|
|
|
|
* immediately is working correctly and adds them to the array.
|
|
|
|
*/
|
|
|
|
public function testIgnoredExceptionsAreHandledProperly()
|
|
|
|
{
|
|
|
|
$this->configureExceptionMock();
|
|
|
|
|
|
|
|
$user = factory(User::class)->make();
|
2018-01-06 00:27:47 +00:00
|
|
|
$node = factory(Node::class)->make();
|
2017-12-03 20:00:47 +00:00
|
|
|
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
|
2018-01-06 00:27:47 +00:00
|
|
|
$key->setRelation('node', $node);
|
2017-12-03 20:00:47 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key]));
|
|
|
|
$this->daemonRepository->shouldReceive('setNode->revokeAccessKey')->with([$key->secret])->once()->andThrow($this->getExceptionMock());
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('deleteKeys')->with([$key->id])->once()->andReturnNull();
|
|
|
|
|
|
|
|
$service = $this->getService();
|
|
|
|
$service->handle($user, true);
|
|
|
|
$this->assertNotEmpty($service->getExceptions());
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->assertArrayHasKey($node->id, $service->getExceptions());
|
|
|
|
$this->assertSame(array_get($service->getExceptions(), $node->id), $this->getExceptionMock());
|
2017-12-03 20:00:47 +00:00
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the service for testing.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\DaemonKeys\RevokeMultipleDaemonKeysService
|
|
|
|
*/
|
|
|
|
private function getService(): RevokeMultipleDaemonKeysService
|
|
|
|
{
|
|
|
|
return new RevokeMultipleDaemonKeysService($this->repository, $this->daemonRepository);
|
|
|
|
}
|
|
|
|
}
|