snipe-it/tests/Feature/Notifications/Webhooks/SlackNotificationsUponCheckoutTest.php

190 lines
5.7 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature\Notifications\Webhooks;
use App\Events\CheckoutableCheckedOut;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\Component;
use App\Models\Consumable;
2024-02-27 17:14:35 -08:00
use App\Models\LicenseSeat;
use App\Models\Location;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\CheckoutAccessoryNotification;
2024-02-27 17:14:35 -08:00
use App\Notifications\CheckoutAssetNotification;
use App\Notifications\CheckoutConsumableNotification;
2024-02-27 17:14:35 -08:00
use App\Notifications\CheckoutLicenseSeatNotification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Notification;
2023-04-17 17:31:12 -07:00
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class SlackNotificationsUponCheckoutTest extends TestCase
{
2023-04-17 17:31:12 -07:00
use InteractsWithSettings;
protected function setUp(): void
{
parent::setUp();
Notification::fake();
}
2024-02-27 17:14:35 -08:00
public function assetCheckoutTargets(): array
{
return [
'Asset checked out to user' => [fn() => User::factory()->create()],
'Asset checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()],
'Asset checked out to location' => [fn() => Location::factory()->create()],
];
}
public function licenseCheckoutTargets(): array
{
return [
'License checked out to user' => [fn() => User::factory()->create()],
'License checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()],
];
}
2024-02-27 16:48:17 -08:00
public function testAccessoryCheckoutSendsSlackNotificationWhenSettingEnabled()
{
$this->settings->enableSlackWebhook();
$this->fireCheckOutEvent(
Accessory::factory()->appleBtKeyboard()->create(),
User::factory()->create(),
);
$this->assertSlackNotificationSent(CheckoutAccessoryNotification::class);
}
2024-02-27 16:48:17 -08:00
public function testAccessoryCheckoutDoesNotSendSlackNotificationWhenSettingDisabled()
{
2024-02-27 16:45:49 -08:00
$this->settings->disableSlackWebhook();
$this->fireCheckOutEvent(
Accessory::factory()->appleBtKeyboard()->create(),
User::factory()->create(),
);
$this->assertNoSlackNotificationSent(CheckoutAccessoryNotification::class);
}
2024-02-27 17:14:35 -08:00
/** @dataProvider assetCheckoutTargets */
public function testAssetCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget)
{
$this->settings->enableSlackWebhook();
$this->fireCheckOutEvent(
2024-02-27 17:14:35 -08:00
Asset::factory()->laptopMbp()->create(),
$checkoutTarget(),
);
2024-02-27 17:14:35 -08:00
$this->assertSlackNotificationSent(CheckoutAssetNotification::class);
2024-02-27 17:14:35 -08:00
}
/** @dataProvider assetCheckoutTargets */
public function testAssetCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget)
{
$this->settings->disableSlackWebhook();
$this->fireCheckOutEvent(
2024-02-27 17:14:35 -08:00
Asset::factory()->laptopMbp()->create(),
$checkoutTarget(),
);
2024-02-27 17:14:35 -08:00
$this->assertNoSlackNotificationSent(CheckoutAssetNotification::class);
2024-02-27 17:14:35 -08:00
}
public function testComponentCheckoutDoesNotSendSlackNotification()
{
$this->settings->enableSlackWebhook();
$this->fireCheckOutEvent(
Component::factory()->ramCrucial8()->create(),
Asset::factory()->laptopMbp()->create(),
);
Notification::assertNothingSent();
}
public function testConsumableCheckoutSendsSlackNotificationWhenSettingEnabled()
{
$this->settings->enableSlackWebhook();
$this->fireCheckOutEvent(
Consumable::factory()->cardstock()->create(),
User::factory()->create(),
);
$this->assertSlackNotificationSent(CheckoutConsumableNotification::class);
}
public function testConsumableCheckoutDoesNotSendSlackNotificationWhenSettingDisabled()
{
2024-02-27 16:45:49 -08:00
$this->settings->disableSlackWebhook();
$this->fireCheckOutEvent(
Consumable::factory()->cardstock()->create(),
User::factory()->create(),
);
$this->assertNoSlackNotificationSent(CheckoutConsumableNotification::class);
}
2024-02-27 17:14:35 -08:00
/** @dataProvider licenseCheckoutTargets */
public function testLicenseCheckoutSendsSlackNotificationWhenSettingEnabled($checkoutTarget)
{
$this->settings->enableSlackWebhook();
$this->fireCheckOutEvent(
2024-02-27 17:14:35 -08:00
LicenseSeat::factory()->create(),
$checkoutTarget(),
);
2024-02-27 17:14:35 -08:00
$this->assertSlackNotificationSent(CheckoutLicenseSeatNotification::class);
2024-02-27 17:14:35 -08:00
}
/** @dataProvider licenseCheckoutTargets */
public function testLicenseCheckoutDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget)
{
$this->settings->disableSlackWebhook();
$this->fireCheckOutEvent(
2024-02-27 17:14:35 -08:00
LicenseSeat::factory()->create(),
$checkoutTarget(),
);
2024-02-27 17:14:35 -08:00
$this->assertNoSlackNotificationSent(CheckoutLicenseSeatNotification::class);
2024-02-27 17:14:35 -08:00
}
private function fireCheckOutEvent(Model $checkoutable, Model $target)
{
event(new CheckoutableCheckedOut(
$checkoutable,
$target,
User::factory()->superuser()->create(),
'',
));
}
private function assertSlackNotificationSent(string $notificationClass)
{
Notification::assertSentTo(
new AnonymousNotifiable,
$notificationClass,
function ($notification, $channels, $notifiable) {
return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint;
}
);
}
private function assertNoSlackNotificationSent(string $notificationClass)
{
Notification::assertNotSentTo(new AnonymousNotifiable, $notificationClass);
}
}