2023-03-30 16:58:16 -07:00
|
|
|
<?php
|
|
|
|
|
2024-02-27 17:07:40 -08:00
|
|
|
namespace Tests\Feature\Notifications\Webhooks;
|
2023-03-30 16:58:16 -07:00
|
|
|
|
2023-04-05 12:36:24 -07:00
|
|
|
use App\Events\CheckoutableCheckedIn;
|
2023-03-30 16:58:16 -07:00
|
|
|
use App\Models\Accessory;
|
2024-02-27 17:07:40 -08:00
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Component;
|
2024-02-27 17:14:35 -08:00
|
|
|
use App\Models\LicenseSeat;
|
|
|
|
use App\Models\Location;
|
2023-03-30 16:58:16 -07:00
|
|
|
use App\Models\User;
|
2023-04-05 12:36:24 -07:00
|
|
|
use App\Notifications\CheckinAccessoryNotification;
|
2024-02-27 17:14:35 -08:00
|
|
|
use App\Notifications\CheckinAssetNotification;
|
|
|
|
use App\Notifications\CheckinLicenseSeatNotification;
|
2024-02-27 17:37:07 -08:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-03-30 16:58:16 -07:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-02-27 18:05:18 -08:00
|
|
|
/**
|
|
|
|
* @group notifications
|
|
|
|
*/
|
2024-02-27 17:07:40 -08:00
|
|
|
class SlackNotificationsUponCheckinTest extends TestCase
|
2023-03-30 16:58:16 -07:00
|
|
|
{
|
2024-02-27 17:23:30 -08:00
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Notification::fake();
|
|
|
|
}
|
|
|
|
|
2024-03-13 16:37:50 -07:00
|
|
|
public function assetCheckInTargets(): array
|
2024-02-27 17:14:35 -08:00
|
|
|
{
|
|
|
|
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()],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-03-13 16:37:50 -07:00
|
|
|
public function licenseCheckInTargets(): array
|
2024-02-27 17:14:35 -08:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'License checked out to user' => [fn() => User::factory()->create()],
|
|
|
|
'License checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-02-27 17:07:40 -08:00
|
|
|
public function testAccessoryCheckinSendsSlackNotificationWhenSettingEnabled()
|
2023-03-30 16:58:16 -07:00
|
|
|
{
|
2024-02-14 18:17:34 -08:00
|
|
|
$this->settings->enableSlackWebhook();
|
2023-03-30 16:58:16 -07:00
|
|
|
|
2024-02-27 17:37:07 -08:00
|
|
|
$this->fireCheckInEvent(
|
2024-02-27 17:46:27 -08:00
|
|
|
Accessory::factory()->create(),
|
2023-03-30 16:58:16 -07:00
|
|
|
User::factory()->create(),
|
2024-02-27 17:37:07 -08:00
|
|
|
);
|
2023-03-30 16:58:16 -07:00
|
|
|
|
2024-02-27 17:44:19 -08:00
|
|
|
$this->assertSlackNotificationSent(CheckinAccessoryNotification::class);
|
2023-03-30 16:58:16 -07:00
|
|
|
}
|
|
|
|
|
2024-02-27 17:07:40 -08:00
|
|
|
public function testAccessoryCheckinDoesNotSendSlackNotificationWhenSettingDisabled()
|
2023-03-30 16:58:16 -07:00
|
|
|
{
|
2024-02-27 16:45:49 -08:00
|
|
|
$this->settings->disableSlackWebhook();
|
2023-03-30 16:58:16 -07:00
|
|
|
|
2024-02-27 17:37:07 -08:00
|
|
|
$this->fireCheckInEvent(
|
2024-02-27 17:46:27 -08:00
|
|
|
Accessory::factory()->create(),
|
2023-03-30 16:58:16 -07:00
|
|
|
User::factory()->create(),
|
2024-02-27 17:37:07 -08:00
|
|
|
);
|
2023-03-30 16:58:16 -07:00
|
|
|
|
2024-02-27 17:44:19 -08:00
|
|
|
$this->assertNoSlackNotificationSent(CheckinAccessoryNotification::class);
|
2023-03-30 16:58:16 -07:00
|
|
|
}
|
2023-04-05 12:36:24 -07:00
|
|
|
|
2024-03-13 16:37:50 -07:00
|
|
|
/** @dataProvider assetCheckInTargets */
|
2024-02-27 17:14:35 -08:00
|
|
|
public function testAssetCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget)
|
|
|
|
{
|
|
|
|
$this->settings->enableSlackWebhook();
|
|
|
|
|
2024-02-27 17:37:07 -08:00
|
|
|
$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:37:07 -08:00
|
|
|
);
|
2024-02-27 17:14:35 -08:00
|
|
|
|
2024-02-27 17:44:19 -08:00
|
|
|
$this->assertSlackNotificationSent(CheckinAssetNotification::class);
|
2024-02-27 17:14:35 -08:00
|
|
|
}
|
|
|
|
|
2024-03-13 16:37:50 -07:00
|
|
|
/** @dataProvider assetCheckInTargets */
|
2024-02-27 17:14:35 -08:00
|
|
|
public function testAssetCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget)
|
|
|
|
{
|
|
|
|
$this->settings->disableSlackWebhook();
|
|
|
|
|
2024-02-27 17:37:07 -08:00
|
|
|
$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:37:07 -08:00
|
|
|
);
|
2024-02-27 17:14:35 -08:00
|
|
|
|
2024-02-27 17:44:19 -08:00
|
|
|
$this->assertNoSlackNotificationSent(CheckinAssetNotification::class);
|
2024-02-27 17:14:35 -08:00
|
|
|
}
|
|
|
|
|
2024-02-27 17:07:40 -08:00
|
|
|
public function testComponentCheckinDoesNotSendSlackNotification()
|
2023-04-05 12:36:24 -07:00
|
|
|
{
|
2024-02-14 18:17:34 -08:00
|
|
|
$this->settings->enableSlackWebhook();
|
2023-04-05 12:36:24 -07:00
|
|
|
|
2024-02-27 17:37:07 -08:00
|
|
|
$this->fireCheckInEvent(
|
2024-02-27 18:03:53 -08:00
|
|
|
Component::factory()->create(),
|
2024-02-27 17:07:40 -08:00
|
|
|
Asset::factory()->laptopMbp()->create(),
|
2024-02-27 17:37:07 -08:00
|
|
|
);
|
2023-04-05 12:36:24 -07:00
|
|
|
|
2024-02-27 17:07:40 -08:00
|
|
|
Notification::assertNothingSent();
|
2023-04-05 12:36:24 -07:00
|
|
|
}
|
|
|
|
|
2024-03-13 16:37:50 -07:00
|
|
|
/** @dataProvider licenseCheckInTargets */
|
2024-02-27 17:14:35 -08:00
|
|
|
public function testLicenseCheckinSendsSlackNotificationWhenSettingEnabled($checkoutTarget)
|
|
|
|
{
|
|
|
|
$this->settings->enableSlackWebhook();
|
|
|
|
|
2024-02-27 17:37:07 -08:00
|
|
|
$this->fireCheckInEvent(
|
2024-02-27 17:14:35 -08:00
|
|
|
LicenseSeat::factory()->create(),
|
|
|
|
$checkoutTarget(),
|
2024-02-27 17:37:07 -08:00
|
|
|
);
|
2024-02-27 17:14:35 -08:00
|
|
|
|
2024-02-27 17:44:19 -08:00
|
|
|
$this->assertSlackNotificationSent(CheckinLicenseSeatNotification::class);
|
2024-02-27 17:14:35 -08:00
|
|
|
}
|
|
|
|
|
2024-03-13 16:37:50 -07:00
|
|
|
/** @dataProvider licenseCheckInTargets */
|
2024-02-27 17:14:35 -08:00
|
|
|
public function testLicenseCheckinDoesNotSendSlackNotificationWhenSettingDisabled($checkoutTarget)
|
|
|
|
{
|
|
|
|
$this->settings->disableSlackWebhook();
|
|
|
|
|
2024-02-27 17:37:07 -08:00
|
|
|
$this->fireCheckInEvent(
|
2024-02-27 17:14:35 -08:00
|
|
|
LicenseSeat::factory()->create(),
|
|
|
|
$checkoutTarget(),
|
2024-02-27 17:37:07 -08:00
|
|
|
);
|
|
|
|
|
2024-02-27 17:44:19 -08:00
|
|
|
$this->assertNoSlackNotificationSent(CheckinLicenseSeatNotification::class);
|
2024-02-27 17:37:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private function fireCheckInEvent(Model $checkoutable, Model $target)
|
|
|
|
{
|
|
|
|
event(new CheckoutableCheckedIn(
|
|
|
|
$checkoutable,
|
|
|
|
$target,
|
2024-02-27 17:14:35 -08:00
|
|
|
User::factory()->superuser()->create(),
|
|
|
|
''
|
|
|
|
));
|
|
|
|
}
|
2023-03-30 16:58:16 -07:00
|
|
|
}
|