Merge branch 'develop' into feature/api-v1

# Conflicts:
#	app/Contracts/Repository/RepositoryInterface.php
#	app/Repositories/Eloquent/EloquentRepository.php
#	app/Services/Nodes/NodeUpdateService.php
#	tests/Unit/Services/Nodes/NodeUpdateServiceTest.php
This commit is contained in:
Dane Everitt 2018-01-10 20:55:22 -06:00
commit 800e2df6b2
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
187 changed files with 1878 additions and 3143 deletions

View file

@ -45,11 +45,12 @@ trait RequestMockHelpers
/**
* Generates a new request user model and also returns the generated model.
*
* @param array $args
* @return \Pterodactyl\Models\User
*/
public function generateRequestUserModel(): User
public function generateRequestUserModel(array $args = []): User
{
$user = factory(User::class)->make();
$user = factory(User::class)->make($args);
$this->setRequestUserModel($user);
return $user;

View file

@ -34,7 +34,7 @@ class CleanServiceBackupFilesCommandTest extends CommandTestCase
{
parent::setUp();
Carbon::setTestNow();
Carbon::setTestNow(Carbon::now());
$this->disk = m::mock(Filesystem::class);
$this->filesystem = m::mock(Factory::class);
$this->filesystem->shouldReceive('disk')->withNoArgs()->once()->andReturn($this->disk);

View file

@ -1,11 +1,4 @@
<?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\Commands\User;
@ -60,7 +53,7 @@ class DeleteUserCommandTest extends CommandTestCase
$user2 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('search')->with($user1->username)->once()->andReturnSelf()
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
$this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull();
@ -82,9 +75,9 @@ class DeleteUserCommandTest extends CommandTestCase
$user1 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('search')->with('noResults')->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn([]);
$this->repository->shouldReceive('search')->with($user1->username)->once()->andReturnSelf()
$this->repository->shouldReceive('setSearchTerm')->with('noResults')->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn(collect());
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
$this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull();
@ -107,7 +100,7 @@ class DeleteUserCommandTest extends CommandTestCase
$user1 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('search')->with($user1->username)->twice()->andReturnSelf()
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->twice()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->twice()->andReturn($users);
$this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull();
@ -130,7 +123,7 @@ class DeleteUserCommandTest extends CommandTestCase
$user1 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('search')->with($user1->username)->once()->andReturnSelf()
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
$this->deletionService->shouldNotReceive('handle');
@ -149,7 +142,7 @@ class DeleteUserCommandTest extends CommandTestCase
$user1 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('search')->with($user1->username)->once()->andReturnSelf()
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
$this->deletionService->shouldReceive('handle')->with($user1)->once()->andReturnNull();
@ -169,7 +162,7 @@ class DeleteUserCommandTest extends CommandTestCase
$user2 = factory(User::class)->make(),
]);
$this->repository->shouldReceive('search')->with($user1->username)->once()->andReturnSelf()
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
$this->deletionService->shouldNotReceive('handle');
@ -184,8 +177,8 @@ class DeleteUserCommandTest extends CommandTestCase
*/
public function testNoInteractionWithNoResults()
{
$this->repository->shouldReceive('search')->with(123456)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn([]);
$this->repository->shouldReceive('setSearchTerm')->with(123456)->once()->andReturnSelf()
->shouldReceive('all')->withNoArgs()->once()->andReturn(collect());
$display = $this->withoutInteraction()->runCommand($this->command, ['--user' => 123456]);

View file

@ -47,9 +47,9 @@ class DisableTwoFactorCommandTest extends CommandTestCase
{
$user = factory(User::class)->make();
$this->repository->shouldReceive('withColumns')->with(['id', 'email'])->once()->andReturnSelf()
$this->repository->shouldReceive('setColumns')->with(['id', 'email'])->once()->andReturnSelf()
->shouldReceive('findFirstWhere')->with([['email', '=', $user->email]])->once()->andReturn($user);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($user->id, [
'use_totp' => false,
'totp_secret' => null,
@ -68,9 +68,9 @@ class DisableTwoFactorCommandTest extends CommandTestCase
{
$user = factory(User::class)->make();
$this->repository->shouldReceive('withColumns')->with(['id', 'email'])->once()->andReturnSelf()
$this->repository->shouldReceive('setColumns')->with(['id', 'email'])->once()->andReturnSelf()
->shouldReceive('findFirstWhere')->with([['email', '=', $user->email]])->once()->andReturn($user);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($user->id, [
'use_totp' => false,
'totp_secret' => null,

View file

@ -11,6 +11,7 @@ namespace Tests\Unit\Http\Controllers\Admin;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\DatabaseHost;
use Prologue\Alerts\AlertsMessageBag;
use Tests\Assertions\ControllerAssertionsTrait;
use Pterodactyl\Http\Controllers\Admin\DatabaseController;
@ -74,8 +75,8 @@ class DatabaseControllerTest extends TestCase
*/
public function testIndexController()
{
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn('getAllWithNodes');
$this->repository->shouldReceive('getWithViewDetails')->withNoArgs()->once()->andReturn('getWithViewDetails');
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn(collect(['getAllWithNodes']));
$this->repository->shouldReceive('getWithViewDetails')->withNoArgs()->once()->andReturn(collect(['getWithViewDetails']));
$response = $this->getController()->index();
@ -83,8 +84,8 @@ class DatabaseControllerTest extends TestCase
$this->assertViewNameEquals('admin.databases.index', $response);
$this->assertViewHasKey('locations', $response);
$this->assertViewHasKey('hosts', $response);
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $response);
$this->assertViewKeyEquals('hosts', 'getWithViewDetails', $response);
$this->assertViewKeyEquals('locations', collect(['getAllWithNodes']), $response);
$this->assertViewKeyEquals('hosts', collect(['getWithViewDetails']), $response);
}
/**
@ -92,8 +93,10 @@ class DatabaseControllerTest extends TestCase
*/
public function testViewController()
{
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn('getAllWithNodes');
$this->repository->shouldReceive('getWithServers')->with(1)->once()->andReturn('getWithServers');
$model = factory(DatabaseHost::class)->make();
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn(collect(['getAllWithNodes']));
$this->repository->shouldReceive('getWithServers')->with(1)->once()->andReturn($model);
$response = $this->getController()->view(1);
@ -101,8 +104,8 @@ class DatabaseControllerTest extends TestCase
$this->assertViewNameEquals('admin.databases.view', $response);
$this->assertViewHasKey('locations', $response);
$this->assertViewHasKey('host', $response);
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $response);
$this->assertViewKeyEquals('host', 'getWithServers', $response);
$this->assertViewKeyEquals('locations', collect(['getAllWithNodes']), $response);
$this->assertViewKeyEquals('host', $model, $response);
}
/**

View file

@ -46,15 +46,15 @@ class APIControllerTest extends ControllerTestCase
*/
public function testIndexController()
{
$model = $this->setRequestUser();
$model = $this->generateRequestUserModel();
$this->repository->shouldReceive('findWhere')->with([['user_id', '=', $model->id]])->once()->andReturn(['testkeys']);
$this->repository->shouldReceive('findWhere')->with([['user_id', '=', $model->id]])->once()->andReturn(collect(['testkeys']));
$response = $this->getController()->index($this->request);
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('base.api.index', $response);
$this->assertViewHasKey('keys', $response);
$this->assertViewKeyEquals('keys', ['testkeys'], $response);
$this->assertViewKeyEquals('keys', collect(['testkeys']), $response);
}
/**
@ -64,7 +64,7 @@ class APIControllerTest extends ControllerTestCase
*/
public function testCreateController($admin)
{
$this->setRequestUser(factory(User::class)->make(['root_admin' => $admin]));
$this->generateRequestUserModel(['root_admin' => $admin]);
$response = $this->getController()->create($this->request);
$this->assertIsViewResponse($response);
@ -87,7 +87,7 @@ class APIControllerTest extends ControllerTestCase
public function testStoreController($admin)
{
$this->setRequestMockClass(ApiKeyFormRequest::class);
$model = $this->setRequestUser(factory(User::class)->make(['root_admin' => $admin]));
$model = $this->generateRequestUserModel(['root_admin' => $admin]);
$keyModel = factory(APIKey::class)->make();
if ($admin) {
@ -118,12 +118,12 @@ class APIControllerTest extends ControllerTestCase
*/
public function testRevokeController()
{
$model = $this->setRequestUser();
$model = $this->generateRequestUserModel();
$this->repository->shouldReceive('deleteWhere')->with([
['user_id', '=', $model->id],
['token', '=', 'testKey123'],
])->once()->andReturnNull();
])->once()->andReturn(1);
$response = $this->getController()->revoke($this->request, 'testKey123');
$this->assertIsResponse($response);

View file

@ -11,10 +11,12 @@ namespace Tests\Unit\Http\Controllers\Base;
use Mockery as m;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Tests\Assertions\ControllerAssertionsTrait;
use Tests\Unit\Http\Controllers\ControllerTestCase;
use Pterodactyl\Http\Controllers\Base\IndexController;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
@ -62,19 +64,19 @@ class IndexControllerTest extends ControllerTestCase
*/
public function testIndexController()
{
$model = $this->setRequestUser();
$paginator = m::mock(LengthAwarePaginator::class);
$model = $this->generateRequestUserModel();
$this->request->shouldReceive('input')->with('query')->once()->andReturn('searchTerm');
$this->repository->shouldReceive('search')->with('searchTerm')->once()->andReturnSelf()
->shouldReceive('filterUserAccessServers')->with(
$model->id, $model->root_admin, 'all', ['user']
)->once()->andReturn(['test']);
$this->repository->shouldReceive('setSearchTerm')->with('searchTerm')->once()->andReturnSelf()
->shouldReceive('filterUserAccessServers')->with($model, User::FILTER_LEVEL_ALL)
->once()->andReturn($paginator);
$response = $this->controller->getIndex($this->request);
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('base.index', $response);
$this->assertViewHasKey('servers', $response);
$this->assertViewKeyEquals('servers', ['test'], $response);
$this->assertViewKeyEquals('servers', $paginator, $response);
}
/**
@ -82,22 +84,20 @@ class IndexControllerTest extends ControllerTestCase
*/
public function testStatusController()
{
$user = $this->setRequestUser();
$user = $this->generateRequestUserModel();
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
$psrResponse = new Response;
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('test123')->once()->andReturnSelf()
->shouldReceive('details')->withNoArgs()->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('getBody')->withNoArgs()->once()->andReturn('["test"]');
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('test123')->once()->andReturnSelf()
->shouldReceive('details')->withNoArgs()->once()->andReturn($psrResponse);
$response = $this->controller->status($this->request, $server->uuidShort);
$this->assertIsJsonResponse($response);
$this->assertResponseJsonEquals(['test'], $response);
$this->assertResponseJsonEquals(json_encode($psrResponse->getBody()), $response);
}
/**
@ -105,7 +105,7 @@ class IndexControllerTest extends ControllerTestCase
*/
public function testStatusControllerWhenServerNotInstalled()
{
$user = $this->setRequestUser();
$user = $this->generateRequestUserModel();
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 0]);
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);

View file

@ -61,13 +61,13 @@ class SecurityControllerTest extends ControllerTestCase
$model = $this->generateRequestUserModel();
$this->config->shouldReceive('get')->with('session.driver')->once()->andReturn('database');
$this->repository->shouldReceive('getUserSessions')->with($model->id)->once()->andReturn(['sessions']);
$this->repository->shouldReceive('getUserSessions')->with($model->id)->once()->andReturn(collect(['sessions']));
$response = $this->getController()->index($this->request);
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('base.security', $response);
$this->assertViewHasKey('sessions', $response);
$this->assertViewKeyEquals('sessions', ['sessions'], $response);
$this->assertViewKeyEquals('sessions', collect(['sessions']), $response);
}
/**

View file

@ -98,12 +98,11 @@ class FileActionsControllerTest extends ControllerTestCase
$this->setRequestAttribute('file_stats', 'fileStatsObject');
$this->mockInjectJavascript(['stat' => 'fileStatsObject']);
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('getContent')->with($file)->once()->andReturn('file contents');
$this->repository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('getContent')->with($file)->once()->andReturn((object) ['test']);
$response = $controller->update($this->request, '1234', $file);
$response = $controller->view($this->request, '1234', $file);
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('server.files.edit', $response);
$this->assertViewHasKey('file', $response);
@ -112,7 +111,7 @@ class FileActionsControllerTest extends ControllerTestCase
$this->assertViewHasKey('directory', $response);
$this->assertViewKeyEquals('file', $file, $response);
$this->assertViewKeyEquals('stat', 'fileStatsObject', $response);
$this->assertViewKeyEquals('contents', 'file contents', $response);
$this->assertViewKeyEquals('contents', (object) ['test'], $response);
$this->assertViewKeyEquals('directory', $expected, $response);
}
@ -131,10 +130,10 @@ class FileActionsControllerTest extends ControllerTestCase
$this->setRequestAttribute('server_token', 'abc123');
$this->setRequestAttribute('file_stats', 'fileStatsObject');
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
$this->repository->shouldReceive('setServer')->with($server)->once()->andThrow($this->getExceptionMock());
try {
$controller->update($this->request, '1234', 'file.txt');
$controller->view($this->request, '1234', 'file.txt');
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());

View file

@ -10,6 +10,7 @@
namespace Tests\Unit\Http\Controllers\Server\Files;
use Mockery as m;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Tests\Traits\MocksRequestException;
use GuzzleHttp\Exception\RequestException;
@ -58,9 +59,8 @@ class RemoteRequestControllerTest extends ControllerTestCase
$controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/');
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf()
$this->repository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('getDirectory')->with('/')->once()->andReturn(['folders' => 1, 'files' => 2]);
$this->config->shouldReceive('get')->with('pterodactyl.files.editable')->once()->andReturn([]);
@ -91,7 +91,7 @@ class RemoteRequestControllerTest extends ControllerTestCase
$controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/');
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
$this->repository->shouldReceive('setServer')->with($server)->once()->andThrow($this->getExceptionMock());
try {
$controller->directory($this->request);
@ -115,10 +115,9 @@ class RemoteRequestControllerTest extends ControllerTestCase
$controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('input')->with('file')->once()->andReturn('file.txt');
$this->request->shouldReceive('input')->with('contents')->once()->andReturn('file contents');
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('putContent')->with('file.txt', 'file contents')->once()->andReturnNull();
$this->repository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('putContent')->with('file.txt', 'file contents')->once()->andReturn(new Response);
$response = $controller->store($this->request);
$this->assertIsResponse($response);
@ -137,7 +136,7 @@ class RemoteRequestControllerTest extends ControllerTestCase
$this->setRequestAttribute('server', $server);
$controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
$this->repository->shouldReceive('setServer')->with($server)->once()->andThrow($this->getExceptionMock());
try {
$controller->store($this->request);

View file

@ -76,7 +76,7 @@ class SubuserControllerTest extends ControllerTestCase
$this->mockInjectJavascript();
$controller->shouldReceive('authorize')->with('list-subusers', $server)->once()->andReturnNull();
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $server->id]])->once()->andReturn([]);
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $server->id]])->once()->andReturn(collect());
$response = $controller->index($this->request);
$this->assertIsViewResponse($response);

View file

@ -34,7 +34,7 @@ class DaemonAuthenticateTest extends MiddlewareTestCase
$this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('random.name');
$this->request->shouldReceive('header')->with('X-Access-Node')->twice()->andReturn($node->uuid);
$this->repository->shouldReceive('findWhere')->with(['daemonSecret' => $node->uuid])->once()->andReturn($node);
$this->repository->shouldReceive('findFirstWhere')->with(['daemonSecret' => $node->uuid])->once()->andReturn($node);
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
$this->assertRequestHasAttribute('node');

View file

@ -1,11 +1,4 @@
<?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\Jobs\Schedule;
@ -14,6 +7,7 @@ use Carbon\Carbon;
use Tests\TestCase;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Schedule;
use Illuminate\Support\Facades\Bus;
@ -86,19 +80,19 @@ class RunTaskJobTest extends TestCase
$schedule = factory(Schedule::class)->make();
$task = factory(Task::class)->make(['action' => 'power', 'sequence_id' => 1]);
$task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
$this->powerRepository->shouldReceive('setNode')->with($task->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($task->server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('sendSignal')->with($task->payload)->once()->andReturnNull();
$this->powerRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('sendSignal')->with($task->payload)->once()->andReturn(new Response);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull();
$this->taskRepository->shouldReceive('getNextTask')->with($schedule->id, $task->sequence_id)->once()->andReturnNull();
$this->scheduleRepository->shouldReceive('withoutFresh->update')->with($schedule->id, [
$this->scheduleRepository->shouldReceive('withoutFreshModel->update')->with($schedule->id, [
'is_processing' => false,
'last_run_at' => Carbon::now()->toDateTimeString(),
])->once()->andReturnNull();
@ -116,19 +110,19 @@ class RunTaskJobTest extends TestCase
$schedule = factory(Schedule::class)->make();
$task = factory(Task::class)->make(['action' => 'command', 'sequence_id' => 1]);
$task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
$this->commandRepository->shouldReceive('setNode')->with($task->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($task->server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('send')->with($task->payload)->once()->andReturnNull();
$this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('send')->with($task->payload)->once()->andReturn(new Response);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull();
$this->taskRepository->shouldReceive('getNextTask')->with($schedule->id, $task->sequence_id)->once()->andReturnNull();
$this->scheduleRepository->shouldReceive('withoutFresh->update')->with($schedule->id, [
$this->scheduleRepository->shouldReceive('withoutFreshModel->update')->with($schedule->id, [
'is_processing' => false,
'last_run_at' => Carbon::now()->toDateTimeString(),
])->once()->andReturnNull();
@ -146,14 +140,14 @@ class RunTaskJobTest extends TestCase
$schedule = factory(Schedule::class)->make();
$task = factory(Task::class)->make(['action' => 'command', 'sequence_id' => 1]);
$task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
$this->commandRepository->shouldReceive('setNode')->with($task->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($task->server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('send')->with($task->payload)->once()->andReturnNull();
$this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('send')->with($task->payload)->once()->andReturn(new Response);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull();
@ -178,19 +172,45 @@ class RunTaskJobTest extends TestCase
* Test that an exception is thrown if an invalid task action is supplied.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cannot run a task that points to a non-existant action.
* @expectedExceptionMessage Cannot run a task that points to a non-existent action.
*/
public function testInvalidActionPassedToJob()
{
$schedule = factory(Schedule::class)->make();
$task = factory(Task::class)->make(['action' => 'invalid', 'sequence_id' => 1]);
$task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
$this->getJobInstance($task->id, 1234);
}
/**
* Test that a schedule marked as disabled does not get processed.
*/
public function testScheduleMarkedAsDisabledDoesNotProcess()
{
$schedule = factory(Schedule::class)->make(['is_active' => false]);
$task = factory(Task::class)->make(['action' => 'invalid', 'sequence_id' => 1]);
$task->setRelation('server', $server = factory(Server::class)->make());
$task->setRelation('schedule', $schedule);
$server->setRelation('user', factory(User::class)->make());
$this->taskRepository->shouldReceive('getTaskForJobProcess')->with($task->id)->once()->andReturn($task);
$this->scheduleRepository->shouldReceive('withoutFreshModel->update')->with($schedule->id, [
'is_processing' => false,
'last_run_at' => Carbon::now()->toDateTimeString(),
])->once()->andReturn(1);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturn(1);
$this->getJobInstance($task->id, $schedule->id);
$this->assertTrue(true);
}
/**
* Run the job using the mocks provided.
*

View file

@ -1,72 +0,0 @@
<?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\Repositories\Eloquent;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Allocation;
use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
class AllocationRepositoryTest extends TestCase
{
/**
* @var \Illuminate\Database\Eloquent\Builder
*/
protected $builder;
/**
* @var \Pterodactyl\Repositories\Eloquent\AllocationRepository
*/
protected $repository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->builder = m::mock(Builder::class);
$this->repository = m::mock(AllocationRepository::class)->makePartial();
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
}
/**
* Test that we are returning the correct model.
*/
public function testCorrectModelIsAssigned()
{
$this->assertEquals(Allocation::class, $this->repository->model());
}
/**
* Test that allocations can be assigned to a server correctly.
*/
public function testAllocationsAreAssignedToAServer()
{
$this->builder->shouldReceive('whereIn')->with('id', [1, 2])->once()->andReturnSelf()
->shouldReceive('update')->with(['server_id' => 10])->once()->andReturn(true);
$this->assertTrue($this->repository->assignAllocationsToServer(10, [1, 2]));
}
/**
* Test that allocations with a node relationship are returned.
*/
public function testAllocationsForANodeAreReturned()
{
$this->builder->shouldReceive('where')->with('node_id', 1)->once()->andReturnSelf()
->shouldReceive('get')->once()->andReturn(factory(Allocation::class)->make());
$this->assertInstanceOf(Allocation::class, $this->repository->getAllocationsForNode(1));
}
}

View file

@ -1,50 +0,0 @@
<?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\Repositories\Eloquent;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\APIKey;
use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Repositories\Eloquent\ApiKeyRepository;
class ApiKeyRepositoryTest extends TestCase
{
/**
* @var \Illuminate\Database\Eloquent\Builder
*/
protected $builder;
/**
* @var \Pterodactyl\Repositories\Eloquent\ApiKeyRepository
*/
protected $repository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->builder = m::mock(Builder::class);
$this->repository = m::mock(ApiKeyRepository::class)->makePartial();
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
}
/**
* Test that we are returning the correct model.
*/
public function testCorrectModelIsAssigned()
{
$this->assertEquals(APIKey::class, $this->repository->model());
}
}

View file

@ -1,50 +0,0 @@
<?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\Repositories\Eloquent;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\APIPermission;
use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Repositories\Eloquent\ApiPermissionRepository;
class ApiPermissionRepositoryTest extends TestCase
{
/**
* @var \Illuminate\Database\Eloquent\Builder
*/
protected $builder;
/**
* @var \Pterodactyl\Repositories\Eloquent\ApiPermissionRepository
*/
protected $repository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->builder = m::mock(Builder::class);
$this->repository = m::mock(ApiPermissionRepository::class)->makePartial();
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
}
/**
* Test that we are returning the correct model.
*/
public function testCorrectModelIsAssigned()
{
$this->assertEquals(APIPermission::class, $this->repository->model());
}
}

