mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 01:36:00 -04:00
Added testing for webhook management interface
This commit is contained in:
parent
8716b1922b
commit
6381041252
@ -1,11 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Database\Factories;
|
namespace Database\Factories\Actions;
|
||||||
|
|
||||||
|
use BookStack\Actions\Webhook;
|
||||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
class WebhookFactory extends Factory
|
class WebhookFactory extends Factory
|
||||||
{
|
{
|
||||||
|
|
||||||
|
protected $model = Webhook::class;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define the model's default state.
|
* Define the model's default state.
|
||||||
*
|
*
|
23
database/factories/Actions/WebhookTrackedEventFactory.php
Normal file
23
database/factories/Actions/WebhookTrackedEventFactory.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use BookStack\Actions\ActivityType;
|
||||||
|
use BookStack\Actions\Webhook;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
class WebhookTrackedEventFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function definition()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'webhook_id' => Webhook::factory(),
|
||||||
|
'event' => ActivityType::all()[array_rand(ActivityType::all())],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Tests;
|
namespace Tests\Actions;
|
||||||
|
|
||||||
use BookStack\Actions\Activity;
|
use BookStack\Actions\Activity;
|
||||||
use BookStack\Actions\ActivityService;
|
use BookStack\Actions\ActivityService;
|
||||||
@ -11,6 +11,9 @@ use BookStack\Entities\Models\Page;
|
|||||||
use BookStack\Entities\Repos\PageRepo;
|
use BookStack\Entities\Repos\PageRepo;
|
||||||
use BookStack\Entities\Tools\TrashCan;
|
use BookStack\Entities\Tools\TrashCan;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Tests\TestCase;
|
||||||
|
use function app;
|
||||||
|
use function config;
|
||||||
|
|
||||||
class AuditLogTest extends TestCase
|
class AuditLogTest extends TestCase
|
||||||
{
|
{
|
168
tests/Actions/WebhookManagementTest.php
Normal file
168
tests/Actions/WebhookManagementTest.php
Normal file
@ -0,0 +1,168 @@
|
|||||||
|
<?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');
|
||||||
|
}
|
||||||
|
|
||||||
|
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'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$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',
|
||||||
|
]);
|
||||||
|
|
||||||
|
/** @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],
|
||||||
|
]);
|
||||||
|
$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',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,10 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace Tests;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Illuminate\Cache\ArrayStore;
|
use Illuminate\Cache\ArrayStore;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Session;
|
use Illuminate\Support\Facades\Session;
|
||||||
use Tests\TestCase;
|
use Mockery;
|
||||||
|
|
||||||
class StatusTest extends TestCase
|
class StatusTest extends TestCase
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user