2023-03-23 17:18:33 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Notifications;
|
|
|
|
|
2023-04-05 12:57:53 -07:00
|
|
|
use App\Events\CheckoutableCheckedIn;
|
2023-03-23 17:18:33 -07:00
|
|
|
use App\Events\CheckoutableCheckedOut;
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\LicenseSeat;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Models\User;
|
2023-04-05 12:57:53 -07:00
|
|
|
use App\Notifications\CheckinLicenseSeatNotification;
|
2023-03-23 17:18:33 -07:00
|
|
|
use App\Notifications\CheckoutLicenseSeatNotification;
|
|
|
|
use Illuminate\Notifications\AnonymousNotifiable;
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
2023-04-17 17:31:12 -07:00
|
|
|
use Tests\Support\InteractsWithSettings;
|
2023-03-23 17:18:33 -07:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
2023-04-05 12:23:11 -07:00
|
|
|
class LicenseWebhookTest extends TestCase
|
2023-03-23 17:18:33 -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-23 17:18:33 -07:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'License checked out to user' => [fn() => User::factory()->create()],
|
|
|
|
'License checked out to asset' => [fn() => Asset::factory()->laptopMbp()->create()],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-04-19 18:10:23 -07:00
|
|
|
/** @dataProvider targets */
|
2023-04-05 12:27:18 -07:00
|
|
|
public function testLicenseCheckoutSendsWebhookNotificationWhenSettingEnabled($checkoutTarget)
|
2023-03-23 17:18:33 -07:00
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2024-02-14 18:17:34 -08:00
|
|
|
$this->settings->enableSlackWebhook();
|
2023-03-23 17:18:33 -07:00
|
|
|
|
2023-03-27 14:09:21 -07:00
|
|
|
event(new CheckoutableCheckedOut(
|
|
|
|
LicenseSeat::factory()->create(),
|
|
|
|
$checkoutTarget(),
|
|
|
|
User::factory()->superuser()->create(),
|
|
|
|
''
|
|
|
|
));
|
2023-03-23 17:18:33 -07:00
|
|
|
|
|
|
|
Notification::assertSentTo(
|
|
|
|
new AnonymousNotifiable,
|
|
|
|
CheckoutLicenseSeatNotification::class,
|
|
|
|
function ($notification, $channels, $notifiable) {
|
|
|
|
return $notifiable->routes['slack'] === Setting::getSettings()->webhook_endpoint;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-03-30 15:38:22 -07:00
|
|
|
|
2023-04-19 18:10:23 -07:00
|
|
|
/** @dataProvider targets */
|
2023-04-05 12:27:18 -07:00
|
|
|
public function testLicenseCheckoutDoesNotSendWebhookNotificationWhenSettingDisabled($checkoutTarget)
|
2023-03-30 15:38:22 -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:22 -07:00
|
|
|
event(new CheckoutableCheckedOut(
|
|
|
|
LicenseSeat::factory()->create(),
|
|
|
|
$checkoutTarget(),
|
|
|
|
User::factory()->superuser()->create(),
|
|
|
|
''
|
|
|
|
));
|
|
|
|
|
|
|
|
Notification::assertNotSentTo(new AnonymousNotifiable, CheckoutLicenseSeatNotification::class);
|
|
|
|
}
|
2023-04-05 12:57:53 -07:00
|
|
|
|
2023-04-19 18:10:23 -07:00
|
|
|
/** @dataProvider targets */
|
2023-04-05 12:57:53 -07:00
|
|
|
public function testLicenseCheckinSendsWebhookNotificationWhenSettingEnabled($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(
|
|
|
|
LicenseSeat::factory()->create(),
|
|
|
|
$checkoutTarget(),
|
|
|
|
User::factory()->superuser()->create(),
|
|
|
|
''
|
|
|
|
));
|
|
|
|
|
|
|
|
Notification::assertSentTo(
|
|
|
|
new AnonymousNotifiable,
|
|
|
|
CheckinLicenseSeatNotification::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 testLicenseCheckinDoesNotSendWebhookNotificationWhenSettingDisabled($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(
|
|
|
|
LicenseSeat::factory()->create(),
|
|
|
|
$checkoutTarget(),
|
|
|
|
User::factory()->superuser()->create(),
|
|
|
|
''
|
|
|
|
));
|
|
|
|
|
|
|
|
Notification::assertNotSentTo(new AnonymousNotifiable, CheckinLicenseSeatNotification::class);
|
|
|
|
}
|
2023-03-23 17:18:33 -07:00
|
|
|
}
|