2017-07-08 19:07:51 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-07-08 19:07:51 +00:00
|
|
|
*/
|
|
|
|
|
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-12-03 20:29:14 +00:00
|
|
|
use Pterodactyl\Models\APIKey;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-07-09 17:29:18 +00:00
|
|
|
use Pterodactyl\Services\Api\PermissionService;
|
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;
|
|
|
|
|
|
|
|
/**
|
2017-12-03 20:29:14 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
2017-07-08 19:07:51 +00:00
|
|
|
*/
|
2017-12-03 20:29:14 +00:00
|
|
|
private $connection;
|
2017-07-08 19:07:51 +00:00
|
|
|
|
|
|
|
/**
|
2017-12-03 20:29:14 +00:00
|
|
|
* @var \Pterodactyl\Services\Api\PermissionService|\Mockery\Mock
|
2017-07-08 19:07:51 +00:00
|
|
|
*/
|
2017-12-03 20:29:14 +00:00
|
|
|
private $permissionService;
|
2017-07-08 19:07:51 +00:00
|
|
|
|
|
|
|
/**
|
2017-12-03 20:29:14 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface|\Mockery\Mock
|
2017-07-08 19:07:51 +00:00
|
|
|
*/
|
2017-12-03 20:29:14 +00:00
|
|
|
private $repository;
|
2017-07-08 19:07:51 +00:00
|
|
|
|
|
|
|
/**
|
2017-12-03 20:29:14 +00:00
|
|
|
* Setup tests.
|
2017-07-08 19:07:51 +00:00
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->connection = m::mock(ConnectionInterface::class);
|
2017-12-03 20:29:14 +00:00
|
|
|
$this->permissionService = m::mock(PermissionService::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-12-03 20:29:14 +00:00
|
|
|
$model = factory(APIKey::class)->make();
|
|
|
|
|
2017-09-14 04:07:02 +00:00
|
|
|
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
|
2017-12-03 20:29:14 +00:00
|
|
|
->expects($this->exactly(1))->with(APIKey::KEY_LENGTH)->willReturn($model->token);
|
2017-07-08 19:07:51 +00:00
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2017-07-08 19:07:51 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('create')->with([
|
|
|
|
'test-data' => 'test',
|
2017-12-03 20:29:14 +00:00
|
|
|
'token' => $model->token,
|
|
|
|
], true, true)->once()->andReturn($model);
|
2017-07-08 19:07:51 +00:00
|
|
|
|
2017-12-03 20:29:14 +00:00
|
|
|
$this->permissionService->shouldReceive('getPermissions')->withNoArgs()->once()->andReturn([
|
2017-11-05 19:42:57 +00:00
|
|
|
'_user' => ['server' => ['list', 'multiple-dash-test']],
|
|
|
|
'server' => ['create', 'admin-dash-test'],
|
2017-07-08 19:07:51 +00:00
|
|
|
]);
|
|
|
|
|
2017-12-03 20:29:14 +00:00
|
|
|
$this->permissionService->shouldReceive('create')->with($model->id, 'user.server-list')->once()->andReturnNull();
|
|
|
|
$this->permissionService->shouldReceive('create')->with($model->id, 'user.server-multiple-dash-test')->once()->andReturnNull();
|
|
|
|
$this->permissionService->shouldReceive('create')->with($model->id, 'server-create')->once()->andReturnNull();
|
|
|
|
$this->permissionService->shouldReceive('create')->with($model->id, 'server-admin-dash-test')->once()->andReturnNull();
|
2017-07-08 19:07:51 +00:00
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
2017-07-08 19:07:51 +00:00
|
|
|
|
2017-12-03 20:29:14 +00:00
|
|
|
$response = $this->getService()->handle(
|
2017-08-22 03:10:48 +00:00
|
|
|
['test-data' => 'test'],
|
2017-11-05 19:42:57 +00:00
|
|
|
['invalid-node', 'server-list', 'server-multiple-dash-test'],
|
|
|
|
['invalid-node', 'server-create', 'server-admin-dash-test']
|
2017-07-08 19:07:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($response);
|
2017-12-03 20:29:14 +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
|
|
|
|
{
|
|
|
|
return new KeyCreationService($this->repository, $this->connection, $this->permissionService);
|
2017-07-08 19:07:51 +00:00
|
|
|
}
|
|
|
|
}
|