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

104 lines
3.1 KiB
PHP
Raw Normal View History

<?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-09 17:29:18 +00:00
namespace Tests\Unit\Services\Api;
use Mockery as m;
2017-08-05 22:26:30 +00:00
use Tests\TestCase;
use phpmock\phpunit\PHPMock;
2017-08-05 22:26:30 +00:00
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
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;
class KeyCreationServiceTest extends TestCase
{
use PHPMock;
/**
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
protected $encrypter;
/**
2017-07-09 17:29:18 +00:00
* @var \Pterodactyl\Services\Api\PermissionService
*/
protected $permissions;
/**
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Api\KeyCreationService
*/
protected $service;
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->encrypter = m::mock(Encrypter::class);
2017-07-09 17:29:18 +00:00
$this->permissions = m::mock(PermissionService::class);
$this->repository = m::mock(ApiKeyRepositoryInterface::class);
$this->service = new KeyCreationService(
2017-08-22 03:10:48 +00:00
$this->repository,
$this->connection,
2017-08-22 03:10:48 +00:00
$this->encrypter,
$this->permissions
);
}
/**
* Test that the service is able to create a keypair and assign the correct permissions.
*/
public function testKeyIsCreated()
{
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
->expects($this->exactly(2))->willReturn('random_string');
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->encrypter->shouldReceive('encrypt')->with('random_string')->once()->andReturn('encrypted-secret');
$this->repository->shouldReceive('create')->with([
'test-data' => 'test',
'public' => 'random_string',
'secret' => 'encrypted-secret',
], true, true)->once()->andReturn((object) ['id' => 1]);
$this->permissions->shouldReceive('getPermissions')->withNoArgs()->once()->andReturn([
'_user' => ['server' => ['list']],
'server' => ['create'],
]);
$this->permissions->shouldReceive('create')->with(1, 'user.server-list')->once()->andReturnNull();
$this->permissions->shouldReceive('create')->with(1, 'server-create')->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle(
2017-08-22 03:10:48 +00:00
['test-data' => 'test'],
['invalid-node', 'server-list'],
['invalid-node', 'server-create']
);
$this->assertNotEmpty($response);
$this->assertEquals('random_string', $response);
}
}