diff --git a/CHANGELOG.md b/CHANGELOG.md index fa25d0db..a623e9d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. * `[beta.3]` — Fixes an edge case caused by the Laravel 5.5 upgrade that would try to perform an in_array check aganist a null value. * `[beta.3]` — Fixes a bug that would cause an error when attempting to create a new user on the Panel. * `[beta.3]` — Fixes error handling of the settings service provider when no migrations have been run. +* `[beta.3]` — Fixes validation error when trying to use 'None' as the 'Copy Script From' option for an egg script. +* Fixes a design bug in the database that prevented the storage of negative numbers, thus preventing a server from being assigned unlimited swap. + +### Added +* Nest and Egg listings now show the associated ID in order to make API requests easier. ## v0.7.0-beta.3 (Derelict Dermodactylus) ### Fixed diff --git a/app/Http/Controllers/Server/Files/FileActionsController.php b/app/Http/Controllers/Server/Files/FileActionsController.php index 9c5b77ea..08e6fa88 100644 --- a/app/Http/Controllers/Server/Files/FileActionsController.php +++ b/app/Http/Controllers/Server/Files/FileActionsController.php @@ -95,11 +95,10 @@ class FileActionsController extends Controller * @param string $file * @return \Illuminate\View\View * - * @throws \Illuminate\Auth\Access\AuthorizationException - * @throws \Pterodactyl\Exceptions\DisplayException + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ - public function update(UpdateFileContentsFormRequest $request, string $uuid, string $file): View + public function view(UpdateFileContentsFormRequest $request, string $uuid, string $file): View { $server = $request->attributes->get('server'); diff --git a/app/Http/Controllers/Server/Files/RemoteRequestController.php b/app/Http/Controllers/Server/Files/RemoteRequestController.php index c087a555..e5ec8253 100644 --- a/app/Http/Controllers/Server/Files/RemoteRequestController.php +++ b/app/Http/Controllers/Server/Files/RemoteRequestController.php @@ -101,7 +101,7 @@ class RemoteRequestController extends Controller $this->repository->setNode($server->node_id) ->setAccessServer($server->uuid) ->setAccessToken($request->attributes->get('server_token')) - ->putContent($request->input('file'), $request->input('contents')); + ->putContent($request->input('file'), $request->input('contents') ?? ''); return response('', 204); } catch (RequestException $exception) { diff --git a/app/Http/Requests/Admin/UserFormRequest.php b/app/Http/Requests/Admin/UserFormRequest.php index ab760a7f..c6a35839 100644 --- a/app/Http/Requests/Admin/UserFormRequest.php +++ b/app/Http/Requests/Admin/UserFormRequest.php @@ -7,34 +7,21 @@ use Pterodactyl\Models\User; class UserFormRequest extends AdminFormRequest { /** - * {@inheritdoc} + * Rules to apply to requests for updating or creating a user + * in the Admin CP. */ public function rules() { + $rules = collect(User::getCreateRules()); if ($this->method() === 'PATCH') { - $rules = User::getUpdateRulesForId($this->route()->parameter('user')->id); - - return array_merge($rules, [ - 'ignore_connection_error' => 'sometimes|nullable|boolean', + $rules = collect(User::getUpdateRulesForId($this->route()->parameter('user')->id))->merge([ + 'ignore_connection_error' => ['sometimes', 'nullable', 'boolean'], ]); } - return User::getCreateRules(); - } - - /** - * @param array|null $only - * @return array - */ - public function normalize(array $only = null) - { - if ($this->method === 'PATCH') { - return array_merge( - $this->all(['password']), - $this->only(['email', 'username', 'name_first', 'name_last', 'root_admin', 'language', 'ignore_connection_error']) - ); - } - - return parent::normalize(); + return $rules->only([ + 'email', 'username', 'name_first', 'name_last', 'password', + 'language', 'ignore_connection_error', 'root_admin', + ])->toArray(); } } diff --git a/app/Http/Requests/Server/UpdateFileContentsFormRequest.php b/app/Http/Requests/Server/UpdateFileContentsFormRequest.php index 051e78db..d2337ba6 100644 --- a/app/Http/Requests/Server/UpdateFileContentsFormRequest.php +++ b/app/Http/Requests/Server/UpdateFileContentsFormRequest.php @@ -12,6 +12,7 @@ namespace Pterodactyl\Http\Requests\Server; use GuzzleHttp\Exception\RequestException; use Illuminate\Contracts\Config\Repository; use Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface; use Pterodactyl\Exceptions\Http\Server\FileTypeNotEditableException; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; @@ -80,7 +81,12 @@ class UpdateFileContentsFormRequest extends ServerFormRequest ->setAccessToken($token) ->getFileStat($this->route()->parameter('file')); } catch (RequestException $exception) { - throw new DaemonConnectionException($exception); + switch ($exception->getCode()) { + case 404: + throw new NotFoundHttpException; + default: + throw new DaemonConnectionException($exception); + } } if (! $stats->file || ! in_array($stats->mime, $config->get('pterodactyl.files.editable'))) { diff --git a/app/Models/Server.php b/app/Models/Server.php index 04ac19e4..6b5af511 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -76,7 +76,7 @@ class Server extends Model implements CleansAttributes, ValidableContract 'owner_id' => 'exists:users,id', 'name' => 'regex:/^([\w .-]{1,200})$/', 'node_id' => 'exists:nodes,id', - 'description' => 'nullable|string', + 'description' => 'string', 'memory' => 'numeric|min:0', 'swap' => 'numeric|min:-1', 'io' => 'numeric|between:10,1000', diff --git a/app/Models/User.php b/app/Models/User.php index a7492431..7d064424 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -115,6 +115,7 @@ class User extends Model implements * @var array */ protected static $applicationRules = [ + 'uuid' => 'required', 'email' => 'required', 'username' => 'required', 'name_first' => 'required', @@ -130,6 +131,7 @@ class User extends Model implements * @var array */ protected static $dataIntegrityRules = [ + 'uuid' => 'string|size:36|unique:users,uuid', 'email' => 'email|unique:users,email', 'username' => 'alpha_dash|between:1,255|unique:users,username', 'name_first' => 'string|between:1,255', diff --git a/app/Notifications/AccountCreated.php b/app/Notifications/AccountCreated.php index d83ee3fc..8312c3bd 100644 --- a/app/Notifications/AccountCreated.php +++ b/app/Notifications/AccountCreated.php @@ -1,14 +1,8 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Notifications; +use Pterodactyl\Models\User; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; @@ -19,7 +13,15 @@ class AccountCreated extends Notification implements ShouldQueue use Queueable; /** - * The password reset token to send. + * The authentication token to be used for the user to set their + * password for the first time. + * + * @var string|null + */ + public $token; + + /** + * The user model for the created user. * * @var object */ @@ -28,11 +30,13 @@ class AccountCreated extends Notification implements ShouldQueue /** * Create a new notification instance. * - * @param aray $user + * @param \Pterodactyl\Models\User $user + * @param string|null $token */ - public function __construct(array $user) + public function __construct(User $user, string $token = null) { - $this->user = (object) $user; + $this->token = $token; + $this->user = $user; } /** @@ -56,12 +60,12 @@ class AccountCreated extends Notification implements ShouldQueue { $message = (new MailMessage) ->greeting('Hello ' . $this->user->name . '!') - ->line('You are recieving this email because an account has been created for you on Pterodactyl Panel.') + ->line('You are recieving this email because an account has been created for you on ' . config('app.name') . '.') ->line('Username: ' . $this->user->username) - ->line('Email: ' . $notifiable->email); + ->line('Email: ' . $this->user->email); - if (! is_null($this->user->token)) { - return $message->action('Setup Your Account', url('/auth/password/reset/' . $this->user->token . '?email=' . $notifiable->email)); + if (! is_null($this->token)) { + return $message->action('Setup Your Account', url('/auth/password/reset/' . $this->token . '?email=' . $this->user->email)); } return $message; diff --git a/app/Services/Servers/BuildModificationService.php b/app/Services/Servers/BuildModificationService.php index bc4cd533..9aa1fa4c 100644 --- a/app/Services/Servers/BuildModificationService.php +++ b/app/Services/Servers/BuildModificationService.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Services\Servers; @@ -113,6 +106,7 @@ class BuildModificationService * * @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function handle($server, array $data) { @@ -138,11 +132,11 @@ class BuildModificationService } $server = $this->repository->update($server->id, [ - 'memory' => array_get($data, 'memory', $server->memory), - 'swap' => array_get($data, 'swap', $server->swap), - 'io' => array_get($data, 'io', $server->io), - 'cpu' => array_get($data, 'cpu', $server->cpu), - 'disk' => array_get($data, 'disk', $server->disk), + 'memory' => (int) array_get($data, 'memory', $server->memory), + 'swap' => (int) array_get($data, 'swap', $server->swap), + 'io' => (int) array_get($data, 'io', $server->io), + 'cpu' => (int) array_get($data, 'cpu', $server->cpu), + 'disk' => (int) array_get($data, 'disk', $server->disk), 'allocation_id' => array_get($data, 'allocation_id', $server->allocation_id), ]); diff --git a/app/Services/Servers/DetailsModificationService.php b/app/Services/Servers/DetailsModificationService.php index 5149a886..d18bccd7 100644 --- a/app/Services/Servers/DetailsModificationService.php +++ b/app/Services/Servers/DetailsModificationService.php @@ -95,9 +95,9 @@ class DetailsModificationService $this->connection->beginTransaction(); $this->repository->withoutFresh()->update($server->id, [ - 'owner_id' => array_get($data, 'owner_id') ?? $server->owner_id, - 'name' => array_get($data, 'name') ?? $server->name, - 'description' => array_get($data, 'description') ?? $server->description, + 'owner_id' => array_get($data, 'owner_id'), + 'name' => array_get($data, 'name'), + 'description' => array_get($data, 'description', ''), ], true, true); if (array_get($data, 'owner_id') != $server->owner_id) { diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index 86e580a2..5aa1d7b2 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -106,6 +106,7 @@ class ServerCreationService * @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + * @throws \Illuminate\Validation\ValidationException */ public function create(array $data) { @@ -117,7 +118,7 @@ class ServerCreationService 'uuidShort' => str_random(8), 'node_id' => array_get($data, 'node_id'), 'name' => array_get($data, 'name'), - 'description' => array_get($data, 'description'), + 'description' => array_get($data, 'description', ''), 'skip_scripts' => isset($data['skip_scripts']), 'suspended' => false, 'owner_id' => array_get($data, 'owner_id'), diff --git a/app/Services/Users/UserCreationService.php b/app/Services/Users/UserCreationService.php index b267a18f..f4824e48 100644 --- a/app/Services/Users/UserCreationService.php +++ b/app/Services/Users/UserCreationService.php @@ -1,77 +1,52 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Services\Users; use Ramsey\Uuid\Uuid; -use Illuminate\Foundation\Application; use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Database\ConnectionInterface; -use Illuminate\Notifications\ChannelManager; use Pterodactyl\Notifications\AccountCreated; use Pterodactyl\Services\Helpers\TemporaryPasswordService; use Pterodactyl\Contracts\Repository\UserRepositoryInterface; class UserCreationService { - /** - * @var \Illuminate\Foundation\Application - */ - protected $app; - /** * @var \Illuminate\Database\ConnectionInterface */ - protected $connection; + private $connection; /** * @var \Illuminate\Contracts\Hashing\Hasher */ - protected $hasher; - - /** - * @var \Illuminate\Notifications\ChannelManager - */ - protected $notification; + private $hasher; /** * @var \Pterodactyl\Services\Helpers\TemporaryPasswordService */ - protected $passwordService; + private $passwordService; /** * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface */ - protected $repository; + private $repository; /** * CreationService constructor. * - * @param \Illuminate\Foundation\Application $application - * @param \Illuminate\Notifications\ChannelManager $notification * @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Contracts\Hashing\Hasher $hasher * @param \Pterodactyl\Services\Helpers\TemporaryPasswordService $passwordService * @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository */ public function __construct( - Application $application, - ChannelManager $notification, ConnectionInterface $connection, Hasher $hasher, TemporaryPasswordService $passwordService, UserRepositoryInterface $repository ) { - $this->app = $application; $this->connection = $connection; $this->hasher = $hasher; - $this->notification = $notification; $this->passwordService = $passwordService; $this->repository = $repository; } @@ -97,20 +72,13 @@ class UserCreationService $token = $this->passwordService->handle($data['email']); } + /** @var \Pterodactyl\Models\User $user */ $user = $this->repository->create(array_merge($data, [ 'uuid' => Uuid::uuid4()->toString(), - ])); + ]), true, true); $this->connection->commit(); - - // @todo fire event, handle notification there - $this->notification->send($user, $this->app->makeWith(AccountCreated::class, [ - 'user' => [ - 'name' => $user->name_first, - 'username' => $user->username, - 'token' => $token ?? null, - ], - ])); + $user->notify(new AccountCreated($user, $token ?? null)); return $user; } diff --git a/composer.lock b/composer.lock index 40dbb34d..d7798684 100644 --- a/composer.lock +++ b/composer.lock @@ -61,16 +61,16 @@ }, { "name": "aws/aws-sdk-php", - "version": "3.48.0", + "version": "3.48.6", "source": { "type": "git", "url": "https://github.com/aws/aws-sdk-php.git", - "reference": "f5aef5671cd7f6e3b9ede0a3ff17c131cc05f4d3" + "reference": "53e489f7d13c2f65bb28fde21d96a4f261092dda" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/f5aef5671cd7f6e3b9ede0a3ff17c131cc05f4d3", - "reference": "f5aef5671cd7f6e3b9ede0a3ff17c131cc05f4d3", + "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/53e489f7d13c2f65bb28fde21d96a4f261092dda", + "reference": "53e489f7d13c2f65bb28fde21d96a4f261092dda", "shasum": "" }, "require": { @@ -137,7 +137,7 @@ "s3", "sdk" ], - "time": "2017-12-15T19:49:31+00:00" + "time": "2017-12-29T17:28:50+00:00" }, { "name": "dnoegel/php-xdg-base-dir", @@ -1250,16 +1250,16 @@ }, { "name": "laravel/framework", - "version": "v5.5.25", + "version": "v5.5.28", "source": { "type": "git", "url": "https://github.com/laravel/framework.git", - "reference": "0a5b6112f325c56ae5a6679c08a0a10723153fe0" + "reference": "cfafae1f2043208390a7c984e3070696f4969605" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/framework/zipball/0a5b6112f325c56ae5a6679c08a0a10723153fe0", - "reference": "0a5b6112f325c56ae5a6679c08a0a10723153fe0", + "url": "https://api.github.com/repos/laravel/framework/zipball/cfafae1f2043208390a7c984e3070696f4969605", + "reference": "cfafae1f2043208390a7c984e3070696f4969605", "shasum": "" }, "require": { @@ -1380,20 +1380,20 @@ "framework", "laravel" ], - "time": "2017-12-11T14:59:28+00:00" + "time": "2017-12-26T16:24:40+00:00" }, { "name": "laravel/tinker", - "version": "v1.0.2", + "version": "v1.0.3", "source": { "type": "git", "url": "https://github.com/laravel/tinker.git", - "reference": "203978fd67f118902acff95925847e70b72e3daf" + "reference": "852c2abe0b0991555a403f1c0583e64de6acb4a6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/tinker/zipball/203978fd67f118902acff95925847e70b72e3daf", - "reference": "203978fd67f118902acff95925847e70b72e3daf", + "url": "https://api.github.com/repos/laravel/tinker/zipball/852c2abe0b0991555a403f1c0583e64de6acb4a6", + "reference": "852c2abe0b0991555a403f1c0583e64de6acb4a6", "shasum": "" }, "require": { @@ -1402,7 +1402,7 @@ "illuminate/support": "~5.1", "php": ">=5.5.9", "psy/psysh": "0.7.*|0.8.*", - "symfony/var-dumper": "~3.0" + "symfony/var-dumper": "~3.0|~4.0" }, "require-dev": { "phpunit/phpunit": "~4.0|~5.0" @@ -1443,7 +1443,7 @@ "laravel", "psysh" ], - "time": "2017-07-13T13:11:05+00:00" + "time": "2017-12-18T16:25:11+00:00" }, { "name": "league/flysystem", @@ -1919,16 +1919,16 @@ }, { "name": "nikic/php-parser", - "version": "v3.1.2", + "version": "v3.1.3", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "08131e7ff29de6bb9f12275c7d35df71f25f4d89" + "reference": "579f4ce846734a1cf55d6a531d00ca07a43e3cda" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/08131e7ff29de6bb9f12275c7d35df71f25f4d89", - "reference": "08131e7ff29de6bb9f12275c7d35df71f25f4d89", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/579f4ce846734a1cf55d6a531d00ca07a43e3cda", + "reference": "579f4ce846734a1cf55d6a531d00ca07a43e3cda", "shasum": "" }, "require": { @@ -1966,7 +1966,7 @@ "parser", "php" ], - "time": "2017-11-04T11:48:34+00:00" + "time": "2017-12-26T14:43:21+00:00" }, { "name": "paragonie/constant_time_encoding", @@ -2435,16 +2435,16 @@ }, { "name": "psy/psysh", - "version": "v0.8.16", + "version": "v0.8.17", "source": { "type": "git", "url": "https://github.com/bobthecow/psysh.git", - "reference": "d4c8eab0683dc056f2ca54ca67f5388527c068b1" + "reference": "5069b70e8c4ea492c2b5939b6eddc78bfe41cfec" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/bobthecow/psysh/zipball/d4c8eab0683dc056f2ca54ca67f5388527c068b1", - "reference": "d4c8eab0683dc056f2ca54ca67f5388527c068b1", + "url": "https://api.github.com/repos/bobthecow/psysh/zipball/5069b70e8c4ea492c2b5939b6eddc78bfe41cfec", + "reference": "5069b70e8c4ea492c2b5939b6eddc78bfe41cfec", "shasum": "" }, "require": { @@ -2503,7 +2503,7 @@ "interactive", "shell" ], - "time": "2017-12-10T21:49:27+00:00" + "time": "2017-12-28T16:14:16+00:00" }, { "name": "ramsey/uuid", @@ -5764,16 +5764,16 @@ }, { "name": "sebastian/comparator", - "version": "2.1.0", + "version": "2.1.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "1174d9018191e93cb9d719edec01257fc05f8158" + "reference": "b11c729f95109b56a0fe9650c6a63a0fcd8c439f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/1174d9018191e93cb9d719edec01257fc05f8158", - "reference": "1174d9018191e93cb9d719edec01257fc05f8158", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/b11c729f95109b56a0fe9650c6a63a0fcd8c439f", + "reference": "b11c729f95109b56a0fe9650c6a63a0fcd8c439f", "shasum": "" }, "require": { @@ -5824,7 +5824,7 @@ "compare", "equality" ], - "time": "2017-11-03T07:16:52+00:00" + "time": "2017-12-22T14:50:35+00:00" }, { "name": "sebastian/diff", diff --git a/database/migrations/2018_01_01_122821_AllowNegativeValuesForServerSwap.php b/database/migrations/2018_01_01_122821_AllowNegativeValuesForServerSwap.php new file mode 100644 index 00000000..8f9938da --- /dev/null +++ b/database/migrations/2018_01_01_122821_AllowNegativeValuesForServerSwap.php @@ -0,0 +1,32 @@ +integer('swap')->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('servers', function (Blueprint $table) { + $table->unsignedInteger('swap')->change(); + }); + } +} diff --git a/resources/themes/pterodactyl/admin/eggs/scripts.blade.php b/resources/themes/pterodactyl/admin/eggs/scripts.blade.php index 8452cf26..44252b26 100644 --- a/resources/themes/pterodactyl/admin/eggs/scripts.blade.php +++ b/resources/themes/pterodactyl/admin/eggs/scripts.blade.php @@ -53,7 +53,7 @@
+

