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;
|
|
|
|
|
|
|
|
class AssetCheckoutSlackNotificationTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testNotificationSentToSlackWhenAssetCheckedOutToUserAndSlackNotificationEnabled()
|
|
|
|
{
|
|
|
|
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(
|
|
|
|
$user,
|
|
|
|
function (CheckoutAssetNotification $notification, $channels) {
|
|
|
|
// @todo: is this actually accurate?
|
|
|
|
return in_array('slack', $channels);
|
|
|
|
}
|
|
|
|
);
|
2023-03-13 15:47:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotificationSentToSlackWhenAssetCheckedOutToAssetAndSlackNotificationEnabled()
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoesNotSendNotificationViaSlackIfWebHookEndpointIsNotSetWhenCheckingOutAssetToAsset()
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotificationSentToSlackWhenAssetCheckedOutToLocationAndSlackNotificationEnabled()
|
|
|
|
{
|
|
|
|
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 11:56:54 -07:00
|
|
|
public function testDoesNotSendNotificationViaSlackIfWebHookEndpointIsNotSetWhenCheckingOutAssetToLocation()
|
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
|
|
|
}
|