Skip to content

Commit 67df127

Browse files
committed
API: Added to, and updated, testing to cover audit log additions
1 parent 3946158 commit 67df127

File tree

12 files changed

+83
-23
lines changed

12 files changed

+83
-23
lines changed

app/Activity/ActivityQueries.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function entityActivity(Entity $entity, int $count = 20, int $page = 1):
6666
});
6767

6868
$activity = $query->orderBy('created_at', 'desc')
69-
->with(['entity' => function (Relation $query) {
69+
->with(['loggable' => function (Relation $query) {
7070
$query->withTrashed();
7171
}, 'user.avatar'])
7272
->skip($count * ($page - 1))

app/Activity/Models/Activity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* @property string $type
1717
* @property User $user
18-
* @property Entity $entity
18+
* @property Entity $loggable
1919
* @property string $detail
2020
* @property string $loggable_type
2121
* @property int $loggable_id

resources/views/common/activity-item.blade.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
{{ $activity->getText() }}
1818

19-
@if($activity->entity && is_null($activity->entity->deleted_at))
20-
<a href="{{ $activity->entity->getUrl() }}">{{ $activity->entity->name }}</a>
19+
@if($activity->loggable && is_null($activity->loggable->deleted_at))
20+
<a href="{{ $activity->loggable->getUrl() }}">{{ $activity->loggable->name }}</a>
2121
@endif
2222

23-
@if($activity->entity && !is_null($activity->entity->deleted_at))
24-
"{{ $activity->entity->name }}"
23+
@if($activity->loggable && !is_null($activity->loggable->deleted_at))
24+
"{{ $activity->loggable->name }}"
2525
@endif
2626

2727
<br>

tests/Activity/AuditLogApiTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Activity;
4+
5+
use BookStack\Activity\ActivityType;
6+
use BookStack\Facades\Activity;
7+
use Tests\Api\TestsApi;
8+
use Tests\TestCase;
9+
10+
class AuditLogApiTest extends TestCase
11+
{
12+
use TestsApi;
13+
14+
public function test_user_and_settings_manage_permissions_needed()
15+
{
16+
$editor = $this->users->editor();
17+
18+
$assertPermissionErrorOnCall = function () use ($editor) {
19+
$resp = $this->actingAsForApi($editor)->getJson('/api/audit-log');
20+
$resp->assertStatus(403);
21+
$resp->assertJson($this->permissionErrorResponse());
22+
};
23+
24+
$assertPermissionErrorOnCall();
25+
$this->permissions->grantUserRolePermissions($editor, ['users-manage']);
26+
$assertPermissionErrorOnCall();
27+
$this->permissions->removeUserRolePermissions($editor, ['users-manage']);
28+
$this->permissions->grantUserRolePermissions($editor, ['settings-manage']);
29+
$assertPermissionErrorOnCall();
30+
31+
$this->permissions->grantUserRolePermissions($editor, ['settings-manage', 'users-manage']);
32+
$resp = $this->actingAsForApi($editor)->getJson('/api/audit-log');
33+
$resp->assertOk();
34+
}
35+
36+
public function test_index_endpoint_returns_expected_data()
37+
{
38+
$page = $this->entities->page();
39+
$admin = $this->users->admin();
40+
$this->actingAsForApi($admin);
41+
Activity::add(ActivityType::PAGE_UPDATE, $page);
42+
43+
$resp = $this->get("/api/audit-log?filter[loggable_id]={$page->id}");
44+
$resp->assertJson(['data' => [
45+
[
46+
'type' => 'page_update',
47+
'detail' => "({$page->id}) {$page->name}",
48+
'user_id' => $admin->id,
49+
'loggable_id' => $page->id,
50+
'loggable_type' => 'page',
51+
'ip' => '127.0.0.1',
52+
'user' => [
53+
'id' => $admin->id,
54+
'name' => $admin->name,
55+
'slug' => $admin->slug,
56+
],
57+
]
58+
]]);
59+
}
60+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Actions;
3+
namespace Activity;
44

55
use BookStack\Activity\ActivityType;
66
use BookStack\Activity\Models\Activity;
@@ -156,7 +156,7 @@ public function test_ip_address_logged_and_visible()
156156
'type' => ActivityType::PAGE_UPDATE,
157157
'ip' => '192.123.45.1',
158158
'user_id' => $editor->id,
159-
'entity_id' => $page->id,
159+
'loggable_id' => $page->id,
160160
]);
161161

162162
$resp = $this->asAdmin()->get('/settings/audit');
@@ -207,7 +207,7 @@ public function test_ip_address_not_logged_in_demo_mode()
207207
'type' => ActivityType::PAGE_UPDATE,
208208
'ip' => '127.0.0.1',
209209
'user_id' => $editor->id,
210-
'entity_id' => $page->id,
210+
'loggable_id' => $page->id,
211211
]);
212212
}
213213

@@ -229,7 +229,7 @@ public function test_ip_address_respects_precision_setting()
229229
'type' => ActivityType::PAGE_UPDATE,
230230
'ip' => '192.123.x.x',
231231
'user_id' => $editor->id,
232-
'entity_id' => $page->id,
232+
'loggable_id' => $page->id,
233233
]);
234234
}
235235
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Actions;
3+
namespace Activity;
44

55
use BookStack\Activity\ActivityType;
66
use BookStack\Activity\DispatchWebhookJob;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Actions;
3+
namespace Activity;
44

55
use BookStack\Activity\ActivityType;
66
use BookStack\Activity\Models\Webhook;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Actions;
3+
namespace Activity;
44

55
use BookStack\Activity\ActivityType;
66
use BookStack\Activity\Models\Webhook;

tests/Commands/ClearActivityCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function test_clear_activity_command()
1818

1919
$this->assertDatabaseHas('activities', [
2020
'type' => 'page_update',
21-
'entity_id' => $page->id,
21+
'loggable_id' => $page->id,
2222
'user_id' => $this->users->editor()->id,
2323
]);
2424

tests/Settings/RecycleBinTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,22 @@ public function test_permanent_entity_delete_updates_existing_activity_with_enti
153153

154154
$this->assertDatabaseHas('activities', [
155155
'type' => 'page_delete',
156-
'entity_id' => $page->id,
157-
'entity_type' => $page->getMorphClass(),
156+
'loggable_id' => $page->id,
157+
'loggable_type' => $page->getMorphClass(),
158158
]);
159159

160160
$this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
161161

162162
$this->assertDatabaseMissing('activities', [
163163
'type' => 'page_delete',
164-
'entity_id' => $page->id,
165-
'entity_type' => $page->getMorphClass(),
164+
'loggable_id' => $page->id,
165+
'loggable_type' => $page->getMorphClass(),
166166
]);
167167

168168
$this->assertDatabaseHas('activities', [
169169
'type' => 'page_delete',
170-
'entity_id' => null,
171-
'entity_type' => null,
170+
'loggable_id' => null,
171+
'loggable_type' => null,
172172
'detail' => $page->name,
173173
]);
174174
}

0 commit comments

Comments
 (0)