A unique ID used for identification of this nest internally and through the API.

+
+
@@ -60,7 +67,7 @@
-

A unique identifier that all servers using this option are assigned for identification purposes.

+

A UUID that all servers using this option are assigned for identification purposes.

@@ -76,6 +83,7 @@
+ @@ -83,6 +91,7 @@ @foreach($nest->eggs as $egg) + diff --git a/resources/themes/pterodactyl/server/files/list.blade.php b/resources/themes/pterodactyl/server/files/list.blade.php index d5df9984..bbe2b24d 100644 --- a/resources/themes/pterodactyl/server/files/list.blade.php +++ b/resources/themes/pterodactyl/server/files/list.blade.php @@ -137,7 +137,7 @@
ID Name Description Servers
{{ $egg->id }} {{ $egg->name }} {!! $egg->description !!} {{ $egg->servers->count() }} @if(in_array($file['mime'], $editableMime)) @can('edit-files', $server) - {{ $file['entry'] }} + {{ $file['entry'] }} @else {{ $file['entry'] }} @endcan diff --git a/routes/server.php b/routes/server.php index 5945f4fd..31a707e1 100644 --- a/routes/server.php +++ b/routes/server.php @@ -52,7 +52,7 @@ Route::group(['prefix' => 'databases'], function () { Route::group(['prefix' => 'files'], function () { Route::get('/', 'Files\FileActionsController@index')->name('server.files.index'); Route::get('/add', 'Files\FileActionsController@create')->name('server.files.add'); - Route::get('/edit/{file}', 'Files\FileActionsController@update')->name('server.files.edit')->where('file', '.*'); + Route::get('/edit/{file}', 'Files\FileActionsController@view')->name('server.files.edit')->where('file', '.*'); Route::get('/download/{file}', 'Files\DownloadController@index')->name('server.files.edit')->where('file', '.*'); Route::post('/directory-list', 'Files\RemoteRequestController@directory')->name('server.files.directory-list'); diff --git a/tests/Unit/Services/Users/UserCreationServiceTest.php b/tests/Unit/Services/Users/UserCreationServiceTest.php index 5650b032..4012dc65 100644 --- a/tests/Unit/Services/Users/UserCreationServiceTest.php +++ b/tests/Unit/Services/Users/UserCreationServiceTest.php @@ -4,11 +4,11 @@ namespace Tests\Unit\Services; use Mockery as m; use Tests\TestCase; +use Pterodactyl\Models\User; use Tests\Traits\MocksUuids; -use Illuminate\Foundation\Application; use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Database\ConnectionInterface; -use Illuminate\Notifications\ChannelManager; +use Illuminate\Support\Facades\Notification; use Pterodactyl\Notifications\AccountCreated; use Pterodactyl\Services\Users\UserCreationService; use Pterodactyl\Services\Helpers\TemporaryPasswordService; @@ -19,39 +19,24 @@ class UserCreationServiceTest extends TestCase use MocksUuids; /** - * @var \Illuminate\Foundation\Application + * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock */ - protected $appMock; + private $connection; /** - * @var \Illuminate\Database\ConnectionInterface + * @var \Illuminate\Contracts\Hashing\Hasher|\Mockery\Mock */ - protected $database; + private $hasher; /** - * @var \Illuminate\Contracts\Hashing\Hasher + * @var \Pterodactyl\Services\Helpers\TemporaryPasswordService|\Mockery\Mock */ - protected $hasher; + private $passwordService; /** - * @var \Illuminate\Notifications\ChannelManager + * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock */ - protected $notification; - - /** - * @var \Pterodactyl\Services\Helpers\TemporaryPasswordService - */ - protected $passwordService; - - /** - * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface - */ - protected $repository; - - /** - * @var \Pterodactyl\Services\Users\UserCreationService - */ - protected $service; + private $repository; /** * Setup tests. @@ -60,21 +45,11 @@ class UserCreationServiceTest extends TestCase { parent::setUp(); - $this->appMock = m::mock(Application::class); - $this->database = m::mock(ConnectionInterface::class); + Notification::fake(); + $this->connection = m::mock(ConnectionInterface::class); $this->hasher = m::mock(Hasher::class); - $this->notification = m::mock(ChannelManager::class); $this->passwordService = m::mock(TemporaryPasswordService::class); $this->repository = m::mock(UserRepositoryInterface::class); - - $this->service = new UserCreationService( - $this->appMock, - $this->notification, - $this->database, - $this->hasher, - $this->passwordService, - $this->repository - ); } /** @@ -82,35 +57,27 @@ class UserCreationServiceTest extends TestCase */ public function testUserIsCreatedWhenPasswordIsProvided() { - $user = (object) [ - 'name_first' => 'FirstName', - 'username' => 'user_name', - ]; + $user = factory(User::class)->make(); $this->hasher->shouldReceive('make')->with('raw-password')->once()->andReturn('enc-password'); - $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->repository->shouldReceive('create')->with([ 'password' => 'enc-password', 'uuid' => $this->getKnownUuid(), - ])->once()->andReturn($user); - $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); - $this->appMock->shouldReceive('makeWith')->with(AccountCreated::class, [ - 'user' => [ - 'name' => 'FirstName', - 'username' => 'user_name', - 'token' => null, - ], - ])->once()->andReturnNull(); + ], true, true)->once()->andReturn($user); + $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); - $this->notification->shouldReceive('send')->with($user, null)->once()->andReturnNull(); - - $response = $this->service->handle([ + $response = $this->getService()->handle([ 'password' => 'raw-password', ]); $this->assertNotNull($response); - $this->assertEquals($user->username, $response->username); - $this->assertEquals($user->name_first, 'FirstName'); + Notification::assertSentTo($user, AccountCreated::class, function ($notification) use ($user) { + $this->assertSame($user, $notification->user); + $this->assertNull($notification->token); + + return true; + }); } /** @@ -119,29 +86,29 @@ class UserCreationServiceTest extends TestCase */ public function testUuidPassedInDataIsIgnored() { - $user = (object) [ - 'name_first' => 'FirstName', - 'username' => 'user_name', - ]; + $user = factory(User::class)->make(); $this->hasher->shouldReceive('make')->andReturn('enc-password'); - $this->database->shouldReceive('beginTransaction')->andReturnNull(); + $this->connection->shouldReceive('beginTransaction')->andReturnNull(); $this->repository->shouldReceive('create')->with([ 'password' => 'enc-password', 'uuid' => $this->getKnownUuid(), - ])->once()->andReturn($user); - $this->database->shouldReceive('commit')->andReturnNull(); - $this->appMock->shouldReceive('makeWith')->andReturnNull(); - $this->notification->shouldReceive('send')->andReturnNull(); + ], true, true)->once()->andReturn($user); + $this->connection->shouldReceive('commit')->andReturnNull(); - $response = $this->service->handle([ + $response = $this->getService()->handle([ 'password' => 'raw-password', 'uuid' => 'test-uuid', ]); $this->assertNotNull($response); - $this->assertEquals($user->username, $response->username); - $this->assertEquals($user->name_first, 'FirstName'); + $this->assertInstanceOf(User::class, $response); + Notification::assertSentTo($user, AccountCreated::class, function ($notification) use ($user) { + $this->assertSame($user, $notification->user); + $this->assertNull($notification->token); + + return true; + }); } /** @@ -149,44 +116,42 @@ class UserCreationServiceTest extends TestCase */ public function testUserIsCreatedWhenNoPasswordIsProvided() { - $user = (object) [ - 'name_first' => 'FirstName', - 'username' => 'user_name', - 'email' => 'user@example.com', - ]; + $user = factory(User::class)->make(); $this->hasher->shouldNotReceive('make'); - $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->hasher->shouldReceive('make')->once()->andReturn('created-enc-password'); - $this->passwordService->shouldReceive('handle') - ->with('user@example.com') - ->once() - ->andReturn('random-token'); + $this->passwordService->shouldReceive('handle')->with($user->email)->once()->andReturn('random-token'); $this->repository->shouldReceive('create')->with([ 'password' => 'created-enc-password', - 'email' => 'user@example.com', + 'email' => $user->email, 'uuid' => $this->getKnownUuid(), - ])->once()->andReturn($user); + ], true, true)->once()->andReturn($user); - $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); - $this->appMock->shouldReceive('makeWith')->with(AccountCreated::class, [ - 'user' => [ - 'name' => 'FirstName', - 'username' => 'user_name', - 'token' => 'random-token', - ], - ])->once()->andReturnNull(); + $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); - $this->notification->shouldReceive('send')->with($user, null)->once()->andReturnNull(); - - $response = $this->service->handle([ - 'email' => 'user@example.com', + $response = $this->getService()->handle([ + 'email' => $user->email, ]); $this->assertNotNull($response); - $this->assertEquals($user->username, $response->username); - $this->assertEquals($user->name_first, 'FirstName'); - $this->assertEquals($user->email, $response->email); + $this->assertInstanceOf(User::class, $response); + Notification::assertSentTo($user, AccountCreated::class, function ($notification) use ($user) { + $this->assertSame($user, $notification->user); + $this->assertSame('random-token', $notification->token); + + return true; + }); + } + + /** + * Return a new instance of the service using mocked dependencies. + * + * @return \Pterodactyl\Services\Users\UserCreationService + */ + private function getService(): UserCreationService + { + return new UserCreationService($this->connection, $this->hasher, $this->passwordService, $this->repository); } }