forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrateWebhookSettingsTest.php
More file actions
69 lines (54 loc) · 2.18 KB
/
Copy pathMigrateWebhookSettingsTest.php
File metadata and controls
69 lines (54 loc) · 2.18 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
<?php
use App\Enums\WebhookEvent;
use App\Models\Webhook;
use Illuminate\Support\Facades\DB;
/**
* Set a notification setting value directly in the settings table.
*/
function setWebhookSetting(string $name, mixed $value): void
{
DB::table('settings')->updateOrInsert(
['group' => 'notification', 'name' => $name],
['payload' => json_encode($value), 'locked' => false, 'updated_at' => now()],
);
}
function runWebhookSettingsMigration(): void
{
$migration = require base_path('database/migrations/2026_05_29_000002_migrate_webhook_settings_to_table.php');
$migration->up();
}
it('migrates configured webhook urls into the webhooks table with mapped events', function () {
setWebhookSetting('webhook_enabled', true);
setWebhookSetting('webhook_on_speedtest_run', true);
setWebhookSetting('webhook_on_threshold_failure', true);
setWebhookSetting('webhook_urls', [
['url' => 'https://example.com/hook-a'],
['url' => 'https://example.com/hook-b'],
]);
runWebhookSettingsMigration();
expect(Webhook::count())->toBe(2);
$webhook = Webhook::where('url', 'https://example.com/hook-a')->first();
expect($webhook->enabled)->toBeTrue()
->and($webhook->events)->toBe([
WebhookEvent::Completed->value,
WebhookEvent::BenchmarkUnhealthy->value,
]);
});
it('maps only the enabled triggers to events', function () {
setWebhookSetting('webhook_enabled', false);
setWebhookSetting('webhook_on_speedtest_run', true);
setWebhookSetting('webhook_on_threshold_failure', false);
setWebhookSetting('webhook_urls', [['url' => 'https://example.com/hook']]);
runWebhookSettingsMigration();
$webhook = Webhook::sole();
expect($webhook->enabled)->toBeFalse()
->and($webhook->events)->toBe([WebhookEvent::Completed->value]);
});
it('creates no webhooks when none were configured', function () {
setWebhookSetting('webhook_enabled', false);
setWebhookSetting('webhook_on_speedtest_run', false);
setWebhookSetting('webhook_on_threshold_failure', false);
setWebhookSetting('webhook_urls', null);
runWebhookSettingsMigration();
expect(Webhook::count())->toBe(0);
});