View file

@ -1,88 +0,0 @@
<?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\Repositories\Eloquent;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\DatabaseHost;
use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
class DatabaseHostRepositoryTest extends TestCase
{
/**
* @var \Illuminate\Database\Eloquent\Builder
*/
protected $builder;
/**
* @var \Pterodactyl\Repositories\Eloquent\DatabaseHostRepository
*/
protected $repository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->builder = m::mock(Builder::class);
$this->repository = m::mock(DatabaseHostRepository::class)->makePartial();
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
}
/**
* Test that we are returning the correct model.
*/
public function testCorrectModelIsAssigned()
{
$this->assertEquals(DatabaseHost::class, $this->repository->model());
}
/**
* Test query to reutrn all of the default view data.
*/
public function testHostWithDefaultViewDataIsReturned()
{
$this->builder->shouldReceive('withCount')->with('databases')->once()->andReturnSelf()
->shouldReceive('with')->with('node')->once()->andReturnSelf()
->shouldReceive('get')->withNoArgs()->once()->andReturnNull();
$this->assertNull($this->repository->getWithViewDetails());
}
/**
* Test query to return host and servers.
*/
public function testHostIsReturnedWithServers()
{
$model = factory(DatabaseHost::class)->make();
$this->builder->shouldReceive('with')->with('databases.server')->once()->andReturnSelf()
->shouldReceive('find')->with(1, ['*'])->once()->andReturn($model);
$this->assertEquals($model, $this->repository->getWithServers(1));
}
/**
* Test exception is found if no host is found when querying for servers.
*
* @expectedException \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function testExceptionIsThrownIfNoRecordIsFoundWithServers()
{
$this->builder->shouldReceive('with')->with('databases.server')->once()->andReturnSelf()
->shouldReceive('find')->with(1, ['*'])->once()->andReturnNull();
$this->repository->getWithServers(1);
}
}

View file

@ -1,157 +0,0 @@
<?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\Repositories\Eloquent;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Database;
use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
class DatabaseRepositoryTest extends TestCase
{
/**
* @var \Illuminate\Database\Eloquent\Builder
*/
protected $builder;
/**
* @var \Pterodactyl\Repositories\Eloquent\DatabaseRepository
*/
protected $repository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->builder = m::mock(Builder::class);
$this->repository = m::mock(DatabaseRepository::class)->makePartial()->shouldAllowMockingProtectedMethods();
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
$this->repository->shouldNotReceive('runStatement');
}
/**
* Test that we are returning the correct model.
*/
public function testCorrectModelIsAssigned()
{
$this->assertEquals(Database::class, $this->repository->model());
}
/**
* Test that a database can be created if it does not already exist.
*/
public function testDatabaseIsCreatedIfNotExists()
{
$data = [
'server_id' => 1,
'database_host_id' => 100,
'database' => 'somename',
];
$this->builder->shouldReceive('where')->with([
['server_id', '=', array_get($data, 'server_id')],
['database_host_id', '=', array_get($data, 'database_host_id')],
['database', '=', array_get($data, 'database')],
])->once()->andReturnSelf()
->shouldReceive('count')->withNoArgs()->once()->andReturn(0);
$this->repository->shouldReceive('create')->with($data)->once()->andReturn(true);
$this->assertTrue($this->repository->createIfNotExists($data));
}
/**
* Test that an exception is thrown if a database already exists with the given name.
*/
public function testExceptionIsThrownIfDatabaseAlreadyExists()
{
$this->builder->shouldReceive('where->count')->once()->andReturn(1);
$this->repository->shouldNotReceive('create');
try {
$this->repository->createIfNotExists([]);
} catch (DisplayException $exception) {
$this->assertInstanceOf(DuplicateDatabaseNameException::class, $exception);
$this->assertEquals('A database with those details already exists for the specified server.', $exception->getMessage());
}
}
/**
* Test SQL used to create a database.
*/
public function testCreateDatabaseStatement()
{
$query = sprintf('CREATE DATABASE IF NOT EXISTS `%s`', 'test_database');
$this->repository->shouldReceive('runStatement')->with($query)->once()->andReturn(true);
$this->assertTrue($this->repository->createDatabase('test_database', 'test'));
}
/**
* Test SQL used to create a user.
*/
public function testCreateUserStatement()
{
$query = sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', 'test', '%', 'password');
$this->repository->shouldReceive('runStatement')->with($query)->once()->andReturn(true);
$this->assertTrue($this->repository->createUser('test', '%', 'password', 'test'));
}
/**
* Test that a user is assigned the correct permissions on a database.
*/
public function testUserAssignmentToDatabaseStatement()
{
$query = sprintf('GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, EXECUTE ON `%s`.* TO `%s`@`%s`', 'test_database', 'test', '%');
$this->repository->shouldReceive('runStatement')->with($query)->once()->andReturn(true);
$this->assertTrue($this->repository->assignUserToDatabase('test_database', 'test', '%', 'test'));
}
/**
* Test SQL for flushing privileges.
*/
public function testFlushStatement()
{
$this->repository->shouldReceive('runStatement')->with('FLUSH PRIVILEGES')->once()->andReturn(true);
$this->assertTrue($this->repository->flush('test'));
}
/**
* Test SQL to drop a database.
*/
public function testDropDatabaseStatement()
{
$query = sprintf('DROP DATABASE IF EXISTS `%s`', 'test_database');
$this->repository->shouldReceive('runStatement')->with($query)->once()->andReturn(true);
$this->assertTrue($this->repository->dropDatabase('test_database', 'test'));
}
/**
* Test SQL to drop a user.
*/
public function testDropUserStatement()
{
$query = sprintf('DROP USER IF EXISTS `%s`@`%s`', 'test', '%');
$this->repository->shouldReceive('runStatement')->with($query)->once()->andReturn(true);
$this->assertTrue($this->repository->dropUser('test', '%', 'test'));
}
}

