2017-09-02 23:56:15 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-09-02 23:56:15 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-09-02 23:56:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Http\Controllers\Base;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Pterodactyl\Models\User;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2017-09-02 23:56:15 +00:00
|
|
|
use Tests\Assertions\ControllerAssertionsTrait;
|
2017-11-05 18:38:39 +00:00
|
|
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Base\IndexController;
|
2017-09-25 03:28:16 +00:00
|
|
|
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
class IndexControllerTest extends ControllerTestCase
|
2017-09-02 23:56:15 +00:00
|
|
|
{
|
|
|
|
use ControllerAssertionsTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Http\Controllers\Base\IndexController
|
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
2017-09-02 23:56:15 +00:00
|
|
|
*/
|
|
|
|
protected $daemonRepository;
|
|
|
|
|
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
protected $keyProviderService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
2017-09-02 23:56:15 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
|
2017-09-25 03:28:16 +00:00
|
|
|
$this->keyProviderService = m::mock(DaemonKeyProviderService::class);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->repository = m::mock(ServerRepositoryInterface::class);
|
|
|
|
|
2017-09-25 03:28:16 +00:00
|
|
|
$this->controller = new IndexController($this->keyProviderService, $this->daemonRepository, $this->repository);
|
2017-09-02 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the index controller.
|
|
|
|
*/
|
|
|
|
public function testIndexController()
|
|
|
|
{
|
2017-11-05 18:38:39 +00:00
|
|
|
$model = $this->setRequestUser();
|
2017-09-02 23:56:15 +00:00
|
|
|
|
|
|
|
$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']);
|
|
|
|
|
|
|
|
$response = $this->controller->getIndex($this->request);
|
|
|
|
$this->assertIsViewResponse($response);
|
|
|
|
$this->assertViewNameEquals('base.index', $response);
|
|
|
|
$this->assertViewHasKey('servers', $response);
|
|
|
|
$this->assertViewKeyEquals('servers', ['test'], $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the status controller.
|
|
|
|
*/
|
|
|
|
public function testStatusController()
|
|
|
|
{
|
2017-11-05 18:38:39 +00:00
|
|
|
$user = $this->setRequestUser();
|
2017-09-02 23:56:15 +00:00
|
|
|
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
|
|
|
|
|
2017-09-25 03:28:16 +00:00
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
|
2017-11-05 18:38:39 +00:00
|
|
|
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
|
2017-09-02 23:56:15 +00:00
|
|
|
|
|
|
|
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
|
2017-09-25 03:28:16 +00:00
|
|
|
->shouldReceive('setAccessToken')->with('test123')->once()->andReturnSelf()
|
2017-09-02 23:56:15 +00:00
|
|
|
->shouldReceive('details')->withNoArgs()->once()->andReturnSelf();
|
|
|
|
|
|
|
|
$this->daemonRepository->shouldReceive('getBody')->withNoArgs()->once()->andReturn('["test"]');
|
|
|
|
|
2017-09-25 03:28:16 +00:00
|
|
|
$response = $this->controller->status($this->request, $server->uuidShort);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsJsonResponse($response);
|
|
|
|
$this->assertResponseJsonEquals(['test'], $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the status controller if a server is not installed.
|
|
|
|
*/
|
|
|
|
public function testStatusControllerWhenServerNotInstalled()
|
|
|
|
{
|
2017-11-05 18:38:39 +00:00
|
|
|
$user = $this->setRequestUser();
|
2017-09-02 23:56:15 +00:00
|
|
|
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 0]);
|
|
|
|
|
2017-09-25 03:28:16 +00:00
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
|
2017-11-05 18:38:39 +00:00
|
|
|
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2017-09-25 03:28:16 +00:00
|
|
|
$response = $this->controller->status($this->request, $server->uuidShort);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsJsonResponse($response);
|
|
|
|
$this->assertResponseCodeEquals(200, $response);
|
|
|
|
$this->assertResponseJsonEquals(['status' => 20], $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the status controller when a server is suspended.
|
|
|
|
*/
|
|
|
|
public function testStatusControllerWhenServerIsSuspended()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->make();
|
|
|
|
$server = factory(Server::class)->make(['suspended' => 1, 'installed' => 1]);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
|
2017-09-25 03:28:16 +00:00
|
|
|
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
|
2017-11-05 18:38:39 +00:00
|
|
|
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2017-09-25 03:28:16 +00:00
|
|
|
$response = $this->controller->status($this->request, $server->uuidShort);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsJsonResponse($response);
|
|
|
|
$this->assertResponseCodeEquals(200, $response);
|
|
|
|
$this->assertResponseJsonEquals(['status' => 30], $response);
|
|
|
|
}
|
|
|
|
}
|