mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-26 06:04:08 -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)
|
||||
{
|
||||
|
||||
// @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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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(
|
||||
$targetAsset,
|
||||
$this->createAsset(),
|
||||
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()
|
||||
{
|
||||
$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);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue