2023-03-13 15:47:50 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Notifications;
|
|
|
|
|
2023-04-05 12:57:53 -07:00
|
|
|
use App\Events\CheckoutableCheckedIn;
|
2023-03-30 15:38:00 -07:00
|
|
|
use App\Events\CheckoutableCheckedOut;
|
2023-03-13 15:47:50 -07:00
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Location;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Models\User;
|
2023-04-05 12:57:53 -07:00
|
|
|
use App\Notifications\CheckinAssetNotification;
|
2023-03-13 15:47:50 -07:00
|
|
|
use App\Notifications\CheckoutAssetNotification;
|
2023-03-21 17:03:51 -07:00
|
|
|
use Illuminate\Notifications\AnonymousNotifiable;
|
2023-03-13 15:47:50 -07:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
2023-04-17 17:31:12 -07:00
|
|
|
use Tests\Support\InteractsWithSettings;
|
2023-03-13 15:47:50 -07:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
2023-04-05 12:23:11 -07:00
|
|
|
class AssetWebhookTest extends TestCase
|
2023-03-13 15:47:50 -07:00
|
|
|
{
|
2023-04-17 17:31:12 -07:00
|
|
|
use InteractsWithSettings;
|
|
|
|
|
2023-04-19 18:10:23 -07:00
|
|
|
public function targets(): array
|
2023-03-13 15:47:50 -07:00
|
|
|
{
|
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-13 15:47:50 -07:00
|
|
|
}
|
|
|
|
|
2023-04-19 18:10:23 -07:00
|
|
|
/** @dataProvider targets */
|
2023-04-05 12:27:18 -07:00
|
|
|
public function testAssetCheckoutSendsWebhookNotificationWhenSettingEnabled($checkoutTarget)
|
2023-03-13 15:47:50 -07:00
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2024-02-14 18:17:34 -08:00
|
|
|
$this->settings->enableSlackWebhook();
|
2023-03-13 15:47:50 -07:00
|
|
|
|
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-13 15:47:50 -07:00
|
|
|
|
2023-03-21 17:03:51 -07:00
|
|
|
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-21 17:03:51 -07:00
|
|
|
}
|
|
|
|
);
|
2023-03-13 15:47:50 -07:00
|
|
|
}
|
|
|
|
|
2023-04-19 18:10:23 -07:00
|
|
|
/** @dataProvider targets */
|
2023-04-05 12:27:18 -07:00
|
|
|
public function testAssetCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget)
|
2023-03-13 15:47:50 -07:00
|
|
|
{
|
2023-03-21 17:03:51 -07:00
|
|
|
Notification::fake();
|
|
|
|
|
2023-04-17 17:31:12 -07:00
|
|
|
$this->settings->disableWebhook();
|
2023-03-30 16:40:21 -07:00
|
|
|
|
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-21 17:03:51 -07:00
|
|
|
|
2023-03-23 16:42:21 -07:00
|
|
|
Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutAssetNotification::class);
|
2023-03-22 12:38:14 -07:00
|
|
|
}
|
|
|
|
|
2023-04-19 18:10:23 -07:00
|
|
|
/** @dataProvider targets */
|
2023-04-05 12:57:53 -07:00
|
|
|
public function testAssetCheckinSendsWebhookNotificationWhenSettingEnabled($checkoutTarget)
|
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2024-02-14 18:17:34 -08:00
|
|
|
$this->settings->enableSlackWebhook();
|
2023-04-05 12:57:53 -07:00
|
|
|
|
|
|
|
event(new CheckoutableCheckedIn(
|
|
|
|
$this->createAsset(),
|
|
|
|
$checkoutTarget(),
|
|
|
|
User::factory()->superuser()->create(),
|
|
|
|
''
|
|
|
|
));
|
|
|
|
|
|
|
|
Notification::assertSentTo(
|
|
|
|
new AnonymousNotifiable,
|
|
|
|
CheckinAssetNotification::class,
|
|
|
|
function ($notification, $channels, $notifiable) {
|
|
|
|
return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-19 18:10:23 -07:00
|
|
|
/** @dataProvider targets */
|
2023-04-05 12:57:53 -07:00
|
|
|
public function testAssetCheckinDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget)
|
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2023-04-17 17:31:12 -07:00
|
|
|
$this->settings->disableWebhook();
|
2023-04-05 12:57:53 -07:00
|
|
|
|
|
|
|
event(new CheckoutableCheckedIn(
|
|
|
|
$this->createAsset(),
|
|
|
|
$checkoutTarget(),
|
|
|
|
User::factory()->superuser()->create(),
|
|
|
|
''
|
|
|
|
));
|
|
|
|
|
|
|
|
Notification::assertNotSentTo(new AnonymousNotifiable, CheckinAssetNotification::class);
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:38:14 -07:00
|
|
|
private function createAsset()
|
|
|
|
{
|
|
|
|
return Asset::factory()->laptopMbp()->create();
|
|
|
|
}
|
2023-03-13 15:47:50 -07:00
|
|
|
}
|