View file

@ -1,100 +0,0 @@
<?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\Repositories\Eloquent;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Location;
use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Repositories\Eloquent\LocationRepository;
class LocationRepositoryTest extends TestCase
{
/**
* @var \Illuminate\Database\Eloquent\Builder
*/
protected $builder;
/**
* @var \Pterodactyl\Repositories\Eloquent\LocationRepository
*/
protected $repository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->builder = m::mock(Builder::class);
$this->repository = m::mock(LocationRepository::class)->makePartial();
$this->repository->shouldReceive('getBuilder')->withNoArgs()->andReturn($this->builder);
}
/**
* Test that we are returning the correct model.
*/
public function testCorrectModelIsAssigned()
{
$this->assertEquals(Location::class, $this->repository->model());
}
/**
* Test that all locations with associated node and server counts are returned.
*/
public function testAllLocationsWithDetailsAreReturned()
{
$this->builder->shouldReceive('withCount')->with('nodes', 'servers')->once()->andReturnSelf()
->shouldReceive('get')->with(['*'])->once()->andReturnNull();
$this->assertNull($this->repository->getAllWithDetails());
}
/**
* Test that all locations with associated node are returned.
*/
public function testAllLocationsWithNodes()
{
$this->builder->shouldReceive('with')->with('nodes')->once()->andReturnSelf()
->shouldReceive('get')->with(['*'])->once()->andReturnNull();
$this->assertNull($this->repository->getAllWithNodes());
}
/**
* Test that a single location with associated node is returned.
*/
public function testLocationWithNodeIsReturned()
{
$model = factory(Location::class)->make();
$this->builder->shouldReceive('with')->with('nodes.servers')->once()->andReturnSelf()
->shouldReceive('find')->with(1, ['*'])->once()->andReturn($model);
$response = $this->repository->getWithNodes(1);
$this->assertInstanceOf(Location::class, $response);
$this->assertEquals($model, $response);
}
/**
* Test that an exception is thrown when getting location with nodes if no location is found.
*
* @expectedException \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function testExceptionIsThrownIfNoLocationIsFoundWithNodes()
{
$this->builder->shouldReceive('with')->with('nodes.servers')->once()->andReturnSelf()
->shouldReceive('find')->with(1, ['*'])->once()->andReturnNull();
$this->repository->getWithNodes(1);
}
}

View file

@ -84,7 +84,7 @@ class AssignmentServiceTest extends TestCase
'ip_alias' => null,
'server_id' => null,
],
])->once()->andReturnNull();
])->once()->andReturn(true);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($this->node->id, $data);
@ -123,7 +123,7 @@ class AssignmentServiceTest extends TestCase
'ip_alias' => null,
'server_id' => null,
],
])->once()->andReturnNull();
])->once()->andReturn(true);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($this->node->id, $data);
@ -149,7 +149,7 @@ class AssignmentServiceTest extends TestCase
'ip_alias' => 'my.alias.net',
'server_id' => null,
],
])->once()->andReturnNull();
])->once()->andReturn(true);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($this->node->id, $data);
@ -177,7 +177,7 @@ class AssignmentServiceTest extends TestCase
'ip_alias' => null,
'server_id' => null,
],
])->once()->andReturnNull();
])->once()->andReturn(true);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($this->node->id, $data);
@ -202,7 +202,7 @@ class AssignmentServiceTest extends TestCase
'ip_alias' => null,
'server_id' => null,
],
])->once()->andReturnNull();
])->once()->andReturn(true);
$this->repository->shouldReceive('insertIgnore')->with([
[
@ -212,7 +212,7 @@ class AssignmentServiceTest extends TestCase
'ip_alias' => null,
'server_id' => null,
],
])->once()->andReturnNull();
])->once()->andReturn(true);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($this->node->id, $data);
@ -303,7 +303,7 @@ class AssignmentServiceTest extends TestCase
'ip_alias' => null,
'server_id' => null,
],
])->once()->andReturnNull();
])->once()->andReturn(true);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($this->node, $data);

View file

@ -4,6 +4,7 @@ namespace Tests\Unit\Services\Allocations;
use Mockery as m;
use Tests\TestCase;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Allocation;
use Tests\Traits\MocksRequestException;
@ -68,13 +69,12 @@ class SetDefaultAllocationServiceTest extends TestCase
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn($allocations);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->serverRepository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf();
$this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
$this->serverRepository->shouldReceive('update')->with($model->id, [
'allocation_id' => $allocations->first()->id,
])->once()->andReturnNull();
])->once()->andReturn(new Response);
$this->daemonRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('update')->with([
'build' => [
'default' => [
@ -85,7 +85,7 @@ class SetDefaultAllocationServiceTest extends TestCase
return $item->pluck('port');
})->toArray(),
],
])->once()->andReturnNull();
])->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->handle($useModel ? $model : 1234, $allocations->first()->id);
@ -118,12 +118,12 @@ class SetDefaultAllocationServiceTest extends TestCase
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn(collect([$allocation]));
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->serverRepository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf();
$this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
$this->serverRepository->shouldReceive('update')->with($model->id, [
'allocation_id' => $allocation->id,
])->once()->andReturnNull();
])->once()->andReturn(new Response);
$this->daemonRepository->shouldReceive('setAccessServer->setNode->update')->once()->andThrow($this->getExceptionMock());
$this->daemonRepository->shouldReceive('setServer->update')->once()->andThrow($this->getExceptionMock());
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try {

View file

@ -43,7 +43,7 @@ class PermissionServiceTest extends TestCase
*/
public function test_create_function()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('create')->with(['key_id' => 1, 'permission' => 'test-permission'])
->once()->andReturn(true);

