mirror of
https://github.com/pterodactyl/panel.git
synced 2024-11-22 09:02:28 +01:00
More tests for daemon keys
This commit is contained in:
parent
85e35f0bdf
commit
8908a758ca
@ -202,6 +202,5 @@ $factory->define(Pterodactyl\Models\DaemonKey::class, function (Faker\Generator
|
||||
'server_id' => $faker->randomNumber(),
|
||||
'user_id' => $faker->randomNumber(),
|
||||
'secret' => 'i_' . str_random(40),
|
||||
'expires_at' => \Carbon\Carbon::now()->addMinutes(10)->toDateTimeString(),
|
||||
];
|
||||
});
|
||||
|
114
tests/Unit/Services/DaemonKeys/DaemonKeyProviderServiceTest.php
Normal file
114
tests/Unit/Services/DaemonKeys/DaemonKeyProviderServiceTest.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?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\DaemonKeys;
|
||||
|
||||
use Mockery as m;
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\DaemonKey;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
||||
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
||||
|
||||
class DaemonKeyProviderServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Carbon\Carbon|\Mockery\Mock
|
||||
*/
|
||||
protected $carbon;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService|\Mockery\Mock
|
||||
*/
|
||||
protected $keyUpdateService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->carbon = m::mock(Carbon::class);
|
||||
$this->keyUpdateService = m::mock(DaemonKeyUpdateService::class);
|
||||
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
|
||||
|
||||
$this->service = new DaemonKeyProviderService($this->carbon, $this->keyUpdateService, $this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a key is returned.
|
||||
*/
|
||||
public function testKeyIsReturned()
|
||||
{
|
||||
$key = factory(DaemonKey::class)->make();
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([
|
||||
['user_id', '=', $key->user_id],
|
||||
['server_id', '=', $key->server_id],
|
||||
])->once()->andReturn($key);
|
||||
|
||||
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf();
|
||||
$this->carbon->shouldReceive('diffInSeconds')->with($key->expires_at, false)->once()->andReturn(100);
|
||||
|
||||
$response = $this->service->handle($key->server_id, $key->user_id);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals($key->secret, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an expired key is updated and then returned.
|
||||
*/
|
||||
public function testExpiredKeyIsUpdated()
|
||||
{
|
||||
$key = factory(DaemonKey::class)->make();
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([
|
||||
['user_id', '=', $key->user_id],
|
||||
['server_id', '=', $key->server_id],
|
||||
])->once()->andReturn($key);
|
||||
|
||||
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf();
|
||||
$this->carbon->shouldReceive('diffInSeconds')->with($key->expires_at, false)->once()->andReturn(-100);
|
||||
|
||||
$this->keyUpdateService->shouldReceive('handle')->with($key->id)->once()->andReturn(true);
|
||||
|
||||
$response = $this->service->handle($key->server_id, $key->user_id);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertTrue($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an expired key is not updated and the expired key is returned.
|
||||
*/
|
||||
public function testExpiredKeyIsNotUpdated()
|
||||
{
|
||||
$key = factory(DaemonKey::class)->make();
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([
|
||||
['user_id', '=', $key->user_id],
|
||||
['server_id', '=', $key->server_id],
|
||||
])->once()->andReturn($key);
|
||||
|
||||
$response = $this->service->handle($key->server_id, $key->user_id, false);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals($key->secret, $response);
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
<?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\DaemonKeys;
|
||||
|
||||
use Mockery as m;
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use phpmock\phpunit\PHPMock;
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService;
|
||||
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
||||
|
||||
class DaemonKeyUpdateServiceTest extends TestCase
|
||||
{
|
||||
use PHPMock;
|
||||
|
||||
/**
|
||||
* @var \Carbon\Carbon|\Mockery\Mock
|
||||
*/
|
||||
protected $carbon;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->carbon = m::Mock(Carbon::class);
|
||||
$this->config = m::mock(Repository::class);
|
||||
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
|
||||
|
||||
$this->service = new DaemonKeyUpdateService($this->carbon, $this->config, $this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a key is updated.
|
||||
*/
|
||||
public function testKeyIsUpdated()
|
||||
{
|
||||
$secret = DaemonKeyRepositoryInterface::INTERNAL_KEY_IDENTIFIER . 'random_string';
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\DaemonKeys', 'str_random')
|
||||
->expects($this->once())->with(40)->willReturn('random_string');
|
||||
|
||||
$this->config->shouldReceive('get')->with('pterodactyl.api.key_expire_time')->once()->andReturn(100);
|
||||
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('addMinutes')->with(100)->once()->andReturnSelf()
|
||||
->shouldReceive('toDateTimeString')->withNoArgs()->once()->andReturn('00:00:00');
|
||||
|
||||
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf();
|
||||
$this->repository->shouldReceive('update')->with(123, [
|
||||
'secret' => $secret,
|
||||
'expires_at' => '00:00:00',
|
||||
])->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->handle(123);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals($secret, $response);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user