Skip to content

A free open source IT asset/license management system

fix asset checkout notification, add tests for building notifications

Godfrey Ma8fa90adca9f
diff --git a/app/Notifications/CheckoutAssetNotification.php b/app/Notifications/CheckoutAssetNotification.php
index 3b2af7b542..7225adfef1 100644
--- a/app/Notifications/CheckoutAssetNotification.php
+++ b/app/Notifications/CheckoutAssetNotification.php
@@ -82,12 +82,13 @@ class CheckoutAssetNotification extends Notification implements ShouldQueue
public function toSlack(): SlackMessage
{
+ $settings = Setting::getSettings();
$target = $this->target;
$admin = $this->admin;
$item = $this->item;
$note = $this->note;
- $botname = ($this->settings->webhook_botname) ?: 'Snipe-Bot';
- $channel = ($this->settings->webhook_channel) ? $this->settings->webhook_channel : '';
+ $botname = ($settings->webhook_botname) ?: 'Snipe-Bot';
+ $channel = ($settings->webhook_channel) ? $settings->webhook_channel : '';
$fields = [
trans('general.to_user') => '<'.$target->present()->viewUrl().'|'.$target->display_name.'>',
@@ -119,14 +120,15 @@ class CheckoutAssetNotification extends Notification implements ShouldQueue
public function toMicrosoftTeams()
{
+ $settings = Setting::getSettings();
$target = $this->target;
$admin = $this->admin;
$item = $this->item;
$note = $this->note;
- if (! Str::contains(Setting::getSettings()->webhook_endpoint, 'workflows')) {
+ if (!Str::contains($settings->webhook_endpoint, 'workflows')) {
return MicrosoftTeamsMessage::create()
- ->to($this->settings->webhook_endpoint)
+ ->to($settings->webhook_endpoint)
->type('success')
->title(trans('mail.Asset_Checkout_Notification', ['tag' => '']))
->addStartGroupToSection('activityText')
@@ -149,12 +151,13 @@ class CheckoutAssetNotification extends Notification implements ShouldQueue
public function toGoogleChat()
{
+ $settings = Setting::getSettings();
$target = $this->target;
$item = $this->item;
$note = $this->note;
return GoogleChatMessage::create()
- ->to($this->settings->webhook_endpoint)
+ ->to($settings->webhook_endpoint)
->card(
Card::create()
->header(
diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php
index d6f658dfc4..793e505ab4 100644
--- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php
+++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckinTest.php
@@ -15,9 +15,12 @@ use App\Notifications\CheckinAccessoryNotification;
use App\Notifications\CheckinAssetNotification;
use App\Notifications\CheckinComponentNotification;
use App\Notifications\CheckinLicenseSeatNotification;
+use App\Notifications\CheckoutAssetNotification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\AnonymousNotifiable;
+use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Support\Facades\Notification;
+use NotificationChannels\GoogleChat\GoogleChatMessage;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
@@ -203,4 +206,90 @@ class SlackNotificationsUponCheckinTest extends TestCase
''
));
}
+
+ #[DataProvider('checkinNotificationCases')]
+ public function test_checkin_slack_notifications_can_be_built($createNotification)
+ {
+ $this->settings->enableSlackWebhook();
+
+ $notification = $createNotification();
+
+ $this->assertInstanceOf(
+ SlackMessage::class,
+ $notification->toSlack()
+ );
+ }
+
+ #[DataProvider('checkinNotificationCases')]
+ public function test_checkin_google_chat_notifications_can_be_built($createNotification)
+ {
+ $this->settings->enableGoogleChatWebhook();
+
+ $notification = $createNotification();
+
+ $this->assertInstanceOf(
+ GoogleChatMessage::class,
+ $notification->toGoogleChat()
+ );
+ }
+
+ #[DataProvider('checkinNotificationCases')]
+ public function test_checkin_microsoft_teams_workflow_notifications_can_be_built($createNotification)
+ {
+ $this->settings->enableMicrosoftTeamsWebhook();
+
+ $notification = $createNotification();
+
+ $message = $notification->toMicrosoftTeams();
+
+ $this->assertIsArray($message);
+ $this->assertCount(2, $message);
+ $this->assertIsString($message[0]);
+ $this->assertIsArray($message[1]);
+ }
+
+ public static function checkinNotificationCases(): array
+ {
+ return [
+ 'Asset' => [
+ fn() => new CheckinAssetNotification(
+ Asset::factory()->create(),
+ User::factory()->create(),
+ User::factory()->superuser()->create(),
+ null,
+ '',
+ ),
+ ],
+
+ 'Accessory' => [
+ fn() => new CheckinAccessoryNotification(
+ Accessory::factory()->create(),
+ User::factory()->create(),
+ User::factory()->superuser()->create(),
+ null,
+ '',
+ ),
+ ],
+
+ 'Component' => [
+ fn() => new CheckinComponentNotification(
+ Component::factory()->create(),
+ User::factory()->create(),
+ User::factory()->superuser()->create(),
+ null,
+ '',
+ ),
+ ],
+
+ 'License seat' => [
+ fn() => new CheckinLicenseSeatNotification(
+ LicenseSeat::factory()->create(),
+ User::factory()->create(),
+ User::factory()->superuser()->create(),
+ null,
+ '',
+ ),
+ ],
+ ];
+ }
}
diff --git a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php
index 5f9e876d85..6584b73fe7 100644
--- a/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php
+++ b/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php
@@ -24,6 +24,9 @@ use Illuminate\Support\Facades\Notification;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
+use Illuminate\Notifications\Messages\SlackMessage;
+use NotificationChannels\GoogleChat\GoogleChatMessage;
+use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;
#[Group('notifications')]
class SlackNotificationsUponCheckoutTest extends TestCase
@@ -241,4 +244,98 @@ class SlackNotificationsUponCheckoutTest extends TestCase
'',
));
}
+
+ #[DataProvider('checkoutNotificationCases')]
+ public function test_checkout_slack_notifications_can_be_built($createNotification)
+ {
+ $this->settings->enableSlackWebhook();
+
+ $notification = $createNotification();
+
+ $this->assertInstanceOf(
+ SlackMessage::class,
+ $notification->toSlack()
+ );
+ }
+
+ #[DataProvider('checkoutNotificationCases')]
+ public function test_checkout_google_chat_notifications_can_be_built($createNotification)
+ {
+ $this->settings->enableGoogleChatWebhook();
+ $notification = $createNotification();
+
+ $this->assertInstanceOf(
+ GoogleChatMessage::class,
+ $notification->toGoogleChat()
+ );
+ }
+
+ #[DataProvider('checkoutNotificationCases')]
+ public function test_checkout_microsoft_teams_workflow_notifications_can_be_built($createNotification)
+ {
+ $this->settings->enableMicrosoftTeamsWebhook();
+ $notification = $createNotification();
+
+ $message = $notification->toMicrosoftTeams();
+
+ $this->assertIsArray($message);
+ $this->assertCount(2, $message);
+ $this->assertIsString($message[0]);
+ $this->assertIsArray($message[1]);
+ }
+
+ public static function checkoutNotificationCases(): array
+ {
+ return [
+ 'Accessory' => [
+ fn() => new CheckoutAccessoryNotification(
+ Accessory::factory()->create(),
+ User::factory()->create(),
+ User::factory()->superuser()->create(),
+ null,
+ 'Test note',
+ ),
+ ],
+
+ 'Asset' => [
+ fn() => new CheckoutAssetNotification(
+ Asset::factory()->create(),
+ User::factory()->create(),
+ User::factory()->superuser()->create(),
+ null,
+ 'Test note',
+ ),
+ ],
+
+ 'Component' => [
+ fn() => new CheckoutComponentNotification(
+ Component::factory()->create(),
+ User::factory()->create(),
+ User::factory()->superuser()->create(),
+ null,
+ 'Test note',
+ ),
+ ],
+
+ 'Consumable' => [
+ fn() => new CheckoutConsumableNotification(
+ Consumable::factory()->create(),
+ User::factory()->create(),
+ User::factory()->superuser()->create(),
+ null,
+ 'Test note',
+ ),
+ ],
+
+ 'License seat' => [
+ fn() => new CheckoutLicenseSeatNotification(
+ LicenseSeat::factory()->create(),
+ User::factory()->create(),
+ User::factory()->superuser()->create(),
+ null,
+ 'Test note',
+ ),
+ ],
+ ];
+ }
}