Skip to content

A free open source IT asset/license management system

Add tests for sending webhooks on asset and license seat checkin

Marcus Moore5b4d5cadf4e8
diff --git a/tests/Feature/Notifications/AssetWebhookTest.php b/tests/Feature/Notifications/AssetWebhookTest.php
index fad1deb1ce..0e1ef9f201 100644
--- a/tests/Feature/Notifications/AssetWebhookTest.php
+++ b/tests/Feature/Notifications/AssetWebhookTest.php
@@ -2,11 +2,13 @@
namespace Tests\Feature\Notifications;
+use App\Events\CheckoutableCheckedIn;
use App\Events\CheckoutableCheckedOut;
use App\Models\Asset;
use App\Models\Location;
use App\Models\Setting;
use App\Models\User;
+use App\Notifications\CheckinAssetNotification;
use App\Notifications\CheckoutAssetNotification;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Event;
@@ -15,7 +17,7 @@ use Tests\TestCase;
class AssetWebhookTest extends TestCase
{
- public function checkoutTargets()
+ public function checkoutTargets(): array
{
return [
'Asset checked out to user' => [fn() => User::factory()->create()],
@@ -73,6 +75,46 @@ class AssetWebhookTest extends TestCase
Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAssetNotification::class);
}
+ /** @dataProvider checkoutTargets */
+ public function testAssetCheckinSendsWebhookNotificationWhenSettingEnabled($checkoutTarget)
+ {
+ Notification::fake();
+
+ Setting::factory()->withWebhookEnabled()->create();
+
+ event(new CheckoutableCheckedIn(
+ $this->createAsset(),
+ $checkoutTarget(),
+ User::factory()->superuser()->create(),
+ ''
+ ));
+
+ Notification::assertSentTo(
+ new AnonymousNotifiable,
+ CheckinAssetNotification::class,
+ function ($notification, $channels, $notifiable) {
+ return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint;
+ }
+ );
+ }
+
+ /** @dataProvider checkoutTargets */
+ public function testAssetCheckinDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget)
+ {
+ Notification::fake();
+
+ Setting::factory()->withWebhookDisabled()->create();
+
+ event(new CheckoutableCheckedIn(
+ $this->createAsset(),
+ $checkoutTarget(),
+ User::factory()->superuser()->create(),
+ ''
+ ));
+
+ Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAssetNotification::class);
+ }
+
private function createAsset()
{
return Asset::factory()->laptopMbp()->create();
diff --git a/tests/Feature/Notifications/LicenseWebhookTest.php b/tests/Feature/Notifications/LicenseWebhookTest.php
index f12fcceaea..9931f80a2c 100644
--- a/tests/Feature/Notifications/LicenseWebhookTest.php
+++ b/tests/Feature/Notifications/LicenseWebhookTest.php
@@ -2,11 +2,13 @@
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;
@@ -14,7 +16,7 @@ use Tests\TestCase;
class LicenseWebhookTest extends TestCase
{
- public function checkoutTargets()
+ public function checkoutTargets(): array
{
return [
'License checked out to user' => [fn() => User::factory()->create()],
@@ -61,4 +63,44 @@ class LicenseWebhookTest extends TestCase
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);
+ }
}