forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookResourceTest.php
More file actions
80 lines (62 loc) · 2.13 KB
/
Copy pathWebhookResourceTest.php
File metadata and controls
80 lines (62 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
use App\Enums\UserRole;
use App\Enums\WebhookEvent;
use App\Filament\Resources\Webhooks\Pages\ListWebhooks;
use App\Filament\Resources\Webhooks\WebhookResource;
use App\Models\User;
use App\Models\Webhook;
use Filament\Actions\Testing\TestAction;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;
use Spatie\WebhookServer\CallWebhookJob;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\assertDatabaseHas;
beforeEach(function () {
$this->admin = User::factory()->create(['role' => UserRole::Admin]);
});
it('lists webhooks for an admin', function () {
$webhooks = Webhook::factory()->count(3)->create();
actingAs($this->admin);
Livewire::test(ListWebhooks::class)
->assertOk()
->assertCanSeeTableRecords($webhooks);
});
it('creates a webhook through the form', function () {
actingAs($this->admin);
Livewire::test(ListWebhooks::class)
->callAction('create', [
'url' => 'https://example.com/hook',
'events' => [WebhookEvent::Completed->value, WebhookEvent::Failed->value],
'enabled' => true,
])
->assertHasNoActionErrors();
assertDatabaseHas('webhooks', [
'url' => 'https://example.com/hook',
'enabled' => true,
]);
});
it('validates the webhook form', function () {
actingAs($this->admin);
Livewire::test(ListWebhooks::class)
->callAction('create', [
'url' => 'not-a-valid-url',
'events' => [],
])
->assertHasActionErrors([
'url' => 'url',
'events' => 'required',
]);
});
it('queues a test webhook and notifies the user', function () {
Bus::fake();
$webhook = Webhook::factory()->create();
actingAs($this->admin);
Livewire::test(ListWebhooks::class)
->callAction(TestAction::make('test')->table($webhook))
->assertNotified(__('webhooks.test_queued'));
Bus::assertDispatchedTimes(CallWebhookJob::class, 1);
});
it('denies access to non-admin users', function () {
actingAs(User::factory()->create(['role' => UserRole::User]));
expect(WebhookResource::canAccess())->toBeFalse();
});