2017-07-08 19:07:51 +00:00
|
|
|
<?php
|
|
|
|
|
2017-07-09 17:29:18 +00:00
|
|
|
namespace Tests\Unit\Services\Api;
|
2017-07-08 19:07:51 +00:00
|
|
|
|
|
|
|
use Mockery as m;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Tests\TestCase;
|
2017-07-08 19:07:51 +00:00
|
|
|
use phpmock\phpunit\PHPMock;
|
2018-01-14 18:06:15 +00:00
|
|
|
use Pterodactyl\Models\ApiKey;
|
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;
|
2017-07-08 19:07:51 +00:00
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
class KeyCreationServiceTest extends TestCase
|
2017-07-08 19:07:51 +00:00
|
|
|
{
|
|
|
|
use PHPMock;
|
|
|
|
|
|
|
|
/**
|
2018-01-13 22:23:43 +00:00
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter|\Mockery\Mock
|
2017-07-08 19:07:51 +00:00
|
|
|
*/
|
2018-01-13 22:23:43 +00:00
|
|
|
private $encrypter;
|
2017-07-08 19:07:51 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-19 19:32:17 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface|\Mockery\Mock
|
2017-07-08 19:07:51 +00:00
|
|
|
*/
|
2017-11-19 19:32:17 +00:00
|
|
|
private $repository;
|
2017-07-08 19:07:51 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-19 19:32:17 +00:00
|
|
|
* Setup tests.
|
2017-07-08 19:07:51 +00:00
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-07-08 19:07:51 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-01-13 22:23:43 +00:00
|
|
|
$this->encrypter = m::mock(Encrypter::class);
|
2017-07-08 19:07:51 +00:00
|
|
|
$this->repository = m::mock(ApiKeyRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the service is able to create a keypair and assign the correct permissions.
|
|
|
|
*/
|
2017-08-31 02:11:14 +00:00
|
|
|
public function testKeyIsCreated()
|
2017-07-08 19:07:51 +00:00
|
|
|
{
|
2018-01-14 18:06:15 +00:00
|
|
|
$model = factory(ApiKey::class)->make();
|
2017-11-19 19:32:17 +00:00
|
|
|
|
2017-09-14 04:07:02 +00:00
|
|
|
$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;
|
|
|
|
});
|
2017-07-08 19:07:51 +00:00
|
|
|
|
2018-01-14 18:06:15 +00:00
|
|
|
$this->encrypter->shouldReceive('encrypt')->with('str_' . ApiKey::KEY_LENGTH)->once()->andReturn($model->token);
|
2017-07-08 19:07:51 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('create')->with([
|
|
|
|
'test-data' => 'test',
|
2018-01-14 19:30:55 +00:00
|
|
|
'key_type' => ApiKey::TYPE_NONE,
|
2018-01-14 18:06:15 +00:00
|
|
|
'identifier' => 'str_' . ApiKey::IDENTIFIER_LENGTH,
|
2017-11-19 19:32:17 +00:00
|
|
|
'token' => $model->token,
|
|
|
|
], true, true)->once()->andReturn($model);
|
2017-07-08 19:07:51 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:30:55 +00:00
|
|
|
/**
|
|
|
|
* Test that an identifier is only set by the function.
|
|
|
|
*/
|
2018-01-13 22:23:43 +00:00
|
|
|
public function testIdentifierAndTokenAreOnlySetByFunction()
|
|
|
|
{
|
2018-01-14 18:06:15 +00:00
|
|
|
$model = factory(ApiKey::class)->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;
|
|
|
|
});
|
2017-07-08 19:07:51 +00:00
|
|
|
|
2018-01-14 18:06:15 +00:00
|
|
|
$this->encrypter->shouldReceive('encrypt')->with('str_' . ApiKey::KEY_LENGTH)->once()->andReturn($model->token);
|
2017-07-08 19:07:51 +00:00
|
|
|
|
2018-01-13 22:23:43 +00:00
|
|
|
$this->repository->shouldReceive('create')->with([
|
2018-01-14 19:30:55 +00:00
|
|
|
'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);
|
2017-07-08 19:07:51 +00:00
|
|
|
|
2018-01-13 22:23:43 +00:00
|
|
|
$response = $this->getService()->handle(['identifier' => 'customIdentifier', 'token' => 'customToken']);
|
2017-07-08 19:07:51 +00:00
|
|
|
|
|
|
|
$this->assertNotEmpty($response);
|
2018-01-14 18:06:15 +00:00
|
|
|
$this->assertInstanceOf(ApiKey::class, $response);
|
2017-11-19 19:32:17 +00:00
|
|
|
$this->assertSame($model, $response);
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:30:55 +00:00
|
|
|
/**
|
|
|
|
* Test that permissions passed in are loaded onto the key data.
|
|
|
|
*/
|
|
|
|
public function testPermissionsAreRetrievedForApplicationKeys()
|
|
|
|
{
|
|
|
|
$model = factory(ApiKey::class)->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 = factory(ApiKey::class)->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.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function keyTypeDataProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[ApiKey::TYPE_NONE], [ApiKey::TYPE_ACCOUNT], [ApiKey::TYPE_DAEMON_USER], [ApiKey::TYPE_DAEMON_APPLICATION],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-11-19 19:32:17 +00:00
|
|
|
/**
|
|
|
|
* Return an instance of the service with mocked dependencies for testing.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Api\KeyCreationService
|
|
|
|
*/
|
|
|
|
private function getService(): KeyCreationService
|
|
|
|
{
|
2018-01-13 22:23:43 +00:00
|
|
|
return new KeyCreationService($this->repository, $this->encrypter);
|
2017-07-08 19:07:51 +00:00
|
|
|
}
|
|
|
|
}
|