View file

@ -83,7 +83,7 @@ class DaemonKeyCreationServiceTest extends TestCase
->shouldReceive('addMinutes')->with(100)->once()->andReturnSelf()
->shouldReceive('toDateTimeString')->withNoArgs()->once()->andReturn('00:00:00');
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('create')->with([
'user_id' => 1,
'server_id' => 2,

View file

@ -12,6 +12,7 @@ namespace Tests\Unit\Services\DaemonKeys;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\DaemonKey;
use GuzzleHttp\Exception\RequestException;
@ -97,9 +98,9 @@ class DaemonKeyDeletionServiceTest extends TestCase
['server_id', '=', $server->id],
])->once()->andReturn($key);
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturnNull();
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1);
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($server, 100);
@ -121,9 +122,9 @@ class DaemonKeyDeletionServiceTest extends TestCase
['server_id', '=', $server->id],
])->once()->andReturn($key);
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturnNull();
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1);
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($server->id, 100);
@ -144,8 +145,8 @@ class DaemonKeyDeletionServiceTest extends TestCase
['server_id', '=', $server->id],
])->once()->andReturn($key);
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturnNull();
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->exception);
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1);
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();

View file

@ -70,7 +70,7 @@ class DaemonKeyUpdateServiceTest extends TestCase
->shouldReceive('addMinutes')->with(100)->once()->andReturnSelf()
->shouldReceive('toDateTimeString')->withNoArgs()->once()->andReturn('00:00:00');
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf();
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
$this->repository->shouldReceive('update')->with(123, [
'secret' => $secret,
'expires_at' => '00:00:00',

View file

@ -4,8 +4,9 @@ namespace Tests\Unit\Services\DaemonKeys;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\DaemonKey;
use Tests\Traits\MocksRequestException;
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
@ -43,13 +44,13 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase
public function testSuccessfulKeyRevocation()
{
$user = factory(User::class)->make();
$server = factory(Server::class)->make();
$node = factory(Node::class)->make();
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
$key->setRelation('server', $server);
$key->setRelation('node', $node);
$this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key]));
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('revokeAccessKey')->with([$key->secret])->once()->andReturnNull();
$this->daemonRepository->shouldReceive('setNode')->with($node)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('revokeAccessKey')->with([$key->secret])->once()->andReturn(new Response);
$this->repository->shouldReceive('deleteKeys')->with([$key->id])->once()->andReturnNull();
@ -67,9 +68,9 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase
$this->configureExceptionMock();
$user = factory(User::class)->make();
$server = factory(Server::class)->make();
$node = factory(Node::class)->make();
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
$key->setRelation('server', $server);
$key->setRelation('node', $node);
$this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key]));
$this->daemonRepository->shouldReceive('setNode->revokeAccessKey')->with([$key->secret])->once()->andThrow($this->getExceptionMock());
@ -86,9 +87,9 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase
$this->configureExceptionMock();
$user = factory(User::class)->make();
$server = factory(Server::class)->make();
$node = factory(Node::class)->make();
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
$key->setRelation('server', $server);
$key->setRelation('node', $node);
$this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key]));
$this->daemonRepository->shouldReceive('setNode->revokeAccessKey')->with([$key->secret])->once()->andThrow($this->getExceptionMock());
@ -98,8 +99,8 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase
$service = $this->getService();
$service->handle($user, true);
$this->assertNotEmpty($service->getExceptions());
$this->assertArrayHasKey($server->node_id, $service->getExceptions());
$this->assertSame(array_get($service->getExceptions(), $server->node_id), $this->getExceptionMock());
$this->assertArrayHasKey($node->id, $service->getExceptions());
$this->assertSame(array_get($service->getExceptions(), $node->id), $this->getExceptionMock());
$this->assertTrue(true);
}

View file

@ -63,14 +63,14 @@ class DatabasePasswordServiceTest extends TestCase
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->encrypter->shouldReceive('encrypt')->with('test123')->once()->andReturn('enc123');
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf();
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
$this->repository->shouldReceive('update')->with($model->id, ['password' => 'enc123'])->once()->andReturn(true);
$this->repository->shouldReceive('dropUser')->with($model->username, $model->remote)->once()->andReturnNull();
$this->repository->shouldReceive('createUser')->with($model->username, $model->remote, 'test123')->once()->andReturnNull();
$this->repository->shouldReceive('assignUserToDatabase')->with($model->database, $model->username, $model->remote)->once()->andReturnNull();
$this->repository->shouldReceive('flush')->withNoArgs()->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('dropUser')->with($model->username, $model->remote)->once()->andReturn(true);
$this->repository->shouldReceive('createUser')->with($model->username, $model->remote, 'test123')->once()->andReturn(true);
$this->repository->shouldReceive('assignUserToDatabase')->with($model->database, $model->username, $model->remote)->once()->andReturn(true);
$this->repository->shouldReceive('flush')->withNoArgs()->once()->andReturn(true);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturn(true);
$response = $this->getService()->handle($useModel ? $model : 1234, 'test123');
$this->assertNotEmpty($response);

View file

