misc_pterodactyl-panel/tests/Unit/Services/Api/KeyCreationServiceTest.php

168 lines
5.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Pterodactyl\Tests\Unit\Services\Api;
use Mockery as m;
use phpmock\phpunit\PHPMock;
2018-01-14 18:06:15 +00:00
use Pterodactyl\Models\ApiKey;
use Pterodactyl\Tests\TestCase;
2018-01-13 22:23:43 +00:00
use Illuminate\Contracts\Encryption\Encrypter;
2017-08-31 02:14:20 +00:00
use Pterodactyl\Services\Api\KeyCreationService;
2017-08-05 22:26:30 +00:00
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
class KeyCreationServiceTest extends TestCase
{
use PHPMock;
/**
2018-01-13 22:23:43 +00:00
* @var \Illuminate\Contracts\Encryption\Encrypter|\Mockery\Mock
*/
2018-01-13 22:23:43 +00:00
private $encrypter;
/**
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
2018-01-13 22:23:43 +00:00
$this->encrypter = m::mock(Encrypter::class);
$this->repository = m::mock(ApiKeyRepositoryInterface::class);
}
/**
* Test that the service is able to create a keypair and assign the correct permissions.
*/
public function testKeyIsCreated()
{
$model = ApiKey::factory()->make();
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
2018-01-13 22:23:43 +00:00
->expects($this->exactly(2))->willReturnCallback(function ($length) {
return 'str_' . $length;
});
2018-01-14 18:06:15 +00:00
$this->encrypter->shouldReceive('encrypt')->with('str_' . ApiKey::KEY_LENGTH)->once()->andReturn($model->token);
$this->repository->shouldReceive('create')->with([
'test-data' => 'test',
'key_type' => ApiKey::TYPE_NONE,
2018-01-14 18:06:15 +00:00
'identifier' => 'str_' . ApiKey::IDENTIFIER_LENGTH,
'token' => $model->token,
], true, true)->once()->andReturn($model);
2018-01-13 22:23:43 +00:00
$response = $this->getService()->handle(['test-data' => 'test']);
$this->assertNotEmpty($response);
2018-01-14 18:06:15 +00:00
$this->assertInstanceOf(ApiKey::class, $response);
2018-01-13 22:23:43 +00:00
$this->assertSame($model, $response);
}
/**
* Test that an identifier is only set by the function.
*/
2018-01-13 22:23:43 +00:00
public function testIdentifierAndTokenAreOnlySetByFunction()
{
$model = ApiKey::factory()->make();
2018-01-13 22:23:43 +00:00
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
->expects($this->exactly(2))->willReturnCallback(function ($length) {
return 'str_' . $length;
});
2018-01-14 18:06:15 +00:00
$this->encrypter->shouldReceive('encrypt')->with('str_' . ApiKey::KEY_LENGTH)->once()->andReturn($model->token);
2018-01-13 22:23:43 +00:00
$this->repository->shouldReceive('create')->with([
'key_type' => ApiKey::TYPE_NONE,
2018-01-14 18:06:15 +00:00
'identifier' => 'str_' . ApiKey::IDENTIFIER_LENGTH,
2018-01-13 22:23:43 +00:00
'token' => $model->token,
], true, true)->once()->andReturn($model);
2018-01-13 22:23:43 +00:00
$response = $this->getService()->handle(['identifier' => 'customIdentifier', 'token' => 'customToken']);
$this->assertNotEmpty($response);
2018-01-14 18:06:15 +00:00
$this->assertInstanceOf(ApiKey::class, $response);
$this->assertSame($model, $response);
}
/**
* Test that permissions passed in are loaded onto the key data.
*/
public function testPermissionsAreRetrievedForApplicationKeys()
{
$model = ApiKey::factory()->make();
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
->expects($this->exactly(2))->willReturnCallback(function ($length) {
return 'str_' . $length;
});
$this->encrypter->shouldReceive('encrypt')->with('str_' . ApiKey::KEY_LENGTH)->once()->andReturn($model->token);
$this->repository->shouldReceive('create')->with([
'key_type' => ApiKey::TYPE_APPLICATION,
'identifier' => 'str_' . ApiKey::IDENTIFIER_LENGTH,
'token' => $model->token,
'permission-key' => 'exists',
], true, true)->once()->andReturn($model);
$response = $this->getService()->setKeyType(ApiKey::TYPE_APPLICATION)->handle([], ['permission-key' => 'exists']);
$this->assertNotEmpty($response);
$this->assertInstanceOf(ApiKey::class, $response);
$this->assertSame($model, $response);
}
/**
* Test that permissions are not retrieved for any key that is not an application key.
*
* @dataProvider keyTypeDataProvider
*/
public function testPermissionsAreNotRetrievedForNonApplicationKeys($keyType)
{
$model = ApiKey::factory()->make();
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
->expects($this->exactly(2))->willReturnCallback(function ($length) {
return 'str_' . $length;
});
$this->encrypter->shouldReceive('encrypt')->with('str_' . ApiKey::KEY_LENGTH)->once()->andReturn($model->token);
$this->repository->shouldReceive('create')->with([
'key_type' => $keyType,
'identifier' => 'str_' . ApiKey::IDENTIFIER_LENGTH,
'token' => $model->token,
], true, true)->once()->andReturn($model);
$response = $this->getService()->setKeyType($keyType)->handle([], ['fake-permission' => 'should-not-exist']);
$this->assertNotEmpty($response);
$this->assertInstanceOf(ApiKey::class, $response);
$this->assertSame($model, $response);
}
/**
* Provide key types that are not an application specific key.
*/
public function keyTypeDataProvider(): array
{
return [
[ApiKey::TYPE_NONE], [ApiKey::TYPE_ACCOUNT], [ApiKey::TYPE_DAEMON_USER], [ApiKey::TYPE_DAEMON_APPLICATION],
];
}
/**
* Return an instance of the service with mocked dependencies for testing.
*/
private function getService(): KeyCreationService
{
2018-01-13 22:23:43 +00:00
return new KeyCreationService($this->repository, $this->encrypter);
}
}