snipe-it/tests/Feature/Notifications/LicenseWebhookTest.php

107 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature\Notifications;
use App\Events\CheckoutableCheckedIn;
use App\Events\CheckoutableCheckedOut;
use App\Models\Asset;
use App\Models\LicenseSeat;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckinLicenseSeatNotification;
use App\Notifications\CheckoutLicenseSeatNotification;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
2023-04-05 12:23:11 -07:00
class LicenseWebhookTest extends TestCase
{
public function checkoutTargets(): array
{
return [
'License checked out to user' => [fn() => User::factory()->create()],
'License checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()],
];
}
/** @dataProvider checkoutTargets */
2023-04-05 12:27:18 -07:00
public function testLicenseCheckoutSendsWebhookNotificationWhenSettingEnabled($checkoutTarget)
{
Notification::fake();
Setting::factory()->withWebhookEnabled()->create();
event(new CheckoutableCheckedOut(
LicenseSeat::factory()->create(),
$checkoutTarget(),
User::factory()->superuser()->create(),
''
));
Notification::assertSentTo(
new AnonymousNotifiable,
CheckoutLicenseSeatNotification::class,
function ($notification, $channels, $notifiable) {
return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint;
}
);
}
/** @dataProvider checkoutTargets */
2023-04-05 12:27:18 -07:00
public function testLicenseCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget)
{
Notification::fake();
Setting::factory()->withWebhookDisabled()->create();
event(new CheckoutableCheckedOut(
LicenseSeat::factory()->create(),
$checkoutTarget(),
User::factory()->superuser()->create(),
''
));
Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutLicenseSeatNotification::class);
}
/** @dataProvider checkoutTargets */
public function testLicenseCheckinSendsWebhookNotificationWhenSettingEnabled($checkoutTarget)
{
Notification::fake();
Setting::factory()->withWebhookEnabled()->create();
event(new CheckoutableCheckedIn(
LicenseSeat::factory()->create(),
$checkoutTarget(),
User::factory()->superuser()->create(),
''
));
Notification::assertSentTo(
new AnonymousNotifiable,
CheckinLicenseSeatNotification::class,
function ($notification, $channels, $notifiable) {
return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint;
}
);
}
/** @dataProvider checkoutTargets */
public function testLicenseCheckinDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget)
{
Notification::fake();
Setting::factory()->withWebhookDisabled()->create();
event(new CheckoutableCheckedIn(
LicenseSeat::factory()->create(),
$checkoutTarget(),
User::factory()->superuser()->create(),
''
));
Notification::assertNotSentTo(new AnonymousNotifiable, CheckinLicenseSeatNotification::class);
}
}