Skip to content

Commit 68f0811

Browse files
committed
Merge branch 'feature/api-integration-testing' into develop
2 parents f1a76ec + 565c5dd commit 68f0811

30 files changed

+1374
-80
lines changed

.env.travis

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ APP_THEME=pterodactyl
55
APP_TIMEZONE=UTC
66
APP_URL=http://localhost/
77

8-
DB_HOST=127.0.0.1
9-
DB_DATABASE=travis
10-
DB_USERNAME=root
11-
DB_PASSWORD=""
8+
TESTING_DB_HOST=127.0.0.1
9+
TESTING_DB_DATABASE=travis
10+
TESTING_DB_USERNAME=root
11+
TESTING_DB_PASSWORD=""
1212

1313
CACHE_DRIVER=array
1414
SESSION_DRIVER=array

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ before_script:
1414
- echo 'opcache.enable_cli=1' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
1515
- cp .env.travis .env
1616
- travis_retry composer install --no-interaction --prefer-dist --no-suggest
17-
- php artisan migrate --seed
1817
script:
19-
- vendor/bin/phpunit --coverage-clover coverage.xml
18+
- vendor/bin/phpunit --bootstrap vendor/autoload.php --coverage-clover coverage.xml tests/Unit
19+
- vendor/bin/phpunit tests/Integration
2020
notifications:
2121
email: false
2222
webhooks:

app/Exceptions/Handler.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717

