Path: blob/1.0-develop/tests/Integration/Services/Servers/ServerDeletionServiceTest.php
7460 views
<?php12namespace Pterodactyl\Tests\Integration\Services\Servers;34use Mockery\MockInterface;5use GuzzleHttp\Psr7\Request;6use GuzzleHttp\Psr7\Response;7use Pterodactyl\Models\Database;8use Pterodactyl\Models\DatabaseHost;9use GuzzleHttp\Exception\BadResponseException;10use Pterodactyl\Tests\Integration\IntegrationTestCase;11use Pterodactyl\Services\Servers\ServerDeletionService;12use Pterodactyl\Repositories\Wings\DaemonServerRepository;13use Pterodactyl\Services\Databases\DatabaseManagementService;14use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;1516class ServerDeletionServiceTest extends IntegrationTestCase17{18private MockInterface $daemonServerRepository;1920private MockInterface $databaseManagementService;2122private static ?string $defaultLogger;2324/**25* Stub out services that we don't want to test in here.26*/27public function setUp(): void28{29parent::setUp();3031self::$defaultLogger = config('logging.default');32// There will be some log calls during this test, don't actually write to the disk.33config()->set('logging.default', 'null');3435$this->daemonServerRepository = \Mockery::mock(DaemonServerRepository::class);36$this->databaseManagementService = \Mockery::mock(DatabaseManagementService::class);3738$this->app->instance(DaemonServerRepository::class, $this->daemonServerRepository);39$this->app->instance(DatabaseManagementService::class, $this->databaseManagementService);40}4142/**43* Reset the log driver.44*/45protected function tearDown(): void46{47config()->set('logging.default', self::$defaultLogger);48self::$defaultLogger = null;4950parent::tearDown();51}5253/**54* Test that a server is not deleted if the force option is not set and an error55* is returned by wings.56*/57public function testRegularDeleteFailsIfWingsReturnsError()58{59$server = $this->createServerModel();6061$this->expectException(DaemonConnectionException::class);6263$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andThrows(64new DaemonConnectionException(new BadResponseException('Bad request', new Request('GET', '/test'), new Response()))65);6667$this->getService()->handle($server);6869$this->assertDatabaseHas('servers', ['id' => $server->id]);70}7172/**73* Test that a 404 from Wings while deleting a server does not cause the deletion to fail.74*/75public function testRegularDeleteIgnores404FromWings()76{77$server = $this->createServerModel();7879$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andThrows(80new DaemonConnectionException(new BadResponseException('Bad request', new Request('GET', '/test'), new Response(404)))81);8283$this->getService()->handle($server);8485$this->assertDatabaseMissing('servers', ['id' => $server->id]);86}8788/**89* Test that an error from Wings does not cause the deletion to fail if the server is being90* force deleted.91*/92public function testForceDeleteIgnoresExceptionFromWings()93{94$server = $this->createServerModel();9596$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andThrows(97new DaemonConnectionException(new BadResponseException('Bad request', new Request('GET', '/test'), new Response(500)))98);99100$this->getService()->withForce()->handle($server);101102$this->assertDatabaseMissing('servers', ['id' => $server->id]);103}104105/**106* Test that a non-force-delete call does not delete the server if one of the databases107* cannot be deleted from the host.108*/109public function testExceptionWhileDeletingStopsProcess()110{111$server = $this->createServerModel();112$host = DatabaseHost::factory()->create();113114/** @var Database $db */115$db = Database::factory()->create(['database_host_id' => $host->id, 'server_id' => $server->id]);116117$server->refresh();118119$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andReturnUndefined();120$this->databaseManagementService->expects('delete')->with(\Mockery::on(function ($value) use ($db) {121return $value instanceof Database && $value->id === $db->id;122}))->andThrows(new \Exception());123124$this->expectException(\Exception::class);125$this->getService()->handle($server);126127$this->assertDatabaseHas('servers', ['id' => $server->id]);128$this->assertDatabaseHas('databases', ['id' => $db->id]);129}130131/**132* Test that a server is deleted even if the server databases cannot be deleted from the host.133*/134public function testExceptionWhileDeletingDatabasesDoesNotAbortIfForceDeleted()135{136$server = $this->createServerModel();137$host = DatabaseHost::factory()->create();138139/** @var Database $db */140$db = Database::factory()->create(['database_host_id' => $host->id, 'server_id' => $server->id]);141142$server->refresh();143144$this->daemonServerRepository->expects('setServer->delete')->withNoArgs()->andReturnUndefined();145$this->databaseManagementService->expects('delete')->with(\Mockery::on(function ($value) use ($db) {146return $value instanceof Database && $value->id === $db->id;147}))->andThrows(new \Exception());148149$this->getService()->withForce(true)->handle($server);150151$this->assertDatabaseMissing('servers', ['id' => $server->id]);152$this->assertDatabaseMissing('databases', ['id' => $db->id]);153}154155private function getService(): ServerDeletionService156{157return $this->app->make(ServerDeletionService::class);158}159}160161162