2023-03-13 15:47:50 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Notifications;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Location;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use App\Models\User;
|
|
|
|
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;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2023-03-22 12:41:30 -07:00
|
|
|
class AssetCheckoutWebhookNotificationTest extends TestCase
|
2023-03-13 15:47:50 -07:00
|
|
|
{
|
2023-03-22 12:41:30 -07:00
|
|
|
public function testNotificationSentToWebhookWhenAssetCheckedOutToUserAndWebhookNotificationEnabled()
|
2023-03-13 15:47:50 -07:00
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2023-03-22 12:38:14 -07:00
|
|
|
$this->enableWebhookSettings();
|
2023-03-13 15:47:50 -07:00
|
|
|
|
2023-03-22 12:38:14 -07:00
|
|
|
$user = $this->createUser();
|
2023-03-13 15:47:50 -07:00
|
|
|
|
2023-03-22 12:38:14 -07:00
|
|
|
$this->createAsset()->checkOut(
|
2023-03-13 15:47:50 -07:00
|
|
|
$user,
|
2023-03-22 12:38:14 -07:00
|
|
|
$this->createSuperUser()->id
|
2023-03-13 15:47:50 -07:00
|
|
|
);
|
|
|
|
|
2023-03-21 17:03:51 -07:00
|
|
|
Notification::assertSentTo(
|
2023-03-23 16:31:40 -07:00
|
|
|
new AnonymousNotifiable,
|
|
|
|
CheckoutAssetNotification::class,
|
|
|
|
function ($notification, $channels, $notifiable) {
|
|
|
|
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-03-22 12:41:30 -07:00
|
|
|
public function testNotificationSentToWebhookWhenAssetCheckedOutToAssetAndWebhookNotificationEnabled()
|
2023-03-13 15:47:50 -07:00
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2023-03-22 12:38:14 -07:00
|
|
|
$this->enableWebhookSettings();
|
2023-03-13 15:47:50 -07:00
|
|
|
|
2023-03-22 12:38:14 -07:00
|
|
|
$this->createAsset()->checkOut(
|
|
|
|
$this->createAsset(),
|
|
|
|
$this->createSuperUser()->id
|
2023-03-21 17:03:51 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
// Since the target is not a user with an email address we have
|
|
|
|
// to check if an AnonymousNotifiable was sent.
|
|
|
|
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-22 12:41:30 -07:00
|
|
|
public function testDoesNotSendNotificationViaWebhookIfWebHookEndpointIsNotSetWhenCheckingOutAssetToAsset()
|
2023-03-21 17:03:51 -07:00
|
|
|
{
|
|
|
|
Notification::fake();
|
2023-03-13 15:47:50 -07:00
|
|
|
|
2023-03-22 12:38:14 -07:00
|
|
|
$this->createAsset()->checkOut(
|
|
|
|
$this->createAsset(),
|
|
|
|
$this->createSuperUser()->id
|
2023-03-13 15:47:50 -07:00
|
|
|
);
|
|
|
|
|
2023-03-21 17:03:51 -07:00
|
|
|
Notification::assertNotSentTo(
|
|
|
|
new AnonymousNotifiable,
|
|
|
|
CheckoutAssetNotification::class,
|
|
|
|
);
|
2023-03-13 15:47:50 -07:00
|
|
|
}
|
|
|
|
|
2023-03-22 12:41:30 -07:00
|
|
|
public function testNotificationSentToWebhookWhenAssetCheckedOutToLocationAndWebhookNotificationEnabled()
|
2023-03-13 15:47:50 -07:00
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
2023-03-22 12:38:14 -07:00
|
|
|
$this->enableWebhookSettings();
|
2023-03-13 15:47:50 -07:00
|
|
|
|
2023-03-22 12:38:14 -07:00
|
|
|
$this->createAsset()->checkOut(
|
2023-03-21 17:03:51 -07:00
|
|
|
Location::factory()->create(),
|
2023-03-22 12:38:14 -07:00
|
|
|
$this->createSuperUser()->id
|
2023-03-13 15:47:50 -07:00
|
|
|
);
|
|
|
|
|
2023-03-21 17:03:51 -07:00
|
|
|
// Since the target is not a user with an email address we have
|
|
|
|
// to check if an AnonymousNotifiable was sent.
|
|
|
|
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-03-22 12:41:30 -07:00
|
|
|
public function testDoesNotSendNotificationViaWebhookIfWebHookEndpointIsNotSetWhenCheckingOutAssetToLocation()
|
2023-03-13 15:47:50 -07:00
|
|
|
{
|
2023-03-21 17:03:51 -07:00
|
|
|
Notification::fake();
|
|
|
|
|
2023-03-22 12:38:14 -07:00
|
|
|
$this->createAsset()->checkOut(
|
2023-03-21 17:03:51 -07:00
|
|
|
Location::factory()->create(),
|
2023-03-22 12:38:14 -07:00
|
|
|
$this->createSuperUser()->id
|
2023-03-21 17:03:51 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
Notification::assertNotSentTo(
|
|
|
|
new AnonymousNotifiable,
|
|
|
|
CheckoutAssetNotification::class,
|
|
|
|
);
|
2023-03-13 15:47:50 -07:00
|
|
|
}
|
2023-03-22 12:38:14 -07:00
|
|
|
|
|
|
|
private function enableWebhookSettings()
|
|
|
|
{
|
|
|
|
Setting::factory()->withWebhookEnabled()->create();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createAsset()
|
|
|
|
{
|
|
|
|
return Asset::factory()->laptopMbp()->create();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createUser()
|
|
|
|
{
|
|
|
|
return User::factory()->create();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createSuperUser()
|
|
|
|
{
|
|
|
|
return User::factory()->superuser()->create();
|
|
|
|
}
|
2023-03-13 15:47:50 -07:00
|
|
|
}
|