Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Services/Backups/DeleteBackupServiceTest.php
7460 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Services\Backups;
4
5
use GuzzleHttp\Psr7\Request;
6
use GuzzleHttp\Psr7\Response;
7
use Pterodactyl\Models\Backup;
8
use GuzzleHttp\Exception\ClientException;
9
use Pterodactyl\Extensions\Backups\BackupManager;
10
use Pterodactyl\Extensions\Filesystem\S3Filesystem;
11
use Pterodactyl\Services\Backups\DeleteBackupService;
12
use Pterodactyl\Tests\Integration\IntegrationTestCase;
13
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
14
use Pterodactyl\Exceptions\Service\Backup\BackupLockedException;
15
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
16
17
class DeleteBackupServiceTest extends IntegrationTestCase
18
{
19
public function testLockedBackupCannotBeDeleted()
20
{
21
$server = $this->createServerModel();
22
$backup = Backup::factory()->create([
23
'server_id' => $server->id,
24
'is_locked' => true,
25
]);
26
27
$this->expectException(BackupLockedException::class);
28
29
$this->app->make(DeleteBackupService::class)->handle($backup);
30
}
31
32
public function testFailedBackupThatIsLockedCanBeDeleted()
33
{
34
$server = $this->createServerModel();
35
$backup = Backup::factory()->create([
36
'server_id' => $server->id,
37
'is_locked' => true,
38
'is_successful' => false,
39
]);
40
41
$mock = $this->mock(DaemonBackupRepository::class);
42
$mock->expects('setServer->delete')->with($backup)->andReturn(new Response());
43
44
$this->app->make(DeleteBackupService::class)->handle($backup);
45
46
$backup->refresh();
47
48
$this->assertNotNull($backup->deleted_at);
49
}
50
51
public function testExceptionThrownDueToMissingBackupIsIgnored()
52
{
53
$server = $this->createServerModel();
54
$backup = Backup::factory()->create(['server_id' => $server->id]);
55
56
$mock = $this->mock(DaemonBackupRepository::class);
57
$mock->expects('setServer->delete')->with($backup)->andThrow(
58
new DaemonConnectionException(
59
new ClientException('', new Request('DELETE', '/'), new Response(404))
60
)
61
);
62
63
$this->app->make(DeleteBackupService::class)->handle($backup);
64
65
$backup->refresh();
66
67
$this->assertNotNull($backup->deleted_at);
68
}
69
70
public function testExceptionIsThrownIfNot404()
71
{
72
$server = $this->createServerModel();
73
$backup = Backup::factory()->create(['server_id' => $server->id]);
74
75
$mock = $this->mock(DaemonBackupRepository::class);
76
$mock->expects('setServer->delete')->with($backup)->andThrow(
77
new DaemonConnectionException(
78
new ClientException('', new Request('DELETE', '/'), new Response(500))
79
)
80
);
81
82
$this->expectException(DaemonConnectionException::class);
83
84
$this->app->make(DeleteBackupService::class)->handle($backup);
85
86
$backup->refresh();
87
88
$this->assertNull($backup->deleted_at);
89
}
90
91
public function testS3ObjectCanBeDeleted()
92
{
93
$server = $this->createServerModel();
94
$backup = Backup::factory()->create([
95
'disk' => Backup::ADAPTER_AWS_S3,
96
'server_id' => $server->id,
97
]);
98
99
$manager = $this->mock(BackupManager::class);
100
$adapter = $this->mock(S3Filesystem::class);
101
102
$manager->expects('adapter')->with(Backup::ADAPTER_AWS_S3)->andReturn($adapter);
103
104
$adapter->expects('getBucket')->andReturn('foobar');
105
$adapter->expects('getClient->deleteObject')->with([
106
'Bucket' => 'foobar',
107
'Key' => sprintf('%s/%s.tar.gz', $server->uuid, $backup->uuid),
108
]);
109
110
$this->app->make(DeleteBackupService::class)->handle($backup);
111
112
$this->assertSoftDeleted($backup);
113
}
114
}
115
116