API: Added to, and updated, testing to cover audit log additions

This commit is contained in:
Dan Brown 2024-05-05 15:44:58 +01:00
parent 3946158e88
commit 67df127c26
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
12 changed files with 83 additions and 23 deletions

View File

@ -66,7 +66,7 @@ class ActivityQueries
});
$activity = $query->orderBy('created_at', 'desc')
->with(['entity' => function (Relation $query) {
->with(['loggable' => function (Relation $query) {
$query->withTrashed();
}, 'user.avatar'])
->skip($count * ($page - 1))

View File

@ -15,7 +15,7 @@ use Illuminate\Support\Str;
/**
* @property string $type
* @property User $user
* @property Entity $entity
* @property Entity $loggable
* @property string $detail
* @property string $loggable_type
* @property int $loggable_id

View File

@ -16,12 +16,12 @@
{{ $activity->getText() }}
@if($activity->entity && is_null($activity->entity->deleted_at))
<a href="{{ $activity->entity->getUrl() }}">{{ $activity->entity->name }}</a>
@if($activity->loggable && is_null($activity->loggable->deleted_at))
<a href="{{ $activity->loggable->getUrl() }}">{{ $activity->loggable->name }}</a>
@endif
@if($activity->entity && !is_null($activity->entity->deleted_at))
"{{ $activity->entity->name }}"
@if($activity->loggable && !is_null($activity->loggable->deleted_at))
"{{ $activity->loggable->name }}"
@endif
<br>

View File

@ -0,0 +1,60 @@
<?php
namespace Activity;
use BookStack\Activity\ActivityType;
use BookStack\Facades\Activity;
use Tests\Api\TestsApi;
use Tests\TestCase;
class AuditLogApiTest extends TestCase
{
use TestsApi;
public function test_user_and_settings_manage_permissions_needed()
{
$editor = $this->users->editor();
$assertPermissionErrorOnCall = function () use ($editor) {
$resp = $this->actingAsForApi($editor)->getJson('/api/audit-log');
$resp->assertStatus(403);
$resp->assertJson($this->permissionErrorResponse());
};
$assertPermissionErrorOnCall();
$this->permissions->grantUserRolePermissions($editor, ['users-manage']);
$assertPermissionErrorOnCall();
$this->permissions->removeUserRolePermissions($editor, ['users-manage']);
$this->permissions->grantUserRolePermissions($editor, ['settings-manage']);
$assertPermissionErrorOnCall();
$this->permissions->grantUserRolePermissions($editor, ['settings-manage', 'users-manage']);
$resp = $this->actingAsForApi($editor)->getJson('/api/audit-log');
$resp->assertOk();
}
public function test_index_endpoint_returns_expected_data()
{
$page = $this->entities->page();
$admin = $this->users->admin();
$this->actingAsForApi($admin);
Activity::add(ActivityType::PAGE_UPDATE, $page);
$resp = $this->get("/api/audit-log?filter[loggable_id]={$page->id}");
$resp->assertJson(['data' => [
[
'type' => 'page_update',
'detail' => "({$page->id}) {$page->name}",
'user_id' => $admin->id,
'loggable_id' => $page->id,
'loggable_type' => 'page',
'ip' => '127.0.0.1',
'user' => [
'id' => $admin->id,
'name' => $admin->name,
'slug' => $admin->slug,
],
]
]]);
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace Tests\Actions;
namespace Activity;
use BookStack\Activity\ActivityType;
use BookStack\Activity\Models\Activity;
@ -156,7 +156,7 @@ class AuditLogTest extends TestCase
'type' => ActivityType::PAGE_UPDATE,
'ip' => '192.123.45.1',
'user_id' => $editor->id,
'entity_id' => $page->id,
'loggable_id' => $page->id,
]);
$resp = $this->asAdmin()->get('/settings/audit');
@ -207,7 +207,7 @@ class AuditLogTest extends TestCase
'type' => ActivityType::PAGE_UPDATE,
'ip' => '127.0.0.1',
'user_id' => $editor->id,
'entity_id' => $page->id,
'loggable_id' => $page->id,
]);
}
@ -229,7 +229,7 @@ class AuditLogTest extends TestCase
'type' => ActivityType::PAGE_UPDATE,
'ip' => '192.123.x.x',
'user_id' => $editor->id,
'entity_id' => $page->id,
'loggable_id' => $page->id,
]);
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace Tests\Actions;
namespace Activity;
use BookStack\Activity\ActivityType;
use BookStack\Activity\DispatchWebhookJob;

View File

@ -1,6 +1,6 @@
<?php
namespace Tests\Actions;
namespace Activity;
use BookStack\Activity\ActivityType;
use BookStack\Activity\Models\Webhook;

View File

@ -1,6 +1,6 @@
<?php
namespace Tests\Actions;
namespace Activity;
use BookStack\Activity\ActivityType;
use BookStack\Activity\Models\Webhook;

View File

@ -18,7 +18,7 @@ class ClearActivityCommandTest extends TestCase
$this->assertDatabaseHas('activities', [
'type' => 'page_update',
'entity_id' => $page->id,
'loggable_id' => $page->id,
'user_id' => $this->users->editor()->id,
]);

View File

@ -153,22 +153,22 @@ class RecycleBinTest extends TestCase
$this->assertDatabaseHas('activities', [
'type' => 'page_delete',
'entity_id' => $page->id,
'entity_type' => $page->getMorphClass(),
'loggable_id' => $page->id,
'loggable_type' => $page->getMorphClass(),
]);
$this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
$this->assertDatabaseMissing('activities', [
'type' => 'page_delete',
'entity_id' => $page->id,
'entity_type' => $page->getMorphClass(),
'loggable_id' => $page->id,
'loggable_type' => $page->getMorphClass(),
]);
$this->assertDatabaseHas('activities', [
'type' => 'page_delete',
'entity_id' => null,
'entity_type' => null,
'loggable_id' => null,
'loggable_type' => null,
'detail' => $page->name,
]);
}

View File

@ -248,8 +248,8 @@ abstract class TestCase extends BaseTestCase
$detailsToCheck = ['type' => $type];
if ($entity) {
$detailsToCheck['entity_type'] = $entity->getMorphClass();
$detailsToCheck['entity_id'] = $entity->id;
$detailsToCheck['loggable_type'] = $entity->getMorphClass();
$detailsToCheck['loggable_id'] = $entity->id;
}
if ($detail) {

View File

@ -2,8 +2,8 @@
namespace Tests\User;
use Activity;
use BookStack\Activity\ActivityType;
use BookStack\Facades\Activity;
use BookStack\Users\Models\User;
use Tests\TestCase;