mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 01:36:00 -04:00
API: Added to, and updated, testing to cover audit log additions
This commit is contained in:
parent
3946158e88
commit
67df127c26
@ -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))
|
||||
|
@ -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
|
||||
|
@ -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>
|
||||
|
60
tests/Activity/AuditLogApiTest.php
Normal file
60
tests/Activity/AuditLogApiTest.php
Normal 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,
|
||||
],
|
||||
]
|
||||
]]);
|
||||
}
|
||||
}
|
@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Actions;
|
||||
namespace Activity;
|
||||
|
||||
use BookStack\Activity\ActivityType;
|
||||
use BookStack\Activity\DispatchWebhookJob;
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Actions;
|
||||
namespace Activity;
|
||||
|
||||
use BookStack\Activity\ActivityType;
|
||||
use BookStack\Activity\Models\Webhook;
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Actions;
|
||||
namespace Activity;
|
||||
|
||||
use BookStack\Activity\ActivityType;
|
||||
use BookStack\Activity\Models\Webhook;
|
@ -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,
|
||||
]);
|
||||
|
||||
|
@ -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,
|
||||
]);
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user