snipe-it/tests/Feature/Notifications/AssetSlackTest.php

111 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature\Notifications;
use App\Events\CheckoutableCheckedIn;
2023-03-30 15:38:00 -07:00
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\Notification;
2023-04-17 17:31:12 -07:00
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
2024-02-27 16:48:17 -08:00
class AssetSlackTest extends TestCase
{
2023-04-17 17:31:12 -07:00
use InteractsWithSettings;
2024-02-27 17:09:22 -08:00
public function assetCheckoutTargets(): array
{
2023-03-23 16:42:21 -07:00
return [
'Asset checked out to user' => [fn() => User::factory()->create()],
2024-02-27 17:09:22 -08:00
'Asset checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()],
2023-03-23 16:42:21 -07:00
'Asset checked out to location' => [fn() => Location::factory()->create()],
];
}
2024-02-27 17:09:22 -08:00
/** @dataProvider assetCheckoutTargets */
2024-02-27 16:48:17 -08:00
public function testAssetCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget)
{
Notification::fake();
$this->settings->enableSlackWebhook();
2023-03-30 15:38:00 -07:00
event(new CheckoutableCheckedOut(
2024-02-27 17:09:22 -08:00
Asset::factory()->laptopMbp()->create(),
2023-03-23 16:42:21 -07:00
$checkoutTarget(),
2023-03-30 15:38:00 -07:00
User::factory()->superuser()->create(),
''
));
Notification::assertSentTo(
new AnonymousNotifiable,
CheckoutAssetNotification::class,
function ($notification, $channels, $notifiable) {
2023-03-22 12:31:47 -07:00
return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint;
}
);
}
2024-02-27 17:09:22 -08:00
/** @dataProvider assetCheckoutTargets */
2024-02-27 16:48:17 -08:00
public function testAssetCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget)
{
Notification::fake();
2024-02-27 16:45:49 -08:00
$this->settings->disableSlackWebhook();
2023-03-30 15:38:00 -07:00
event(new CheckoutableCheckedOut(
2024-02-27 17:09:22 -08:00
Asset::factory()->laptopMbp()->create(),
2023-03-23 16:42:21 -07:00
$checkoutTarget(),
2023-03-30 15:38:00 -07:00
User::factory()->superuser()->create(),
''
));
2023-03-23 16:42:21 -07:00
Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAssetNotification::class);
2023-03-22 12:38:14 -07:00
}
2024-02-27 17:09:22 -08:00
/** @dataProvider assetCheckoutTargets */
2024-02-27 16:48:17 -08:00
public function testAssetCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget)
{
Notification::fake();
$this->settings->enableSlackWebhook();
event(new CheckoutableCheckedIn(
2024-02-27 17:09:22 -08:00
Asset::factory()->laptopMbp()->create(),
$checkoutTarget(),
User::factory()->superuser()->create(),
''
));
Notification::assertSentTo(
new AnonymousNotifiable,
CheckinAssetNotification::class,
function ($notification, $channels, $notifiable) {
return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint;
}
);
}
2024-02-27 17:09:22 -08:00
/** @dataProvider assetCheckoutTargets */
2024-02-27 16:48:17 -08:00
public function testAssetCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget)
{
Notification::fake();
2024-02-27 16:45:49 -08:00
$this->settings->disableSlackWebhook();
event(new CheckoutableCheckedIn(
2024-02-27 17:09:22 -08:00
Asset::factory()->laptopMbp()->create(),
$checkoutTarget(),
User::factory()->superuser()->create(),
''
));
Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAssetNotification::class);
}
}