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;
|
2017-11-19 19:32:17 +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
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2017-11-19 19:32:17 +00:00
|
|
|
$model = factory(APIKey::class)->make();
|
|
|
|
|
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-13 22:23:43 +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-13 22:23:43 +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);
|
|
|
|
$this->assertInstanceOf(APIKey::class, $response);
|
|
|
|
$this->assertSame($model, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIdentifierAndTokenAreOnlySetByFunction()
|
|
|
|
{
|
|
|
|
$model = factory(APIKey::class)->make();
|
|
|
|
|
|
|
|
$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-13 22:23:43 +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([
|
|
|
|
'identifier' => 'str_' . APIKey::IDENTIFIER_LENGTH,
|
|
|
|
'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);
|
2017-11-19 19:32:17 +00:00
|
|
|
$this->assertInstanceOf(APIKey::class, $response);
|
|
|
|
$this->assertSame($model, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
}
|