Skip to content

A free open source IT asset/license management system

Send slack notification for check outs to assets and locations

Marcus Moore315f5231cd30
diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php
index 9d6cd942bd..5a39b5f513 100644
--- a/app/Listeners/CheckoutableListener.php
+++ b/app/Listeners/CheckoutableListener.php
@@ -32,11 +32,26 @@ class CheckoutableListener
public function onCheckedOut($event)
{
+ // @todo: update docblock
/**
* When the item wasn't checked out to a user, we can't send notifications
*/
+ // @todo: update comment
if (! $event->checkedOutTo instanceof User) {
+
+ // @todo: comment
+ if (Setting::getSettings() && Setting::getSettings()->slack_endpoint) {
+ Notification::route('slack', Setting::getSettings()->slack_endpoint)
+ ->notify(new CheckoutAssetNotification(
+ $event->checkoutable,
+ $event->checkedOutTo,
+ $event->checkedOutBy,
+ null,
+ $event->note)
+ );
+ }
+
return;
}
diff --git a/tests/Feature/Notifications/AssetCheckoutSlackNotificationTest.php b/tests/Feature/Notifications/AssetCheckoutSlackNotificationTest.php
index 75df4a1626..468d81678a 100644
--- a/tests/Feature/Notifications/AssetCheckoutSlackNotificationTest.php
+++ b/tests/Feature/Notifications/AssetCheckoutSlackNotificationTest.php
@@ -9,16 +9,27 @@ use App\Models\Location;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckoutAssetNotification;
+use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class AssetCheckoutSlackNotificationTest extends TestCase
{
+ private Category $assetLaptopCategory;
+ private string $slackWebhookUrl = 'https://hooks.slack.com/services/NZ59O2F54K/Q4465WNLM8/672N8MU5JV15RP436WDHRN58';
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ $this->assetLaptopCategory = Category::factory()->assetLaptopCategory();
+ }
+
public function testNotificationSentToSlackWhenAssetCheckedOutToUserAndSlackNotificationEnabled()
{
Notification::fake();
- $this->setSlackWebhook();
+ Setting::factory()->create(['slack_endpoint' => $this->slackWebhookUrl]);
$asset = $this->createAsset();
$user = User::factory()->create();
@@ -28,70 +39,99 @@ class AssetCheckoutSlackNotificationTest extends TestCase
User::factory()->superuser()->create()->id
);
- $this->assetSlackNotificationSentTo($user);
+ Notification::assertSentTo(
+ $user,
+ function (CheckoutAssetNotification $notification, $channels) {
+ // @todo: is this actually accurate?
+ return in_array('slack', $channels);
+ }
+ );
}
public function testNotificationSentToSlackWhenAssetCheckedOutToAssetAndSlackNotificationEnabled()
{
- $this->markTestIncomplete();
-
Notification::fake();
- $this->setSlackWebhook();
+ Setting::factory()->create(['slack_endpoint' => $this->slackWebhookUrl]);
$assetBeingCheckedOut = $this->createAsset();
- $targetAsset = $this->createAsset();
+ $assetBeingCheckedOut->checkOut(
+ $this->createAsset(),
+ User::factory()->superuser()->create()->id
+ );
+
+ // Since the target is not a user with an email address we have
+ // to check if an AnonymousNotifiable was sent.
+ Notification::assertSentTo(
+ new AnonymousNotifiable,
+ CheckoutAssetNotification::class,
+ function ($notification, $channels, $notifiable) {
+ return $notifiable->routes['slack'] === $this->slackWebhookUrl;
+ }
+ );
+ }
+
+ public function testDoesNotSendNotificationViaSlackIfWebHookEndpointIsNotSetWhenCheckingOutAssetToAsset()
+ {
+ Notification::fake();
+ $assetBeingCheckedOut = $this->createAsset();
$assetBeingCheckedOut->checkOut(
- $targetAsset,
+ $this->createAsset(),
User::factory()->superuser()->create()->id
);
- $this->assetSlackNotificationSentTo($targetAsset);
+ Notification::assertNotSentTo(
+ new AnonymousNotifiable,
+ CheckoutAssetNotification::class,
+ );
}
public function testNotificationSentToSlackWhenAssetCheckedOutToLocationAndSlackNotificationEnabled()
{
- $this->markTestIncomplete();
-
Notification::fake();
- $this->setSlackWebhook();
+ Setting::factory()->create(['slack_endpoint' => $this->slackWebhookUrl]);
$asset = $this->createAsset();
- $location = Location::factory()->create();
-
$asset->checkOut(
- $location,
+ Location::factory()->create(),
User::factory()->superuser()->create()->id
);
- $this->assetSlackNotificationSentTo($location);
+ // Since the target is not a user with an email address we have
+ // to check if an AnonymousNotifiable was sent.
+ Notification::assertSentTo(
+ new AnonymousNotifiable,
+ CheckoutAssetNotification::class,
+ function ($notification, $channels, $notifiable) {
+ return $notifiable->routes['slack'] === $this->slackWebhookUrl;
+ }
+ );
}
- private function setSlackWebhook()
+ public function testtestDoesNotSendNotificationViaSlackIfWebHookEndpointIsNotSetWhenCheckingOutAssetToLocation()
{
- Setting::factory()->create([
- 'slack_endpoint' => 'https://hooks.slack.com/services/NZ59O2F54K/Q4465WNLM8/672N8MU5JV15RP436WDHRN58',
- ]);
+ Notification::fake();
+
+ $asset = $this->createAsset();
+ $asset->checkOut(
+ Location::factory()->create(),
+ User::factory()->superuser()->create()->id
+ );
+
+ Notification::assertNotSentTo(
+ new AnonymousNotifiable,
+ CheckoutAssetNotification::class,
+ );
}
private function createAsset()
{
return Asset::factory()->create([
'model_id' => AssetModel::factory()->create([
- 'category_id' => Category::factory()->assetLaptopCategory()->id,
+ 'category_id' => $this->assetLaptopCategory->id,
])->id,
]);
}
-
- private function assetSlackNotificationSentTo($target)
- {
- Notification::assertSentTo(
- $target,
- function (CheckoutAssetNotification $notification, $channels) {
- return in_array('slack', $channels);
- }
- );
- }
}