@ -9,7 +9,6 @@
namespace Tests\Unit\Services\Services\Options;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Egg;
@ -53,7 +52,7 @@ class EggUpdateServiceTest extends TestCase
*/
public function testEggIsUpdatedWhenNoConfigFromIsProvided()
{
$this->repository->shouldReceive('withoutFresh->update')
$this->repository->shouldReceive('withoutFreshModel->update')
->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull();
$this->service->handle($this->model, ['test_field' => 'field_value']);
@ -71,7 +70,7 @@ class EggUpdateServiceTest extends TestCase
['id', '=', 1],
])->once()->andReturn(1);
$this->repository->shouldReceive('withoutFresh->update')
$this->repository->shouldReceive('withoutFreshModel->update')
->with($this->model->id, ['config_from' => 1])->once()->andReturnNull();
$this->service->handle($this->model, ['config_from' => 1]);
@ -103,7 +102,7 @@ class EggUpdateServiceTest extends TestCase
public function testIntegerCanBePassedInPlaceOfModel()
{
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
$this->repository->shouldReceive('withoutFresh->update')
$this->repository->shouldReceive('withoutFreshModel->update')
->with($this->model->id, ['test_field' => 'field_value'])->once()->andReturnNull();
$this->service->handle($this->model->id, ['test_field' => 'field_value']);

View file

@ -66,7 +66,7 @@ class InstallScriptServiceTest extends TestCase
$this->data['copy_script_from'] = 1;
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(true);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model, $this->data);
@ -93,7 +93,7 @@ class InstallScriptServiceTest extends TestCase
*/
public function testUpdateWithoutNewCopyScriptFromAttribute()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model, $this->data);
@ -105,7 +105,7 @@ class InstallScriptServiceTest extends TestCase
public function testFunctionAcceptsIntegerInPlaceOfModel()
{
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
$this->service->handle($this->model->id, $this->data);

View file

@ -81,13 +81,13 @@ class EggUpdateImporterServiceTest extends TestCase
'name' => $egg->name,
]), true, true)->once()->andReturn($egg);
$this->variableRepository->shouldReceive('withoutFresh->updateOrCreate')->with([
$this->variableRepository->shouldReceive('withoutFreshModel->updateOrCreate')->with([
'egg_id' => $egg->id,
'env_variable' => $variable->env_variable,
], collect($variable)->except(['egg_id', 'env_variable'])->toArray())->once()->andReturnNull();
$this->variableRepository->shouldReceive('withColumns')->with(['id', 'env_variable'])->once()->andReturnSelf()
->shouldReceive('findWhere')->with([['egg_id', '=', $egg->id]])->once()->andReturn([$variable]);
$this->variableRepository->shouldReceive('setColumns')->with(['id', 'env_variable'])->once()->andReturnSelf()
->shouldReceive('findWhere')->with([['egg_id', '=', $egg->id]])->once()->andReturn(collect([$variable]));
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -121,18 +121,18 @@ class EggUpdateImporterServiceTest extends TestCase
'name' => $egg->name,
]), true, true)->once()->andReturn($egg);
$this->variableRepository->shouldReceive('withoutFresh->updateOrCreate')->with([
$this->variableRepository->shouldReceive('withoutFreshModel->updateOrCreate')->with([
'egg_id' => $egg->id,
'env_variable' => $variable1->env_variable,
], collect($variable1)->except(['egg_id', 'env_variable'])->toArray())->once()->andReturnNull();
$this->variableRepository->shouldReceive('withColumns')->with(['id', 'env_variable'])->once()->andReturnSelf()
->shouldReceive('findWhere')->with([['egg_id', '=', $egg->id]])->once()->andReturn([$variable1, $variable2]);
$this->variableRepository->shouldReceive('setColumns')->with(['id', 'env_variable'])->once()->andReturnSelf()
->shouldReceive('findWhere')->with([['egg_id', '=', $egg->id]])->once()->andReturn(collect([$variable1, $variable2]));
$this->variableRepository->shouldReceive('deleteWhere')->with([
['egg_id', '=', $egg->id],
['env_variable', '=', $variable2->env_variable],
])->once()->andReturnNull();
])->once()->andReturn(1);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();

View file

@ -45,7 +45,7 @@ class VariableUpdateServiceTest extends TestCase
*/
public function testVariableIsUpdatedWhenNoEnvironmentVariableIsPassed()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
@ -61,7 +61,7 @@ class VariableUpdateServiceTest extends TestCase
public function testVariableIdCanBePassedInPlaceOfModel()
{
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
@ -76,14 +76,14 @@ class VariableUpdateServiceTest extends TestCase
*/
public function testVariableIsUpdatedWhenValidEnvironmentVariableIsPassed()
{
$this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->repository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'],
['egg_id', '=', $this->model->option_id],
['id', '!=', $this->model->id],
])->once()->andReturn(0);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
@ -101,7 +101,7 @@ class VariableUpdateServiceTest extends TestCase
*/
public function testNullOptionValueIsPassedAsArray()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
@ -116,14 +116,14 @@ class VariableUpdateServiceTest extends TestCase
*/
public function testDataPassedIntoHandlerTakesLowerPriorityThanDataSet()
{
$this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->repository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'],
['egg_id', '=', $this->model->option_id],
['id', '!=', $this->model->id],
])->once()->andReturn(0);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, [
'user_viewable' => false,
'user_editable' => false,
@ -138,7 +138,7 @@ class VariableUpdateServiceTest extends TestCase
*/
public function testExceptionIsThrownIfEnvironmentVariableIsNotUnique()
{
$this->repository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->repository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([
['env_variable', '=', 'TEST_VAR_123'],
['egg_id', '=', $this->model->option_id],

View file

@ -43,7 +43,7 @@ class NestUpdateServiceTest extends TestCase
*/
public function testAuthorArrayKeyIsRemovedIfPassed()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with(1, ['otherfield' => 'value'])->once()->andReturnNull();
$this->service->handle(1, ['author' => 'author1', 'otherfield' => 'value']);
@ -54,7 +54,7 @@ class NestUpdateServiceTest extends TestCase
*/
public function testServiceIsUpdatedWhenNoAuthorKeyIsPassed()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with(1, ['otherfield' => 'value'])->once()->andReturnNull();
$this->service->handle(1, ['otherfield' => 'value']);

View file

@ -62,14 +62,11 @@ class NodeDeletionServiceTest extends TestCase
*/
public function testNodeIsDeletedIfNoServersAreAttached()
{
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(0);
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(true);
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(1);
$this->assertTrue(
$this->service->handle(1),
'Assert that deletion returns a positive boolean value.'
);
$this->assertEquals(1, $this->service->handle(1));
}
/**
@ -79,7 +76,7 @@ class NodeDeletionServiceTest extends TestCase
*/
public function testExceptionIsThrownIfServersAreAttachedToNode()
{
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['node_id', '=', 1]])->once()->andReturn(1);
$this->translator->shouldReceive('trans')->with('exceptions.node.servers_attached')->once()->andReturnNull();
$this->repository->shouldNotReceive('delete');
@ -94,13 +91,10 @@ class NodeDeletionServiceTest extends TestCase
{
$node = factory(Node::class)->make();
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['node_id', '=', $node->id]])->once()->andReturn(0);
$this->repository->shouldReceive('delete')->with($node->id)->once()->andReturn(true);
$this->repository->shouldReceive('delete')->with($node->id)->once()->andReturn(1);
$this->assertTrue(
$this->service->handle($node),
'Assert that deletion returns a positive boolean value.'
);
$this->assertEquals(1, $this->service->handle($node));
}
}

View file

@ -15,6 +15,7 @@ use Tests\TestCase;
use Illuminate\Log\Writer;
use phpmock\phpunit\PHPMock;
use Pterodactyl\Models\Node;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Nodes\NodeUpdateService;
@ -84,13 +85,14 @@ class NodeUpdateServiceTest extends TestCase
$this->getFunctionMock('\\Pterodactyl\\Services\\Nodes', 'str_random')
->expects($this->once())->willReturn('random_string');
$this->repository->->shouldReceive('update')->with($this->node->id, [
'name' => 'NewName',
'daemonSecret' => 'random_string',
])->andReturn($this->node);
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->node->id, [
'name' => 'NewName',
'daemonSecret' => 'random_string',
])->andReturn(true);
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
$this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
$response = $this->service->handle($this->node, ['name' => 'NewName', 'reset_secret' => true]);
$this->assertInstanceOf(Node::class, $response);
@ -102,12 +104,13 @@ class NodeUpdateServiceTest extends TestCase
*/
public function testNodeIsUpdatedAndDaemonSecretIsNotChanged()
{
$this->repository->shouldReceive('update')->with($this->node->id, [
'name' => 'NewName',
])->andReturn($this->node);
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->node->id, [
'name' => 'NewName',
])->andReturn(true);
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
$this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
$response = $this->service->handle($this->node, ['name' => 'NewName']);
$this->assertInstanceOf(Node::class, $response);
@ -119,11 +122,12 @@ class NodeUpdateServiceTest extends TestCase
*/
public function testExceptionCausedByDaemonIsHandled()
{
$this->repository->->shouldReceive('update')->with($this->node->id, [
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->node->id, [
'name' => 'NewName',
])->andReturn(true);
])->andReturn(new Response);
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andThrow($this->exception);
$this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andThrow($this->exception);
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
@ -145,13 +149,13 @@ class NodeUpdateServiceTest extends TestCase
public function testFunctionCanAcceptANodeIdInPlaceOfModel()
{
$this->repository->shouldReceive('find')->with($this->node->id)->once()->andReturn($this->node);
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->node->id, [
'name' => 'NewName',
])->andReturn(true);
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
$this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
$this->assertTrue($this->service->handle($this->node->id, ['name' => 'NewName']));
}

View file

@ -76,7 +76,7 @@ class PackDeletionServiceTest extends TestCase
$this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(0);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturn(1);
$this->storage->shouldReceive('disk')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('deleteDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -91,11 +91,11 @@ class PackDeletionServiceTest extends TestCase
{
$model = factory(Pack::class)->make();
$this->repository->shouldReceive('withColumns')->with(['id', 'uuid'])->once()->andReturnSelf()
$this->repository->shouldReceive('setColumns')->with(['id', 'uuid'])->once()->andReturnSelf()
->shouldReceive('find')->with($model->id)->once()->andReturn($model);
$this->serverRepository->shouldReceive('findCountWhere')->with([['pack_id', '=', $model->id]])->once()->andReturn(0);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturn(1);
$this->storage->shouldReceive('disk')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('deleteDirectory')->with('packs/' . $model->uuid)->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();

View file

@ -53,7 +53,7 @@ class PackUpdateServiceTest extends TestCase
public function testPackIsUpdated()
{
$model = factory(Pack::class)->make();
$this->repository->shouldReceive('withoutFresh->update')->with($model->id, [
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
'locked' => false,
'visible' => false,
'selectable' => false,
@ -85,9 +85,9 @@ class PackUpdateServiceTest extends TestCase
{
$model = factory(Pack::class)->make();
$this->repository->shouldReceive('withColumns')->with(['id', 'egg_id'])->once()->andReturnSelf()
$this->repository->shouldReceive('setColumns')->with(['id', 'egg_id'])->once()->andReturnSelf()
->shouldReceive('find')->with($model->id)->once()->andReturn($model);
$this->repository->shouldReceive('withoutFresh->update')->with($model->id, [
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
'locked' => false,
'visible' => false,
'selectable' => false,

View file

@ -1,15 +1,9 @@
<?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\Schedules;
use Mockery as m;
use Carbon\Carbon;
use Tests\TestCase;
use Cron\CronExpression;
use Pterodactyl\Models\Task;
@ -46,8 +40,8 @@ class ProcessScheduleServiceTest extends TestCase
public function setUp()
{
parent::setUp();
Carbon::setTestNow(Carbon::now());
$this->cron = m::mock('overload:' . CronExpression::class);
$this->repository = m::mock(ScheduleRepositoryInterface::class);
$this->runnerService = m::mock(RunTaskService::class);
@ -64,14 +58,12 @@ class ProcessScheduleServiceTest extends TestCase
'sequence_id' => 1,
])]));
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
$formatted = sprintf('%s %s %s * %s *', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
$this->cron->shouldReceive('factory')->with($formatted)->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('00:00:00');
$this->repository->shouldReceive('update')->with($model->id, [
'is_processing' => true,
'next_run_at' => '00:00:00',
'next_run_at' => CronExpression::factory($formatted)->getNextRunDate(),
]);
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
@ -80,58 +72,23 @@ class ProcessScheduleServiceTest extends TestCase
$this->assertTrue(true);
}
/**
* Test that passing a schedule model without a tasks relation is handled.
*/
public function testScheduleModelWithoutTasksIsHandled()
{
$nonRelationModel = factory(Schedule::class)->make();
$model = clone $nonRelationModel;
$model->setRelation('tasks', collect([$task = factory(Task::class)->make([
'sequence_id' => 1,
])]));
$formatted = sprintf('%s %s %s * %s *', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
$this->repository->shouldReceive('getScheduleWithTasks')->with($nonRelationModel->id)->once()->andReturn($model);
$this->cron->shouldReceive('factory')->with($formatted)->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('00:00:00');
$this->repository->shouldReceive('update')->with($model->id, [
'is_processing' => true,
'next_run_at' => '00:00:00',
]);
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
$this->service->handle($nonRelationModel);
$this->assertTrue(true);
}
/**
* Test that a task ID can be passed in place of the task model.
*/
public function testPassingScheduleIdInPlaceOfModelIsHandled()
public function testScheduleRunTimeCanBeOverridden()
{
$model = factory(Schedule::class)->make();
$model->setRelation('tasks', collect([$task = factory(Task::class)->make([
'sequence_id' => 1,
])]));
$formatted = sprintf('%s %s %s * %s *', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
$this->repository->shouldReceive('getScheduleWithTasks')->with($model->id)->once()->andReturn($model);
$this->cron->shouldReceive('factory')->with($formatted)->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('00:00:00');
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
$this->repository->shouldReceive('update')->with($model->id, [
'is_processing' => true,
'next_run_at' => '00:00:00',
'next_run_at' => Carbon::now()->addSeconds(15)->toDateTimeString(),
]);
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
$this->service->handle($model->id);
$this->service->setRunTimeOverride(Carbon::now()->addSeconds(15))->handle($model);
$this->assertTrue(true);
}
}

View file

@ -1,17 +1,10 @@
<?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\Schedules;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Node;
use Cron\CronExpression;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Schedule;
use Illuminate\Database\ConnectionInterface;
@ -54,7 +47,6 @@ class ScheduleCreationServiceTest extends TestCase
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->cron = m::mock('overload:\Cron\CronExpression');
$this->repository = m::mock(ScheduleRepositoryInterface::class);
$this->taskCreationService = m::mock(TaskCreationService::class);
@ -69,18 +61,15 @@ class ScheduleCreationServiceTest extends TestCase
$schedule = factory(Schedule::class)->make();
$server = factory(Server::class)->make();
$this->cron->shouldReceive('factory')->with('* * * * * *')->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('nextDate');
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'server_id' => $server->id,
'next_run_at' => 'nextDate',
'test_data' => 'test_value',
'next_run_at' => CronExpression::factory('* * * * * *')->getNextRunDate(),
'test_key' => 'value',
])->once()->andReturn($schedule);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($server, ['test_data' => 'test_value']);
$this->assertNotEmpty($response);
$response = $this->service->handle($server, ['test_key' => 'value', 'server_id' => '123abc']);
$this->assertInstanceOf(Schedule::class, $response);
$this->assertEquals($schedule, $response);
}
@ -93,14 +82,13 @@ class ScheduleCreationServiceTest extends TestCase
$schedule = factory(Schedule::class)->make();
$server = factory(Server::class)->make();
$this->cron->shouldReceive('factory')->with('* * * * * *')->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('nextDate');
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'next_run_at' => 'nextDate',
'server_id' => $server->id,
'test_data' => 'test_value',
'next_run_at' => CronExpression::factory('* * * * * *')->getNextRunDate(),
'test_key' => 'value',
])->once()->andReturn($schedule);
$this->taskCreationService->shouldReceive('handle')->with($schedule, [
'time_interval' => 'm',
'time_value' => 10,
@ -111,62 +99,10 @@ class ScheduleCreationServiceTest extends TestCase
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($server, ['test_data' => 'test_value'], [
$response = $this->service->handle($server, ['test_key' => 'value'], [
['time_interval' => 'm', 'time_value' => 10, 'action' => 'test', 'payload' => 'testpayload'],
]);
$this->assertNotEmpty($response);
$this->assertInstanceOf(Schedule::class, $response);
$this->assertEquals($schedule, $response);
}
/**
* Test that an ID can be passed in place of the server model.
*/
public function testIdCanBePassedInPlaceOfServerModel()
{
$schedule = factory(Schedule::class)->make();
$this->cron->shouldReceive('factory')->with('* * * * * *')->once()->andReturnSelf()
->shouldReceive('getNextRunDate')->withNoArgs()->once()->andReturn('nextDate');
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'next_run_at' => 'nextDate',
'server_id' => 1234,
'test_data' => 'test_value',
])->once()->andReturn($schedule);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle(1234, ['test_data' => 'test_value']);
$this->assertNotEmpty($response);
$this->assertInstanceOf(Schedule::class, $response);
$this->assertEquals($schedule, $response);
}
/**
* Test that an exception is raised if invalid data is passed.
*
* @dataProvider invalidServerArgumentProvider
* @expectedException \InvalidArgumentException
*/
public function testExceptionIsThrownIfServerIsInvalid($attribute)
{
$this->service->handle($attribute, []);
}
/**
* Return an array of invalid server data to test aganist.
*
* @return array
*/
public function invalidServerArgumentProvider()
{
return [
[123.456],
['server'],
['abc123'],
['123_test'],
[new Node()],
[Server::class],
];
}
}

View file

@ -81,7 +81,7 @@ class TaskCreationServiceTest extends TestCase
{
$schedule = factory(Schedule::class)->make();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('create')->with([
'schedule_id' => $schedule->id,
'sequence_id' => 1,
@ -108,7 +108,7 @@ class TaskCreationServiceTest extends TestCase
*/
public function testIdCanBePassedInPlaceOfScheduleModel()
{
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('create')->with([
'schedule_id' => 1234,
'sequence_id' => 1,

View file

@ -1,42 +1,28 @@
<?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\Servers;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Servers\ContainerRebuildService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
class ContainerRebuildServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
*/
protected $daemonServerRepository;
protected $repository;
/**
* @var \GuzzleHttp\Exception\RequestException
*/
protected $exception;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Models\Server
*/
@ -47,11 +33,6 @@ class ContainerRebuildServiceTest extends TestCase
*/
protected $service;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
/**
* Setup tests.
*/
@ -59,81 +40,33 @@ class ContainerRebuildServiceTest extends TestCase
{
parent::setUp();
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
$this->exception = m::mock(RequestException::class)->makePartial();
$this->repository = m::mock(ServerRepositoryInterface::class);
$this->writer = m::mock(Writer::class);
$this->server = factory(Server::class)->make(['node_id' => 1]);
$this->service = new ContainerRebuildService(
$this->daemonServerRepository,
$this->repository,
$this->writer
);
$this->service = new ContainerRebuildService($this->repository);
}
/**
* Test that a server is marked for rebuild when it's model is passed to the function.
* Test that a server is marked for rebuild.
*/
public function testServerShouldBeMarkedForARebuildWhenModelIsPassed()
public function testServerIsMarkedForRebuild()
{
$this->repository->shouldNotReceive('find');
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('rebuild')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('rebuild')->withNoArgs()->once()->andReturn(new Response);
$this->service->rebuild($this->server);
}
/**
* Test that a server is marked for rebuild when the ID of the server is passed to the function.
*/
public function testServerShouldBeMarkedForARebuildWhenServerIdIsPassed()
{
$this->repository->shouldReceive('find')->with($this->server->id)->once()->andReturn($this->server);
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('rebuild')->withNoArgs()->once()->andReturnNull();
$this->service->rebuild($this->server->id);
$this->service->handle($this->server);
}
/**
* Test that an exception thrown by guzzle is rendered as a displayable exception.
*/
public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable()
{
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
try {
$this->service->rebuild($this->server);
} catch (Exception $exception) {
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(
trans('admin/server.exceptions.daemon_exception', ['code' => 400]),
$exception->getMessage()
);
}
}
/**
* Test that an exception thrown by something other than guzzle is not transformed to a displayable.
*
* @expectedException \Exception
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function testExceptionNotThrownByGuzzleShouldNotBeTransformedToDisplayable()
public function testExceptionThrownByGuzzle()
{
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
->once()->andThrow(new Exception());
$this->repository->shouldReceive('setServer')->with($this->server)->once()->andThrow($this->exception);
$this->service->rebuild($this->server);
$this->service->handle($this->server);
}
}

View file

@ -13,6 +13,7 @@ use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
@ -103,7 +104,7 @@ class DetailsModificationServiceTest extends TestCase
$data = ['owner_id' => 1, 'name' => 'New Name', 'description' => 'New Description'];
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'owner_id' => $data['owner_id'],
'name' => $data['name'],
@ -129,7 +130,7 @@ class DetailsModificationServiceTest extends TestCase
$this->repository->shouldReceive('find')->with($server->id)->once()->andReturn($server);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'owner_id' => $data['owner_id'],
'name' => $data['name'],
@ -155,7 +156,7 @@ class DetailsModificationServiceTest extends TestCase
$data = ['owner_id' => 2, 'name' => 'New Name', 'description' => 'New Description'];
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'owner_id' => $data['owner_id'],
'name' => $data['name'],
@ -178,18 +179,17 @@ class DetailsModificationServiceTest extends TestCase
$server = factory(Server::class)->make(['node_id' => 1]);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'image' => 'new/image',
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
$this->daemonServerRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('update')->with([
'build' => [
'image' => 'new/image',
],
])->once()->andReturnNull();
])->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -206,18 +206,17 @@ class DetailsModificationServiceTest extends TestCase
$this->repository->shouldReceive('find')->with($server->id)->once()->andReturn($server);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'image' => 'new/image',
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
$this->daemonServerRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('update')->with([
'build' => [
'image' => 'new/image',
],
])->once()->andReturnNull();
])->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -233,12 +232,12 @@ class DetailsModificationServiceTest extends TestCase
$server = factory(Server::class)->make(['node_id' => 1]);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'image' => 'new/image',
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->andThrow($this->exception);
$this->daemonServerRepository->shouldReceive('setServer')->andThrow($this->exception);
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
@ -266,7 +265,7 @@ class DetailsModificationServiceTest extends TestCase
$server = factory(Server::class)->make(['node_id' => 1]);
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'image' => 'new/image',
])->once()->andReturnNull();

View file

@ -12,11 +12,10 @@ namespace Tests\Unit\Services\Servers;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Servers\ReinstallServerService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
@ -53,11 +52,6 @@ class ReinstallServerServiceTest extends TestCase
*/
protected $service;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
/**
* Setup tests.
*/
@ -69,15 +63,13 @@ class ReinstallServerServiceTest extends TestCase
$this->database = m::mock(ConnectionInterface::class);
$this->exception = m::mock(RequestException::class)->makePartial();
$this->repository = m::mock(ServerRepositoryInterface::class);
$this->writer = m::mock(Writer::class);
$this->server = factory(Server::class)->make(['node_id' => 1]);
$this->service = new ReinstallServerService(
$this->database,
$this->daemonServerRepository,
$this->repository,
$this->writer
$this->repository
);
}
@ -89,14 +81,13 @@ class ReinstallServerServiceTest extends TestCase
$this->repository->shouldNotReceive('find');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->server->id, [
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->reinstall($this->server);
@ -110,14 +101,13 @@ class ReinstallServerServiceTest extends TestCase
$this->repository->shouldReceive('find')->with($this->server->id)->once()->andReturn($this->server);
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->server->id, [
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->reinstall($this->server->id);
@ -125,32 +115,20 @@ class ReinstallServerServiceTest extends TestCase
/**
* Test that an exception thrown by guzzle is rendered as a displayable exception.
*
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable()
{
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->server->id, [
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
->once()->andThrow($this->exception);
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
try {
$this->service->reinstall($this->server);
} catch (Exception $exception) {
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(
trans('admin/server.exceptions.daemon_exception', ['code' => 400]),
$exception->getMessage()
);
}
$this->service->reinstall($this->server);
}
/**
@ -161,13 +139,12 @@ class ReinstallServerServiceTest extends TestCase
public function testExceptionNotThrownByGuzzleShouldNotBeTransformedToDisplayable()
{
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->server->id, [
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
->once()->andThrow(new Exception());
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow(new Exception());
$this->service->reinstall($this->server);
}

View file

@ -4,6 +4,7 @@ namespace Tests\Unit\Services\Servers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\User;
use Tests\Traits\MocksUuids;
use Pterodactyl\Models\Server;
@ -116,7 +117,7 @@ class ServerCreationServiceTest extends TestCase
'egg_id' => $model->egg_id,
]))->once()->andReturn($model);
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->with($model->id, [$model->allocation_id])->once()->andReturnNull();
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->with($model->id, [$model->allocation_id])->once()->andReturn(1);
$this->validatorService->shouldReceive('setUserLevel')->with(User::USER_LEVEL_ADMIN)->once()->andReturnNull();
$this->validatorService->shouldReceive('handle')->with($model->egg_id, [])->once()->andReturn(
@ -129,10 +130,13 @@ class ServerCreationServiceTest extends TestCase
'variable_id' => 123,
'variable_value' => 'var1-value',
],
])->once()->andReturnNull();
])->once()->andReturn(true);
$this->configurationStructureService->shouldReceive('handle')->with($model)->once()->andReturn(['test' => 'struct']);
$this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf();
$node = factory(Node::class)->make();
$this->nodeRepository->shouldReceive('find')->with($model->node_id)->once()->andReturn($node);
$this->daemonServerRepository->shouldReceive('setNode')->with($node)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -154,11 +158,14 @@ class ServerCreationServiceTest extends TestCase
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->once()->andReturn($model);
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturnNull();
$this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturn(1);
$this->validatorService->shouldReceive('setUserLevel')->once()->andReturnNull();
$this->validatorService->shouldReceive('handle')->once()->andReturn(collect([]));
$this->configurationStructureService->shouldReceive('handle')->once()->andReturn([]);
$this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andThrow($this->exception);
$node = factory(Node::class)->make();
$this->nodeRepository->shouldReceive('find')->with($model->node_id)->once()->andReturn($node);
$this->daemonServerRepository->shouldReceive('setNode')->with($node)->once()->andThrow($this->exception);
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try {

View file

@ -13,6 +13,7 @@ use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
@ -111,18 +112,17 @@ class ServerDeletionServiceTest extends TestCase
*/
public function testServerCanBeDeletedWithoutForce()
{
$this->daemonServerRepository->shouldReceive('setNode')->with($this->model->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->model->uuid)->once()->andReturnSelf()
->shouldReceive('delete')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf()
->shouldReceive('delete')->withNoArgs()->once()->andReturn(new Response);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->databaseRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findWhere')->with([
['server_id', '=', $this->model->id],
])->once()->andReturn(collect([(object) ['id' => 50]]));
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturn(1);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($this->model);
@ -133,20 +133,19 @@ class ServerDeletionServiceTest extends TestCase
*/
public function testServerShouldBeDeletedEvenWhenFailureOccursIfForceIsSet()
{
$this->daemonServerRepository->shouldReceive('setNode')->with($this->model->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->model->uuid)->once()->andReturnSelf()
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf()
->shouldReceive('delete')->withNoArgs()->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->databaseRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findWhere')->with([
['server_id', '=', $this->model->id],
])->once()->andReturn(collect([(object) ['id' => 50]]));
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturn(1);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->withForce()->handle($this->model);
@ -157,7 +156,7 @@ class ServerDeletionServiceTest extends TestCase
*/
public function testExceptionShouldBeThrownIfDaemonReturnsAnErrorAndForceIsNotSet()
{
$this->daemonServerRepository->shouldReceive('setNode->setAccessServer->delete')->once()->andThrow($this->exception);
$this->daemonServerRepository->shouldReceive('setServer->delete')->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
@ -176,21 +175,20 @@ class ServerDeletionServiceTest extends TestCase
*/
public function testIntegerCanBePassedInPlaceOfServerModel()
{
$this->repository->shouldReceive('withColumns')->with(['id', 'node_id', 'uuid'])->once()->andReturnSelf()
$this->repository->shouldReceive('setColumns')->with(['id', 'node_id', 'uuid'])->once()->andReturnSelf()
->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
$this->daemonServerRepository->shouldReceive('setNode')->with($this->model->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->model->uuid)->once()->andReturnSelf()
->shouldReceive('delete')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf()
->shouldReceive('delete')->withNoArgs()->once()->andReturn(new Response);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->databaseRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findWhere')->with([
['server_id', '=', $this->model->id],
])->once()->andReturn(collect([(object) ['id' => 50]]));
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturn(1);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($this->model->id);

View file

@ -12,6 +12,7 @@ namespace Tests\Unit\Services\Servers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Servers\EnvironmentService;
@ -81,18 +82,17 @@ class StartupModificationServiceTest extends TestCase
collect([(object) ['id' => 1, 'value' => 'stored-value']])
);
$this->serverVariableRepository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf();
$this->serverVariableRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
$this->serverVariableRepository->shouldReceive('updateOrCreate')->with([
'server_id' => $model->id,
'variable_id' => 1,
], ['variable_value' => 'stored-value'])->once()->andReturnNull();
$this->environmentService->shouldReceive('handle')->with($model)->once()->andReturn(['env']);
$this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('update')->with([
'build' => ['env|overwrite' => ['env']],
])->once()->andReturnSelf();
])->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -116,7 +116,7 @@ class StartupModificationServiceTest extends TestCase
collect([(object) ['id' => 1, 'value' => 'stored-value']])
);
$this->serverVariableRepository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf();
$this->serverVariableRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
$this->serverVariableRepository->shouldReceive('updateOrCreate')->with([
'server_id' => $model->id,
'variable_id' => 1,
@ -135,8 +135,7 @@ class StartupModificationServiceTest extends TestCase
$this->environmentService->shouldReceive('handle')->with($model)->once()->andReturn(['env']);
$this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('update')->with([
'build' => [
'env|overwrite' => ['env'],
@ -147,7 +146,7 @@ class StartupModificationServiceTest extends TestCase
'pack' => 'xyz987',
'skip_scripts' => false,
],
])->once()->andReturnSelf();
])->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();

View file

@ -13,6 +13,7 @@ use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
@ -101,12 +102,11 @@ class SuspensionServiceTest extends TestCase
$this->server->suspended = 0;
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->server->id, ['suspended' => true])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('suspend')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('suspend')->withNoArgs()->once()->andReturn(new Response);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->assertTrue($this->service->toggle($this->server));
@ -120,12 +120,11 @@ class SuspensionServiceTest extends TestCase
$this->server->suspended = 1;
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->server->id, ['suspended' => false])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('unsuspend')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('unsuspend')->withNoArgs()->once()->andReturn(new Response);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->assertTrue($this->service->toggle($this->server, 'unsuspend'));
@ -159,10 +158,10 @@ class SuspensionServiceTest extends TestCase
$this->server->suspended = 0;
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->server->id, ['suspended' => true])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)
->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()

