]> BookStack Code Mirror - bookstack/commitdiff
API: Added to, and updated, testing to cover audit log additions
authorDan Brown <redacted>
Sun, 5 May 2024 14:44:58 +0000 (15:44 +0100)
committerDan Brown <redacted>
Sun, 5 May 2024 14:44:58 +0000 (15:44 +0100)
12 files changed:
app/Activity/ActivityQueries.php
app/Activity/Models/Activity.php
resources/views/common/activity-item.blade.php
tests/Activity/AuditLogApiTest.php [new file with mode: 0644]
tests/Activity/AuditLogTest.php [moved from tests/Actions/AuditLogTest.php with 98% similarity]
tests/Activity/WebhookCallTest.php [moved from tests/Actions/WebhookCallTest.php with 99% similarity]
tests/Activity/WebhookFormatTesting.php [moved from tests/Actions/WebhookFormatTesting.php with 98% similarity]
tests/Activity/WebhookManagementTest.php [moved from tests/Actions/WebhookManagementTest.php with 99% similarity]
tests/Commands/ClearActivityCommandTest.php
tests/Settings/RecycleBinTest.php
tests/TestCase.php
tests/User/UserProfileTest.php

index 9de70f022a31351a9ebc94343411f8174673bf23..86326fb80af5c93575d71e54dda143d95e7d8de0 100644 (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))
index ebe5d66d6cca09fdc7a01126f98b43a1764a943f..ac9fec5178b6836d8a373763f2f65616b70d554f 100644 (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
index 89d44b15231a37037a4fe6475f6e4b8e3672cf15..1c970084f6a98785b54d9f13b2eebff2b5012248 100644 (file)
 
     {{ $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>
diff --git a/tests/Activity/AuditLogApiTest.php b/tests/Activity/AuditLogApiTest.php
new file mode 100644 (file)
index 0000000..75cc364
--- /dev/null
@@ -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,
+                ],
+            ]
+        ]]);
+    }
+}
similarity index 98%
rename from tests/Actions/AuditLogTest.php
rename to tests/Activity/AuditLogTest.php
index 5e355ca096b81914de60a0b2f3aff7859d195b90..350cd92871b05d69d5877e422f6967ff5edb6ddb 100644 (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,
         ]);
     }
 }
similarity index 99%
rename from tests/Actions/WebhookCallTest.php
rename to tests/Activity/WebhookCallTest.php
index 16986ba2e97ac11610ce4b8266767fe2d85b8533..37c87267a8ad96114ad4f5f0e98f814181249ca3 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace Tests\Actions;
+namespace Activity;
 
 use BookStack\Activity\ActivityType;
 use BookStack\Activity\DispatchWebhookJob;
similarity index 98%
rename from tests/Actions/WebhookFormatTesting.php
rename to tests/Activity/WebhookFormatTesting.php
index b4fa49532c44f1c2f592588532b4fd645ce3cfb7..edd16ebbad7b46e52291baf104375e771d56838d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace Tests\Actions;
+namespace Activity;
 
 use BookStack\Activity\ActivityType;
 use BookStack\Activity\Models\Webhook;
similarity index 99%
rename from tests/Actions/WebhookManagementTest.php
rename to tests/Activity/WebhookManagementTest.php
index 05475b6997c01bf371082eb9769a2cad72445401..eb7bb4c15a470a8e7356ea1c08d7f6604596ef3c 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace Tests\Actions;
+namespace Activity;
 
 use BookStack\Activity\ActivityType;
 use BookStack\Activity\Models\Webhook;
index 410a39aa870e60209c0712e4826122dd9c42890d..a3d7456b68ebf427b6597d5cfbfacde0de06fc0e 100644 (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,
         ]);
 
index 8adc92f253853e75805dc6e4c52100cb9343bb04..33284b4b3ff85f2297c024873e5851c2e8544c7f 100644 (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,
         ]);
     }
index c59f843e94bdd877c4e8601c6d214c1977405e40..b63de307624201899ee8734b0ceabf4b770f9799 100644 (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) {
index 4bfb3c87822a5085fbb0f8c7a862c6177b5821c6..065ae8dc896c3c10a5af47fbb005f843425a0dcd 100644 (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;
 
Morty Proxy This is a proxified and sanitized view of the page, visit original site.