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

64 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature\Notifications;
use App\Models\Asset;
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;
2023-03-22 12:41:30 -07:00
class AssetCheckoutWebhookNotificationTest extends TestCase
{
2023-03-23 16:42:21 -07:00
public function checkoutTargets()
{
2023-03-23 16:42:21 -07:00
return [
'Asset checked out to user' => [fn() => User::factory()->create()],
'Asset checked out to asset' => [fn() => $this->createAsset()],
'Asset checked out to location' => [fn() => Location::factory()->create()],
];
}
2023-03-23 16:42:21 -07:00
/** @dataProvider checkoutTargets */
public function testWebhookNotificationsAreSentOnAssetCheckoutWhenWebhookSettingEnabled($checkoutTarget)
{
Notification::fake();
2023-03-23 16:42:21 -07:00
Setting::factory()->withWebhookEnabled()->create();
2023-03-22 12:38:14 -07:00
$this->createAsset()->checkOut(
2023-03-23 16:42:21 -07:00
$checkoutTarget(),
User::factory()->superuser()->create()->id
);
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;
}
);
}
2023-03-23 16:42:21 -07:00
/** @dataProvider checkoutTargets */
public function testWebhookNotificationsAreNotSentOnAssetCheckoutWhenWebhookSettingNotEnabled($checkoutTarget)
{
Notification::fake();
2023-03-22 12:38:14 -07:00
$this->createAsset()->checkOut(
2023-03-23 16:42:21 -07:00
$checkoutTarget(),
User::factory()->superuser()->create()->id
);
2023-03-23 16:42:21 -07:00
Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAssetNotification::class);
2023-03-22 12:38:14 -07:00
}
private function createAsset()
{
return Asset::factory()->laptopMbp()->create();
}
}