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

158 lines
4.5 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature\Notifications\Webhooks;
use App\Events\CheckoutableCheckedIn;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\Component;
2024-02-27 17:14:35 -08:00
use App\Models\LicenseSeat;
use App\Models\Location;
use App\Models\User;
use App\Notifications\CheckinAccessoryNotification;
2024-02-27 17:14:35 -08:00
use App\Notifications\CheckinAssetNotification;
use App\Notifications\CheckinLicenseSeatNotification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Notification;
2024-02-27 18:01:08 -08:00
use Tests\Support\AssertsAgainstSlackNotifications;
2023-04-17 17:31:12 -07:00
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class SlackNotificationsUponCheckinTest extends TestCase
{
2024-02-27 18:01:08 -08:00
use AssertsAgainstSlackNotifications;
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()],
];
}
public function testAccessoryCheckinSendsSlackNotificationWhenSettingEnabled()
{
$this->settings->enableSlackWebhook();
$this->fireCheckInEvent(
2024-02-27 17:46:27 -08:00
Accessory::factory()->create(),
User::factory()->create(),
);
$this->assertSlackNotificationSent(CheckinAccessoryNotification::class);
}
public function testAccessoryCheckinDoesNotSendSlackNotificationWhenSettingDisabled()
{
2024-02-27 16:45:49 -08:00
$this->settings->disableSlackWebhook();
$this->fireCheckInEvent(
2024-02-27 17:46:27 -08:00
Accessory::factory()->create(),
User::factory()->create(),
);
$this->assertNoSlackNotificationSent(CheckinAccessoryNotification::class);
}
2024-02-27 17:14:35 -08:00
/** @dataProvider assetCheckoutTargets */
public function testAssetCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget)
{
$this->settings->enableSlackWebhook();
$this->fireCheckInEvent(
2024-02-27 18:03:53 -08:00
Asset::factory()->create(),
2024-02-27 17:14:35 -08:00
$checkoutTarget(),
);
2024-02-27 17:14:35 -08:00
$this->assertSlackNotificationSent(CheckinAssetNotification::class);
2024-02-27 17:14:35 -08:00
}
/** @dataProvider assetCheckoutTargets */
public function testAssetCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget)
{
$this->settings->disableSlackWebhook();
$this->fireCheckInEvent(
2024-02-27 18:03:53 -08:00
Asset::factory()->create(),
2024-02-27 17:14:35 -08:00
$checkoutTarget(),
);
2024-02-27 17:14:35 -08:00
$this->assertNoSlackNotificationSent(CheckinAssetNotification::class);
2024-02-27 17:14:35 -08:00
}
public function testComponentCheckinDoesNotSendSlackNotification()
{
$this->settings->enableSlackWebhook();
$this->fireCheckInEvent(
2024-02-27 18:03:53 -08:00
Component::factory()->create(),
Asset::factory()->laptopMbp()->create(),
);
Notification::assertNothingSent();
}
public function testConsumableCheckinSendSlackNotificationWhenSettingEnabled()
{
$this->markTestIncomplete();
}
public function testConsumableCheckinDoesNotSendSlackNotificationWhenSettingDisabled()
{
$this->markTestIncomplete();
}
2024-02-27 17:14:35 -08:00
/** @dataProvider licenseCheckoutTargets */
public function testLicenseCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget)
{
$this->settings->enableSlackWebhook();
$this->fireCheckInEvent(
2024-02-27 17:14:35 -08:00
LicenseSeat::factory()->create(),
$checkoutTarget(),
);
2024-02-27 17:14:35 -08:00
$this->assertSlackNotificationSent(CheckinLicenseSeatNotification::class);
2024-02-27 17:14:35 -08:00
}
/** @dataProvider licenseCheckoutTargets */
public function testLicenseCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget)
{
$this->settings->disableSlackWebhook();
$this->fireCheckInEvent(
2024-02-27 17:14:35 -08:00
LicenseSeat::factory()->create(),
$checkoutTarget(),
);
$this->assertNoSlackNotificationSent(CheckinLicenseSeatNotification::class);
}
private function fireCheckInEvent(Model $checkoutable, Model $target)
{
event(new CheckoutableCheckedIn(
$checkoutable,
$target,
2024-02-27 17:14:35 -08:00
User::factory()->superuser()->create(),
''
));
}
}