mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
Send slack notification for check outs to assets and locations
This commit is contained in:
parent
2813b7ea58
commit
315f5231cd
|
@ -32,11 +32,26 @@ class CheckoutableListener
|
||||||
public function onCheckedOut($event)
|
public function onCheckedOut($event)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// @todo: update docblock
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When the item wasn't checked out to a user, we can't send notifications
|
* When the item wasn't checked out to a user, we can't send notifications
|
||||||
*/
|
*/
|
||||||
|
// @todo: update comment
|
||||||
if (! $event->checkedOutTo instanceof User) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,16 +9,27 @@ use App\Models\Location;
|
||||||
use App\Models\Setting;
|
use App\Models\Setting;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Notifications\CheckoutAssetNotification;
|
use App\Notifications\CheckoutAssetNotification;
|
||||||
|
use Illuminate\Notifications\AnonymousNotifiable;
|
||||||
use Illuminate\Support\Facades\Notification;
|
use Illuminate\Support\Facades\Notification;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
class AssetCheckoutSlackNotificationTest extends 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()
|
public function testNotificationSentToSlackWhenAssetCheckedOutToUserAndSlackNotificationEnabled()
|
||||||
{
|
{
|
||||||
Notification::fake();
|
Notification::fake();
|
||||||
|
|
||||||
$this->setSlackWebhook();
|
Setting::factory()->create(['slack_endpoint' => $this->slackWebhookUrl]);
|
||||||
|
|
||||||
$asset = $this->createAsset();
|
$asset = $this->createAsset();
|
||||||
$user = User::factory()->create();
|
$user = User::factory()->create();
|
||||||
|
@ -28,70 +39,99 @@ class AssetCheckoutSlackNotificationTest extends TestCase
|
||||||
User::factory()->superuser()->create()->id
|
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()
|
public function testNotificationSentToSlackWhenAssetCheckedOutToAssetAndSlackNotificationEnabled()
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
|
||||||
|
|
||||||
Notification::fake();
|
Notification::fake();
|
||||||
|
|
||||||
$this->setSlackWebhook();
|
Setting::factory()->create(['slack_endpoint' => $this->slackWebhookUrl]);
|
||||||
|
|
||||||
$assetBeingCheckedOut = $this->createAsset();
|
$assetBeingCheckedOut = $this->createAsset();
|
||||||
$targetAsset = $this->createAsset();
|
|
||||||
|
|
||||||
$assetBeingCheckedOut->checkOut(
|
$assetBeingCheckedOut->checkOut(
|
||||||
$targetAsset,
|
$this->createAsset(),
|
||||||
User::factory()->superuser()->create()->id
|
User::factory()->superuser()->create()->id
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assetSlackNotificationSentTo($targetAsset);
|
// 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(
|
||||||
|
$this->createAsset(),
|
||||||
|
User::factory()->superuser()->create()->id
|
||||||
|
);
|
||||||
|
|
||||||
|
Notification::assertNotSentTo(
|
||||||
|
new AnonymousNotifiable,
|
||||||
|
CheckoutAssetNotification::class,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNotificationSentToSlackWhenAssetCheckedOutToLocationAndSlackNotificationEnabled()
|
public function testNotificationSentToSlackWhenAssetCheckedOutToLocationAndSlackNotificationEnabled()
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
|
||||||
|
|
||||||
Notification::fake();
|
Notification::fake();
|
||||||
|
|
||||||
$this->setSlackWebhook();
|
Setting::factory()->create(['slack_endpoint' => $this->slackWebhookUrl]);
|
||||||
|
|
||||||
$asset = $this->createAsset();
|
$asset = $this->createAsset();
|
||||||
$location = Location::factory()->create();
|
|
||||||
|
|
||||||
$asset->checkOut(
|
$asset->checkOut(
|
||||||
$location,
|
Location::factory()->create(),
|
||||||
User::factory()->superuser()->create()->id
|
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([
|
Notification::fake();
|
||||||
'slack_endpoint' => 'https://hooks.slack.com/services/NZ59O2F54K/Q4465WNLM8/672N8MU5JV15RP436WDHRN58',
|
|
||||||
]);
|
$asset = $this->createAsset();
|
||||||
|
$asset->checkOut(
|
||||||
|
Location::factory()->create(),
|
||||||
|
User::factory()->superuser()->create()->id
|
||||||
|
);
|
||||||
|
|
||||||
|
Notification::assertNotSentTo(
|
||||||
|
new AnonymousNotifiable,
|
||||||
|
CheckoutAssetNotification::class,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createAsset()
|
private function createAsset()
|
||||||
{
|
{
|
||||||
return Asset::factory()->create([
|
return Asset::factory()->create([
|
||||||
'model_id' => AssetModel::factory()->create([
|
'model_id' => AssetModel::factory()->create([
|
||||||
'category_id' => Category::factory()->assetLaptopCategory()->id,
|
'category_id' => $this->assetLaptopCategory->id,
|
||||||
])->id,
|
])->id,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function assetSlackNotificationSentTo($target)
|
|
||||||
{
|
|
||||||
Notification::assertSentTo(
|
|
||||||
$target,
|
|
||||||
function (CheckoutAssetNotification $notification, $channels) {
|
|
||||||
return in_array('slack', $channels);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue