2020-06-27 19:04:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Client;
|
|
|
|
|
2020-06-28 20:50:07 +00:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use ReflectionClass;
|
|
|
|
use Carbon\CarbonImmutable;
|
2020-06-27 19:04:41 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
2020-06-28 21:41:22 +00:00
|
|
|
use Pterodactyl\Models\Task;
|
2020-06-27 19:04:41 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2020-06-28 21:41:22 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2020-06-27 19:04:41 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2021-01-20 05:20:55 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2020-06-27 19:04:41 +00:00
|
|
|
use Pterodactyl\Models\Subuser;
|
|
|
|
use Pterodactyl\Models\Location;
|
2020-06-28 21:41:22 +00:00
|
|
|
use Pterodactyl\Models\Schedule;
|
2021-01-20 05:20:55 +00:00
|
|
|
use Pterodactyl\Models\Database;
|
2020-06-28 20:50:07 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2020-07-11 04:17:28 +00:00
|
|
|
use Pterodactyl\Models\Allocation;
|
2021-01-20 05:20:55 +00:00
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
2021-01-20 04:11:00 +00:00
|
|
|
use Pterodactyl\Tests\Integration\TestResponse;
|
2020-06-27 19:04:41 +00:00
|
|
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
2020-06-28 20:50:07 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\BaseClientTransformer;
|
2020-06-27 19:04:41 +00:00
|
|
|
|
|
|
|
abstract class ClientApiIntegrationTestCase extends IntegrationTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Cleanup after running tests.
|
|
|
|
*/
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
2021-01-20 05:20:55 +00:00
|
|
|
Database::query()->forceDelete();
|
|
|
|
DatabaseHost::query()->forceDelete();
|
|
|
|
Backup::query()->forceDelete();
|
2020-06-27 19:04:41 +00:00
|
|
|
Server::query()->forceDelete();
|
|
|
|
Node::query()->forceDelete();
|
|
|
|
Location::query()->forceDelete();
|
|
|
|
User::query()->forceDelete();
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2020-06-28 20:50:07 +00:00
|
|
|
/**
|
|
|
|
* Setup tests and ensure all of the times are always the same.
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Carbon::setTestNow(Carbon::now());
|
|
|
|
CarbonImmutable::setTestNow(Carbon::now());
|
|
|
|
}
|
|
|
|
|
2021-01-20 04:11:00 +00:00
|
|
|
/**
|
|
|
|
* Override the default createTestResponse from Illuminate so that we can
|
|
|
|
* just dump 500-level errors to the screen in the tests without having
|
|
|
|
* to keep re-assigning variables.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Response $response
|
|
|
|
* @return \Illuminate\Testing\TestResponse
|
|
|
|
*/
|
|
|
|
protected function createTestResponse($response)
|
|
|
|
{
|
|
|
|
return TestResponse::fromBaseResponse($response);
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:41:22 +00:00
|
|
|
/**
|
|
|
|
* Returns a link to the specific resource using the client API.
|
|
|
|
*
|
|
|
|
* @param mixed $model
|
|
|
|
* @param string|null $append
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function link($model, $append = null): string
|
|
|
|
{
|
2020-07-11 04:17:28 +00:00
|
|
|
Assert::isInstanceOfAny($model, [Server::class, Schedule::class, Task::class, Allocation::class]);
|
2020-06-28 21:41:22 +00:00
|
|
|
|
|
|
|
$link = '';
|
|
|
|
switch (get_class($model)) {
|
|
|
|
case Server::class:
|
|
|
|
$link = "/api/client/servers/{$model->uuid}";
|
|
|
|
break;
|
|
|
|
case Schedule::class:
|
|
|
|
$link = "/api/client/servers/{$model->server->uuid}/schedules/{$model->id}";
|
|
|
|
break;
|
|
|
|
case Task::class:
|
|
|
|
$link = "/api/client/servers/{$model->schedule->server->uuid}/schedules/{$model->schedule->id}/tasks/{$model->id}";
|
|
|
|
break;
|
2020-07-11 04:17:28 +00:00
|
|
|
case Allocation::class:
|
|
|
|
$link = "/api/client/servers/{$model->server->uuid}/network/allocations/{$model->id}";
|
|
|
|
break;
|
2020-06-28 21:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $link . ($append ? '/' . ltrim($append, '/') : '');
|
|
|
|
}
|
|
|
|
|
2020-06-27 19:04:41 +00:00
|
|
|
/**
|
|
|
|
* Generates a user and a server for that user. If an array of permissions is passed it
|
|
|
|
* is assumed that the user is actually a subuser of the server.
|
|
|
|
*
|
|
|
|
* @param string[] $permissions
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function generateTestAccount(array $permissions = []): array
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
|
|
|
|
if (empty($permissions)) {
|
|
|
|
return [$user, $this->createServerModel(['user_id' => $user->id])];
|
|
|
|
}
|
|
|
|
|
|
|
|
$server = $this->createServerModel();
|
|
|
|
|
|
|
|
Subuser::query()->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'server_id' => $server->id,
|
|
|
|
'permissions' => $permissions,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return [$user, $server];
|
|
|
|
}
|
2020-06-28 20:50:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that the data passed through matches the output of the data from the transformer. This
|
|
|
|
* will remove the "relationships" key when performing the comparison.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param \Pterodactyl\Models\Model|\Illuminate\Database\Eloquent\Model $model
|
|
|
|
*/
|
|
|
|
protected function assertJsonTransformedWith(array $data, $model)
|
|
|
|
{
|
|
|
|
$reflect = new ReflectionClass($model);
|
|
|
|
$transformer = sprintf('\\Pterodactyl\\Transformers\\Api\\Client\\%sTransformer', $reflect->getShortName());
|
|
|
|
|
|
|
|
$transformer = new $transformer;
|
|
|
|
$this->assertInstanceOf(BaseClientTransformer::class, $transformer);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$transformer->transform($model),
|
|
|
|
Collection::make($data)->except(['relationships'])->toArray()
|
|
|
|
);
|
|
|
|
}
|
2020-06-27 19:04:41 +00:00
|
|
|
}
|