snipe-it/tests/Feature/Notifications/AssetCheckoutSlackNotificationTest.php

132 lines
3.6 KiB
PHP
Raw Normal View History

<?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;
use Illuminate\Notifications\AnonymousNotifiable;
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-22 12:38:14 -07:00
$user = $this->createUser();
2023-03-22 12:38:14 -07:00
$this->createAsset()->checkOut(
$user,
2023-03-22 12:38:14 -07:00
$this->createSuperUser()->id
);
Notification::assertSentTo(
$user,
function (CheckoutAssetNotification $notification, $channels) {
// @todo: is this actually accurate?
return in_array('slack', $channels);
}
);
}
public function testNotificationSentToSlackWhenAssetCheckedOutToAssetAndSlackNotificationEnabled()
{
Notification::fake();
2023-03-22 12:38:14 -07:00
$this->enableWebhookSettings();
2023-03-22 12:38:14 -07:00
$this->createAsset()->checkOut(
$this->createAsset(),
$this->createSuperUser()->id
);
// 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;
}
);
}
public function testDoesNotSendNotificationViaSlackIfWebHookEndpointIsNotSetWhenCheckingOutAssetToAsset()
{
Notification::fake();
2023-03-22 12:38:14 -07:00
$this->createAsset()->checkOut(
$this->createAsset(),
$this->createSuperUser()->id
);
Notification::assertNotSentTo(
new AnonymousNotifiable,
CheckoutAssetNotification::class,
);
}
public function testNotificationSentToSlackWhenAssetCheckedOutToLocationAndSlackNotificationEnabled()
{
Notification::fake();
2023-03-22 12:38:14 -07:00
$this->enableWebhookSettings();
2023-03-22 12:38:14 -07:00
$this->createAsset()->checkOut(
Location::factory()->create(),
2023-03-22 12:38:14 -07:00
$this->createSuperUser()->id
);
// 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-22 11:56:54 -07:00
public function testDoesNotSendNotificationViaSlackIfWebHookEndpointIsNotSetWhenCheckingOutAssetToLocation()
{
Notification::fake();
2023-03-22 12:38:14 -07:00
$this->createAsset()->checkOut(
Location::factory()->create(),
2023-03-22 12:38:14 -07:00
$this->createSuperUser()->id
);
Notification::assertNotSentTo(
new AnonymousNotifiable,
CheckoutAssetNotification::class,
);
}
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();
}
}