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

81 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature\Notifications;
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\CheckoutAssetNotification;
use Illuminate\Notifications\AnonymousNotifiable;
2023-03-30 15:38:00 -07:00
use Illuminate\Support\Facades\Event;
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-30 15:38:00 -07:00
public function testAssetCheckoutFiresCheckoutEvent()
{
Event::fake([CheckoutableCheckedOut::class]);
$this->createAsset()->checkOut(User::factory()->create(), User::factory()->create());
Event::assertDispatched(CheckoutableCheckedOut::class);
}
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-30 15:38:00 -07:00
event(new CheckoutableCheckedOut(
$this->createAsset(),
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;
}
);
}
2023-03-23 16:42:21 -07:00
/** @dataProvider checkoutTargets */
public function testWebhookNotificationsAreNotSentOnAssetCheckoutWhenWebhookSettingNotEnabled($checkoutTarget)
{
Notification::fake();
Setting::factory()->withWebhookDisabled()->create();
2023-03-30 15:38:00 -07:00
event(new CheckoutableCheckedOut(
$this->createAsset(),
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
}
private function createAsset()
{
return Asset::factory()->laptopMbp()->create();
}
}