mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-30 04:23:11 +03:00
Merge branch 'webhooks'
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
namespace Tests\Actions;
|
||||
|
||||
use BookStack\Actions\Activity;
|
||||
use BookStack\Actions\ActivityService;
|
||||
use BookStack\Actions\ActivityLogger;
|
||||
use BookStack\Actions\ActivityType;
|
||||
use BookStack\Auth\UserRepo;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
@ -11,16 +11,19 @@ use BookStack\Entities\Models\Page;
|
||||
use BookStack\Entities\Repos\PageRepo;
|
||||
use BookStack\Entities\Tools\TrashCan;
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use function app;
|
||||
use function config;
|
||||
|
||||
class AuditLogTest extends TestCase
|
||||
{
|
||||
/** @var ActivityService */
|
||||
/** @var ActivityLogger */
|
||||
protected $activityService;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->activityService = app(ActivityService::class);
|
||||
$this->activityService = app(ActivityLogger::class);
|
||||
}
|
||||
|
||||
public function test_only_accessible_with_right_permissions()
|
||||
@ -46,7 +49,7 @@ class AuditLogTest extends TestCase
|
||||
$admin = $this->getAdmin();
|
||||
$this->actingAs($admin);
|
||||
$page = Page::query()->first();
|
||||
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
|
||||
$this->activityService->add(ActivityType::PAGE_CREATE, $page);
|
||||
$activity = Activity::query()->orderBy('id', 'desc')->first();
|
||||
|
||||
$resp = $this->get('settings/audit');
|
||||
@ -61,7 +64,7 @@ class AuditLogTest extends TestCase
|
||||
$this->actingAs($this->getAdmin());
|
||||
$page = Page::query()->first();
|
||||
$pageName = $page->name;
|
||||
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
|
||||
$this->activityService->add(ActivityType::PAGE_CREATE, $page);
|
||||
|
||||
app(PageRepo::class)->destroy($page);
|
||||
app(TrashCan::class)->empty();
|
||||
@ -76,7 +79,7 @@ class AuditLogTest extends TestCase
|
||||
$viewer = $this->getViewer();
|
||||
$this->actingAs($viewer);
|
||||
$page = Page::query()->first();
|
||||
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
|
||||
$this->activityService->add(ActivityType::PAGE_CREATE, $page);
|
||||
|
||||
$this->actingAs($this->getAdmin());
|
||||
app(UserRepo::class)->destroy($viewer);
|
||||
@ -89,7 +92,7 @@ class AuditLogTest extends TestCase
|
||||
{
|
||||
$this->actingAs($this->getAdmin());
|
||||
$page = Page::query()->first();
|
||||
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
|
||||
$this->activityService->add(ActivityType::PAGE_CREATE, $page);
|
||||
|
||||
$resp = $this->get('settings/audit');
|
||||
$resp->assertSeeText($page->name);
|
||||
@ -102,7 +105,7 @@ class AuditLogTest extends TestCase
|
||||
{
|
||||
$this->actingAs($this->getAdmin());
|
||||
$page = Page::query()->first();
|
||||
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
|
||||
$this->activityService->add(ActivityType::PAGE_CREATE, $page);
|
||||
|
||||
$yesterday = (Carbon::now()->subDay()->format('Y-m-d'));
|
||||
$tomorrow = (Carbon::now()->addDay()->format('Y-m-d'));
|
||||
@ -126,11 +129,11 @@ class AuditLogTest extends TestCase
|
||||
$editor = $this->getEditor();
|
||||
$this->actingAs($admin);
|
||||
$page = Page::query()->first();
|
||||
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
|
||||
$this->activityService->add(ActivityType::PAGE_CREATE, $page);
|
||||
|
||||
$this->actingAs($editor);
|
||||
$chapter = Chapter::query()->first();
|
||||
$this->activityService->addForEntity($chapter, ActivityType::CHAPTER_UPDATE);
|
||||
$this->activityService->add(ActivityType::CHAPTER_UPDATE, $chapter);
|
||||
|
||||
$resp = $this->actingAs($admin)->get('settings/audit?user=' . $admin->id);
|
||||
$resp->assertSeeText($page->name);
|
116
tests/Actions/WebhookCallTest.php
Normal file
116
tests/Actions/WebhookCallTest.php
Normal file
@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Actions;
|
||||
|
||||
use BookStack\Actions\ActivityLogger;
|
||||
use BookStack\Actions\ActivityType;
|
||||
use BookStack\Actions\DispatchWebhookJob;
|
||||
use BookStack\Actions\Webhook;
|
||||
use BookStack\Auth\User;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use Illuminate\Http\Client\Request;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
class WebhookCallTest extends TestCase
|
||||
{
|
||||
|
||||
public function test_webhook_listening_to_all_called_on_event()
|
||||
{
|
||||
$this->newWebhook([], ['all']);
|
||||
Bus::fake();
|
||||
$this->runEvent(ActivityType::ROLE_CREATE);
|
||||
Bus::assertDispatched(DispatchWebhookJob::class);
|
||||
}
|
||||
|
||||
public function test_webhook_listening_to_specific_event_called_on_event()
|
||||
{
|
||||
$this->newWebhook([], [ActivityType::ROLE_UPDATE]);
|
||||
Bus::fake();
|
||||
$this->runEvent(ActivityType::ROLE_UPDATE);
|
||||
Bus::assertDispatched(DispatchWebhookJob::class);
|
||||
}
|
||||
|
||||
public function test_webhook_listening_to_specific_event_not_called_on_other_event()
|
||||
{
|
||||
$this->newWebhook([], [ActivityType::ROLE_UPDATE]);
|
||||
Bus::fake();
|
||||
$this->runEvent(ActivityType::ROLE_CREATE);
|
||||
Bus::assertNotDispatched(DispatchWebhookJob::class);
|
||||
}
|
||||
|
||||
public function test_inactive_webhook_not_called_on_event()
|
||||
{
|
||||
$this->newWebhook(['active' => false], ['all']);
|
||||
Bus::fake();
|
||||
$this->runEvent(ActivityType::ROLE_CREATE);
|
||||
Bus::assertNotDispatched(DispatchWebhookJob::class);
|
||||
}
|
||||
|
||||
public function test_failed_webhook_call_logs_error()
|
||||
{
|
||||
$logger = $this->withTestLogger();
|
||||
Http::fake([
|
||||
'*' => Http::response('', 500),
|
||||
]);
|
||||
$this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
|
||||
|
||||
$this->runEvent(ActivityType::ROLE_CREATE);
|
||||
|
||||
$this->assertTrue($logger->hasError('Webhook call to endpoint https://wh.example.com failed with status 500'));
|
||||
}
|
||||
|
||||
public function test_webhook_call_data_format()
|
||||
{
|
||||
Http::fake([
|
||||
'*' => Http::response('', 200),
|
||||
]);
|
||||
$webhook = $this->newWebhook(['active' => true, 'endpoint' => 'https://wh.example.com'], ['all']);
|
||||
/** @var Page $page */
|
||||
$page = Page::query()->first();
|
||||
$editor = $this->getEditor();
|
||||
|
||||
$this->runEvent(ActivityType::PAGE_UPDATE, $page, $editor);
|
||||
|
||||
Http::assertSent(function(Request $request) use ($editor, $page, $webhook) {
|
||||
$reqData = $request->data();
|
||||
return $request->isJson()
|
||||
&& $reqData['event'] === 'page_update'
|
||||
&& $reqData['text'] === ($editor->name . ' updated page "' . $page->name . '"')
|
||||
&& is_string($reqData['triggered_at'])
|
||||
&& $reqData['triggered_by']['name'] === $editor->name
|
||||
&& $reqData['triggered_by_profile_url'] === $editor->getProfileUrl()
|
||||
&& $reqData['webhook_id'] === $webhook->id
|
||||
&& $reqData['webhook_name'] === $webhook->name
|
||||
&& $reqData['url'] === $page->getUrl()
|
||||
&& $reqData['related_item']['name'] === $page->name;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
protected function runEvent(string $event, $detail = '', ?User $user = null)
|
||||
{
|
||||
if (is_null($user)) {
|
||||
$user = $this->getEditor();
|
||||
}
|
||||
|
||||
$this->actingAs($user);
|
||||
|
||||
$activityLogger = $this->app->make(ActivityLogger::class);
|
||||
$activityLogger->add($event, $detail);
|
||||
}
|
||||
|
||||
protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook
|
||||
{
|
||||
/** @var Webhook $webhook */
|
||||
$webhook = Webhook::factory()->create($attrs);
|
||||
|
||||
foreach ($events as $event) {
|
||||
$webhook->trackedEvents()->create(['event' => $event]);
|
||||
}
|
||||
|
||||
return $webhook;
|
||||
}
|
||||
|
||||
}
|
173
tests/Actions/WebhookManagementTest.php
Normal file
173
tests/Actions/WebhookManagementTest.php
Normal file
@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Actions;
|
||||
|
||||
use BookStack\Actions\ActivityType;
|
||||
use BookStack\Actions\Webhook;
|
||||
use Tests\TestCase;
|
||||
|
||||
class WebhookManagementTest extends TestCase
|
||||
{
|
||||
|
||||
public function test_index_view()
|
||||
{
|
||||
$webhook = $this->newWebhook([
|
||||
'name' => 'My awesome webhook',
|
||||
'endpoint' => 'https://example.com/donkey/webhook',
|
||||
], ['all']);
|
||||
|
||||
$resp = $this->asAdmin()->get('/settings/webhooks');
|
||||
$resp->assertOk();
|
||||
$resp->assertElementContains('a[href$="/settings/webhooks/create"]', 'Create New Webhook');
|
||||
$resp->assertElementExists('a[href="' . $webhook->getUrl() . '"]', $webhook->name);
|
||||
$resp->assertSee($webhook->endpoint);
|
||||
$resp->assertSee('All system events');
|
||||
$resp->assertSee('Active');
|
||||
}
|
||||
|
||||
public function test_create_view()
|
||||
{
|
||||
$resp = $this->asAdmin()->get('/settings/webhooks/create');
|
||||
$resp->assertOk();
|
||||
$resp->assertSee('Create New Webhook');
|
||||
$resp->assertElementContains('form[action$="/settings/webhooks/create"] button', 'Save Webhook');
|
||||
}
|
||||
|
||||
public function test_store()
|
||||
{
|
||||
$resp = $this->asAdmin()->post('/settings/webhooks/create', [
|
||||
'name' => 'My first webhook',
|
||||
'endpoint' => 'https://example.com/webhook',
|
||||
'events' => ['all'],
|
||||
'active' => 'true'
|
||||
]);
|
||||
|
||||
$resp->assertRedirect('/settings/webhooks');
|
||||
$this->assertActivityExists(ActivityType::WEBHOOK_CREATE);
|
||||
|
||||
$resp = $this->followRedirects($resp);
|
||||
$resp->assertSee('Webhook successfully created');
|
||||
|
||||
$this->assertDatabaseHas('webhooks', [
|
||||
'name' => 'My first webhook',
|
||||
'endpoint' => 'https://example.com/webhook',
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
/** @var Webhook $webhook */
|
||||
$webhook = Webhook::query()->where('name', '=', 'My first webhook')->first();
|
||||
$this->assertDatabaseHas('webhook_tracked_events', [
|
||||
'webhook_id' => $webhook->id,
|
||||
'event' => 'all',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_edit_view()
|
||||
{
|
||||
$webhook = $this->newWebhook();
|
||||
|
||||
$resp = $this->asAdmin()->get('/settings/webhooks/' . $webhook->id);
|
||||
$resp->assertOk();
|
||||
$resp->assertSee('Edit Webhook');
|
||||
$resp->assertElementContains('form[action="' . $webhook->getUrl() . '"] button', 'Save Webhook');
|
||||
$resp->assertElementContains('a[href="' . $webhook->getUrl('/delete') . '"]', 'Delete Webhook');
|
||||
$resp->assertElementExists('input[type="checkbox"][value="all"][name="events[]"]');
|
||||
}
|
||||
|
||||
public function test_update()
|
||||
{
|
||||
$webhook = $this->newWebhook();
|
||||
|
||||
$resp = $this->asAdmin()->put('/settings/webhooks/' . $webhook->id, [
|
||||
'name' => 'My updated webhook',
|
||||
'endpoint' => 'https://example.com/updated-webhook',
|
||||
'events' => [ActivityType::PAGE_CREATE, ActivityType::PAGE_UPDATE],
|
||||
'active' => 'true'
|
||||
]);
|
||||
$resp->assertRedirect('/settings/webhooks');
|
||||
|
||||
$resp = $this->followRedirects($resp);
|
||||
$resp->assertSee('Webhook successfully updated');
|
||||
|
||||
$this->assertDatabaseHas('webhooks', [
|
||||
'id' => $webhook->id,
|
||||
'name' => 'My updated webhook',
|
||||
'endpoint' => 'https://example.com/updated-webhook',
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
$trackedEvents = $webhook->trackedEvents()->get();
|
||||
$this->assertCount(2, $trackedEvents);
|
||||
$this->assertEquals(['page_create', 'page_update'], $trackedEvents->pluck('event')->values()->all());
|
||||
|
||||
$this->assertActivityExists(ActivityType::WEBHOOK_UPDATE);
|
||||
}
|
||||
|
||||
public function test_delete_view()
|
||||
{
|
||||
$webhook = $this->newWebhook(['name' => 'Webhook to delete']);
|
||||
|
||||
$resp = $this->asAdmin()->get('/settings/webhooks/' . $webhook->id . '/delete');
|
||||
$resp->assertOk();
|
||||
$resp->assertSee('Delete Webhook');
|
||||
$resp->assertSee('This will fully delete this webhook, with the name \'Webhook to delete\', from the system.');
|
||||
$resp->assertElementContains('form[action$="/settings/webhooks/' . $webhook->id . '"]', 'Delete');
|
||||
}
|
||||
|
||||
public function test_destroy()
|
||||
{
|
||||
$webhook = $this->newWebhook();
|
||||
|
||||
$resp = $this->asAdmin()->delete('/settings/webhooks/' . $webhook->id);
|
||||
$resp->assertRedirect('/settings/webhooks');
|
||||
|
||||
$resp = $this->followRedirects($resp);
|
||||
$resp->assertSee('Webhook successfully deleted');
|
||||
|
||||
$this->assertDatabaseMissing('webhooks', ['id' => $webhook->id]);
|
||||
$this->assertDatabaseMissing('webhook_tracked_events', ['webhook_id' => $webhook->id]);
|
||||
|
||||
$this->assertActivityExists(ActivityType::WEBHOOK_DELETE);
|
||||
}
|
||||
|
||||
public function test_settings_manage_permission_required_for_webhook_routes()
|
||||
{
|
||||
$editor = $this->getEditor();
|
||||
$this->actingAs($editor);
|
||||
|
||||
$routes = [
|
||||
['GET', '/settings/webhooks'],
|
||||
['GET', '/settings/webhooks/create'],
|
||||
['POST', '/settings/webhooks/create'],
|
||||
['GET', '/settings/webhooks/1'],
|
||||
['PUT', '/settings/webhooks/1'],
|
||||
['DELETE', '/settings/webhooks/1'],
|
||||
['GET', '/settings/webhooks/1/delete'],
|
||||
];
|
||||
|
||||
foreach ($routes as [$method, $endpoint]) {
|
||||
$resp = $this->call($method, $endpoint);
|
||||
$this->assertPermissionError($resp);
|
||||
}
|
||||
|
||||
$this->giveUserPermissions($editor, ['settings-manage']);
|
||||
|
||||
foreach ($routes as [$method, $endpoint]) {
|
||||
$resp = $this->call($method, $endpoint);
|
||||
$this->assertNotPermissionError($resp);
|
||||
}
|
||||
}
|
||||
|
||||
protected function newWebhook(array $attrs = [], array $events = ['all']): Webhook
|
||||
{
|
||||
/** @var Webhook $webhook */
|
||||
$webhook = Webhook::factory()->create($attrs);
|
||||
|
||||
foreach ($events as $event) {
|
||||
$webhook->trackedEvents()->create(['event' => $event]);
|
||||
}
|
||||
|
||||
return $webhook;
|
||||
}
|
||||
|
||||
}
|
@ -4,6 +4,8 @@ namespace Tests\Commands;
|
||||
|
||||
use BookStack\Actions\ActivityType;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Facades\Activity;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCase;
|
||||
|
||||
@ -12,8 +14,9 @@ class ClearActivityCommandTest extends TestCase
|
||||
public function test_clear_activity_command()
|
||||
{
|
||||
$this->asEditor();
|
||||
$page = Page::first();
|
||||
\Activity::addForEntity($page, ActivityType::PAGE_UPDATE);
|
||||
/** @var Page $page */
|
||||
$page = Page::query()->first();
|
||||
Activity::add(ActivityType::PAGE_UPDATE, $page);
|
||||
|
||||
$this->assertDatabaseHas('activities', [
|
||||
'type' => 'page_update',
|
||||
@ -22,7 +25,7 @@ class ClearActivityCommandTest extends TestCase
|
||||
]);
|
||||
|
||||
DB::rollBack();
|
||||
$exitCode = \Artisan::call('bookstack:clear-activity');
|
||||
$exitCode = Artisan::call('bookstack:clear-activity');
|
||||
DB::beginTransaction();
|
||||
$this->assertTrue($exitCode === 0, 'Command executed successfully');
|
||||
|
||||
|
@ -1,10 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Cache\ArrayStore;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
use Tests\TestCase;
|
||||
use Mockery;
|
||||
|
||||
class StatusTest extends TestCase
|
||||
{
|
||||
|
@ -64,8 +64,8 @@ class UserProfileTest extends TestCase
|
||||
$newUser = User::factory()->create();
|
||||
$this->actingAs($newUser);
|
||||
$entities = $this->createEntityChainBelongingToUser($newUser, $newUser);
|
||||
Activity::addForEntity($entities['book'], ActivityType::BOOK_UPDATE);
|
||||
Activity::addForEntity($entities['page'], ActivityType::PAGE_CREATE);
|
||||
Activity::add(ActivityType::BOOK_UPDATE, $entities['book']);
|
||||
Activity::add(ActivityType::PAGE_CREATE, $entities['page']);
|
||||
|
||||
$this->asAdmin()->get('/user/' . $newUser->slug)
|
||||
->assertElementContains('#recent-user-activity', 'updated book')
|
||||
@ -78,8 +78,8 @@ class UserProfileTest extends TestCase
|
||||
$newUser = User::factory()->create();
|
||||
$this->actingAs($newUser);
|
||||
$entities = $this->createEntityChainBelongingToUser($newUser, $newUser);
|
||||
Activity::addForEntity($entities['book'], ActivityType::BOOK_UPDATE);
|
||||
Activity::addForEntity($entities['page'], ActivityType::PAGE_CREATE);
|
||||
Activity::add(ActivityType::BOOK_UPDATE, $entities['book']);
|
||||
Activity::add(ActivityType::PAGE_CREATE, $entities['page']);
|
||||
|
||||
$linkSelector = '#recent-activity a[href$="/user/' . $newUser->slug . '"]';
|
||||
$this->asAdmin()->get('/')
|
||||
|
Reference in New Issue
Block a user