Fix failing ApiKeyCreationService test
This commit is contained in:
parent
e3df0738da
commit
e0fb7fa30c
2 changed files with 35 additions and 50 deletions
|
@ -3,17 +3,11 @@
|
||||||
namespace Pterodactyl\Services\Api;
|
namespace Pterodactyl\Services\Api;
|
||||||
|
|
||||||
use Pterodactyl\Models\APIKey;
|
use Pterodactyl\Models\APIKey;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
|
||||||
use Illuminate\Contracts\Encryption\Encrypter;
|
use Illuminate\Contracts\Encryption\Encrypter;
|
||||||
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
||||||
|
|
||||||
class KeyCreationService
|
class KeyCreationService
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var \Illuminate\Database\ConnectionInterface
|
|
||||||
*/
|
|
||||||
private $connection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Illuminate\Contracts\Encryption\Encrypter
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
||||||
*/
|
*/
|
||||||
|
@ -28,17 +22,12 @@ class KeyCreationService
|
||||||
* ApiKeyService constructor.
|
* ApiKeyService constructor.
|
||||||
*
|
*
|
||||||
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
|
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
|
||||||
* @param \Illuminate\Database\ConnectionInterface $connection
|
|
||||||
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(ApiKeyRepositoryInterface $repository, Encrypter $encrypter)
|
||||||
ApiKeyRepositoryInterface $repository,
|
{
|
||||||
ConnectionInterface $connection,
|
|
||||||
Encrypter $encrypter
|
|
||||||
) {
|
|
||||||
$this->repository = $repository;
|
|
||||||
$this->connection = $connection;
|
|
||||||
$this->encrypter = $encrypter;
|
$this->encrypter = $encrypter;
|
||||||
|
$this->repository = $repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,7 +43,7 @@ class KeyCreationService
|
||||||
public function handle(array $data): APIKey
|
public function handle(array $data): APIKey
|
||||||
{
|
{
|
||||||
$data = array_merge($data, [
|
$data = array_merge($data, [
|
||||||
'identifer' => str_random(APIKey::IDENTIFIER_LENGTH),
|
'identifier' => str_random(APIKey::IDENTIFIER_LENGTH),
|
||||||
'token' => $this->encrypter->encrypt(str_random(APIKey::KEY_LENGTH)),
|
'token' => $this->encrypter->encrypt(str_random(APIKey::KEY_LENGTH)),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,4 @@
|
||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Pterodactyl - Panel
|
|
||||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
||||||
*
|
|
||||||
* This software is licensed under the terms of the MIT license.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Tests\Unit\Services\Api;
|
namespace Tests\Unit\Services\Api;
|
||||||
|
|
||||||
|
@ -13,8 +6,7 @@ use Mockery as m;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
use phpmock\phpunit\PHPMock;
|
use phpmock\phpunit\PHPMock;
|
||||||
use Pterodactyl\Models\APIKey;
|
use Pterodactyl\Models\APIKey;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Contracts\Encryption\Encrypter;
|
||||||
use Pterodactyl\Services\Api\PermissionService;
|
|
||||||
use Pterodactyl\Services\Api\KeyCreationService;
|
use Pterodactyl\Services\Api\KeyCreationService;
|
||||||
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
||||||
|
|
||||||
|
@ -23,14 +15,9 @@ class KeyCreationServiceTest extends TestCase
|
||||||
use PHPMock;
|
use PHPMock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
* @var \Illuminate\Contracts\Encryption\Encrypter|\Mockery\Mock
|
||||||
*/
|
*/
|
||||||
private $connection;
|
private $encrypter;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \Pterodactyl\Services\Api\PermissionService|\Mockery\Mock
|
|
||||||
*/
|
|
||||||
private $permissionService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface|\Mockery\Mock
|
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface|\Mockery\Mock
|
||||||
|
@ -44,8 +31,7 @@ class KeyCreationServiceTest extends TestCase
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->connection = m::mock(ConnectionInterface::class);
|
$this->encrypter = m::mock(Encrypter::class);
|
||||||
$this->permissionService = m::mock(PermissionService::class);
|
|
||||||
$this->repository = m::mock(ApiKeyRepositoryInterface::class);
|
$this->repository = m::mock(ApiKeyRepositoryInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,32 +43,42 @@ class KeyCreationServiceTest extends TestCase
|
||||||
$model = factory(APIKey::class)->make();
|
$model = factory(APIKey::class)->make();
|
||||||
|
|
||||||
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
|
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
|
||||||
->expects($this->exactly(1))->with(APIKey::KEY_LENGTH)->willReturn($model->token);
|
->expects($this->exactly(2))->willReturnCallback(function ($length) {
|
||||||
|
return 'str_' . $length;
|
||||||
|
});
|
||||||
|
|
||||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
$this->encrypter->shouldReceive('encrypt')->with('str_' . APIKey::KEY_LENGTH)->once()->andReturn($model->token);
|
||||||
|
|
||||||
$this->repository->shouldReceive('create')->with([
|
$this->repository->shouldReceive('create')->with([
|
||||||
'test-data' => 'test',
|
'test-data' => 'test',
|
||||||
|
'identifier' => 'str_' . APIKey::IDENTIFIER_LENGTH,
|
||||||
'token' => $model->token,
|
'token' => $model->token,
|
||||||
], true, true)->once()->andReturn($model);
|
], true, true)->once()->andReturn($model);
|
||||||
|
|
||||||
$this->permissionService->shouldReceive('getPermissions')->withNoArgs()->once()->andReturn([
|
$response = $this->getService()->handle(['test-data' => 'test']);
|
||||||
'_user' => ['server' => ['list', 'multiple-dash-test']],
|
|
||||||
'server' => ['create', 'admin-dash-test'],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$this->permissionService->shouldReceive('create')->with($model->id, 'user.server-list')->once()->andReturnNull();
|
$this->assertNotEmpty($response);
|
||||||
$this->permissionService->shouldReceive('create')->with($model->id, 'user.server-multiple-dash-test')->once()->andReturnNull();
|
$this->assertInstanceOf(APIKey::class, $response);
|
||||||
$this->permissionService->shouldReceive('create')->with($model->id, 'server-create')->once()->andReturnNull();
|
$this->assertSame($model, $response);
|
||||||
$this->permissionService->shouldReceive('create')->with($model->id, 'server-admin-dash-test')->once()->andReturnNull();
|
}
|
||||||
|
|
||||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
public function testIdentifierAndTokenAreOnlySetByFunction()
|
||||||
|
{
|
||||||
|
$model = factory(APIKey::class)->make();
|
||||||
|
|
||||||
$response = $this->getService()->handle(
|
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
|
||||||
['test-data' => 'test'],
|
->expects($this->exactly(2))->willReturnCallback(function ($length) {
|
||||||
['invalid-node', 'server-list', 'server-multiple-dash-test'],
|
return 'str_' . $length;
|
||||||
['invalid-node', 'server-create', 'server-admin-dash-test']
|
});
|
||||||
);
|
|
||||||
|
$this->encrypter->shouldReceive('encrypt')->with('str_' . APIKey::KEY_LENGTH)->once()->andReturn($model->token);
|
||||||
|
|
||||||
|
$this->repository->shouldReceive('create')->with([
|
||||||
|
'identifier' => 'str_' . APIKey::IDENTIFIER_LENGTH,
|
||||||
|
'token' => $model->token,
|
||||||
|
], true, true)->once()->andReturn($model);
|
||||||
|
|
||||||
|
$response = $this->getService()->handle(['identifier' => 'customIdentifier', 'token' => 'customToken']);
|
||||||
|
|
||||||
$this->assertNotEmpty($response);
|
$this->assertNotEmpty($response);
|
||||||
$this->assertInstanceOf(APIKey::class, $response);
|
$this->assertInstanceOf(APIKey::class, $response);
|
||||||
|
@ -96,6 +92,6 @@ class KeyCreationServiceTest extends TestCase
|
||||||
*/
|
*/
|
||||||
private function getService(): KeyCreationService
|
private function getService(): KeyCreationService
|
||||||
{
|
{
|
||||||
return new KeyCreationService($this->repository, $this->connection, $this->permissionService);
|
return new KeyCreationService($this->repository, $this->encrypter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue