Skip to content

Commit d3e3b1d

Browse files
committed
Test that a deleted backup makes an audit log entry
1 parent d33522c commit d3e3b1d

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

app/Models/AuditLog.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class AuditLog extends Model
6969
* @var string[]
7070
*/
7171
protected $casts = [
72+
'is_system' => 'bool',
7273
'device' => 'array',
7374
'metadata' => 'array',
7475
];

app/Models/Backup.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @property \Carbon\CarbonImmutable $updated_at
2222
* @property \Carbon\CarbonImmutable|null $deleted_at
2323
* @property \Pterodactyl\Models\Server $server
24+
* @property \Pterodactyl\Models\AuditLog[] $audits
2425
*/
2526
class Backup extends Model
2627
{
@@ -98,4 +99,14 @@ public function server()
9899
{
99100
return $this->belongsTo(Server::class);
100101
}
102+
103+
/**
104+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
105+
*/
106+
public function audits()
107+
{
108+
return $this->hasMany(AuditLog::class, 'metadata->backup_uuid', 'uuid')
109+
->where('action', 'LIKE', 'server:backup.%');
110+
// ->where('metadata->backup_uuid', $this->uuid);
111+
}
101112
}

tests/Integration/Api/Client/ClientApiIntegrationTestCase.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Pterodactyl\Models\Task;
88
use Pterodactyl\Models\User;
99
use Webmozart\Assert\Assert;
10+
use InvalidArgumentException;
1011
use Pterodactyl\Models\Backup;
1112
use Pterodactyl\Models\Server;
1213
use Pterodactyl\Models\Subuser;
@@ -60,8 +61,6 @@ protected function createTestResponse($response)
6061
*/
6162
protected function link($model, $append = null): string
6263
{
63-
Assert::isInstanceOfAny($model, [Server::class, Schedule::class, Task::class, Allocation::class]);
64-
6564
$link = '';
6665
switch (get_class($model)) {
6766
case Server::class:
@@ -76,6 +75,11 @@ protected function link($model, $append = null): string
7675
case Allocation::class:
7776
$link = "/api/client/servers/{$model->server->uuid}/network/allocations/{$model->id}";
7877
break;
78+
case Backup::class:
79+
$link = "/api/client/servers/{$model->server->uuid}/backups/{$model->uuid}";
80+
break;
81+
default:
82+
throw new InvalidArgumentException(sprintf('Cannot create link for Model of type %s', class_basename($model)));
7983
}
8084

8185
return $link . ($append ? '/' . ltrim($append, '/') : '');
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Backup;
4+
5+
use Mockery;
6+
use Illuminate\Http\Response;
7+
use Pterodactyl\Models\Backup;
8+
use Pterodactyl\Models\AuditLog;
9+
use Pterodactyl\Models\Permission;
10+
use Pterodactyl\Repositories\Wings\DaemonBackupRepository;
11+
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
12+
13+
class DeleteBackupTest extends ClientApiIntegrationTestCase
14+
{
15+
private $repository;
16+
17+
public function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->repository = $this->mock(DaemonBackupRepository::class);
22+
}
23+
24+
public function testUserWithoutPermissionCannotDeleteBackup()
25+
{
26+
[$user, $server] = $this->generateTestAccount([Permission::ACTION_BACKUP_CREATE]);
27+
28+
$backup = Backup::factory()->create(['server_id' => $server->id]);
29+
30+
$this->actingAs($user)->deleteJson($this->link($backup))
31+
->assertStatus(Response::HTTP_FORBIDDEN);
32+
}
33+
34+
/**
35+
* Tests that a backup can be deleted for a server and that it is properly updated
36+
* in the database. Once deleted there should also be a corresponding record in the
37+
* audit logs table for this API call.
38+
*/
39+
public function testBackupCanBeDeleted()
40+
{
41+
[$user, $server] = $this->generateTestAccount([Permission::ACTION_BACKUP_DELETE]);
42+
43+
/** @var \Pterodactyl\Models\Backup $backup */
44+
$backup = Backup::factory()->create(['server_id' => $server->id]);
45+
46+
$this->repository->expects('setServer->delete')->with(Mockery::on(function ($value) use ($backup) {
47+
return $value instanceof Backup && $value->uuid === $backup->uuid;
48+
}))->andReturn(new Response());
49+
50+
$this->actingAs($user)->deleteJson($this->link($backup))->assertStatus(Response::HTTP_NO_CONTENT);
51+
52+
$backup->refresh();
53+
54+
$this->assertNotNull($backup->deleted_at);
55+
56+
$this->actingAs($user)->deleteJson($this->link($backup))->assertStatus(Response::HTTP_NOT_FOUND);
57+
58+
$event = $backup->audits()->where('action', AuditLog::SERVER__BACKUP_DELETED)->latest()->first();
59+
60+
$this->assertNotNull($event);
61+
$this->assertFalse($event->is_system);
62+
$this->assertEquals($backup->server_id, $event->server_id);
63+
$this->assertEquals($user->id, $event->user_id);
64+
}
65+
}

0 commit comments

Comments
 (0)