View file

@ -49,10 +49,10 @@ class AuthenticateUsingPasswordServiceTest extends TestCase
$user = factory(User::class)->make(['root_admin' => 0]);
$server = factory(Server::class)->make(['node_id' => 1, 'owner_id' => $user->id]);
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('setColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', $user->username]])->once()->andReturn($user);
$this->repository->shouldReceive('withColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
$this->repository->shouldReceive('setColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
$this->repository->shouldReceive('getByUuid')->with($server->uuidShort)->once()->andReturn($server);
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('server_token');
@ -74,10 +74,10 @@ class AuthenticateUsingPasswordServiceTest extends TestCase
$user = factory(User::class)->make(['root_admin' => 1]);
$server = factory(Server::class)->make(['node_id' => 1, 'owner_id' => $user->id + 1]);
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('setColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', $user->username]])->once()->andReturn($user);
$this->repository->shouldReceive('withColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
$this->repository->shouldReceive('setColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
$this->repository->shouldReceive('getByUuid')->with($server->uuidShort)->once()->andReturn($server);
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('server_token');
@ -110,7 +110,7 @@ class AuthenticateUsingPasswordServiceTest extends TestCase
{
$user = factory(User::class)->make();
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('setColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', $user->username]])->once()->andReturn($user);
$this->getService()->handle($user->username, 'wrongpassword', 1, '1234');
@ -123,7 +123,7 @@ class AuthenticateUsingPasswordServiceTest extends TestCase
*/
public function testExceptionIsThrownIfNoUserAccountIsFound()
{
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('setColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', 'something']])->once()->andThrow(new RecordNotFoundException);
$this->getService()->handle('something', 'password', 1, '1234');
@ -140,10 +140,10 @@ class AuthenticateUsingPasswordServiceTest extends TestCase
$user = factory(User::class)->make(['root_admin' => 0]);
$server = factory(Server::class)->make(['node_id' => 1, 'owner_id' => $user->id + 1]);
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('setColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', $user->username]])->once()->andReturn($user);
$this->repository->shouldReceive('withColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
$this->repository->shouldReceive('setColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
$this->repository->shouldReceive('getByUuid')->with($server->uuidShort)->once()->andReturn($server);
$this->getService()->handle($user->username, 'password', 1, $server->uuidShort);
@ -160,10 +160,10 @@ class AuthenticateUsingPasswordServiceTest extends TestCase
$user = factory(User::class)->make(['root_admin' => 0]);
$server = factory(Server::class)->make(['node_id' => 2, 'owner_id' => $user->id]);
$this->userRepository->shouldReceive('withColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('setColumns')->with(['id', 'root_admin', 'password'])->once()->andReturnSelf();
$this->userRepository->shouldReceive('findFirstWhere')->with([['username', '=', $user->username]])->once()->andReturn($user);
$this->repository->shouldReceive('withColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
$this->repository->shouldReceive('setColumns')->with(['id', 'node_id', 'owner_id', 'uuid'])->once()->andReturnSelf();
$this->repository->shouldReceive('getByUuid')->with($server->uuidShort)->once()->andReturn($server);
$this->getService()->handle($user->username, 'password', 1, $server->uuidShort);

View file

@ -44,11 +44,11 @@ class PermissionCreationServiceTest extends TestCase
{
$permissions = ['reset-sftp', 'view-sftp'];
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('insert')->with([
['subuser_id' => 1, 'permission' => 'reset-sftp'],
['subuser_id' => 1, 'permission' => 'view-sftp'],
])->once()->andReturnNull();
])->once()->andReturn(true);
$this->service->handle(1, $permissions);
$this->assertTrue(true);

View file

@ -55,7 +55,7 @@ class SubuserDeletionServiceTest extends TestCase
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->keyDeletionService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($subuser->id)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($subuser->id)->once()->andReturn(1);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->getService()->handle($subuser);

View file

@ -12,6 +12,7 @@ namespace Tests\Unit\Services\Subusers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
use Tests\Traits\MocksRequestException;
@ -86,12 +87,12 @@ class SubuserUpdateServiceTest extends TestCase
$this->repository->shouldReceive('loadServerAndUserRelations')->with($subuser)->once()->andReturn($subuser);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturnNull();
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturn(1);
$this->permissionService->shouldReceive('handle')->with($subuser->id, ['some-permission'])->once()->andReturnNull();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturnNull();
$this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -112,11 +113,11 @@ class SubuserUpdateServiceTest extends TestCase
$this->repository->shouldReceive('loadServerAndUserRelations')->with($subuser)->once()->andReturn($subuser);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturnNull();
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturn(1);
$this->permissionService->shouldReceive('handle')->with($subuser->id, [])->once()->andReturnNull();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andThrow($this->getExceptionMock());
$this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andThrow($this->getExceptionMock());
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try {

View file

@ -63,7 +63,7 @@ class ToggleTwoFactorServiceTest extends TestCase
$model = factory(User::class)->make(['totp_secret' => self::USER_TOTP_SECRET, 'use_totp' => false]);
$this->google2FA->shouldReceive('verifyKey')->with(self::DECRYPTED_USER_SECRET, 'test-token', self::TEST_WINDOW_INT)->once()->andReturn(true);
$this->repository->shouldReceive('withoutFresh->update')->with($model->id, [
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
'totp_authenticated_at' => Carbon::now(),
'use_totp' => true,
])->once()->andReturnNull();
@ -79,7 +79,7 @@ class ToggleTwoFactorServiceTest extends TestCase
$model = factory(User::class)->make(['totp_secret' => self::USER_TOTP_SECRET, 'use_totp' => true]);
$this->google2FA->shouldReceive('verifyKey')->with(self::DECRYPTED_USER_SECRET, 'test-token', self::TEST_WINDOW_INT)->once()->andReturn(true);
$this->repository->shouldReceive('withoutFresh->update')->with($model->id, [
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
'totp_authenticated_at' => Carbon::now(),
'use_totp' => false,
])->once()->andReturnNull();
@ -95,7 +95,7 @@ class ToggleTwoFactorServiceTest extends TestCase
$model = factory(User::class)->make(['totp_secret' => self::USER_TOTP_SECRET, 'use_totp' => false]);
$this->google2FA->shouldReceive('verifyKey')->with(self::DECRYPTED_USER_SECRET, 'test-token', self::TEST_WINDOW_INT)->once()->andReturn(true);
$this->repository->shouldReceive('withoutFresh->update')->with($model->id, [
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
'totp_authenticated_at' => Carbon::now(),
'use_totp' => false,
])->once()->andReturnNull();

View file

@ -58,7 +58,7 @@ class TwoFactorSetupServiceTest extends TestCase
$this->config->shouldReceive('get')->with('app.name')->once()->andReturn('CompanyName');
$this->google2FA->shouldReceive('getQRCodeGoogleUrl')->with('CompanyName', $model->email, 'secretKey')->once()->andReturn('http://url.com');
$this->encrypter->shouldReceive('encrypt')->with('secretKey')->once()->andReturn('encryptedSecret');
$this->repository->shouldReceive('withoutFresh->update')->with($model->id, ['totp_secret' => 'encryptedSecret'])->once()->andReturnNull();
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, ['totp_secret' => 'encryptedSecret'])->once()->andReturnNull();
$response = $this->getService()->handle($model);
$this->assertNotEmpty($response);

View file

@ -68,14 +68,11 @@ class UserDeletionServiceTest extends TestCase
*/
public function testUserIsDeletedIfNoServersAreAttachedToAccount()
{
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn(0);
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(true);
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(1);
$this->assertTrue(
$this->service->handle($this->user->id),
'Assert that service responds true.'
);
$this->assertEquals(1, $this->service->handle($this->user->id));
}
/**
@ -85,7 +82,7 @@ class UserDeletionServiceTest extends TestCase
*/
public function testExceptionIsThrownIfServersAreAttachedToAccount()
{
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn(1);
$this->translator->shouldReceive('trans')->with('admin/user.exceptions.user_has_servers')->once()->andReturnNull();
@ -97,13 +94,10 @@ class UserDeletionServiceTest extends TestCase
*/
public function testModelCanBePassedInPlaceOfUserId()
{
$this->serverRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
->shouldReceive('findCountWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn(0);
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(true);
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(1);
$this->assertTrue(
$this->service->handle($this->user),
'Assert that service responds true.'
);
$this->assertEquals(1, $this->service->handle($this->user));
}
}