forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeleteBackupServiceTest.php
More file actions
123 lines (93 loc) · 3.76 KB
/
DeleteBackupServiceTest.php
File metadata and controls
123 lines (93 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Pterodactyl\Tests\Integration\Services\Backups;
use Mockery;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Backup;
use GuzzleHttp\Exception\ClientException;
use Pterodactyl\Extensions\Backups\BackupManager;
use Pterodactyl\Services\Backups\DeleteBackupService;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
use Pterodactyl\Exceptions\Service\Backup\BackupLockedException;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class DeleteBackupServiceTest extends IntegrationTestCase
{
private $repository;
public function setUp(): void
{
parent::setUp();
$this->repository = Mockery::mock(DaemonBackupRepository::class);
$this->app->instance(DaemonBackupRepository::class, $this->repository);
}
public function testLockedBackupCannotBeDeleted()
{
$server = $this->createServerModel();
$backup = Backup::factory()->create([
'server_id' => $server->id,
'is_locked' => true,
]);
$this->expectException(BackupLockedException::class);
$this->app->make(DeleteBackupService::class)->handle($backup);
}
public function testFailedBackupThatIsLockedCanBeDeleted()
{
$server = $this->createServerModel();
$backup = Backup::factory()->create([
'server_id' => $server->id,
'is_locked' => true,
'is_successful' => false,
]);
$this->repository->expects('setServer->delete')->with($backup)->andReturn(
new Response()
);
$this->app->make(DeleteBackupService::class)->handle($backup);
$backup->refresh();
$this->assertNotNull($backup->deleted_at);
}
public function testExceptionThrownDueToMissingBackupIsIgnored()
{
$server = $this->createServerModel();
$backup = Backup::factory()->create(['server_id' => $server->id]);
$this->repository->expects('setServer->delete')->with($backup)->andThrow(
new DaemonConnectionException(
new ClientException('', new Request('DELETE', '/'), new Response(404))
)
);
$this->app->make(DeleteBackupService::class)->handle($backup);
$backup->refresh();
$this->assertNotNull($backup->deleted_at);
}
public function testExceptionIsThrownIfNot404()
{
$server = $this->createServerModel();
$backup = Backup::factory()->create(['server_id' => $server->id]);
$this->repository->expects('setServer->delete')->with($backup)->andThrow(
new DaemonConnectionException(
new ClientException('', new Request('DELETE', '/'), new Response(500))
)
);
$this->expectException(DaemonConnectionException::class);
$this->app->make(DeleteBackupService::class)->handle($backup);
$backup->refresh();
$this->assertNull($backup->deleted_at);
}
public function testS3ObjectCanBeDeleted()
{
$server = $this->createServerModel();
$backup = Backup::factory()->create([
'disk' => Backup::ADAPTER_AWS_S3,
'server_id' => $server->id,
]);
$manager = $this->mock(BackupManager::class);
$manager->expects('getBucket')->andReturns('foobar');
$manager->expects('adapter')->with(Backup::ADAPTER_AWS_S3)->andReturnSelf();
$manager->expects('getClient->deleteObject')->with([
'Bucket' => 'foobar',
'Key' => sprintf('%s/%s.tar.gz', $server->uuid, $backup->uuid),
]);
$this->app->make(DeleteBackupService::class)->handle($backup);
$backup->refresh();
$this->assertNotNull($backup->deleted_at);
}
}