1818
class Handler extends ExceptionHandler
1919
{
20+
/**
21+
* Laravel's validation parser formats custom rules using the class name
22+
* resulting in some weird rule names. This string will be parsed out and
23+
* replaced with 'p_' in the response code.
24+
*/
25+
private const PTERODACTYL_RULE_STRING = 'pterodactyl\_rules\_';
26+
2027
/**
2128
* A list of the exception types that should not be reported.
2229
*
@@ -156,7 +163,9 @@ public function invalidJson($request, ValidationException $exception)
156163
$response = [];
157164
foreach ($errors as $key => $error) {
158165
$response[] = [
159-
'code' => array_get($codes, str_replace('.', '_', $field) . '.' . $key),
166+
'code' => str_replace(self::PTERODACTYL_RULE_STRING, 'p_', array_get(
167+
$codes, str_replace('.', '_', $field) . '.' . $key
168+
)),
160169
'detail' => $error,
161170
'source' => ['field' => $field],
162171
];

app/Services/Helpers/TemporaryPasswordService.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,14 @@
99

1010
namespace Pterodactyl\Services\Helpers;
1111

12+
use Ramsey\Uuid\Uuid;
1213
use Illuminate\Contracts\Hashing\Hasher;
1314
use Illuminate\Database\ConnectionInterface;
14-
use Illuminate\Contracts\Config\Repository as ConfigRepository;
1515

1616
class TemporaryPasswordService
1717
{
1818
const HMAC_ALGO = 'sha256';
1919

20-
/**
21-
* @var \Illuminate\Contracts\Config\Repository
22-
*/
23-
protected $config;
24-
2520
/**
2621
* @var \Illuminate\Database\ConnectionInterface
2722
*/
@@ -35,16 +30,11 @@ class TemporaryPasswordService
3530
/**
3631
* TemporaryPasswordService constructor.
3732
*
38-
* @param \Illuminate\Contracts\Config\Repository $config
3933
* @param \Illuminate\Database\ConnectionInterface $connection
4034
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
4135
*/
42-
public function __construct(
43-
ConfigRepository $config,
44-
ConnectionInterface $connection,
45-
Hasher $hasher
46-
) {
47-
$this->config = $config;
36+
public function __construct(ConnectionInterface $connection, Hasher $hasher)
37+
{
4838
$this->connection = $connection;
4939
$this->hasher = $hasher;
5040
}
@@ -57,7 +47,7 @@ public function __construct(
5747
*/
5848
public function handle($email)
5949
{
60-
$token = hash_hmac(self::HMAC_ALGO, str_random(40), $this->config->get('app.key'));
50+
$token = hash_hmac(self::HMAC_ALGO, Uuid::uuid4()->toString(), config('app.key'));
6151

6252
$this->connection->table('password_resets')->insert([
6353
'email' => $email,

app/Transformers/Api/Application/BaseTransformer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
use Cake\Chronos\Chronos;
66
use Pterodactyl\Models\ApiKey;
77
use Illuminate\Container\Container;
8+
use Illuminate\Database\Eloquent\Model;
89
use League\Fractal\TransformerAbstract;
910
use Pterodactyl\Services\Acl\Api\AdminAcl;
11+
use Pterodactyl\Transformers\Api\Client\BaseClientTransformer;
1012
use Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException;
1113

14+
/**
15+
* @method array transform(Model $model)
16+
*/
1217
abstract class BaseTransformer extends TransformerAbstract
1318
{
1419
const RESPONSE_TIMEZONE = 'UTC';
@@ -88,7 +93,7 @@ protected function makeTransformer(string $abstract, array $parameters = [])
8893
$transformer = Container::getInstance()->makeWith($abstract, $parameters);
8994
$transformer->setKey($this->getKey());
9095

91-
if (! $transformer instanceof self) {
96+
if (! $transformer instanceof self || $transformer instanceof BaseClientTransformer) {
9297
throw new InvalidTransformerLevelException('Calls to ' . __METHOD__ . ' must return a transformer that is an instance of ' . __CLASS__);
9398
}
9499

app/Transformers/Api/Application/EggTransformer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public function transform(Egg $model)
4646
'description' => $model->description,
4747
'docker_image' => $model->docker_image,
4848
'config' => [
49-
'files' => json_decode($model->config_files),
50-
'startup' => json_decode($model->config_startup),
49+
'files' => json_decode($model->config_files, true),
50+
'startup' => json_decode($model->config_startup, true),
5151
'stop' => $model->config_stop,
52-
'logs' => json_decode($model->config_logs),
52+
'logs' => json_decode($model->config_logs, true),
5353
'extends' => $model->config_from,
5454
],
5555
'startup' => $model->startup,

app/Transformers/Api/Application/LocationTransformer.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,22 @@ public function getResourceName(): string
3232
*/
3333
public function transform(Location $location): array
3434
{
35-
return $location->toArray();
35+
return [
36+
'id' => $location->id,
37+
'short' => $location->short,
38+
'long' => $location->long,
39+
$location->getUpdatedAtColumn() => $this->formatTimestamp($location->updated_at),
40+
$location->getCreatedAtColumn() => $this->formatTimestamp($location->created_at),
41+
];
3642
}
3743

3844
/**
3945
* Return the nodes associated with this location.
4046
*
4147
* @param \Pterodactyl\Models\Location $location
4248
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
49+
*
50+
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
4351
*/
4452
public function includeServers(Location $location)
4553
{
@@ -57,6 +65,8 @@ public function includeServers(Location $location)
5765
*
5866
* @param \Pterodactyl\Models\Location $location
5967
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
68+
*
69+
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
6070
*/
6171
public function includeNodes(Location $location)
6272
{

app/Transformers/Api/Application/NestTransformer.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Pterodactyl\Models\Egg;
66
use Pterodactyl\Models\Nest;
7+
use Pterodactyl\Models\Server;
78
use Pterodactyl\Services\Acl\Api\AdminAcl;
89

910
class NestTransformer extends BaseTransformer
@@ -49,6 +50,8 @@ public function transform(Nest $model)
4950
*
5051
* @param \Pterodactyl\Models\Nest $model
5152
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
53+
*
54+
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
5255
*/
5356
public function includeEggs(Nest $model)
5457
{
@@ -60,4 +63,23 @@ public function includeEggs(Nest $model)
6063

6164
return $this->collection($model->getRelation('eggs'), $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME);
6265
}
66+
67+
/**
68+
* Include the servers relationship on the given Nest model.
69+
*
70+
* @param \Pterodactyl\Models\Nest $model
71+
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
72+
*
73+
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
74+
*/
75+
public function includeServers(Nest $model)
76+
{
77+
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
78+
return $this->null();
79+
}
80+
81+
$model->loadMissing('servers');
82+
83+
return $this->collection($model->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), Server::RESOURCE_NAME);
84+
}
6385
}

app/Transformers/Api/Application/UserTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public function transform(User $user): array
5353
*
5454
* @param \Pterodactyl\Models\User $user
5555
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
56+
*
57+
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
5658
*/
5759
public function includeServers(User $user)
5860
{

bootstrap/tests.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Contracts\Console\Kernel;
4+
use Symfony\Component\Console\Output\ConsoleOutput;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
$app = require __DIR__ . '/app.php';
9+
10+
/** @var \Pterodactyl\Console\Kernel $kernel */
11+
$kernel = $app->make(Kernel::class);
12+
13+
/**
14+
* Bootstrap the kernel and prepare application for testing.
15+
*/
16+
$kernel->bootstrap();
17+
18+
$output = new ConsoleOutput;
19+
20+
/**
21+
* Perform database migrations and reseeding before continuing with
22+
* running the tests.
23+
*/
24+
$output->writeln(PHP_EOL . '<comment>Refreshing database for Integration tests...</comment>');
25+
$kernel->call('migrate:fresh', ['--database' => 'testing']);
26+
27+
$output->writeln('<comment>Seeding database for Integration tests...</comment>' . PHP_EOL);
28+
$kernel->call('db:seed', ['--database' => 'testing']);

0 commit comments

Comments
 (0)